Skip to content

Commit 2102465

Browse files
committed
Refactor bookmarks export to async-await. Remove unnecessary optionals. Remove unnecessary popup
1 parent b3e162d commit 2102465

File tree

1 file changed

+61
-90
lines changed

1 file changed

+61
-90
lines changed

ios/brave-ios/Sources/Brave/Frontend/Browser/Toolbars/BottomToolbar/Menu/Bookmarks/BookmarksViewController.swift

Lines changed: 61 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@ class BookmarksViewController: SiteTableViewController, ToolbarUrlActionsProtoco
1919
weak var toolbarUrlActionsDelegate: ToolbarUrlActionsDelegate?
2020
private weak var addBookmarksFolderOkAction: UIAlertAction?
2121

22-
private lazy var editBookmarksButton: UIBarButtonItem? = UIBarButtonItem().then {
22+
private lazy var editBookmarksButton = UIBarButtonItem().then {
2323
$0.image = UIImage(braveSystemNamed: "leo.edit.pencil")
2424
$0.style = .plain
2525
$0.target = self
2626
$0.action = #selector(onEditBookmarksButton)
2727
}
2828

29-
private lazy var addFolderButton: UIBarButtonItem? = UIBarButtonItem().then {
29+
private lazy var addFolderButton = UIBarButtonItem().then {
3030
$0.image = UIImage(braveSystemNamed: "leo.folder.new")
3131
$0.style = .plain
3232
$0.target = self
3333
$0.action = #selector(onAddBookmarksFolderButton)
3434
}
3535

36-
private lazy var importExportButton: UIBarButtonItem? = UIBarButtonItem().then {
36+
private lazy var importExportButton = UIBarButtonItem().then {
3737
$0.image = UIImage(braveSystemNamed: "leo.share.macos")
3838
$0.style = .plain
3939
$0.target = self
@@ -76,8 +76,6 @@ class BookmarksViewController: SiteTableViewController, ToolbarUrlActionsProtoco
7676
overlayDetails: EmptyOverlayStateDetails(title: Strings.noSearchResultsfound)
7777
)
7878

79-
private var bookmarksExportSuccessful = false
80-
8179
// MARK: Lifecycle
8280

8381
init(folder: Bookmarkv2?, bookmarkManager: BookmarkManager, isPrivateBrowsing: Bool) {
@@ -197,7 +195,7 @@ class BookmarksViewController: SiteTableViewController, ToolbarUrlActionsProtoco
197195
private func updateEditBookmarksButtonStatus() {
198196
guard let objectsCount = bookmarksFRC?.fetchedObjectsCount else { return }
199197

200-
editBookmarksButton?.isEnabled = objectsCount != 0
198+
editBookmarksButton.isEnabled = objectsCount != 0
201199
if tableView.isEditing && objectsCount == 0 {
202200
disableTableEditingMode()
203201
}
@@ -269,17 +267,20 @@ class BookmarksViewController: SiteTableViewController, ToolbarUrlActionsProtoco
269267
let alert = AlertController(title: nil, message: nil, preferredStyle: .actionSheet)
270268
alert.popoverPresentationController?.barButtonItem = sender
271269
let importAction = UIAlertAction(title: Strings.bookmarksImportAction, style: .default) {
272-
[weak self] _ in
270+
[unowned self] _ in
273271
let vc = UIDocumentPickerViewController(forOpeningContentTypes: [.html, .zip])
274272
vc.delegate = self
275-
self?.present(vc, animated: true)
273+
self.present(vc, animated: true)
276274
}
277275

278276
let exportAction = UIAlertAction(title: Strings.bookmarksExportAction, style: .default) {
279-
[weak self] _ in
280-
let fileUrl = FileManager.default.temporaryDirectory.appendingPathComponent("Bookmarks")
281-
.appendingPathExtension("html")
282-
self?.exportBookmarks(to: fileUrl)
277+
[unowned self] _ in
278+
Task {
279+
let fileUrl = FileManager.default.temporaryDirectory
280+
.appendingPathComponent("Bookmarks")
281+
.appendingPathExtension("html")
282+
await self.exportBookmarks(to: fileUrl)
283+
}
283284
}
284285

285286
let cancelAction = UIAlertAction(title: Strings.cancelButtonTitle, style: .cancel)
@@ -363,13 +364,13 @@ class BookmarksViewController: SiteTableViewController, ToolbarUrlActionsProtoco
363364

364365
updateEditBookmarksButton(editMode)
365366

366-
editBookmarksButton?.isEnabled = bookmarksFRC?.fetchedObjectsCount != 0
367-
addFolderButton?.isEnabled = !editMode
367+
editBookmarksButton.isEnabled = bookmarksFRC?.fetchedObjectsCount != 0
368+
addFolderButton.isEnabled = !editMode
368369
}
369370

370371
private func updateEditBookmarksButton(_ tableIsEditing: Bool) {
371-
self.editBookmarksButton?.title = tableIsEditing ? Strings.done : Strings.edit
372-
self.editBookmarksButton?.style = tableIsEditing ? .done : .plain
372+
editBookmarksButton.title = tableIsEditing ? Strings.done : Strings.edit
373+
editBookmarksButton.style = tableIsEditing ? .done : .plain
373374
}
374375

375376
private func addFolder(titled title: String) {
@@ -907,112 +908,82 @@ extension BookmarksViewController: UIDocumentPickerDelegate, UIDocumentInteracti
907908
return
908909
}
909910

910-
DispatchQueue.main.async {
911-
self.importBookmarks(from: url)
912-
self.documentInteractionController = nil
911+
Task { @MainActor in
912+
await importBookmarks(from: url)
913913
}
914914
}
915915

916916
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
917-
self.documentInteractionController = nil
917+
documentInteractionController = nil
918918
}
919919

920920
func documentInteractionControllerDidEndPreview(_ controller: UIDocumentInteractionController) {
921-
if let url = controller.url {
922-
Task {
923-
try await AsyncFileManager.default.removeItem(at: url)
924-
}
925-
}
926-
self.documentInteractionController = nil
921+
documentInteractionController = nil
922+
guard let url = controller.url else { return }
923+
Task { try await AsyncFileManager.default.removeItem(at: url) }
927924
}
928925

929926
func documentInteractionControllerDidDismissOptionsMenu(
930927
_ controller: UIDocumentInteractionController
931928
) {
932-
if let url = controller.url {
933-
Task {
934-
try await AsyncFileManager.default.removeItem(at: url)
935-
}
936-
}
937-
self.documentInteractionController = nil
938-
939-
if bookmarksExportSuccessful {
940-
bookmarksExportSuccessful = false
941-
942-
let alert = UIAlertController(
943-
title: Strings.Sync.bookmarksImportExportPopupTitle,
944-
message: Strings.Sync.bookmarksExportPopupSuccessMessage,
945-
preferredStyle: .alert
946-
)
947-
alert.addAction(UIAlertAction(title: Strings.OKString, style: .default, handler: nil))
948-
self.present(alert, animated: true, completion: nil)
949-
}
929+
documentInteractionController = nil
930+
guard let url = controller.url else { return }
931+
Task { try await AsyncFileManager.default.removeItem(at: url) }
950932
}
951933

952934
func documentInteractionControllerDidDismissOpenInMenu(
953935
_ controller: UIDocumentInteractionController
954936
) {
955-
if let url = controller.url {
956-
Task {
957-
try await AsyncFileManager.default.removeItem(at: url)
958-
}
959-
}
960-
self.documentInteractionController = nil
937+
documentInteractionController = nil
938+
guard let url = controller.url else { return }
939+
Task { try await AsyncFileManager.default.removeItem(at: url) }
961940
}
962941
}
963942

964943
// MARK: Export-Import Bookmarks
965944

966945
extension BookmarksViewController {
967946

968-
func importBookmarks(from url: URL) {
947+
@MainActor
948+
func importBookmarks(from url: URL) async {
969949
isLoading = true
950+
let success = await self.importExportUtility.importBookmarks(from: url)
951+
isLoading = false
952+
953+
let alert = UIAlertController(
954+
title: Strings.Sync.bookmarksImportExportPopupTitle,
955+
message: success
956+
? Strings.Sync.bookmarksImportPopupSuccessMessage
957+
: Strings.Sync.bookmarksImportPopupFailureMessage,
958+
preferredStyle: .alert
959+
)
960+
alert.addAction(UIAlertAction(title: Strings.OKString, style: .default, handler: nil))
961+
present(alert, animated: true, completion: nil)
962+
}
970963

971-
Task { @MainActor in
972-
let success = await self.importExportUtility.importBookmarks(from: url)
973-
self.isLoading = false
964+
@MainActor
965+
func exportBookmarks(to url: URL) async {
966+
isLoading = true
967+
let success = await self.importExportUtility.exportBookmarks(to: url)
968+
isLoading = false
974969

970+
if success {
971+
// Controller must be retained otherwise `AirDrop` and other sharing options will fail!
972+
documentInteractionController = UIDocumentInteractionController(url: url)
973+
guard let vc = documentInteractionController else { return }
974+
975+
vc.uti = UTType.html.identifier
976+
vc.name = "Bookmarks.html"
977+
vc.delegate = self
978+
vc.presentOptionsMenu(from: importExportButton, animated: true)
979+
} else {
975980
let alert = UIAlertController(
976981
title: Strings.Sync.bookmarksImportExportPopupTitle,
977-
message: success
978-
? Strings.Sync.bookmarksImportPopupSuccessMessage
979-
: Strings.Sync.bookmarksImportPopupFailureMessage,
982+
message: Strings.Sync.bookmarksExportPopupFailureMessage,
980983
preferredStyle: .alert
981984
)
982985
alert.addAction(UIAlertAction(title: Strings.OKString, style: .default, handler: nil))
983-
self.present(alert, animated: true, completion: nil)
984-
}
985-
}
986-
987-
func exportBookmarks(to url: URL) {
988-
isLoading = true
989-
990-
Task { @MainActor in
991-
let success = await self.importExportUtility.exportBookmarks(to: url)
992-
993-
self.isLoading = false
994-
995-
if success {
996-
self.bookmarksExportSuccessful = true
997-
998-
// Controller must be retained otherwise `AirDrop` and other sharing options will fail!
999-
self.documentInteractionController = UIDocumentInteractionController(url: url)
1000-
guard let vc = self.documentInteractionController else { return }
1001-
vc.uti = UTType.html.identifier
1002-
vc.name = "Bookmarks.html"
1003-
vc.delegate = self
1004-
1005-
guard let importExportButton = self.importExportButton else { return }
1006-
vc.presentOptionsMenu(from: importExportButton, animated: true)
1007-
} else {
1008-
let alert = UIAlertController(
1009-
title: Strings.Sync.bookmarksImportExportPopupTitle,
1010-
message: Strings.Sync.bookmarksExportPopupFailureMessage,
1011-
preferredStyle: .alert
1012-
)
1013-
alert.addAction(UIAlertAction(title: Strings.OKString, style: .default, handler: nil))
1014-
self.present(alert, animated: true, completion: nil)
1015-
}
986+
present(alert, animated: true, completion: nil)
1016987
}
1017988
}
1018989

0 commit comments

Comments
 (0)