mirror of
https://github.com/kiwix/kiwix-apple.git
synced 2025-09-27 13:59:04 -04:00
Merge pull request #694 from kiwix/532-remove-long-press-re-arrange-buttons
532 remove long press and re-arrange buttons
This commit is contained in:
commit
cc8395535e
@ -1,10 +1,6 @@
|
|||||||
//
|
//
|
||||||
// CompactViewController.swift
|
// CompactViewController.swift
|
||||||
// Kiwix
|
// Kiwix
|
||||||
//
|
|
||||||
// Created by Chris Li on 9/4/23.
|
|
||||||
// Copyright © 2023 Chris Li. All rights reserved.
|
|
||||||
//
|
|
||||||
|
|
||||||
#if os(iOS)
|
#if os(iOS)
|
||||||
import Combine
|
import Combine
|
||||||
@ -16,7 +12,10 @@ final class CompactViewController: UIHostingController<AnyView>, UISearchControl
|
|||||||
private let searchController: UISearchController
|
private let searchController: UISearchController
|
||||||
private var searchTextObserver: AnyCancellable?
|
private var searchTextObserver: AnyCancellable?
|
||||||
private var openURLObserver: NSObjectProtocol?
|
private var openURLObserver: NSObjectProtocol?
|
||||||
|
|
||||||
|
private var trailingNavItemGroups: [UIBarButtonItemGroup] = []
|
||||||
|
private var rightNavItem: UIBarButtonItem?
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
searchViewModel = SearchViewModel()
|
searchViewModel = SearchViewModel()
|
||||||
let searchResult = SearchResults().environmentObject(searchViewModel)
|
let searchResult = SearchResults().environmentObject(searchViewModel)
|
||||||
@ -24,14 +23,14 @@ final class CompactViewController: UIHostingController<AnyView>, UISearchControl
|
|||||||
super.init(rootView: AnyView(CompactView()))
|
super.init(rootView: AnyView(CompactView()))
|
||||||
searchController.searchResultsUpdater = self
|
searchController.searchResultsUpdater = self
|
||||||
}
|
}
|
||||||
|
|
||||||
@MainActor required dynamic init?(coder aDecoder: NSCoder) {
|
@MainActor required dynamic init?(coder aDecoder: NSCoder) {
|
||||||
fatalError("init(coder:) has not been implemented")
|
fatalError("init(coder:) has not been implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
override func viewDidLoad() {
|
override func viewDidLoad() {
|
||||||
super.viewDidLoad()
|
super.viewDidLoad()
|
||||||
|
|
||||||
definesPresentationContext = true
|
definesPresentationContext = true
|
||||||
navigationController?.isToolbarHidden = false
|
navigationController?.isToolbarHidden = false
|
||||||
navigationController?.toolbar.scrollEdgeAppearance = {
|
navigationController?.toolbar.scrollEdgeAppearance = {
|
||||||
@ -70,6 +69,11 @@ final class CompactViewController: UIHostingController<AnyView>, UISearchControl
|
|||||||
|
|
||||||
func willPresentSearchController(_ searchController: UISearchController) {
|
func willPresentSearchController(_ searchController: UISearchController) {
|
||||||
navigationController?.setToolbarHidden(true, animated: true)
|
navigationController?.setToolbarHidden(true, animated: true)
|
||||||
|
if #available(iOS 16.0, *) {
|
||||||
|
trailingNavItemGroups = navigationItem.trailingItemGroups
|
||||||
|
} else {
|
||||||
|
rightNavItem = navigationItem.rightBarButtonItem
|
||||||
|
}
|
||||||
navigationItem.setRightBarButton(
|
navigationItem.setRightBarButton(
|
||||||
UIBarButtonItem(
|
UIBarButtonItem(
|
||||||
title: "common.button.cancel".localized,
|
title: "common.button.cancel".localized,
|
||||||
@ -82,14 +86,19 @@ final class CompactViewController: UIHostingController<AnyView>, UISearchControl
|
|||||||
}
|
}
|
||||||
@objc func onSearchCancelled() {
|
@objc func onSearchCancelled() {
|
||||||
searchController.isActive = false
|
searchController.isActive = false
|
||||||
navigationItem.setRightBarButton(nil, animated: true)
|
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) {
|
func willDismissSearchController(_ searchController: UISearchController) {
|
||||||
navigationController?.setToolbarHidden(false, animated: true)
|
navigationController?.setToolbarHidden(false, animated: true)
|
||||||
searchViewModel.searchText = ""
|
searchViewModel.searchText = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateSearchResults(for searchController: UISearchController) {
|
func updateSearchResults(for searchController: UISearchController) {
|
||||||
searchViewModel.searchText = searchController.searchBar.text ?? ""
|
searchViewModel.searchText = searchController.searchBar.text ?? ""
|
||||||
}
|
}
|
||||||
@ -97,32 +106,74 @@ final class CompactViewController: UIHostingController<AnyView>, UISearchControl
|
|||||||
|
|
||||||
private struct CompactView: View {
|
private struct CompactView: View {
|
||||||
@EnvironmentObject private var navigation: NavigationViewModel
|
@EnvironmentObject private var navigation: NavigationViewModel
|
||||||
|
@State private var presentedSheet: PresentedSheet?
|
||||||
|
|
||||||
|
private enum PresentedSheet: String, Identifiable {
|
||||||
|
var id: String { rawValue }
|
||||||
|
case library, settings
|
||||||
|
}
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
if case let .tab(tabID) = navigation.currentItem {
|
if case let .tab(tabID) = navigation.currentItem {
|
||||||
Content().id(tabID).toolbar {
|
Content()
|
||||||
ToolbarItemGroup(placement: .bottomBar) {
|
.id(tabID)
|
||||||
HStack {
|
.toolbar {
|
||||||
NavigationButtons()
|
ToolbarItemGroup(placement: .bottomBar) {
|
||||||
Spacer()
|
HStack {
|
||||||
OutlineButton()
|
NavigationButtons()
|
||||||
Spacer()
|
Spacer()
|
||||||
BookmarkButton()
|
OutlineButton()
|
||||||
Spacer()
|
Spacer()
|
||||||
ArticleShortcutButtons(displayMode: .randomArticle)
|
BookmarkButton()
|
||||||
Spacer()
|
Spacer()
|
||||||
TabsManagerButton()
|
TabsManagerButton()
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
.environmentObject(BrowserViewModel.getCached(tabID: tabID))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private struct Content: View {
|
private struct Content: View {
|
||||||
@EnvironmentObject private var browser: BrowserViewModel
|
@EnvironmentObject private var browser: BrowserViewModel
|
||||||
|
@FetchRequest(
|
||||||
|
sortDescriptors: [NSSortDescriptor(keyPath: \ZimFile.size, ascending: false)],
|
||||||
|
predicate: ZimFile.openedPredicate
|
||||||
|
) private var zimFiles: FetchedResults<ZimFile>
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
Group {
|
Group {
|
||||||
if browser.url == nil {
|
if browser.url == nil {
|
||||||
@ -141,6 +192,14 @@ private struct Content: View {
|
|||||||
.onDisappear {
|
.onDisappear {
|
||||||
browser.persistState()
|
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
|
#endif
|
||||||
|
@ -18,9 +18,9 @@ struct TabsManagerButton: View {
|
|||||||
) private var zimFiles: FetchedResults<ZimFile>
|
) private var zimFiles: FetchedResults<ZimFile>
|
||||||
@State private var presentedSheet: PresentedSheet?
|
@State private var presentedSheet: PresentedSheet?
|
||||||
|
|
||||||
enum PresentedSheet: String, Identifiable {
|
private enum PresentedSheet: String, Identifiable {
|
||||||
var id: String { rawValue }
|
var id: String { rawValue }
|
||||||
case tabsManager, library, settings
|
case tabsManager
|
||||||
}
|
}
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
@ -43,27 +43,6 @@ struct TabsManagerButton: View {
|
|||||||
Label("common.tab.menu.close_all".localized, systemImage: "xmark.square.fill")
|
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: {
|
||||||
Label("common.tab.manager.title".localized, systemImage: "square.stack")
|
Label("common.tab.manager.title".localized, systemImage: "square.stack")
|
||||||
} primaryAction: {
|
} primaryAction: {
|
||||||
@ -83,20 +62,6 @@ struct TabsManagerButton: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.modifier(MarkAsHalfSheet())
|
}.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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user