Add list creation

This commit is contained in:
Shadowfacts 2019-12-17 21:18:32 -05:00
parent 270cbc2a6c
commit 19f15d8fa9
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 46 additions and 6 deletions

View File

@ -36,26 +36,32 @@ class ExploreViewController: EnhancedTableViewController {
tableView.register(UINib(nibName: "BasicTableViewCell", bundle: .main), forCellReuseIdentifier: "basicCell") tableView.register(UINib(nibName: "BasicTableViewCell", bundle: .main), forCellReuseIdentifier: "basicCell")
dataSource = DataSource(tableView: tableView, cellProvider: { (tableView, indexPath, item) -> UITableViewCell? in dataSource = DataSource(tableView: tableView, cellProvider: { (tableView, indexPath, item) -> UITableViewCell? in
let cell = tableView.dequeueReusableCell(withIdentifier: "basicCell", for: indexPath)
switch item { switch item {
case .bookmarks: case .bookmarks:
let cell = tableView.dequeueReusableCell(withIdentifier: "basicCell", for: indexPath)
cell.imageView!.image = UIImage(systemName: "bookmark.fill") cell.imageView!.image = UIImage(systemName: "bookmark.fill")
cell.textLabel!.text = NSLocalizedString("Bookmarks", comment: "bookmarks nav item title") cell.textLabel!.text = NSLocalizedString("Bookmarks", comment: "bookmarks nav item title")
cell.accessoryType = .disclosureIndicator cell.accessoryType = .disclosureIndicator
return cell
case let .list(list): case let .list(list):
let cell = tableView.dequeueReusableCell(withIdentifier: "basicCell", for: indexPath)
cell.imageView!.image = nil cell.imageView!.image = nil
cell.textLabel!.text = list.title cell.textLabel!.text = list.title
cell.accessoryType = .disclosureIndicator cell.accessoryType = .disclosureIndicator
return cell
case .addList:
cell.imageView!.image = UIImage(systemName: "plus")
cell.textLabel!.text = NSLocalizedString("New List...", comment: "new list nav item title")
cell.accessoryType = .none
} }
return cell
}) })
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>() var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
snapshot.appendSections([.bookmarks, .lists]) snapshot.appendSections([.bookmarks, .lists])
snapshot.appendItems([.bookmarks], toSection: .bookmarks) snapshot.appendItems([.bookmarks], toSection: .bookmarks)
snapshot.appendItems([.addList], toSection: .lists)
// the initial, static items should not be displayed with an animation // the initial, static items should not be displayed with an animation
UIView.performWithoutAnimation { UIView.performWithoutAnimation {
dataSource.apply(snapshot) dataSource.apply(snapshot)
@ -72,6 +78,10 @@ class ExploreViewController: EnhancedTableViewController {
navigationItem.searchController = searchController navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false navigationItem.hidesSearchBarWhenScrolling = false
reloadLists()
}
func reloadLists() {
let request = MastodonController.client.getLists() let request = MastodonController.client.getLists()
MastodonController.client.run(request) { (response) in MastodonController.client.run(request) { (response) in
guard case let .success(lists, _) = response else { guard case let .success(lists, _) = response else {
@ -79,7 +89,9 @@ class ExploreViewController: EnhancedTableViewController {
} }
var snapshot = self.dataSource.snapshot() var snapshot = self.dataSource.snapshot()
snapshot.appendItems(lists.map { .list($0) }, toSection: .lists) snapshot.deleteSections([.lists])
snapshot.appendSections([.lists])
snapshot.appendItems(lists.map { .list($0) } + [.addList], toSection: .lists)
DispatchQueue.main.async { DispatchQueue.main.async {
self.dataSource.apply(snapshot) self.dataSource.apply(snapshot)
@ -99,6 +111,29 @@ class ExploreViewController: EnhancedTableViewController {
case let .list(list): case let .list(list):
show(ListTimelineViewController(for: list), sender: nil) show(ListTimelineViewController(for: list), sender: nil)
case .addList:
tableView.selectRow(at: nil, animated: true, scrollPosition: .none)
let alert = UIAlertController(title: NSLocalizedString("New List", comment: "new list alert title"), message: NSLocalizedString("Choose a title for your new list", comment: "new list alert message"), preferredStyle: .alert)
alert.addTextField(configurationHandler: nil)
alert.addAction(UIAlertAction(title: NSLocalizedString("Cancel", comment: "new list alert cancel button"), style: .cancel, handler: nil))
alert.addAction(UIAlertAction(title: NSLocalizedString("Create List", comment: "new list create button"), style: .default, handler: { (_) in
guard let title = alert.textFields?.first?.text else {
fatalError()
}
let request = MastodonController.client.createList(title: title)
MastodonController.client.run(request) { (response) in
guard case let .success(list, _) = response else { fatalError() }
self.reloadLists()
DispatchQueue.main.async {
self.show(ListTimelineViewController(for: list), sender: nil)
}
}
}))
present(alert, animated: true)
} }
} }
@ -112,6 +147,7 @@ extension ExploreViewController {
enum Item: Hashable { enum Item: Hashable {
case bookmarks case bookmarks
case list(List) case list(List)
case addList
static func == (lhs: ExploreViewController.Item, rhs: ExploreViewController.Item) -> Bool { static func == (lhs: ExploreViewController.Item, rhs: ExploreViewController.Item) -> Bool {
switch (lhs, rhs) { switch (lhs, rhs) {
@ -119,6 +155,8 @@ extension ExploreViewController {
return true return true
case let (.list(a), .list(b)): case let (.list(a), .list(b)):
return a.id == b.id return a.id == b.id
case (.addList, .addList):
return true
default: default:
return false return false
} }
@ -130,6 +168,8 @@ extension ExploreViewController {
case let .list(list): case let .list(list):
hasher.combine("list") hasher.combine("list")
hasher.combine(list.id) hasher.combine(list.id)
case .addList:
hasher.combine("addList")
} }
} }
} }