-
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* view model * setup * library * changelog * macOS * purge cache
- Loading branch information
1 parent
20d4ef2
commit fa397a9
Showing
43 changed files
with
1,499 additions
and
1,365 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// | ||
// 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 | ||
|
||
class CompactViewController: UIHostingController<AnyView>, UISearchControllerDelegate, UISearchResultsUpdating { | ||
private let searchViewModel: SearchViewModel | ||
private let searchController: UISearchController | ||
private var searchTextObserver: AnyCancellable? | ||
private var openURLObserver: NSObjectProtocol? | ||
|
||
init() { | ||
searchViewModel = SearchViewModel() | ||
let searchResult = SearchResults().environmentObject(searchViewModel) | ||
searchController = UISearchController(searchResultsController: UIHostingController(rootView: searchResult)) | ||
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 = { | ||
let apperance = UIToolbarAppearance() | ||
apperance.configureWithDefaultBackground() | ||
return apperance | ||
}() | ||
navigationItem.scrollEdgeAppearance = { | ||
let apperance = UINavigationBarAppearance() | ||
apperance.configureWithDefaultBackground() | ||
return apperance | ||
}() | ||
navigationItem.titleView = searchController.searchBar | ||
searchController.automaticallyShowsCancelButton = false | ||
searchController.delegate = self | ||
searchController.hidesNavigationBarDuringPresentation = false | ||
searchController.showsSearchResultsController = true | ||
|
||
searchTextObserver = searchViewModel.$searchText.sink { [weak self] searchText in | ||
guard self?.searchController.searchBar.text != searchText else { return } | ||
self?.searchController.searchBar.text = searchText | ||
} | ||
} | ||
|
||
override func viewWillAppear(_ animated: Bool) { | ||
super.viewWillAppear(animated) | ||
openURLObserver = NotificationCenter.default.addObserver( | ||
forName: .openURL, object: nil, queue: nil | ||
) { [weak self] _ in | ||
self?.searchController.isActive = false | ||
self?.navigationItem.setRightBarButton(nil, animated: true) | ||
} | ||
} | ||
|
||
override func viewDidDisappear(_ animated: Bool) { | ||
super.viewDidDisappear(animated) | ||
NotificationCenter.default.removeObserver(self) | ||
} | ||
|
||
func willPresentSearchController(_ searchController: UISearchController) { | ||
navigationController?.setToolbarHidden(true, animated: true) | ||
navigationItem.setRightBarButton( | ||
UIBarButtonItem(systemItem: .cancel, primaryAction: UIAction { [unowned self] _ in | ||
searchController.isActive = false | ||
navigationItem.setRightBarButton(nil, animated: true) | ||
}), animated: true | ||
) | ||
} | ||
|
||
func willDismissSearchController(_ searchController: UISearchController) { | ||
navigationController?.setToolbarHidden(false, animated: true) | ||
searchViewModel.searchText = "" | ||
} | ||
|
||
func updateSearchResults(for searchController: UISearchController) { | ||
searchViewModel.searchText = searchController.searchBar.text ?? "" | ||
} | ||
} | ||
|
||
private struct CompactView: View { | ||
@EnvironmentObject private var navigation: NavigationViewModel | ||
|
||
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() | ||
ArticleShortcutButtons(displayMode: .randomArticle) | ||
Spacer() | ||
TabsManagerButton() | ||
} | ||
} | ||
} | ||
.environmentObject(BrowserViewModel.getCached(tabID: tabID)) | ||
} | ||
} | ||
} | ||
|
||
private struct Content: View { | ||
@EnvironmentObject private var browser: BrowserViewModel | ||
|
||
var body: some View { | ||
Group { | ||
if browser.url == nil { | ||
Welcome() | ||
} else { | ||
WebView().ignoresSafeArea() | ||
} | ||
} | ||
.focusedSceneValue(\.browserViewModel, browser) | ||
.focusedSceneValue(\.canGoBack, browser.canGoBack) | ||
.focusedSceneValue(\.canGoForward, browser.canGoForward) | ||
.modifier(ExternalLinkHandler()) | ||
.onAppear { | ||
browser.updateLastOpened() | ||
} | ||
.onDisappear { | ||
browser.persistState() | ||
} | ||
} | ||
} | ||
#endif |
Oops, something went wrong.