@@ -16,9 +16,7 @@ final class ImportBlockListViewController: UIViewController, UIDocumentPickerDel
16
16
var importCompletion : ( ( ) -> ( ) ) ?
17
17
18
18
var titleName = " Import Block List "
19
-
20
- var newListName = " "
21
-
19
+
22
20
private lazy var navigationView : ConfiguredNavigationView = {
23
21
let view = ConfiguredNavigationView ( )
24
22
view. rightNavButton. setTitle ( NSLocalizedString ( " CANCEL " , comment: " " ) , for: . normal)
@@ -173,11 +171,6 @@ final class ImportBlockListViewController: UIViewController, UIDocumentPickerDel
173
171
selectFromFilesButton. anchors. trailing. marginsPin ( )
174
172
selectFromFilesButton. anchors. height. equal ( 56 )
175
173
}
176
-
177
- override func viewWillAppear( _ animated: Bool ) {
178
- super. viewWillAppear ( animated)
179
- showCreateList ( )
180
- }
181
174
}
182
175
183
176
// MARK: - Private functions
@@ -189,6 +182,51 @@ extension ImportBlockListViewController {
189
182
dismiss ( animated: true )
190
183
}
191
184
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
+
192
230
func documentPicker( _ controller: UIDocumentPickerViewController , didPickDocumentsAt urls: [ URL ] ) {
193
231
194
232
guard let url = urls. first else {
@@ -197,40 +235,12 @@ extension ImportBlockListViewController {
197
235
198
236
do {
199
237
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
232
241
}
233
242
243
+ showCreateList ( forDomainList: content)
234
244
} catch {
235
245
dismiss ( animated: true ) {
236
246
let alert = UIAlertController ( title: NSLocalizedString ( " Error " , comment: " " ) ,
@@ -244,15 +254,12 @@ extension ImportBlockListViewController {
244
254
}
245
255
}
246
256
247
- func showCreateList( ) {
257
+ private func showCreateList( forDomainList domains : Set < String > ) {
248
258
let alertController = UIAlertController ( title: " Create New List " , message: nil , preferredStyle: . alert)
249
259
250
260
let saveAction = UIAlertAction ( title: " Save " , style: . default) { [ weak self] ( _) in
251
261
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)
256
263
}
257
264
}
258
265
@@ -278,7 +285,37 @@ extension ImportBlockListViewController {
278
285
alertController. addAction ( saveAction)
279
286
alertController. addAction ( cancelAction)
280
287
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
+ )
281
317
318
+ present ( alertController, animated: true )
282
319
}
283
320
284
321
func documentPickerWasCancelled( _ controller: UIDocumentPickerViewController ) {
0 commit comments