@@ -19,21 +19,21 @@ class BookmarksViewController: SiteTableViewController, ToolbarUrlActionsProtoco
19
19
weak var toolbarUrlActionsDelegate : ToolbarUrlActionsDelegate ?
20
20
private weak var addBookmarksFolderOkAction : UIAlertAction ?
21
21
22
- private lazy var editBookmarksButton : UIBarButtonItem ? = UIBarButtonItem ( ) . then {
22
+ private lazy var editBookmarksButton = UIBarButtonItem ( ) . then {
23
23
$0. image = UIImage ( braveSystemNamed: " leo.edit.pencil " )
24
24
$0. style = . plain
25
25
$0. target = self
26
26
$0. action = #selector( onEditBookmarksButton)
27
27
}
28
28
29
- private lazy var addFolderButton : UIBarButtonItem ? = UIBarButtonItem ( ) . then {
29
+ private lazy var addFolderButton = UIBarButtonItem ( ) . then {
30
30
$0. image = UIImage ( braveSystemNamed: " leo.folder.new " )
31
31
$0. style = . plain
32
32
$0. target = self
33
33
$0. action = #selector( onAddBookmarksFolderButton)
34
34
}
35
35
36
- private lazy var importExportButton : UIBarButtonItem ? = UIBarButtonItem ( ) . then {
36
+ private lazy var importExportButton = UIBarButtonItem ( ) . then {
37
37
$0. image = UIImage ( braveSystemNamed: " leo.share.macos " )
38
38
$0. style = . plain
39
39
$0. target = self
@@ -76,8 +76,6 @@ class BookmarksViewController: SiteTableViewController, ToolbarUrlActionsProtoco
76
76
overlayDetails: EmptyOverlayStateDetails ( title: Strings . noSearchResultsfound)
77
77
)
78
78
79
- private var bookmarksExportSuccessful = false
80
-
81
79
// MARK: Lifecycle
82
80
83
81
init ( folder: Bookmarkv2 ? , bookmarkManager: BookmarkManager , isPrivateBrowsing: Bool ) {
@@ -197,7 +195,7 @@ class BookmarksViewController: SiteTableViewController, ToolbarUrlActionsProtoco
197
195
private func updateEditBookmarksButtonStatus( ) {
198
196
guard let objectsCount = bookmarksFRC? . fetchedObjectsCount else { return }
199
197
200
- editBookmarksButton? . isEnabled = objectsCount != 0
198
+ editBookmarksButton. isEnabled = objectsCount != 0
201
199
if tableView. isEditing && objectsCount == 0 {
202
200
disableTableEditingMode ( )
203
201
}
@@ -270,16 +268,21 @@ class BookmarksViewController: SiteTableViewController, ToolbarUrlActionsProtoco
270
268
alert. popoverPresentationController? . barButtonItem = sender
271
269
let importAction = UIAlertAction ( title: Strings . bookmarksImportAction, style: . default) {
272
270
[ weak self] _ in
271
+ guard let self = self else { return }
273
272
let vc = UIDocumentPickerViewController ( forOpeningContentTypes: [ . html, . zip] )
274
273
vc. delegate = self
275
- self ? . present ( vc, animated: true )
274
+ self . present ( vc, animated: true )
276
275
}
277
276
278
277
let exportAction = UIAlertAction ( title: Strings . bookmarksExportAction, style: . default) {
279
278
[ 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
+ }
283
286
}
284
287
285
288
let cancelAction = UIAlertAction ( title: Strings . cancelButtonTitle, style: . cancel)
@@ -363,13 +366,13 @@ class BookmarksViewController: SiteTableViewController, ToolbarUrlActionsProtoco
363
366
364
367
updateEditBookmarksButton ( editMode)
365
368
366
- editBookmarksButton? . isEnabled = bookmarksFRC? . fetchedObjectsCount != 0
367
- addFolderButton? . isEnabled = !editMode
369
+ editBookmarksButton. isEnabled = bookmarksFRC? . fetchedObjectsCount != 0
370
+ addFolderButton. isEnabled = !editMode
368
371
}
369
372
370
373
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
373
376
}
374
377
375
378
private func addFolder( titled title: String ) {
@@ -907,112 +910,82 @@ extension BookmarksViewController: UIDocumentPickerDelegate, UIDocumentInteracti
907
910
return
908
911
}
909
912
910
- DispatchQueue . main. async {
911
- self . importBookmarks ( from: url)
912
- self . documentInteractionController = nil
913
+ Task { @MainActor in
914
+ await importBookmarks ( from: url)
913
915
}
914
916
}
915
917
916
918
func documentPickerWasCancelled( _ controller: UIDocumentPickerViewController ) {
917
- self . documentInteractionController = nil
919
+ documentInteractionController = nil
918
920
}
919
921
920
922
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) }
927
926
}
928
927
929
928
func documentInteractionControllerDidDismissOptionsMenu(
930
929
_ controller: UIDocumentInteractionController
931
930
) {
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) }
950
934
}
951
935
952
936
func documentInteractionControllerDidDismissOpenInMenu(
953
937
_ controller: UIDocumentInteractionController
954
938
) {
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) }
961
942
}
962
943
}
963
944
964
945
// MARK: Export-Import Bookmarks
965
946
966
947
extension BookmarksViewController {
967
948
968
- func importBookmarks( from url: URL ) {
949
+ @MainActor
950
+ func importBookmarks( from url: URL ) async {
969
951
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
+ }
970
965
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 }
974
976
977
+ vc. uti = UTType . html. identifier
978
+ vc. name = " Bookmarks.html "
979
+ vc. delegate = self
980
+ vc. presentOptionsMenu ( from: importExportButton, animated: true )
981
+ } else {
975
982
let alert = UIAlertController (
976
983
title: Strings . Sync. bookmarksImportExportPopupTitle,
977
- message: success
978
- ? Strings . Sync. bookmarksImportPopupSuccessMessage
979
- : Strings . Sync. bookmarksImportPopupFailureMessage,
984
+ message: Strings . Sync. bookmarksExportPopupFailureMessage,
980
985
preferredStyle: . alert
981
986
)
982
987
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 )
1016
989
}
1017
990
}
1018
991
0 commit comments