Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

Show discard or keep alert after tapping outside of the PR reviewers,… #2633

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 59 additions & 6 deletions Classes/Issues/IssueManagingContextController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,23 @@ final class IssueManagingContextController: NSObject, ContextMenuDelegate {
Haptic.triggerNotification(.success)
}

func didDismiss(selected labels: [RepositoryLabel]) {
func didDismiss(controller: LabelsViewController) {
guard let previous = result,
previous.labels.labels != labels
previous.labels.labels != controller.selected
else { return }

if controller.wasDismissedByDone {
self.update(selected: controller.selected, previous: previous)
} else {
// Ask for confirmation
self.showCancelAlert { [weak self] keepChanges in
guard keepChanges else { return }
self?.update(selected: controller.selected, previous: previous)
}
}
}

private func update(selected labels: [RepositoryLabel], previous: IssueResult) {
delegate?.willMutateModel(from: self)
client.mutateLabels(
previous: previous,
Expand All @@ -323,10 +336,23 @@ final class IssueManagingContextController: NSObject, ContextMenuDelegate {

let selected = controller.selected
guard controller.selectionChanged(newValues: selected) else { return }

if controller.wasDismissedByDone {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we put this logic in a function, as it seems to be repeated from lines 312-320?

self.update(selected: selected, type: controller.type, previous: previous)
} else {
// Ask for confirmation
self.showCancelAlert { [weak self] keepChanges in
guard keepChanges else { return }
self?.update(selected: selected, type: controller.type, previous: previous)
}
}
}

private func update(selected assignees: [IssueAssigneeViewModel], type: PeopleViewController.PeopleType, previous: IssueResult) {
delegate?.willMutateModel(from: self)

let mutationType: V3AddPeopleRequest.PeopleType
switch controller.type {
switch type {
case .assignee: mutationType = .assignees
case .reviewer: mutationType = .reviewers
}
Expand All @@ -337,21 +363,34 @@ final class IssueManagingContextController: NSObject, ContextMenuDelegate {
owner: model.owner,
repo: model.repo,
number: model.number,
people: controller.selected
people: assignees
)
}

func didDismiss(controller: MilestonesViewController) {
guard let previous = result,
previous.milestone != controller.selected
else { return }

if controller.wasDismissedByDone {
self.update(selected: controller.selected, previous: previous)
} else {
// Ask for confirmation
self.showCancelAlert { [weak self] keepChanges in
guard keepChanges else { return }
self?.update(selected: controller.selected, previous: previous)
}
}
}

private func update(selected milestone: Milestone?, previous: IssueResult) {
delegate?.willMutateModel(from: self)
client.setMilestone(
previous: previous,
owner: model.owner,
repo: model.repo,
number: model.number,
milestone: controller.selected
milestone: milestone
)
}

Expand All @@ -363,10 +402,24 @@ final class IssueManagingContextController: NSObject, ContextMenuDelegate {
} else if let people = viewController as? PeopleViewController {
didDismiss(controller: people)
} else if let labels = viewController as? LabelsViewController {
didDismiss(selected: labels.selected)
didDismiss(controller: labels)
}
}

func contextMenuDidDismiss(viewController: UIViewController, animated: Bool) {}

private func showCancelAlert(completion: @escaping (_ keepChanges: Bool) -> Void) {
guard let viewController = self.viewController else { return }

let title = NSLocalizedString("Are you sure?", comment: "")
let message = NSLocalizedString("Some changes have been made. Are you sure you want to discard them?", comment: "")
let alert = UIAlertController.configured(title: title, message: message, preferredStyle: .alert)

alert.addActions([
AlertAction.keep { _ in completion(true) },
AlertAction.discard { _ in completion(false) }
])

viewController.present(alert, animated: trueUnlessReduceMotionEnabled)
}
}
7 changes: 7 additions & 0 deletions Classes/Labels/LabelsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ LabelSectionControllerDelegate {
private let client: GithubClient
private let request: RepositoryLabelsQuery

var wasDismissedByDone = false

init(
selected: [RepositoryLabel],
client: GithubClient,
Expand Down Expand Up @@ -102,6 +104,11 @@ LabelSectionControllerDelegate {
})
}

override func onMenuDone() {
self.wasDismissedByDone = true
super.onMenuDone()
}

// MARK: BaseListViewControllerDataSource

func models(adapter: ListSwiftAdapter) -> [ListSwiftPair] {
Expand Down
7 changes: 7 additions & 0 deletions Classes/Milestones/MilestonesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ MilestoneSectionControllerDelegate {
private let feedRefresh = FeedRefresh()
private var milestones = [Milestone]()

var wasDismissedByDone = false

private let dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateStyle = .long
Expand Down Expand Up @@ -96,6 +98,11 @@ MilestoneSectionControllerDelegate {
}
}

override func onMenuDone() {
self.wasDismissedByDone = true
super.onMenuDone()
}

// MARK: BaseListViewControllerDataSource

func models(adapter: ListSwiftAdapter) -> [ListSwiftPair] {
Expand Down
7 changes: 7 additions & 0 deletions Classes/People/PeopleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ PeopleSectionControllerDelegate {
private var owner: String
private var repo: String

var wasDismissedByDone = false

init(
selections: [String],
exclusions: [String],
Expand Down Expand Up @@ -165,6 +167,11 @@ PeopleSectionControllerDelegate {
}
}

@objc override func onMenuDone() {
self.wasDismissedByDone = true
super.onMenuDone()
}

// MARK: BaseListViewControllerDataSource

func models(adapter: ListSwiftAdapter) -> [ListSwiftPair] {
Expand Down
4 changes: 4 additions & 0 deletions Classes/Utility/AlertAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ struct AlertAction {
return UIAlertAction(title: NSLocalizedString("Discard", comment: ""), style: .destructive, handler: handler)
}

static func keep(_ handler: AlertActionBlock? = nil) -> UIAlertAction {
return UIAlertAction(title: NSLocalizedString("Keep", comment: ""), style: .default, handler: handler)
}

static func delete(_ handler: AlertActionBlock? = nil) -> UIAlertAction {
return UIAlertAction(title: NSLocalizedString("Delete", comment: ""), style: .destructive, handler: handler)
}
Expand Down