[WIP] Add Entries for SettingsViewController (IOS-14)

This commit is contained in:
Nathan Mattes 2023-06-23 16:51:42 +02:00
parent fb6116d4a6
commit 1461a2a082
5 changed files with 227 additions and 13 deletions

View File

@ -138,7 +138,7 @@
D82BD7522ABC42D6009A374A /* Coordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = D82BD7512ABC42D6009A374A /* Coordinator.swift */; };
D8318A802A4466D300C0FB73 /* SettingsCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8318A7F2A4466D300C0FB73 /* SettingsCoordinator.swift */; };
D8318A842A4468A800C0FB73 /* GeneralSettingsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8318A832A4468A800C0FB73 /* GeneralSettingsViewController.swift */; };
D8318A862A4468C700C0FB73 /* SettingsOverviewViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8318A852A4468C700C0FB73 /* SettingsOverviewViewController.swift */; };
D8318A862A4468C700C0FB73 /* SettingsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8318A852A4468C700C0FB73 /* SettingsViewController.swift */; };
D8318A882A4468D300C0FB73 /* NotificationSettingsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8318A872A4468D300C0FB73 /* NotificationSettingsViewController.swift */; };
D8318A8A2A4468DC00C0FB73 /* AboutViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8318A892A4468DC00C0FB73 /* AboutViewController.swift */; };
D8318A8C2A4468E600C0FB73 /* SupportMastodonViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8318A8B2A4468E600C0FB73 /* SupportMastodonViewController.swift */; };
@ -779,7 +779,7 @@
D82BD7512ABC42D6009A374A /* Coordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Coordinator.swift; sourceTree = "<group>"; };
D8318A7F2A4466D300C0FB73 /* SettingsCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsCoordinator.swift; sourceTree = "<group>"; };
D8318A832A4468A800C0FB73 /* GeneralSettingsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GeneralSettingsViewController.swift; sourceTree = "<group>"; };
D8318A852A4468C700C0FB73 /* SettingsOverviewViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsOverviewViewController.swift; sourceTree = "<group>"; };
D8318A852A4468C700C0FB73 /* SettingsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsViewController.swift; sourceTree = "<group>"; };
D8318A872A4468D300C0FB73 /* NotificationSettingsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationSettingsViewController.swift; sourceTree = "<group>"; };
D8318A892A4468DC00C0FB73 /* AboutViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AboutViewController.swift; sourceTree = "<group>"; };
D8318A8B2A4468E600C0FB73 /* SupportMastodonViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SupportMastodonViewController.swift; sourceTree = "<group>"; };
@ -1735,7 +1735,7 @@
D8318A7E2A4466C900C0FB73 /* Legacy */,
D8318A7F2A4466D300C0FB73 /* SettingsCoordinator.swift */,
D8318A832A4468A800C0FB73 /* GeneralSettingsViewController.swift */,
D8318A852A4468C700C0FB73 /* SettingsOverviewViewController.swift */,
D8318A852A4468C700C0FB73 /* SettingsViewController.swift */,
D8318A872A4468D300C0FB73 /* NotificationSettingsViewController.swift */,
D8318A892A4468DC00C0FB73 /* AboutViewController.swift */,
D8318A8B2A4468E600C0FB73 /* SupportMastodonViewController.swift */,
@ -3828,7 +3828,7 @@
DB4F097D26A03A5B00D62E92 /* SearchHistoryItem.swift in Sources */,
DBD5B1FA27BD013700BD6B38 /* DataSourceProvider+StatusTableViewControllerNavigateable.swift in Sources */,
D809907C294D25510050219F /* PrivacyViewModel.swift in Sources */,
D8318A862A4468C700C0FB73 /* SettingsOverviewViewController.swift in Sources */,
D8318A862A4468C700C0FB73 /* SettingsViewController.swift in Sources */,
DB5B549A2833A60400DEF8B2 /* FamiliarFollowersViewController.swift in Sources */,
DB3E6FE22806A50100B035AE /* DiscoveryHashtagsViewModel+Diffable.swift in Sources */,
DB427DD625BAA00100D1B89D /* AppDelegate.swift in Sources */,

View File

@ -23,7 +23,7 @@ class GeneralSettingsViewController: UIViewController {
view.backgroundColor = .systemBackground
title = "Settings"
}
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }

View File

