Skip to content

Commit

Permalink
Reply to solutions (#659)
Browse files Browse the repository at this point in the history
* Update step discussion count only for default thread type

* Remove setGraceTimeInterval

* Add reply
  • Loading branch information
ivan-magda authored Feb 17, 2020
1 parent a8f0ecb commit 132f21f
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 25 deletions.
1 change: 0 additions & 1 deletion Stepic/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
RemoteConfig.shared.setup()

SVProgressHUD.setMinimumDismissTimeInterval(0.5)
SVProgressHUD.setGraceTimeInterval(0.1)
SVProgressHUD.setDefaultMaskType(SVProgressHUDMaskType.clear)
SVProgressHUD.setHapticsEnabled(true)

Expand Down
19 changes: 14 additions & 5 deletions Stepic/Discussions/Comment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,21 @@ final class Comment: JSONSerializable {
var abuseCount: Int = 0
var actions: [Action] = []
var submissionID: Submission.IdType?
var thread: String = DiscussionThread.ThreadType.default.rawValue

var userInfo: UserInfo!
var vote: Vote!
var submission: Submission?

var threadType: DiscussionThread.ThreadType? {
DiscussionThread.ThreadType(rawValue: self.thread)
}

var json: JSON {
var dict: JSON = [
JSONKey.target.rawValue: self.targetID,
JSONKey.text.rawValue: self.text
JSONKey.text.rawValue: self.text,
JSONKey.thread.rawValue: self.thread
]

if let parentID = self.parentID {
Expand All @@ -55,8 +61,7 @@ final class Comment: JSONSerializable {
if let submissionID = self.submissionID {
try? dict.merge(
with: [
JSONKey.submission.rawValue: submissionID,
JSONKey.thread.rawValue: DiscussionThread.ThreadType.solutions.rawValue
JSONKey.submission.rawValue: submissionID
]
)
}
Expand All @@ -72,12 +77,14 @@ final class Comment: JSONSerializable {
targetID: Step.IdType,
text: String,
parentID: IdType? = nil,
submissionID: Submission.IdType? = nil
submissionID: Submission.IdType? = nil,
threadType: DiscussionThread.ThreadType = .default
) {
self.targetID = targetID
self.text = text
self.parentID = parentID
self.submissionID = submissionID
self.thread = threadType.rawValue
}

func update(json: JSON) {
Expand All @@ -97,6 +104,7 @@ final class Comment: JSONSerializable {
self.epicCount = json[JSONKey.epicCount.rawValue].intValue
self.abuseCount = json[JSONKey.abuseCount.rawValue].intValue
self.submissionID = json[JSONKey.submission.rawValue].int
self.thread = json[JSONKey.thread.rawValue].string ?? DiscussionThread.ThreadType.default.rawValue

self.actions.removeAll(keepingCapacity: true)
for (actionKey, value) in json[JSONKey.actions.rawValue].dictionaryValue {
Expand Down Expand Up @@ -168,7 +176,8 @@ extension Comment: CustomDebugStringConvertible {
submissionID: \(submissionID ??? "nil"), \
userInfo: \(userInfo ??? "nil"), \
vote: \(vote ??? "nil"), \
submission: \(submission ??? "nil"))
submission: \(submission ??? "nil")) \
thread: \(thread))
"""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ final class DiscussionsInteractor: DiscussionsInteractorProtocol {
self.currentDiscussions.remove(at: discussionIndex)
self.currentReplies[commentID] = nil

self.provider.decrementStepDiscussionsCount(stepID: self.stepID).cauterize()
if self.discussionThreadType == .default {
self.provider.decrementStepDiscussionsCount(stepID: self.stepID).cauterize()
}
} else {
for (discussionID, replies) in self.currentReplies {
guard let replyIndex = replies.firstIndex(where: { $0.id == commentID }) else {
Expand Down Expand Up @@ -748,7 +750,9 @@ extension DiscussionsInteractor: WriteCommentOutputProtocol {
self.presenter.presentCommentCreate(response: .init(result: self.makeDiscussionsData()))
}.cauterize()

self.provider.incrementStepDiscussionsCount(stepID: self.stepID).cauterize()
if self.discussionThreadType == .default {
self.provider.incrementStepDiscussionsCount(stepID: self.stepID).cauterize()
}
}
}

Expand Down
22 changes: 10 additions & 12 deletions Stepic/Sources/Modules/Discussions/DiscussionsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -503,19 +503,17 @@ extension DiscussionsViewController: DiscussionsTableViewDataSourceDelegate {
)
)

if viewModel.solution == nil {
alert.addAction(
UIAlertAction(
title: NSLocalizedString("Reply", comment: ""),
style: .default,
handler: { [weak self] _ in
self?.interactor.doWriteCommentPresentation(
request: .init(commentID: viewModel.id, presentationContext: .create)
)
}
)
alert.addAction(
UIAlertAction(
title: NSLocalizedString("Reply", comment: ""),
style: .default,
handler: { [weak self] _ in
self?.interactor.doWriteCommentPresentation(
request: .init(commentID: viewModel.id, presentationContext: .create)
)
}
)
}
)

if viewModel.canEdit {
alert.addAction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,6 @@ final class DiscussionsCellView: UIView {
} else {
self.solutionContainerView.isHidden = true
}

self.replyButton.isHidden = viewModel.solution != nil
}

func calculateContentHeight(maxPreferredWidth: CGFloat) -> CGFloat {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ enum WriteComment {

struct CommentData {
let text: String
let parentID: WriteComment.ParentIDType?
let comment: Comment?
let submission: Submission?
let discussionThreadType: DiscussionThread.ThreadType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ final class WriteCommentInteractor: WriteCommentInteractorProtocol {
targetID: self.targetID,
text: htmlText,
parentID: self.parentID,
submissionID: self.submission?.id
submissionID: self.submission?.id,
threadType: self.discussionThreadType
)

var actionPromise: Promise<Comment>
Expand Down Expand Up @@ -126,6 +127,7 @@ final class WriteCommentInteractor: WriteCommentInteractorProtocol {
private func makeCommentData() -> WriteComment.CommentData {
.init(
text: self.currentText,
parentID: self.parentID,
comment: self.comment,
submission: self.submission,
discussionThreadType: self.discussionThreadType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,22 @@ final class WriteCommentPresenter: WriteCommentPresenterProtocol {
return NSLocalizedString("WriteCommentSelectSolutionTitle", comment: "")
}()

let isReply = data.parentID != nil

let isFilled: Bool = {
if data.discussionThreadType == .solutions {
if !isReply && data.discussionThreadType == .solutions {
return !data.text.isEmpty && data.submission != nil
}
return !data.text.isEmpty
}()

let isSolutionHidden = isReply || data.discussionThreadType != .solutions

return .init(
text: data.text,
doneButtonTitle: doneButtonTitle,
isFilled: isFilled,
isSolutionHidden: data.discussionThreadType != .solutions,
isSolutionHidden: isSolutionHidden,
isSolutionSelected: data.submission != nil,
isSolutionCorrect: data.submission?.isCorrect ?? false,
solutionTitle: solutionTitle
Expand Down

0 comments on commit 132f21f

Please sign in to comment.