Skip to content

Commit 697a4b4

Browse files
committedJun 19, 2023
update import domain list flow
https://app.clickup.com/t/14199473/KB-5705
1 parent d82d479 commit 697a4b4

File tree

1 file changed

+82
-45
lines changed

1 file changed

+82
-45
lines changed
 

‎LockdowniOS/ImportBlockListViewController.swift

+82-45
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ final class ImportBlockListViewController: UIViewController, UIDocumentPickerDel
1616
var importCompletion: (() -> ())?
1717

1818
var titleName = "Import Block List"
19-
20-
var newListName = ""
21-
19+
2220
private lazy var navigationView: ConfiguredNavigationView = {
2321
let view = ConfiguredNavigationView()
2422
view.rightNavButton.setTitle(NSLocalizedString("CANCEL", comment: ""), for: .normal)
@@ -173,11 +171,6 @@ final class ImportBlockListViewController: UIViewController, UIDocumentPickerDel
173171
selectFromFilesButton.anchors.trailing.marginsPin()
174172
selectFromFilesButton.anchors.height.equal(56)
175173
}
176-
177-
override func viewWillAppear(_ animated: Bool) {
178-
super.viewWillAppear(animated)
179-
showCreateList()
180-
}
181174
}
182175

183176
// MARK: - Private functions
@@ -189,6 +182,51 @@ extension ImportBlockListViewController {
189182
dismiss(animated: true)
190183
}
191184

185+
private func saveImportedDomains(
186+
_ content: Set<String>,
187+
toNewListName newListName: String
188+
) {
189+
if !content.isEmpty {
190+
addBlockedList(listName: newListName)
191+
var allData = getBlockedLists()
192+
193+
let importedList = UserBlockListsGroup(name: newListName, domains: content)
194+
195+
allData.userBlockListsDefaults[importedList.name] = importedList
196+
197+
let encodedData = try? JSONEncoder().encode(allData)
198+
defaults.set(encodedData, forKey: kUserBlockedLists)
199+
200+
importCompletion?()
201+
}
202+
203+
closeScreen(withSuccess: !content.isEmpty)
204+
}
205+
206+
private func closeScreen(withSuccess success: Bool) {
207+
dismiss(animated: true) {
208+
let title = success
209+
? NSLocalizedString("Success!", comment: "")
210+
: NSLocalizedString("Error", comment: "")
211+
let message = success
212+
? NSLocalizedString("The list has been imported successfully. You can start blocking the list's domains", comment: "")
213+
: NSLocalizedString("Your list of domains is empty or in the wrong format", comment: "")
214+
let alert = UIAlertController(
215+
title: title,
216+
message: message,
217+
preferredStyle: .alert
218+
)
219+
alert.addAction(
220+
UIAlertAction(
221+
title: NSLocalizedString("Close", comment: ""),
222+
style: .default,
223+
handler: nil
224+
)
225+
)
226+
UIApplication.getTopMostViewController()?.present(alert, animated: true, completion: nil)
227+
}
228+
}
229+
192230
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
193231

194232
guard let url = urls.first else {
@@ -197,40 +235,12 @@ extension ImportBlockListViewController {
197235

198236
do {
199237
let content = csvProcessing(data: try String(contentsOf: url, encoding: .utf8))
200-
201-
if !content.isEmpty {
202-
var allData = getBlockedLists()
203-
204-
let importedList = UserBlockListsGroup(name: newListName, domains: content)
205-
206-
allData.userBlockListsDefaults[importedList.name] = importedList
207-
208-
let encodedData = try? JSONEncoder().encode(allData)
209-
defaults.set(encodedData, forKey: kUserBlockedLists)
210-
211-
importCompletion?()
212-
}
213-
214-
dismiss(animated: true) {
215-
if !content.isEmpty {
216-
let alert = UIAlertController(title: NSLocalizedString("Success!", comment: ""),
217-
message: NSLocalizedString("The list has been imported successfully. You can start blocking the list's domains", comment: ""),
218-
preferredStyle: .alert)
219-
alert.addAction(UIAlertAction(title: NSLocalizedString("Close", comment: ""),
220-
style: .default,
221-
handler: nil))
222-
UIApplication.getTopMostViewController()?.present(alert, animated: true, completion: nil)
223-
} else {
224-
let alert = UIAlertController(title: NSLocalizedString("Error", comment: ""),
225-
message: NSLocalizedString("Your list of domains is empty or in the wrong format", comment: ""),
226-
preferredStyle: .alert)
227-
alert.addAction(UIAlertAction(title: NSLocalizedString("Close", comment: ""),
228-
style: .default,
229-
handler: nil))
230-
UIApplication.getTopMostViewController()?.present(alert, animated: true, completion: nil)
231-
}
238+
guard !content.isEmpty else {
239+
closeScreen(withSuccess: false)
240+
return
232241
}
233242

243+
showCreateList(forDomainList: content)
234244
} catch {
235245
dismiss(animated: true) {
236246
let alert = UIAlertController(title: NSLocalizedString("Error", comment: ""),
@@ -244,15 +254,12 @@ extension ImportBlockListViewController {
244254
}
245255
}
246256

247-
func showCreateList() {
257+
private func showCreateList(forDomainList domains: Set<String>) {
248258
let alertController = UIAlertController(title: "Create New List", message: nil, preferredStyle: .alert)
249259

250260
let saveAction = UIAlertAction(title: "Save", style: .default) { [weak self] (_) in
251261
if let txtField = alertController.textFields?.first, let text = txtField.text {
252-
253-
guard let self else { return }
254-
self.newListName = text
255-
addBlockedList(listName: self.newListName)
262+
self?.validateListName(text, forDomainList: domains)
256263
}
257264
}
258265

@@ -278,7 +285,37 @@ extension ImportBlockListViewController {
278285
alertController.addAction(saveAction)
279286
alertController.addAction(cancelAction)
280287
self.present(alertController, animated: true, completion: nil)
288+
}
289+
290+
private func validateListName(_ name: String, forDomainList domains: Set<String>) {
291+
guard !getBlockedLists().userBlockListsDefaults.keys.contains(name) else {
292+
showAlertAboutExistingListName { [weak self] in
293+
self?.showCreateList(forDomainList: domains)
294+
}
295+
return
296+
}
297+
298+
self.saveImportedDomains(domains, toNewListName: name)
299+
}
300+
301+
private func showAlertAboutExistingListName(completion: @escaping () -> Void) {
302+
let alertController = UIAlertController(
303+
title: NSLocalizedString("This list name is already exist!", comment: ""),
304+
message: NSLocalizedString("Please choose another name.", comment: ""),
305+
preferredStyle: .alert
306+
)
307+
308+
alertController.addAction(
309+
.init(
310+
title: NSLocalizedString("Ok", comment: ""),
311+
style: .default,
312+
handler: { _ in
313+
completion()
314+
}
315+
)
316+
)
281317

318+
present(alertController, animated: true)
282319
}
283320

284321
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {

0 commit comments

Comments
 (0)
Please sign in to comment.