@ -7,23 +7,46 @@ class SettingsCoordinator: Coordinator {
let navigationController: UINavigationController
let presentedOn: UIViewController
private let generalSettingsViewController: GeneralSettingsViewController
private let settingsViewController: SettingsViewController
init(presentedOn: UIViewController) {
self.presentedOn = presentedOn
navigationController = UINavigationController()
generalSettingsViewController = GeneralSettingsViewController()
settingsViewController = SettingsViewController(accountName: "born2jort")
}
func start() {
generalSettingsViewController.delegate = self
settingsViewController.delegate = self
navigationController.pushViewController(generalSettingsViewController, animated: false)
navigationController.pushViewController(settingsViewController, animated: false)
presentedOn.present(navigationController, animated: true)
}
}
extension SettingsCoordinator: GeneralSettingsViewControllerDelegate {
extension SettingsCoordinator: SettingsViewControllerDelegate {
func done(_ viewController: UIViewController) {
viewController.dismiss(animated: true)
}
func didSelect(_ viewController: UIViewController, entry: SettingsEntry) {
switch entry {
case .general:
break
// show general
case .notifications:
break
// show notifications
case .aboutMastodon:
break
// show about
case .supportMastodon:
break
// present support-screen
case .logout(let accountName):
break
// show logout-sheet
}
}
}

View File

@ -1,3 +0,0 @@
// Copyright © 2023 Mastodon gGmbH. All rights reserved.
import Foundation

View File

@ -0,0 +1,194 @@
// Copyright © 2023 Mastodon gGmbH. All rights reserved.
import UIKit
struct SettingsSection: Hashable {
let entries: [SettingsEntry]
}
enum SettingsEntry: Hashable {
case general
case notifications
case aboutMastodon
case supportMastodon
case logout(accountName: String)
//TODO: @zeitschlag Add Localization
var title: String {
switch self {
case .general:
return "General"
case .notifications:
return "Notifications"
case .aboutMastodon:
return "About Mastodon"
case .supportMastodon:
return "Suppoprt Mastodon"
case .logout(let accountName):
return "Logout @\(accountName)"
}
}
var accessoryType: UITableViewCell.AccessoryType {
switch self {
case .general, .notifications, .aboutMastodon, .logout(_):
return .disclosureIndicator
case .supportMastodon:
return .none
}
}
var icon: UIImage? {
switch self {
case .general:
return UIImage(systemName: "gear")
case .notifications:
return UIImage(systemName: "bell.badge")
case .aboutMastodon:
return UIImage(systemName: "info.circle.fill")
case .supportMastodon:
return UIImage(systemName: "heart.fill")
case .logout(_):
return nil
}
}
var iconBackgroundColor: UIColor? {
switch self {
case .general:
return .systemGray
case .notifications:
return .systemRed
case .aboutMastodon:
return .systemPurple
case .supportMastodon:
return .systemGreen
case .logout(_):
return nil
}
}
var textColor: UIColor {
switch self {
case .general, .notifications, .aboutMastodon, .supportMastodon:
return .label
case .logout(_):
return .red
}
}
}
class SettingsTableViewCell: UITableViewCell {
static let reuseIdentifier = "SettingsTableViewCell"
}
protocol SettingsViewControllerDelegate: AnyObject {
func done(_ viewController: UIViewController)
func didSelect(_ viewController: UIViewController, entry: SettingsEntry)
}
class SettingsViewController: UIViewController {
let sections: [SettingsSection]
weak var delegate: SettingsViewControllerDelegate?
var tableViewDataSource: UITableViewDiffableDataSource<SettingsSection, SettingsEntry>?
let tableView: UITableView
init(accountName: String) {
sections = [
.init(entries: [.general, .notifications]),
.init(entries: [.supportMastodon, .aboutMastodon]),
.init(entries: [.logout(accountName: accountName)])
]
tableView = UITableView(frame: .zero, style: .insetGrouped)
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.register(SettingsTableViewCell.self, forCellReuseIdentifier: SettingsTableViewCell.reuseIdentifier)
super.init(nibName: nil, bundle: nil)
let tableViewDataSource = UITableViewDiffableDataSource<SettingsSection, SettingsEntry>(tableView: tableView) { [weak self] tableView, indexPath, itemIdentifier in
guard let self,
let cell = tableView.dequeueReusableCell(withIdentifier: SettingsTableViewCell.reuseIdentifier, for: indexPath) as? SettingsTableViewCell
else { fatalError("Wrong cell WTF??") }
let entry = self.sections[indexPath.section].entries[indexPath.row]
var content = cell.defaultContentConfiguration()
content.attributedText = NSAttributedString(string: entry.title, attributes: [
.foregroundColor: entry.textColor
])
content.image = entry.icon
content.imageProperties.cornerRadius = 5
// content.imageProperties.back
// imageproperties
// content.ima
cell.contentConfiguration = content
cell.accessoryType = entry.accessoryType
return cell
}
tableView.dataSource = tableViewDataSource
tableView.delegate = self
self.tableViewDataSource = tableViewDataSource
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(SettingsViewController.done(_:)))
view.backgroundColor = .systemGroupedBackground
view.addSubview(tableView)
title = "Settings"
setupConstraints()
}
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
override func viewDidLoad() {
super.viewDidLoad()
var snapshot = NSDiffableDataSourceSnapshot<SettingsSection, SettingsEntry>()
for section in sections {
snapshot.appendSections([section])
snapshot.appendItems(section.entries)
}
tableViewDataSource?.apply(snapshot)
}
private func setupConstraints() {
let constraints = [
tableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
tableView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
view.trailingAnchor.constraint(equalTo: tableView.safeAreaLayoutGuide.trailingAnchor),
view.bottomAnchor.constraint(equalTo: tableView.safeAreaLayoutGuide.bottomAnchor),
]
NSLayoutConstraint.activate(constraints)
}
//MARK: Actions
@objc
func done(_ sender: Any) {
delegate?.done(self)
}
}
//MARK: UITableViewDelegate
extension SettingsViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let entry = sections[indexPath.section].entries[indexPath.row]
delegate?.didSelect(self, entry: entry)
tableView.deselectRow(at: indexPath, animated: true)
}
}