Skip to content

Commit

Permalink
Merge pull request openedx#377 from edx/fix/discussion_blacked_out
Browse files Browse the repository at this point in the history
chore: Make inactive posting buttons/fields for "posting disabled" courses
  • Loading branch information
rnr authored Apr 2, 2024
2 parents b39d35c + 7b78dda commit 3e632e8
Show file tree
Hide file tree
Showing 28 changed files with 500 additions and 191 deletions.
11 changes: 11 additions & 0 deletions Core/Core/Extensions/DateExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import Foundation


public extension Date {
init(iso8601: String) {
let formats = ["yyyy-MM-dd'T'HH:mm:ssZ", "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ"]
Expand Down Expand Up @@ -194,3 +195,13 @@ public extension Date {
return selfYear == runningYear
}
}

public extension Date {
func isEarlierThanOrEqualTo(date: Date) -> Bool {
timeIntervalSince1970 <= date.timeIntervalSince1970
}

func isLaterThanOrEqualTo(date: Date) -> Bool {
timeIntervalSince1970 >= date.timeIntervalSince1970
}
}
30 changes: 17 additions & 13 deletions Core/Core/Extensions/ViewExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,21 +252,25 @@ public extension View {
func sheetNavigation(isSheet: Bool, onDismiss: (() -> Void)? = nil) -> some View {
if isSheet {
NavigationView {
self
.if(onDismiss != nil) { view in
view
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button {
onDismiss?()
} label: {
Image(systemName: "xmark")
.foregroundColor(Theme.Colors.accentColor)
ZStack {
Theme.Colors.background
.ignoresSafeArea()
self
.if(onDismiss != nil) { view in
view
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button {
onDismiss?()
} label: {
Image(systemName: "xmark")
.foregroundColor(Theme.Colors.accentColor)
}
.accessibilityIdentifier("close_button")
}
.accessibilityIdentifier("close_button")
}
}
}
}
}
}
} else {
self
Expand Down
12 changes: 8 additions & 4 deletions Course/Course/Presentation/Downloads/DownloadsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ public struct DownloadsView: View {
// MARK: - Body

public var body: some View {
content
.sheetNavigation(isSheet: isSheet) {
dismiss()
}
ZStack {
Theme.Colors.background
.ignoresSafeArea()
content
.sheetNavigation(isSheet: isSheet) {
dismiss()
}
}
}

private var content: some View {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,24 @@ struct CourseStructureNestedListView: View {
Button {
onLabelClick(sequential: sequential, chapter: chapter)
} label: {
Group {
if sequential.completion == 1 {
CoreAssets.finished.swiftUIImage
.renderingMode(.template)
.foregroundColor(Theme.Colors.accentXColor)
} else {
sequential.type.image
HStack(spacing: 0) {
Group {
if sequential.completion == 1 {
CoreAssets.finished.swiftUIImage
.renderingMode(.template)
.foregroundColor(Theme.Colors.accentXColor)
} else {
sequential.type.image
}
Text(sequential.displayName)
.font(Theme.Fonts.titleMedium)
.multilineTextAlignment(.leading)
.lineLimit(1)
}
Text(sequential.displayName)
.font(Theme.Fonts.titleMedium)
.multilineTextAlignment(.leading)
.lineLimit(1)
.foregroundColor(Theme.Colors.textPrimary)
Spacer()
}
.foregroundColor(Theme.Colors.textPrimary)
}
Spacer()
downloadButton(
sequential: sequential,
chapter: chapter
Expand Down
8 changes: 8 additions & 0 deletions Discussion/Discussion.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
0766DFCA299AA3D400EBEF6A /* Data_CreatedComment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0766DFC9299AA3D400EBEF6A /* Data_CreatedComment.swift */; };
7527943BE0D66C33B167A41A /* Pods_App_Discussion.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 66A7A0375EDDEB8948165EAD /* Pods_App_Discussion.framework */; };
9FC0EF907C0334E383C300C4 /* Pods_App_Discussion_DiscussionTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C40A586C6164140DC2079231 /* Pods_App_Discussion_DiscussionTests.framework */; };
BA3C45672BA9E13000672C96 /* Data_DiscussionInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA3C45662BA9E13000672C96 /* Data_DiscussionInfo.swift */; };
BA3C45692BA9E18D00672C96 /* DiscussionInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA3C45682BA9E18D00672C96 /* DiscussionInfo.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -135,6 +137,8 @@
ACA8EF2DEDDB7695162C381A /* Pods-App-Discussion.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-App-Discussion.debug.xcconfig"; path = "Target Support Files/Pods-App-Discussion/Pods-App-Discussion.debug.xcconfig"; sourceTree = "<group>"; };
ACDA8EE733B97BCD8689A0B4 /* Pods-App-Discussion.debugstage.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-App-Discussion.debugstage.xcconfig"; path = "Target Support Files/Pods-App-Discussion/Pods-App-Discussion.debugstage.xcconfig"; sourceTree = "<group>"; };
B472516A5A79E87C40766D93 /* Pods-App-Discussion-DiscussionTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-App-Discussion-DiscussionTests.debug.xcconfig"; path = "Target Support Files/Pods-App-Discussion-DiscussionTests/Pods-App-Discussion-DiscussionTests.debug.xcconfig"; sourceTree = "<group>"; };
BA3C45662BA9E13000672C96 /* Data_DiscussionInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Data_DiscussionInfo.swift; sourceTree = "<group>"; };
BA3C45682BA9E18D00672C96 /* DiscussionInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DiscussionInfo.swift; sourceTree = "<group>"; };
BBF5EC7249109D6718A229CF /* Pods-App-Discussion.debugdev.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-App-Discussion.debugdev.xcconfig"; path = "Target Support Files/Pods-App-Discussion/Pods-App-Discussion.debugdev.xcconfig"; sourceTree = "<group>"; };
C214357310D8AC9185B38ABA /* Pods-App-Discussion-DiscussionTests.releasestage.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-App-Discussion-DiscussionTests.releasestage.xcconfig"; path = "Target Support Files/Pods-App-Discussion-DiscussionTests/Pods-App-Discussion-DiscussionTests.releasestage.xcconfig"; sourceTree = "<group>"; };
C40A586C6164140DC2079231 /* Pods_App_Discussion_DiscussionTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_App_Discussion_DiscussionTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -256,6 +260,7 @@
02F3BFE82926A57C0051930C /* Data_ThreadResponse.swift */,
02F3BFEA2926A5B50051930C /* Data_CommentsResponse.swift */,
0766DFC9299AA3D400EBEF6A /* Data_CreatedComment.swift */,
BA3C45662BA9E13000672C96 /* Data_DiscussionInfo.swift */,
);
path = Model;
sourceTree = "<group>";
Expand Down Expand Up @@ -318,6 +323,7 @@
0766DFC1299AA2A500EBEF6A /* DiscussionTopic.swift */,
0766DFC3299AA2C200EBEF6A /* Post.swift */,
0766DFC5299AA30500EBEF6A /* ThreadType.swift */,
BA3C45682BA9E18D00672C96 /* DiscussionInfo.swift */,
);
path = Model;
sourceTree = "<group>";
Expand Down Expand Up @@ -689,6 +695,8 @@
02F28A6028FF23F300AFDE1B /* ThreadViewModel.swift in Sources */,
0766DFBE299AA18A00EBEF6A /* DiscussionPost.swift in Sources */,
02F3BFED2926A6270051930C /* UserComment.swift in Sources */,
BA3C45672BA9E13000672C96 /* Data_DiscussionInfo.swift in Sources */,
BA3C45692BA9E18D00672C96 /* DiscussionInfo.swift in Sources */,
0282DA5F28F893CA003C3F07 /* PostsView.swift in Sources */,
0766DFC0299AA1CD00EBEF6A /* DiscussionNewThread.swift in Sources */,
);
Expand Down
35 changes: 35 additions & 0 deletions Discussion/Discussion/Data/Model/Data_DiscussionInfo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// Data_DiscussionInfo.swift
// Discussion
//
// Created by Eugene Yatsenko on 19.03.2024.
//

import Foundation
import Core

public struct DiscussionBlackout {
var start: String
var end: String
}

public extension DataLayer {
struct DiscussionInfo: Codable {
var discussionID: String?
var blackouts: [DiscussionBlackout]?
}

struct DiscussionBlackout: Codable {
var start: String
var end: String
}
}

public extension DataLayer.DiscussionInfo {
var domain: DiscussionInfo {
.init(
discussionID: discussionID,
blackouts: blackouts?.compactMap { .init(start: $0.start, end: $0.end) }
)
}
}
16 changes: 14 additions & 2 deletions Discussion/Discussion/Data/Network/DiscussionRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Core
import Combine

public protocol DiscussionRepositoryProtocol {
func getCourseDiscussionInfo(courseID: String) async throws -> DiscussionInfo
func getThreads(courseID: String,
type: ThreadType,
sort: SortType,
Expand Down Expand Up @@ -49,7 +50,14 @@ public class DiscussionRepository: DiscussionRepositoryProtocol {
self.config = config
self.router = router
}


public func getCourseDiscussionInfo(courseID: String) async throws -> DiscussionInfo {
let discussionInfo = try await api.requestData(DiscussionEndpoint
.getCourseDiscussionInfo(courseID: courseID))
.mapResponse(DataLayer.DiscussionInfo.self)
return discussionInfo.domain
}

public func getThreads(courseID: String,
type: ThreadType,
sort: SortType,
Expand Down Expand Up @@ -205,7 +213,11 @@ public class DiscussionRepository: DiscussionRepositoryProtocol {
#if DEBUG
// swiftlint:disable all
public class DiscussionRepositoryMock: DiscussionRepositoryProtocol {


public func getCourseDiscussionInfo(courseID: String) async throws -> DiscussionInfo {
DiscussionInfo(discussionID: nil, blackouts: [])
}

public func getThread(threadID: String) async throws -> UserThread {
UserThread(
id: "",
Expand Down
5 changes: 5 additions & 0 deletions Discussion/Discussion/Domain/DiscussionInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Core

//sourcery: AutoMockable
public protocol DiscussionInteractorProtocol {
func getCourseDiscussionInfo(courseID: String) async throws -> DiscussionInfo
func getThreadsList(courseID: String,
type: ThreadType,
sort: SortType,
Expand Down Expand Up @@ -41,6 +42,10 @@ public class DiscussionInteractor: DiscussionInteractorProtocol {
self.repository = repository
}

public func getCourseDiscussionInfo(courseID: String) async throws -> DiscussionInfo {
try await repository.getCourseDiscussionInfo(courseID: courseID)
}

public func getThreadsList(courseID: String,
type: ThreadType,
sort: SortType,
Expand Down
31 changes: 31 additions & 0 deletions Discussion/Discussion/Domain/Model/DiscussionInfo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// DiscussionInfo.swift
// Discussion
//
// Created by Eugene Yatsenko on 19.03.2024.
//

import Foundation

public struct DiscussionInfo {
public var discussionID: String?
public var blackouts: [DiscussionBlackout]?

public func isBlackedOut() -> Bool {
guard let blackouts = blackouts else {
return false
}
var isBlackedOut = false
for blackout in blackouts {
let start = Date(iso8601: blackout.start)
let end = Date(iso8601: blackout.end)

if Date().isEarlierThanOrEqualTo(date: end) &&
Date().isLaterThanOrEqualTo(date: start) {
isBlackedOut = true
}
}

return isBlackedOut
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ public struct ResponsesView: View {

@ObservedObject private var viewModel: ResponsesViewModel
@State private var isShowProgress: Bool = true

public init(commentID: String,
viewModel: ResponsesViewModel,
router: DiscussionRouter,
parentComment: Post) {

public init(
commentID: String,
viewModel: ResponsesViewModel,
router: DiscussionRouter,
parentComment: Post,
isBlackedOut: Bool
) {
self.commentID = commentID
self.parentComment = parentComment
self.title = DiscussionLocalization.Response.commentsResponses
Expand All @@ -33,6 +36,7 @@ public struct ResponsesView: View {
await viewModel.getComments(commentID: commentID, parentComment: parentComment, page: 1)
}
viewModel.addCommentsIsVisible = false
self.viewModel.isBlackedOut = isBlackedOut
}

public var body: some View {
Expand Down Expand Up @@ -152,8 +156,7 @@ public struct ResponsesView: View {
}
.frameLimit(width: proxy.size.width)
}

if !parentComment.closed {
if !(parentComment.closed || viewModel.isBlackedOut) {
FlexibleKeyboardInputView(
hint: DiscussionLocalization.Response.addComment,
sendText: { commentText in
Expand All @@ -167,7 +170,8 @@ public struct ResponsesView: View {
}
}
}
).ignoresSafeArea(.all, edges: .horizontal)
)
.ignoresSafeArea(.all, edges: .horizontal)
}
}
}
Expand Down Expand Up @@ -249,7 +253,8 @@ struct ResponsesView_Previews: PreviewProvider {
commentID: "",
viewModel: viewModel,
router: router,
parentComment: post
parentComment: post,
isBlackedOut: false
)
.loadFonts()
.preferredColorScheme(.light)
Expand All @@ -259,7 +264,8 @@ struct ResponsesView_Previews: PreviewProvider {
commentID: "",
viewModel: viewModel,
router: router,
parentComment: post
parentComment: post,
isBlackedOut: false
)
.loadFonts()
.preferredColorScheme(.dark)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import Combine
public class ResponsesViewModel: BaseResponsesViewModel, ObservableObject {

@Published var scrollTrigger: Bool = false

private let threadStateSubject: CurrentValueSubject<ThreadPostState?, Never>

public var isBlackedOut: Bool = false

public init(
interactor: DiscussionInteractorProtocol,
router: DiscussionRouter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public struct ThreadView: View {
commentID: comment.commentID,
parentComment: comment,
threadStateSubject: viewModel.threadStateSubject,
isBlackedOut: viewModel.isBlackedOut,
animated: true
)
},
Expand Down Expand Up @@ -153,7 +154,7 @@ public struct ThreadView: View {
}
.frameLimit(width: proxy.size.width)
}
if !thread.closed {
if !(thread.closed || viewModel.isBlackedOut) {
FlexibleKeyboardInputView(
hint: DiscussionLocalization.Thread.addResponse,
sendText: { commentText in
Expand All @@ -167,7 +168,8 @@ public struct ThreadView: View {
}
}
}
).ignoresSafeArea(.all, edges: .horizontal)
)
.ignoresSafeArea(.all, edges: .horizontal)
}
}
.onReceive(viewModel.addPostSubject, perform: { newComment in
Expand Down
Loading

0 comments on commit 3e632e8

Please sign in to comment.