From d44a52487cefa18694f22fca1f53c05b9ee86f17 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sat, 14 Nov 2026 20:57:44 -0800 Subject: [PATCH] Convert Sidebar settings on iOS to SwiftUI. --- iOS/Settings/SettingsToggleTint.swift | 17 +++ iOS/Settings/SettingsViewController.swift | 2 +- iOS/Settings/SidebarSettingsView.swift | 53 +++++++ .../SidebarSettingsViewController.swift | 137 ------------------ 4 files changed, 71 insertions(+), 138 deletions(-) create mode 100644 iOS/Settings/SettingsToggleTint.swift create mode 100644 iOS/Settings/SidebarSettingsView.swift delete mode 100644 iOS/Settings/SidebarSettingsViewController.swift diff --git a/iOS/Settings/SettingsToggleTint.swift b/iOS/Settings/SettingsToggleTint.swift new file mode 100644 index 000000000..2cdefaceb --- /dev/null +++ b/iOS/Settings/SettingsToggleTint.swift @@ -0,0 +1,17 @@ +// +// SettingsToggleTint.swift +// NetNewsWire-iOS +// +// Created by Brent Simmons on 11/14/26. +// Copyright © 2026 Ranchero Software. All rights reserved. +// + +import SwiftUI + +extension View { + + /// Gives Settings switches the app’s accent color, matching the rest of the app’s switches. + func settingsToggleTint() -> some View { + tint(Color(uiColor: Assets.Colors.primaryAccent)) + } +} diff --git a/iOS/Settings/SettingsViewController.swift b/iOS/Settings/SettingsViewController.swift index 7721fe84a..c16774612 100644 --- a/iOS/Settings/SettingsViewController.swift +++ b/iOS/Settings/SettingsViewController.swift @@ -281,7 +281,7 @@ final class SettingsViewController: UITableViewController { case .general: navigationController?.pushViewController(GeneralSettingsViewController(), animated: true) case .sidebar: - navigationController?.pushViewController(SidebarSettingsViewController(), animated: true) + navigationController?.pushViewController(UIHostingController(rootView: SidebarSettingsView()), animated: true) case .timeline: navigationController?.pushViewController(TimelineSettingsViewController(), animated: true) case .articles: diff --git a/iOS/Settings/SidebarSettingsView.swift b/iOS/Settings/SidebarSettingsView.swift new file mode 100644 index 000000000..9a6585a27 --- /dev/null +++ b/iOS/Settings/SidebarSettingsView.swift @@ -0,0 +1,53 @@ +// +// SidebarSettingsView.swift +// NetNewsWire-iOS +// +// Created by Brent Simmons on 11/14/26. +// Copyright © 2026 Ranchero Software. All rights reserved. +// + +import SwiftUI + +struct SidebarSettingsView: View { + + @State private var model = SidebarSettingsModel() + + var body: some View { + List { + Section { + Toggle(String(localized: "Show Feed Icons", comment: "Show Feed Icons"), isOn: Binding(get: { model.showFeedIcons }, set: { AppDefaults.sidebarShowsIcons = $0 })) + } + Section(String(localized: "Smart Feeds", comment: "Smart Feeds group title")) { + Toggle(String(localized: "Show Today", comment: "Show Today"), isOn: Binding(get: { model.showToday }, set: { AppDefaults.showTodayFeed = $0 })) + Toggle(String(localized: "Show All Unread", comment: "Show All Unread"), isOn: Binding(get: { model.showAllUnread }, set: { AppDefaults.showUnreadFeed = $0 })) + Toggle(String(localized: "Show Starred", comment: "Show Starred"), isOn: Binding(get: { model.showStarred }, set: { AppDefaults.showStarredFeed = $0 })) + } + } + .settingsToggleTint() + .navigationTitle(String(localized: "Sidebar", comment: "Sidebar")) + } +} + +/// Tracks the Sidebar settings in AppDefaults, updating when they change. +@MainActor @Observable final class SidebarSettingsModel { + + private(set) var showFeedIcons = AppDefaults.sidebarShowsIcons + private(set) var showToday = AppDefaults.showTodayFeed + private(set) var showAllUnread = AppDefaults.showUnreadFeed + private(set) var showStarred = AppDefaults.showStarredFeed + + init() { + NotificationCenter.default.addObserver(self, selector: #selector(handleSidebarShowsIconsDidChange(_:)), name: .sidebarShowsIconsDidChange, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(handleSidebarSmartFeedsVisibilityDidChange(_:)), name: .sidebarSmartFeedsVisibilityDidChange, object: nil) + } + + @objc func handleSidebarShowsIconsDidChange(_ notification: Notification) { + showFeedIcons = AppDefaults.sidebarShowsIcons + } + + @objc func handleSidebarSmartFeedsVisibilityDidChange(_ notification: Notification) { + showToday = AppDefaults.showTodayFeed + showAllUnread = AppDefaults.showUnreadFeed + showStarred = AppDefaults.showStarredFeed + } +} diff --git a/iOS/Settings/SidebarSettingsViewController.swift b/iOS/Settings/SidebarSettingsViewController.swift deleted file mode 100644 index c25df69b7..000000000 --- a/iOS/Settings/SidebarSettingsViewController.swift +++ /dev/null @@ -1,137 +0,0 @@ -// -// SidebarSettingsViewController.swift -// NetNewsWire-iOS -// -// Created by Brent Simmons on 11/15/26. -// Copyright © 2026 Ranchero Software. All rights reserved. -// - -import UIKit - -final class SidebarSettingsViewController: UITableViewController { - - private enum Section: Int, CaseIterable { - case feedIcons - case smartFeeds - } - - private let showFeedIconsSwitch = UISwitch() - private let showTodaySwitch = UISwitch() - private let showAllUnreadSwitch = UISwitch() - private let showStarredSwitch = UISwitch() - - private var cells = [Section: [UITableViewCell]]() - - init() { - super.init(style: .insetGrouped) - } - - required init?(coder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - - override func viewDidLoad() { - super.viewDidLoad() - title = NSLocalizedString("Sidebar", comment: "Sidebar") - - NotificationCenter.default.addObserver(self, selector: #selector(handleSidebarShowsIconsDidChange(_:)), name: .sidebarShowsIconsDidChange, object: nil) - NotificationCenter.default.addObserver(self, selector: #selector(handleSidebarSmartFeedsVisibilityDidChange(_:)), name: .sidebarSmartFeedsVisibilityDidChange, object: nil) - - cells[.feedIcons] = [ - makeCell(switch: showFeedIconsSwitch, title: NSLocalizedString("Show Feed Icons", comment: "Show Feed Icons"), action: #selector(switchShowFeedIcons(_:))) - ] - cells[.smartFeeds] = [ - makeCell(switch: showTodaySwitch, title: NSLocalizedString("Show Today", comment: "Show Today"), action: #selector(switchShowToday(_:))), - makeCell(switch: showAllUnreadSwitch, title: NSLocalizedString("Show All Unread", comment: "Show All Unread"), action: #selector(switchShowAllUnread(_:))), - makeCell(switch: showStarredSwitch, title: NSLocalizedString("Show Starred", comment: "Show Starred"), action: #selector(switchShowStarred(_:))) - ] - - updateShowFeedIconsSwitch() - updateSmartFeedsSwitches() - } - - // MARK: - UITableViewDataSource - - override func numberOfSections(in tableView: UITableView) -> Int { - Section.allCases.count - } - - override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { - switch Section(rawValue: section) { - case .smartFeeds: - NSLocalizedString("Smart Feeds", comment: "Smart Feeds group title") - case .feedIcons, nil: - nil - } - } - - override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - cellsInSection(section).count - } - - override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { - cellsInSection(indexPath.section)[indexPath.row] - } - - // MARK: - Actions - - @objc func switchShowFeedIcons(_ sender: UISwitch) { - AppDefaults.sidebarShowsIcons = sender.isOn - } - - @objc func switchShowToday(_ sender: UISwitch) { - AppDefaults.showTodayFeed = sender.isOn - } - - @objc func switchShowAllUnread(_ sender: UISwitch) { - AppDefaults.showUnreadFeed = sender.isOn - } - - @objc func switchShowStarred(_ sender: UISwitch) { - AppDefaults.showStarredFeed = sender.isOn - } - - // MARK: - Notifications - - @objc func handleSidebarShowsIconsDidChange(_ notification: Notification) { - updateShowFeedIconsSwitch() - } - - @objc func handleSidebarSmartFeedsVisibilityDidChange(_ notification: Notification) { - updateSmartFeedsSwitches() - } -} - -// MARK: - Private - -private extension SidebarSettingsViewController { - - func cellsInSection(_ section: Int) -> [UITableViewCell] { - guard let section = Section(rawValue: section) else { - return [] - } - return cells[section] ?? [] - } - - func makeCell(switch toggle: UISwitch, title: String, action: Selector) -> UITableViewCell { - let cell = VibrantTableViewCell(style: .default, reuseIdentifier: nil) - cell.selectionStyle = .none - cell.textLabel?.text = title - cell.textLabel?.numberOfLines = 0 - cell.textLabel?.adjustsFontForContentSizeCategory = true - toggle.onTintColor = Assets.Colors.primaryAccent - toggle.addTarget(self, action: action, for: .valueChanged) - cell.accessoryView = toggle - return cell - } - - func updateShowFeedIconsSwitch() { - showFeedIconsSwitch.isOn = AppDefaults.sidebarShowsIcons - } - - func updateSmartFeedsSwitches() { - showTodaySwitch.isOn = AppDefaults.showTodayFeed - showAllUnreadSwitch.isOn = AppDefaults.showUnreadFeed - showStarredSwitch.isOn = AppDefaults.showStarredFeed - } -}