This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
ArtistsTableViewController.swift
504 lines (434 loc) · 19.2 KB
/
ArtistsTableViewController.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
//
// ArtistsTableViewController.swift
// Numu Tracker
//
// Created by Bradley Root on 10/15/16.
// Copyright © 2016 Numu Tracker. All rights reserved.
//
import UIKit
import Crashlytics
import Kingfisher
class ArtistsTableViewController: UITableViewController, UISearchBarDelegate, UISearchResultsUpdating {
@IBOutlet weak var importAppleMusicActivityIndicator: UIActivityIndicatorView!
@IBOutlet weak var importAppleMusicButton: UIButton!
@IBAction func importAppleMusicButton(_ sender: Any) {
importAppleMusicActivityIndicator.startAnimating()
let importAMOperation = ImportAppleMusicOperation()
let queue = OperationQueue()
importAMOperation.qualityOfService = .userInteractive
importAMOperation.completionBlock = {
DispatchQueue.main.async {
self.importAppleMusicActivityIndicator.stopAnimating()
}
}
queue.addOperation(importAMOperation)
}
@IBOutlet weak var importSpotifyActivityIndicator: UIActivityIndicatorView!
@IBOutlet weak var importSpotifyButton: NumuUIButton!
@IBAction func importSpotifyButton(_ sender: NumuUIButton) {
importSpotifyActivityIndicator.startAnimating()
let importSpotifyOperation = ImportSpotifyOperation()
let queue = OperationQueue()
importSpotifyOperation.qualityOfService = .userInteractive
importSpotifyOperation.completionBlock = {
DispatchQueue.main.async {
self.importSpotifyActivityIndicator.stopAnimating()
}
}
queue.addOperation(importSpotifyOperation)
}
var artists: [ArtistItem] = [] {
didSet {
if viewState == .user {
artistsDictionary = [:]
artistsSectionTitles = []
for artist in artists {
var artistKey = String(artist.artistName.prefix(1).uppercased())
switch sortMethod {
case "name_first":
artistKey = String(artist.artistName.prefix(1).uppercased())
if CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: artistKey))
|| !CharacterSet.alphanumerics.isSuperset(of: CharacterSet(charactersIn: artistKey)) {
artistKey = "#"
}
case "name":
artistKey = String(artist.sortName.prefix(1).uppercased())
if CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: artistKey))
|| !CharacterSet.alphanumerics.isSuperset(of: CharacterSet(charactersIn: artistKey)) {
artistKey = "#"
}
default:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMMM d, yyyy"
if let date = dateFormatter.date(from: artist.recentRelease) {
dateFormatter.dateFormat = "yyyy"
if artist.recentRelease == "December 31, 1969" {
artistKey = "None"
} else {
artistKey = String(dateFormatter.string(from: date).prefix(4))
}
} else {
artistKey = "None"
}
}
if var artistValues = artistsDictionary[artistKey] {
artistValues.append(artist)
artistsDictionary[artistKey] = artistValues
} else {
artistsDictionary[artistKey] = [artist]
}
}
artistsSectionTitles = [String](artistsDictionary.keys)
switch sortMethod {
case "name_first":
artistsSectionTitles = artistsSectionTitles.sorted(by: { $0 < $1 })
if !artistsSectionTitles.isEmpty {
if artistsSectionTitles[0] == "#" {
artistsSectionTitles.remove(at: 0)
artistsSectionTitles.append("#")
}
}
case "name":
artistsSectionTitles = artistsSectionTitles.sorted(by: { $0 < $1 })
if !artistsSectionTitles.isEmpty {
if artistsSectionTitles[0] == "#" {
artistsSectionTitles.remove(at: 0)
artistsSectionTitles.append("#")
}
}
default:
artistsSectionTitles = artistsSectionTitles.sorted(by: { $0 > $1 })
if !artistsSectionTitles.isEmpty {
if artistsSectionTitles[0] == "None" {
artistsSectionTitles.remove(at: 0)
artistsSectionTitles.append("None")
}
}
}
}
}
}
var artistsDictionary: [String: [ArtistItem]] = [:]
var artistsSectionTitles: [String] = []
var sortMethod: String = UserDefaults.standard.string(forKey: "sortArtists") ?? "name" {
didSet {
UserDefaults.standard.set(self.sortMethod, forKey: "sortArtists")
loadArtists()
}
}
var lastSelectedArtistId: String = ""
var lastSelectedArtistName: String = ""
var searchTerms: String?
@IBOutlet weak var sortButton: UIBarButtonItem!
@IBAction func sortButtonAction(_ sender: Any) {
let sortView = NumuSortView()
sortView.providesPresentationContextTransitionStyle = true
sortView.definesPresentationContext = true
sortView.modalPresentationStyle = UIModalPresentationStyle.overFullScreen
sortView.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
sortView.modalPresentationCapturesStatusBarAppearance = true
sortView.sortDelegate = self
if let appDelegate = UIApplication.shared.delegate,
let appWindow = appDelegate.window!,
let rootViewController = appWindow.rootViewController {
rootViewController.present(sortView, animated: true, completion: nil)
}
}
enum States {
case user, search
}
var viewState = States.user {
didSet {
if viewState == .user {
self.navigationItem.title = "Your Artists"
self.sortButton.isEnabled = true
} else {
self.navigationItem.title = "Search Artists"
self.sortButton.isEnabled = false
}
}
}
@IBOutlet var noResultsView: UIView!
@IBOutlet var noSearchResultsView: UIView!
var artist: ArtistItem?
let searchController = UISearchController(searchResultsController: nil)
@IBOutlet var footerView: UIView!
fileprivate func loadArtists() {
// Get list of artists...
NumuClient.shared.getArtists(sortBy: self.sortMethod) {[weak self](artists) in
self?.artists = artists
DispatchQueue.main.async(execute: {
self?.loadTable()
})
}
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = footerView
tableView.backgroundView = UIView()
tableView.keyboardDismissMode = .onDrag
searchController.searchBar.placeholder = "Search all artists"
searchController.searchBar.showsCancelButton = false
searchController.searchBar.delegate = self
searchController.searchBar.barStyle = .blackTranslucent
searchController.searchBar.keyboardType = .default
searchController.searchBar.keyboardAppearance = .dark
searchController.searchBar.searchBarStyle = .default
searchController.searchBar.backgroundColor = .black
searchController.searchBar.tintColor = .white
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
searchController.definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar
sortButton.tintColor = UIColor.white
self.view?.snapshotView(afterScreenUpdates: true)
importAppleMusicButton.backgroundColor = .clear
importAppleMusicButton.layer.cornerRadius = 5
importAppleMusicButton.layer.borderWidth = 1
importAppleMusicButton.layer.borderColor = UIColor.gray.cgColor
importSpotifyButton.backgroundColor = .clear
importSpotifyButton.layer.cornerRadius = 5
importSpotifyButton.layer.borderWidth = 1
importSpotifyButton.layer.borderColor = UIColor.gray.cgColor
var newFrame = noResultsView.frame
var height: CGFloat = self.tableView.bounds.height
height -= UIApplication.shared.statusBarFrame.size.height
height -= (self.navigationController?.navigationBar.frame.size.height)!
height -= (self.tabBarController?.tabBar.frame.size.height)!
newFrame.size.height = height
noResultsView.frame = newFrame
loadArtists()
NotificationCenter.default.addObserver(self,
selector: #selector(actOnImportNotification),
name: .UpdatedArtists,
object: nil)
self.tableView.addSubview(self.artistRefreshControl)
Answers.logCustomEvent(withName: "Your Artists View", customAttributes: nil)
}
deinit {
NotificationCenter.default.removeObserver(self)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
searchController.dismiss(animated: false, completion: nil)
}
@objc func actOnImportNotification() {
loadArtists()
}
lazy var artistRefreshControl: UIRefreshControl = {
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(handleRefresh), for: .valueChanged)
return refreshControl
}()
@objc func handleRefresh(_ refreshControl: UIRefreshControl) {
self.artists.removeAll()
self.tableView.reloadData()
self.tableView.tableFooterView = UIView()
self.viewState = .user
self.searchController.isActive = false
self.searchController.searchBar.showsCancelButton = false
self.searchController.searchBar.isHidden = true
self.actOnImportNotification()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
if viewState == .user {
return artistsSectionTitles.count
}
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if viewState == .user {
let artistKey = artistsSectionTitles[section]
if let artistValues = artistsDictionary[artistKey] {
return artistValues.count
}
}
return artists.count
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
self.viewState = .search
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
self.resignFirstResponder()
self.viewState = .user
self.artists.removeAll()
self.tableView.reloadData()
self.searchController.searchBar.showsCancelButton = false
self.tableView.tableFooterView = footerView
self.actOnImportNotification()
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
}
func updateSearchResults(for searchController: UISearchController) {
NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(self.reload), object: nil)
self.perform(#selector(self.reload), with: nil, afterDelay: 1)
}
@objc func reload() {
if let searchText = searchController.searchBar.text, !searchText.isEmpty, searchText.count > 2 {
self.viewState = .search
self.artists.removeAll()
self.tableView.reloadData()
self.tableView.tableFooterView = footerView
NumuClient.shared.getArtistSearch(search: searchText) {[weak self](artists) in
self?.artists = artists
DispatchQueue.main.async(execute: {
self?.loadTable()
Answers.logSearch(withQuery: searchText, customAttributes: nil)
})
}
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.searchController.isActive = false
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(
withIdentifier: "artistInfoCell", for: indexPath) as? ArtistTableViewCell else {
return UITableViewCell()
}
// Configure the cell...
if viewState == .user {
let artistKey = artistsSectionTitles[indexPath.section]
if let artistValues = artistsDictionary[artistKey] {
let artistInfo = artistValues[indexPath.row]
cell.configure(artistInfo: artistInfo)
cell.albumActivityIndicator.startAnimating()
cell.thumbUrl = artistInfo.thumbUrl // For recycled cells' late image loads.
cell.artistArt.kf.setImage(
with: cell.thumbUrl,
options: [.transition(.fade(0.2))])
return cell
}
}
let artistInfo = artists[indexPath.row]
cell.configure(artistInfo: artistInfo)
cell.albumActivityIndicator.startAnimating()
cell.thumbUrl = artistInfo.thumbUrl // For recycled cells' late image loads.
cell.artistArt.kf.setImage(
with: cell.thumbUrl,
options: [.transition(.fade(0.2))])
return cell
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if viewState == .user {
return " " + artistsSectionTitles[section]
}
return nil
}
override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
if viewState == .user {
if sortMethod == "date" {
var result: [String] = []
for year in artistsSectionTitles {
if year != "None" {
result.append("’" + String(year.suffix(2)))
}
}
return result
}
return artistsSectionTitles
}
return nil
}
override func tableView(
_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
var artistInfo = artists[indexPath.row]
let unfollow = UITableViewRowAction(style: .normal, title: "Error") { _, index in
if !defaults.logged {
if let loginViewController = self.storyboard?.instantiateViewController(
withIdentifier: "LogRegPrompt") as? UINavigationController {
DispatchQueue.main.async {
self.present(loginViewController, animated: true, completion: nil)
}
}
} else {
artistInfo.unfollowArtist { (success) in
DispatchQueue.main.async(execute: {
if success == "1" {
artistInfo.followStatus = "0"
self.artists[index.row].followStatus = "0"
Answers.logCustomEvent(
withName: "Unfol Swipe", customAttributes: ["Artist ID": artistInfo.artistId])
} else if success == "2" {
artistInfo.followStatus = "1"
self.artists[index.row].followStatus = "1"
Answers.logCustomEvent(
withName: "Follo Swipe", customAttributes: ["Artist ID": artistInfo.artistId])
}
tableView.setEditing(false, animated: true)
})
}
}
}
unfollow.backgroundColor = .background
unfollow.title = artistInfo.followStatus == "0" ? "Follow" : "Unfollow"
return [unfollow]
}
func loadTable() {
self.tableView.reloadData()
self.tableView.beginUpdates()
self.tableView.endUpdates()
self.tableView.tableFooterView = UIView()
self.artistRefreshControl.endRefreshing()
self.searchController.searchBar.isHidden = false
if self.artists.isEmpty,
self.viewState == .search {
self.tableView.tableFooterView = self.noSearchResultsView
}
if self.artists.isEmpty,
self.viewState == .user {
self.tableView.tableFooterView = self.noResultsView
}
if self.viewState == .user {
self.searchController.searchBar.text = ""
}
}
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
view.tintColor = UIColor(red: 0.17, green: 0.17, blue: 0.17, alpha: 1)
if let header = view as? UITableViewHeaderFooterView {
header.textLabel?.textColor = UIColor.lightText
}
}
// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showArtistReleases",
let destination = segue.destination as? ArtistReleasesTableViewController,
let releaseIndex = tableView.indexPathForSelectedRow?.row,
let sectionIndex = tableView.indexPathForSelectedRow?.section {
self.searchTerms = searchController.searchBar.text
var artistId = artists[releaseIndex].artistId
var artistName = artists[releaseIndex].artistName
if viewState == .user {
let section = artistsSectionTitles[sectionIndex]
let dict = artistsDictionary[section]
let artist = dict![releaseIndex]
artistId = artist.artistId
artistName = artist.artistName
}
self.lastSelectedArtistId = artistId
self.lastSelectedArtistName = artistName
destination.artistId = artistId
destination.artistName = artistName
}
}
}
extension ArtistsTableViewController: SortViewDelegate {
func sortOptionTapped(name: String) {
self.sortMethod = name
}
}
extension UISearchController {
override open func viewDidDisappear(_ animated: Bool) {
self.dismiss(animated: false, completion: nil)
super.viewDidDisappear(animated)
}
}