@@ -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
}
@@ -269,17 +267,20 @@ class BookmarksViewController: SiteTableViewController, ToolbarUrlActionsProtoco
269
267
let alert = AlertController ( title: nil , message: nil , preferredStyle: . actionSheet)
270
268
alert. popoverPresentationController? . barButtonItem = sender
271
269
let importAction = UIAlertAction ( title: Strings . bookmarksImportAction, style: . default) {
272
- [ weak self] _ in
270
+ [ unowned self] _ in
273
271
let vc = UIDocumentPickerViewController ( forOpeningContentTypes: [ . html, . zip] )
274
272
vc. delegate = self
275
- self ? . present ( vc, animated: true )
273
+ self . present ( vc, animated: true )
276
274
}
277
275
278
276
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
+ }
283
284
}
284
285
285
286
let cancelAction = UIAlertAction ( title: Strings . cancelButtonTitle, style: . cancel)
@@ -363,13 +364,13 @@ class BookmarksViewController: SiteTableViewController, ToolbarUrlActionsProtoco
363
364
364
365
updateEditBookmarksButton ( editMode)
365
366
366
- editBookmarksButton? . isEnabled = bookmarksFRC? . fetchedObjectsCount != 0
367
- addFolderButton? . isEnabled = !editMode
367
+ editBookmarksButton. isEnabled = bookmarksFRC? . fetchedObjectsCount != 0
368
+ addFolderButton. isEnabled = !editMode
368
369
}
369
370
370
371
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
373
374
}
374
375
375
376
private func addFolder( titled title: String ) {
@@ -907,112 +908,82 @@ extension BookmarksViewController: UIDocumentPickerDelegate, UIDocumentInteracti
907
908
return
908
909
}
909
910
910
- DispatchQueue . main. async {
911
- self . importBookmarks ( from: url)
912
- self . documentInteractionController = nil
911
+ Task { @MainActor in
912
+ await importBookmarks ( from: url)
913
913
}
914
914
}
915
915
916
916
func documentPickerWasCancelled( _ controller: UIDocumentPickerViewController ) {
917
- self . documentInteractionController = nil
917
+ documentInteractionController = nil
918
918
}
919
919
920
920
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) }
927
924
}
928
925
929
926
func documentInteractionControllerDidDismissOptionsMenu(
930
927
_ controller: UIDocumentInteractionController
931
928
) {
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) }
950
932
}
951
933
952
934
func documentInteractionControllerDidDismissOpenInMenu(
953
935
_ controller: UIDocumentInteractionController
954
936
) {
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) }
961
940
}
962
941
}
963
942
964
943
// MARK: Export-Import Bookmarks
965
944
966
945
extension BookmarksViewController {
967
946
968
- func importBookmarks( from url: URL ) {
947
+ @MainActor
948
+ func importBookmarks( from url: URL ) async {
969
949
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
+ }
970
963
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
974
969
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 {
975
980
let alert = UIAlertController (
976
981
title: Strings . Sync. bookmarksImportExportPopupTitle,
977
- message: success
978
- ? Strings . Sync. bookmarksImportPopupSuccessMessage
979
- : Strings . Sync. bookmarksImportPopupFailureMessage,
982
+ message: Strings . Sync. bookmarksExportPopupFailureMessage,
980
983
preferredStyle: . alert
981
984
)
982
985
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 )
1016
987
}
1017
988
}
1018
989
0 commit comments