Skip to content

Commit 0bc8c0f

Browse files
authored
[iOS] - Remove Bookmarks Export Popup (#28136)
* Refactor bookmarks export to async-await.
1 parent 3265ec7 commit 0bc8c0f

File tree

1 file changed

+61
-88
lines changed

1 file changed

+61
-88
lines changed

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

Lines changed: 61 additions & 88 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
}
@@ -270,16 +268,21 @@ class BookmarksViewController: SiteTableViewController, ToolbarUrlActionsProtoco
270268
alert.popoverPresentationController?.barButtonItem = sender
271269
let importAction = UIAlertAction(title: Strings.bookmarksImportAction, style: .default) {
272270
[weak self] _ in
271+
guard let self = self else { return }
273272
let vc = UIDocumentPickerViewController(forOpeningContentTypes: [.html, .zip])
274273
vc.delegate = self
275-
self?.present(vc, animated: true)
274+
self.present(vc, animated: true)
276275
}
277276

278277
let exportAction = UIAlertAction(title: Strings.bookmarksExportAction, style: .default) {
279278
[weak self] _ in
280-
let fileUrl = FileManager.default.temporaryDirectory.appendingPathComponent("Bookmarks")
281-
.appendingPathExtension("html")
282-
self?.exportBookmarks(to: fileUrl)
279+
Task {
280+
guard let self = self else { return }
281+
let fileUrl = FileManager.default.temporaryDirectory
282+
.appendingPathComponent("Bookmarks")
283+
.appendingPathExtension("html")
284+
await self.exportBookmarks(to: fileUrl)
285+
}
283286
}
284287

285288
let cancelAction = UIAlertAction(title: Strings.cancelButtonTitle, style: .cancel)
@@ -363,13 +366,13 @@ class BookmarksViewController: SiteTableViewController, ToolbarUrlActionsProtoco
363366

364367
updateEditBookmarksButton(editMode)
365368

366-
editBookmarksButton?.isEnabled = bookmarksFRC?.fetchedObjectsCount != 0
367-
addFolderButton?.isEnabled = !editMode
369+
editBookmarksButton.isEnabled = bookmarksFRC?.fetchedObjectsCount != 0
370+
addFolderButton.isEnabled = !editMode
368371
}
369372

370373
private func updateEditBookmarksButton(_ tableIsEditing: Bool) {
371-
self.editBookmarksButton?.title = tableIsEditing ? Strings.done : Strings.edit
372-
self.editBookmarksButton?.style = tableIsEditing ? .done : .plain
374+
editBookmarksButton.title = tableIsEditing ? Strings.done : Strings.edit
375+
editBookmarksButton.style = tableIsEditing ? .done : .plain
373376
}
374377

375378
private func addFolder(titled title: String) {
@@ -907,112 +910,82 @@ extension BookmarksViewController: UIDocumentPickerDelegate, UIDocumentInteracti
907910
return
908911
}
909912

910-
DispatchQueue.main.async {
911-
self.importBookmarks(from: url)
912-
self.documentInteractionController = nil
913+
Task { @MainActor in
914+
await importBookmarks(from: url)
913915
}
914916
}
915917

916918
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
917-
self.documentInteractionController = nil
919+
documentInteractionController = nil
918920
}
919921

920922
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
923+
documentInteractionController = nil
924+
guard let url = controller.url else { return }
925+
Task { try await AsyncFileManager.default.removeItem(at: url) }
927926
}
928927

929928
func documentInteractionControllerDidDismissOptionsMenu(
930929
_ controller: UIDocumentInteractionController
931930
) {
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-
}
931+
documentInteractionController = nil
932+
guard let url = controller.url else { return }
933+
Task { try await AsyncFileManager.default.removeItem(at: url) }
950934
}
951935

952936
func documentInteractionControllerDidDismissOpenInMenu(
953937
_ controller: UIDocumentInteractionController
954938
) {
955-
if let url = controller.url {
956-
Task {
957-
try await AsyncFileManager.default.removeItem(at: url)
958-
}
959-
}
960-
self.documentInteractionController = nil
939+
documentInteractionController = nil
940+
guard let url = controller.url else { return }
941+
Task { try await AsyncFileManager.default.removeItem(at: url) }
961942
}
962943
}
963944

964945
// MARK: Export-Import Bookmarks
965946

966947
extension BookmarksViewController {
967948

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

971-
Task { @MainActor in
972-
let success = await self.importExportUtility.importBookmarks(from: url)
973-
self.isLoading = false
966+
@MainActor
967+
func exportBookmarks(to url: URL) async {
968+
isLoading = true
969+
let success = await self.importExportUtility.exportBookmarks(to: url)
970+
isLoading = false
971+
972+
if success {
973+
// Controller must be retained otherwise `AirDrop` and other sharing options will fail!
974+
documentInteractionController = UIDocumentInteractionController(url: url)
975+
guard let vc = documentInteractionController else { return }
974976

977+
vc.uti = UTType.html.identifier
978+
vc.name = "Bookmarks.html"
979+
vc.delegate = self
980+
vc.presentOptionsMenu(from: importExportButton, animated: true)
981+
} else {
975982
let alert = UIAlertController(
976983
title: Strings.Sync.bookmarksImportExportPopupTitle,
977-
message: success
978-
? Strings.Sync.bookmarksImportPopupSuccessMessage
979-
: Strings.Sync.bookmarksImportPopupFailureMessage,
984+
message: Strings.Sync.bookmarksExportPopupFailureMessage,
980985
preferredStyle: .alert
981986
)
982987
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-
}
988+
present(alert, animated: true, completion: nil)
1016989
}
1017990
}
1018991

0 commit comments

Comments
 (0)