forked from duckduckgo/iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddOrEditBookmarkViewController.swift
184 lines (147 loc) · 5.78 KB
/
AddOrEditBookmarkViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//
// AddOrEditBookmarkViewController.swift
// DuckDuckGo
//
// Copyright © 2021 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import UIKit
import Core
import CoreData
import Bookmarks
import Persistence
import Combine
import DDGSync
import WidgetKit
protocol AddOrEditBookmarkViewControllerDelegate: AnyObject {
func finishedEditing(_: AddOrEditBookmarkViewController, entityID: NSManagedObjectID)
}
class AddOrEditBookmarkViewController: UIViewController {
weak var delegate: AddOrEditBookmarkViewControllerDelegate?
private var foldersViewController: BookmarkFoldersViewController?
private let viewModel: BookmarkEditorViewModel
private let bookmarksDatabase: CoreDataDatabase
private let syncService: DDGSyncing
private var viewModelCancellable: AnyCancellable?
init?(coder: NSCoder,
editingEntityID: NSManagedObjectID,
bookmarksDatabase: CoreDataDatabase,
syncService: DDGSyncing) {
self.bookmarksDatabase = bookmarksDatabase
self.viewModel = BookmarkEditorViewModel(editingEntityID: editingEntityID,
bookmarksDatabase: bookmarksDatabase,
syncService: syncService)
self.syncService = syncService
super.init(coder: coder)
}
init?(coder: NSCoder,
parentFolderID: NSManagedObjectID?,
bookmarksDatabase: CoreDataDatabase,
syncService: DDGSyncing) {
self.bookmarksDatabase = bookmarksDatabase
self.viewModel = BookmarkEditorViewModel(creatingFolderWithParentID: parentFolderID,
bookmarksDatabase: bookmarksDatabase,
syncService: syncService)
self.syncService = syncService
super.init(coder: coder)
}
// If you hit this constructor you probably decided to try and instanciate this VC directly.
// However, if it is part of a navigation controller stack this construct gets called.
// Check the segue actions defined in the storyboard.
required init?(coder: NSCoder) {
fatalError("Not implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
updateTitle()
updateSaveButton()
applyTheme(ThemeManager.shared.currentTheme)
viewModelCancellable = viewModel.externalUpdates.sink { [weak self] _ in
self?.foldersViewController?.refresh()
}
}
func updateTitle() {
if viewModel.isNew {
title = UserText.addFolderScreenTitle
} else {
title = viewModel.bookmark.isFolder ? UserText.editFolderScreenTitle : UserText.editBookmarkScreenTitle
}
}
func updateSaveButton() {
guard let saveButton = navigationItem.rightBarButtonItem else { return }
if viewModel.canSave {
saveButton.isEnabled = true
} else {
saveButton.isEnabled = false
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let controller = segue.destination as? BookmarkFoldersViewController {
controller.delegate = self
controller.viewModel = viewModel
foldersViewController = controller
}
}
@IBAction func onCancelPressed(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
@IBAction func onSavePressed(_ sender: Any) {
saveAndDismiss()
}
func saveAndDismiss() {
viewModel.save()
WidgetCenter.shared.reloadAllTimelines()
self.delegate?.finishedEditing(self, entityID: viewModel.bookmark.objectID)
dismiss(animated: true, completion: nil)
syncService.scheduler.notifyDataChanged()
}
@IBSegueAction func onCreateEditor(_ coder: NSCoder, sender: Any?, segueIdentifier: String?) -> AddOrEditBookmarkViewController? {
guard let controller = AddOrEditBookmarkViewController(
coder: coder,
parentFolderID: viewModel.bookmark.parent?.objectID,
bookmarksDatabase: bookmarksDatabase,
syncService: syncService
) else {
fatalError("Failed to create controller")
}
controller.delegate = self
return controller
}
}
extension AddOrEditBookmarkViewController: BookmarkFoldersViewControllerDelegate {
func textDidChange(_ controller: BookmarkFoldersViewController) {
updateSaveButton()
}
func textDidReturn(_ controller: BookmarkFoldersViewController) {
if viewModel.canSave {
saveAndDismiss()
}
}
func addFolder(_ controller: BookmarkFoldersViewController) {
performSegue(withIdentifier: "AddFolder", sender: nil)
}
}
extension AddOrEditBookmarkViewController: AddOrEditBookmarkViewControllerDelegate {
func finishedEditing(_ controller: AddOrEditBookmarkViewController, entityID: NSManagedObjectID) {
viewModel.setParentWithID(entityID)
foldersViewController?.refresh()
}
}
extension AddOrEditBookmarkViewController: Themable {
func decorate(with theme: Theme) {
decorateNavigationBar(with: theme)
decorateToolbar(with: theme)
overrideSystemTheme(with: theme)
}
}