From 39688276ff687b8cc62c0c43ba555163c43f34ee Mon Sep 17 00:00:00 2001 From: Balazs Perlaki-Horvath Date: Sat, 16 Mar 2024 13:24:42 +0100 Subject: [PATCH 1/2] Change buttons --- App/CompactViewController.swift | 63 +++++++++++++++++++++++---- Views/Buttons/TabsManagerButton.swift | 42 +++++++++--------- 2 files changed, 76 insertions(+), 29 deletions(-) diff --git a/App/CompactViewController.swift b/App/CompactViewController.swift index 0feba42e..b5f0c5fd 100644 --- a/App/CompactViewController.swift +++ b/App/CompactViewController.swift @@ -10,18 +10,29 @@ import Combine import SwiftUI import UIKit +import SwiftBackports final class CompactViewController: UIHostingController, UISearchControllerDelegate, UISearchResultsUpdating { + + private enum Const { + #if os(macOS) + static let randomButtonTitle: String = "article_shortcut.random.button.title.mac".localized + #else + static let randomButtonTitle: String = "article_shortcut.random.button.title.ios".localized + #endif + } private let searchViewModel: SearchViewModel private let searchController: UISearchController private var searchTextObserver: AnyCancellable? private var openURLObserver: NSObjectProtocol? - + private var loadRandomArticle: (() -> Void)? init() { searchViewModel = SearchViewModel() let searchResult = SearchResults().environmentObject(searchViewModel) searchController = UISearchController(searchResultsController: UIHostingController(rootView: searchResult)) - super.init(rootView: AnyView(CompactView())) + let compactView = CompactView() + loadRandomArticle = compactView.loadRandomArticle + super.init(rootView: AnyView(compactView)) searchController.searchResultsUpdater = self } @@ -46,6 +57,7 @@ final class CompactViewController: UIHostingController, UISearchControl }() searchController.searchBar.autocorrectionType = .no navigationItem.titleView = searchController.searchBar + navigationItem.rightBarButtonItem = randomArticleButton() searchController.automaticallyShowsCancelButton = false searchController.delegate = self searchController.hidesNavigationBarDuringPresentation = false @@ -60,7 +72,7 @@ final class CompactViewController: UIHostingController, UISearchControl forName: .openURL, object: nil, queue: nil ) { [weak self] _ in self?.searchController.isActive = false - self?.navigationItem.setRightBarButton(nil, animated: true) + self?.navigationItem.setRightBarButton(self?.randomArticleButton(), animated: true) } } @@ -82,7 +94,18 @@ final class CompactViewController: UIHostingController, UISearchControl } @objc func onSearchCancelled() { searchController.isActive = false - navigationItem.setRightBarButton(nil, animated: true) + navigationItem.setRightBarButton(randomArticleButton(), animated: true) + } + + @objc func onRandomTapped(_ target: UIButton) { + loadRandomArticle?() + } + + private func randomArticleButton() -> UIBarButtonItem { + UIBarButtonItem(image: .init(systemName: "die.face.5"), + style: .plain, + target: self, + action: #selector(onRandomTapped)) } func willDismissSearchController(_ searchController: UISearchController) { @@ -97,7 +120,17 @@ final class CompactViewController: UIHostingController, UISearchControl private struct CompactView: View { @EnvironmentObject private var navigation: NavigationViewModel - + + func loadRandomArticle() { + if case let .tab(tabID) = navigation.currentItem { + BrowserViewModel(tabID: tabID).loadRandomArticle() + } + } + + init() { + debugPrint("Create CompactView") + } + var body: some View { if case let .tab(tabID) = navigation.currentItem { Content().id(tabID).toolbar { @@ -109,9 +142,23 @@ private struct CompactView: View { Spacer() BookmarkButton() Spacer() - ArticleShortcutButtons(displayMode: .randomArticle) - Spacer() TabsManagerButton() +// Spacer() +// ArticleShortcutButtons(displayMode: .randomArticle) + if FeatureFlags.hasLibrary { + Spacer() + Button { +// presentedSheet = .library + } label: { + Label("common.tab.menu.library".localized, systemImage: "folder") + } + } + Spacer() + Button { +// presentedSheet = .settings + } label: { + Label("common.tab.menu.settings".localized, systemImage: "gear") + } } } } @@ -122,7 +169,7 @@ private struct CompactView: View { private struct Content: View { @EnvironmentObject private var browser: BrowserViewModel - + var body: some View { Group { if browser.url == nil { diff --git a/Views/Buttons/TabsManagerButton.swift b/Views/Buttons/TabsManagerButton.swift index 80925313..550fe887 100644 --- a/Views/Buttons/TabsManagerButton.swift +++ b/Views/Buttons/TabsManagerButton.swift @@ -43,27 +43,27 @@ struct TabsManagerButton: View { Label("common.tab.menu.close_all".localized, systemImage: "xmark.square.fill") } } - Section { - ForEach(zimFiles.prefix(5)) { zimFile in - Button { - browser.loadMainArticle(zimFileID: zimFile.fileID) - } label: { Label(zimFile.name, systemImage: "house") } - } - } - Section { - if FeatureFlags.hasLibrary { - Button { - presentedSheet = .library - } label: { - Label("common.tab.menu.library".localized, systemImage: "folder") - } - } - Button { - presentedSheet = .settings - } label: { - Label("common.tab.menu.settings".localized, systemImage: "gear") - } - } +// Section { +// ForEach(zimFiles.prefix(5)) { zimFile in +// Button { +// browser.loadMainArticle(zimFileID: zimFile.fileID) +// } label: { Label(zimFile.name, systemImage: "house") } +// } +// } +// Section { +// if FeatureFlags.hasLibrary { +// Button { +// presentedSheet = .library +// } label: { +// Label("common.tab.menu.library".localized, systemImage: "folder") +// } +// } +// Button { +// presentedSheet = .settings +// } label: { +// Label("common.tab.menu.settings".localized, systemImage: "gear") +// } +// } } label: { Label("common.tab.manager.title".localized, systemImage: "square.stack") } primaryAction: { From a19e4845af3b3d3e61f6d30dbd2c76f96389f4fd Mon Sep 17 00:00:00 2001 From: Balazs Perlaki-Horvath Date: Sun, 17 Mar 2024 00:21:18 +0100 Subject: [PATCH 2/2] Re-arrange menu buttons --- App/CompactViewController.swift | 144 ++++++++++++++------------ Views/Buttons/TabsManagerButton.swift | 39 +------ 2 files changed, 80 insertions(+), 103 deletions(-) diff --git a/App/CompactViewController.swift b/App/CompactViewController.swift index b5f0c5fd..06bbdaac 100644 --- a/App/CompactViewController.swift +++ b/App/CompactViewController.swift @@ -1,48 +1,36 @@ // // CompactViewController.swift // Kiwix -// -// Created by Chris Li on 9/4/23. -// Copyright © 2023 Chris Li. All rights reserved. -// #if os(iOS) import Combine import SwiftUI import UIKit -import SwiftBackports final class CompactViewController: UIHostingController, UISearchControllerDelegate, UISearchResultsUpdating { - - private enum Const { - #if os(macOS) - static let randomButtonTitle: String = "article_shortcut.random.button.title.mac".localized - #else - static let randomButtonTitle: String = "article_shortcut.random.button.title.ios".localized - #endif - } private let searchViewModel: SearchViewModel private let searchController: UISearchController private var searchTextObserver: AnyCancellable? private var openURLObserver: NSObjectProtocol? - private var loadRandomArticle: (() -> Void)? + + private var trailingNavItemGroups: [UIBarButtonItemGroup] = [] + private var rightNavItem: UIBarButtonItem? + init() { searchViewModel = SearchViewModel() let searchResult = SearchResults().environmentObject(searchViewModel) searchController = UISearchController(searchResultsController: UIHostingController(rootView: searchResult)) - let compactView = CompactView() - loadRandomArticle = compactView.loadRandomArticle - super.init(rootView: AnyView(compactView)) + super.init(rootView: AnyView(CompactView())) searchController.searchResultsUpdater = self } - + @MainActor required dynamic init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } - + override func viewDidLoad() { super.viewDidLoad() - + definesPresentationContext = true navigationController?.isToolbarHidden = false navigationController?.toolbar.scrollEdgeAppearance = { @@ -57,7 +45,6 @@ final class CompactViewController: UIHostingController, UISearchControl }() searchController.searchBar.autocorrectionType = .no navigationItem.titleView = searchController.searchBar - navigationItem.rightBarButtonItem = randomArticleButton() searchController.automaticallyShowsCancelButton = false searchController.delegate = self searchController.hidesNavigationBarDuringPresentation = false @@ -72,7 +59,7 @@ final class CompactViewController: UIHostingController, UISearchControl forName: .openURL, object: nil, queue: nil ) { [weak self] _ in self?.searchController.isActive = false - self?.navigationItem.setRightBarButton(self?.randomArticleButton(), animated: true) + self?.navigationItem.setRightBarButton(nil, animated: true) } } @@ -82,6 +69,11 @@ final class CompactViewController: UIHostingController, UISearchControl func willPresentSearchController(_ searchController: UISearchController) { navigationController?.setToolbarHidden(true, animated: true) + if #available(iOS 16.0, *) { + trailingNavItemGroups = navigationItem.trailingItemGroups + } else { + rightNavItem = navigationItem.rightBarButtonItem + } navigationItem.setRightBarButton( UIBarButtonItem( title: "common.button.cancel".localized, @@ -94,25 +86,19 @@ final class CompactViewController: UIHostingController, UISearchControl } @objc func onSearchCancelled() { searchController.isActive = false - navigationItem.setRightBarButton(randomArticleButton(), animated: true) - } - - @objc func onRandomTapped(_ target: UIButton) { - loadRandomArticle?() - } - - private func randomArticleButton() -> UIBarButtonItem { - UIBarButtonItem(image: .init(systemName: "die.face.5"), - style: .plain, - target: self, - action: #selector(onRandomTapped)) + if #available(iOS 16.0, *) { + navigationItem.setRightBarButtonItems(nil, animated: false) + navigationItem.trailingItemGroups = trailingNavItemGroups + } else { + navigationItem.setRightBarButtonItems([rightNavItem].compactMap { $0 }, animated: true) + } } func willDismissSearchController(_ searchController: UISearchController) { navigationController?.setToolbarHidden(false, animated: true) searchViewModel.searchText = "" } - + func updateSearchResults(for searchController: UISearchController) { searchViewModel.searchText = searchController.searchBar.text ?? "" } @@ -120,55 +106,73 @@ final class CompactViewController: UIHostingController, UISearchControl private struct CompactView: View { @EnvironmentObject private var navigation: NavigationViewModel + @State private var presentedSheet: PresentedSheet? - func loadRandomArticle() { - if case let .tab(tabID) = navigation.currentItem { - BrowserViewModel(tabID: tabID).loadRandomArticle() - } - } - - init() { - debugPrint("Create CompactView") + private enum PresentedSheet: String, Identifiable { + var id: String { rawValue } + case library, settings } var body: some View { if case let .tab(tabID) = navigation.currentItem { - Content().id(tabID).toolbar { - ToolbarItemGroup(placement: .bottomBar) { - HStack { - NavigationButtons() - Spacer() - OutlineButton() - Spacer() - BookmarkButton() - Spacer() - TabsManagerButton() -// Spacer() -// ArticleShortcutButtons(displayMode: .randomArticle) - if FeatureFlags.hasLibrary { + Content() + .id(tabID) + .toolbar { + ToolbarItemGroup(placement: .bottomBar) { + HStack { + NavigationButtons() + Spacer() + OutlineButton() + Spacer() + BookmarkButton() + Spacer() + TabsManagerButton() + if FeatureFlags.hasLibrary { + Spacer() + Button { + presentedSheet = .library + } label: { + Label("common.tab.menu.library".localized, systemImage: "folder") + } + } Spacer() Button { -// presentedSheet = .library + presentedSheet = .settings } label: { - Label("common.tab.menu.library".localized, systemImage: "folder") + Label("common.tab.menu.settings".localized, systemImage: "gear") + } + } + } + } + .environmentObject(BrowserViewModel.getCached(tabID: tabID)) + .sheet(item: $presentedSheet) { presentedSheet in + switch presentedSheet { + case .library: + Library() + case .settings: + NavigationView { + Settings().toolbar { + ToolbarItem(placement: .navigationBarLeading) { + Button { + self.presentedSheet = nil + } label: { + Text("common.button.done".localized).fontWeight(.semibold) + } + } } } - Spacer() - Button { -// presentedSheet = .settings - } label: { - Label("common.tab.menu.settings".localized, systemImage: "gear") - } } } - } - .environmentObject(BrowserViewModel.getCached(tabID: tabID)) } } } private struct Content: View { @EnvironmentObject private var browser: BrowserViewModel + @FetchRequest( + sortDescriptors: [NSSortDescriptor(keyPath: \ZimFile.size, ascending: false)], + predicate: ZimFile.openedPredicate + ) private var zimFiles: FetchedResults var body: some View { Group { @@ -188,6 +192,14 @@ private struct Content: View { .onDisappear { browser.persistState() } + .toolbar { + ToolbarItemGroup(placement: .primaryAction) { + Button("article_shortcut.random.button.title.ios".localized, + systemImage: "die.face.5", + action: { browser.loadRandomArticle() }) + .disabled(zimFiles.isEmpty) + } + } } } #endif diff --git a/Views/Buttons/TabsManagerButton.swift b/Views/Buttons/TabsManagerButton.swift index 550fe887..3851d6b5 100644 --- a/Views/Buttons/TabsManagerButton.swift +++ b/Views/Buttons/TabsManagerButton.swift @@ -18,9 +18,9 @@ struct TabsManagerButton: View { ) private var zimFiles: FetchedResults @State private var presentedSheet: PresentedSheet? - enum PresentedSheet: String, Identifiable { + private enum PresentedSheet: String, Identifiable { var id: String { rawValue } - case tabsManager, library, settings + case tabsManager } var body: some View { @@ -43,27 +43,6 @@ struct TabsManagerButton: View { Label("common.tab.menu.close_all".localized, systemImage: "xmark.square.fill") } } -// Section { -// ForEach(zimFiles.prefix(5)) { zimFile in -// Button { -// browser.loadMainArticle(zimFileID: zimFile.fileID) -// } label: { Label(zimFile.name, systemImage: "house") } -// } -// } -// Section { -// if FeatureFlags.hasLibrary { -// Button { -// presentedSheet = .library -// } label: { -// Label("common.tab.menu.library".localized, systemImage: "folder") -// } -// } -// Button { -// presentedSheet = .settings -// } label: { -// Label("common.tab.menu.settings".localized, systemImage: "gear") -// } -// } } label: { Label("common.tab.manager.title".localized, systemImage: "square.stack") } primaryAction: { @@ -83,20 +62,6 @@ struct TabsManagerButton: View { } } }.modifier(MarkAsHalfSheet()) - case .library: - Library() - case .settings: - NavigationView { - Settings().toolbar { - ToolbarItem(placement: .navigationBarLeading) { - Button { - self.presentedSheet = nil - } label: { - Text("common.button.done".localized).fontWeight(.semibold) - } - } - } - } } } }