diff --git a/Tusker/Screens/Explore/ExploreViewController.swift b/Tusker/Screens/Explore/ExploreViewController.swift index af49604467..6843042105 100644 --- a/Tusker/Screens/Explore/ExploreViewController.swift +++ b/Tusker/Screens/Explore/ExploreViewController.swift @@ -36,26 +36,32 @@ class ExploreViewController: EnhancedTableViewController { tableView.register(UINib(nibName: "BasicTableViewCell", bundle: .main), forCellReuseIdentifier: "basicCell") dataSource = DataSource(tableView: tableView, cellProvider: { (tableView, indexPath, item) -> UITableViewCell? in + let cell = tableView.dequeueReusableCell(withIdentifier: "basicCell", for: indexPath) + switch item { case .bookmarks: - let cell = tableView.dequeueReusableCell(withIdentifier: "basicCell", for: indexPath) cell.imageView!.image = UIImage(systemName: "bookmark.fill") cell.textLabel!.text = NSLocalizedString("Bookmarks", comment: "bookmarks nav item title") cell.accessoryType = .disclosureIndicator - return cell - + case let .list(list): - let cell = tableView.dequeueReusableCell(withIdentifier: "basicCell", for: indexPath) cell.imageView!.image = nil cell.textLabel!.text = list.title 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() snapshot.appendSections([.bookmarks, .lists]) snapshot.appendItems([.bookmarks], toSection: .bookmarks) + snapshot.appendItems([.addList], toSection: .lists) // the initial, static items should not be displayed with an animation UIView.performWithoutAnimation { dataSource.apply(snapshot) @@ -72,6 +78,10 @@ class ExploreViewController: EnhancedTableViewController { navigationItem.searchController = searchController navigationItem.hidesSearchBarWhenScrolling = false + reloadLists() + } + + func reloadLists() { let request = MastodonController.client.getLists() MastodonController.client.run(request) { (response) in guard case let .success(lists, _) = response else { @@ -79,7 +89,9 @@ class ExploreViewController: EnhancedTableViewController { } 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 { self.dataSource.apply(snapshot) @@ -99,6 +111,29 @@ class ExploreViewController: EnhancedTableViewController { case let .list(list): 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 { case bookmarks case list(List) + case addList static func == (lhs: ExploreViewController.Item, rhs: ExploreViewController.Item) -> Bool { switch (lhs, rhs) { @@ -119,6 +155,8 @@ extension ExploreViewController { return true case let (.list(a), .list(b)): return a.id == b.id + case (.addList, .addList): + return true default: return false } @@ -130,6 +168,8 @@ extension ExploreViewController { case let .list(list): hasher.combine("list") hasher.combine(list.id) + case .addList: + hasher.combine("addList") } } }