parent
2b0ab45c12
commit
17cb8676b1
@ -0,0 +1,78 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Scheme
|
||||
LastUpgradeVersion = "1400"
|
||||
version = "1.3">
|
||||
<BuildAction
|
||||
parallelizeBuildables = "YES"
|
||||
buildImplicitDependencies = "YES">
|
||||
<BuildActionEntries>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "D626E6C228724258000E1AF5"
|
||||
BuildableName = "MastoSearchMobile.app"
|
||||
BlueprintName = "MastoSearchMobile"
|
||||
ReferencedContainer = "container:MastoSearch.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
</BuildActionEntries>
|
||||
</BuildAction>
|
||||
<TestAction
|
||||
buildConfiguration = "Debug"
|
||||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
|
||||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
|
||||
shouldUseLaunchSchemeArgsEnv = "YES">
|
||||
<Testables>
|
||||
</Testables>
|
||||
</TestAction>
|
||||
<LaunchAction
|
||||
buildConfiguration = "Debug"
|
||||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
|
||||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
|
||||
launchStyle = "0"
|
||||
useCustomWorkingDirectory = "NO"
|
||||
ignoresPersistentStateOnLaunch = "NO"
|
||||
debugDocumentVersioning = "YES"
|
||||
debugServiceExtension = "internal"
|
||||
allowLocationSimulation = "YES">
|
||||
<BuildableProductRunnable
|
||||
runnableDebuggingMode = "0">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "D626E6C228724258000E1AF5"
|
||||
BuildableName = "MastoSearchMobile.app"
|
||||
BlueprintName = "MastoSearchMobile"
|
||||
ReferencedContainer = "container:MastoSearch.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildableProductRunnable>
|
||||
</LaunchAction>
|
||||
<ProfileAction
|
||||
buildConfiguration = "Release"
|
||||
shouldUseLaunchSchemeArgsEnv = "YES"
|
||||
savedToolIdentifier = ""
|
||||
useCustomWorkingDirectory = "NO"
|
||||
debugDocumentVersioning = "YES">
|
||||
<BuildableProductRunnable
|
||||
runnableDebuggingMode = "0">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "D626E6C228724258000E1AF5"
|
||||
BuildableName = "MastoSearchMobile.app"
|
||||
BlueprintName = "MastoSearchMobile"
|
||||
ReferencedContainer = "container:MastoSearch.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildableProductRunnable>
|
||||
</ProfileAction>
|
||||
<AnalyzeAction
|
||||
buildConfiguration = "Debug">
|
||||
</AnalyzeAction>
|
||||
<ArchiveAction
|
||||
buildConfiguration = "Release"
|
||||
revealArchiveInOrganizer = "YES">
|
||||
</ArchiveAction>
|
||||
</Scheme>
|
@ -0,0 +1,32 @@
|
||||
//
|
||||
// AppDelegate.swift
|
||||
// MastoSearchMobile
|
||||
//
|
||||
// Created by Shadowfacts on 7/3/22.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
import MastoSearchCore
|
||||
|
||||
@main
|
||||
class AppDelegate: UIResponder, UIApplicationDelegate {
|
||||
|
||||
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
|
||||
|
||||
DatabaseController.shared.initialize()
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func applicationWillTerminate(_ application: UIApplication) {
|
||||
DatabaseController.shared.close()
|
||||
}
|
||||
|
||||
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
|
||||
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
|
||||
}
|
||||
|
||||
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
{
|
||||
"colors" : [
|
||||
{
|
||||
"idiom" : "universal"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"idiom" : "universal",
|
||||
"platform" : "ios",
|
||||
"size" : "1024x1024"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
{
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13122.16" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
|
||||
<dependencies>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13104.12"/>
|
||||
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
|
||||
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
|
||||
</dependencies>
|
||||
<scenes>
|
||||
<!--View Controller-->
|
||||
<scene sceneID="EHf-IW-A2E">
|
||||
<objects>
|
||||
<viewController id="01J-lp-oVM" sceneMemberID="viewController">
|
||||
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
|
||||
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
|
||||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
|
||||
<color key="backgroundColor" xcode11CocoaTouchSystemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
|
||||
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
|
||||
</view>
|
||||
</viewController>
|
||||
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
|
||||
</objects>
|
||||
<point key="canvasLocation" x="53" y="375"/>
|
||||
</scene>
|
||||
</scenes>
|
||||
</document>
|
@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>UIApplicationSceneManifest</key>
|
||||
<dict>
|
||||
<key>UIApplicationSupportsMultipleScenes</key>
|
||||
<false/>
|
||||
<key>UISceneConfigurations</key>
|
||||
<dict>
|
||||
<key>UIWindowSceneSessionRoleApplication</key>
|
||||
<array>
|
||||
<dict>
|
||||
<key>UISceneConfigurationName</key>
|
||||
<string>Default Configuration</string>
|
||||
<key>UISceneDelegateClassName</key>
|
||||
<string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
|
||||
</dict>
|
||||
</array>
|
||||
</dict>
|
||||
</dict>
|
||||
</dict>
|
||||
</plist>
|
@ -0,0 +1,69 @@
|
||||
//
|
||||
// SceneDelegate.swift
|
||||
// MastoSearchMobile
|
||||
//
|
||||
// Created by Shadowfacts on 7/3/22.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
import MastoSearchCore
|
||||
|
||||
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
|
||||
|
||||
var window: UIWindow?
|
||||
|
||||
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
|
||||
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
|
||||
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
|
||||
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
|
||||
guard let windowScene = (scene as? UIWindowScene) else { return }
|
||||
|
||||
window = UIWindow(windowScene: windowScene)
|
||||
|
||||
let nav = UINavigationController(rootViewController: ViewController())
|
||||
nav.navigationBar.scrollEdgeAppearance = nav.navigationBar.standardAppearance
|
||||
window!.rootViewController = nav
|
||||
|
||||
window!.makeKeyAndVisible()
|
||||
}
|
||||
|
||||
func sceneDidDisconnect(_ scene: UIScene) {
|
||||
// Called as the scene is being released by the system.
|
||||
// This occurs shortly after the scene enters the background, or when its session is discarded.
|
||||
// Release any resources associated with this scene that can be re-created the next time the scene connects.
|
||||
// The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
|
||||
}
|
||||
|
||||
func sceneDidBecomeActive(_ scene: UIScene) {
|
||||
// Called when the scene has moved from an inactive state to an active state.
|
||||
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
|
||||
}
|
||||
|
||||
func sceneWillResignActive(_ scene: UIScene) {
|
||||
// Called when the scene will move from an active state to an inactive state.
|
||||
// This may occur due to temporary interruptions (ex. an incoming phone call).
|
||||
}
|
||||
|
||||
func sceneWillEnterForeground(_ scene: UIScene) {
|
||||
// Called as the scene transitions from the background to the foreground.
|
||||
// Use this method to undo the changes made on entering the background.
|
||||
|
||||
syncStatuses()
|
||||
}
|
||||
|
||||
func sceneDidEnterBackground(_ scene: UIScene) {
|
||||
// Called as the scene transitions from the foreground to the background.
|
||||
// Use this method to save data, release shared resources, and store enough scene-specific state information
|
||||
// to restore the scene back to its current state.
|
||||
}
|
||||
|
||||
func syncStatuses() {
|
||||
SyncController.shared.syncStatuses { error in
|
||||
let alert = UIAlertController(title: "Error syncing statuses", message: error.localizedDescription, preferredStyle: .alert)
|
||||
alert.addAction(UIAlertAction(title: "Ok", style: .default))
|
||||
self.window!.rootViewController!.present(alert, animated: true)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,75 @@
|
||||
//
|
||||
// StatusTableHeaderView.swift
|
||||
// MastoSearchMobile
|
||||
//
|
||||
// Created by Shadowfacts on 7/3/22.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
class StatusTableHeaderView: UICollectionReusableView {
|
||||
|
||||
override init(frame: CGRect) {
|
||||
super.init(frame: frame)
|
||||
|
||||
backgroundColor = .systemBackground
|
||||
|
||||
let dateLabel = UILabel()
|
||||
dateLabel.text = "Date"
|
||||
dateLabel.font = UIFont(descriptor: .preferredFontDescriptor(withTextStyle: .callout).withSymbolicTraits(.traitBold)!, size: 0)
|
||||
dateLabel.textColor = .tintColor
|
||||
dateLabel.translatesAutoresizingMaskIntoConstraints = false
|
||||
addSubview(dateLabel)
|
||||
|
||||
let contentWarningLabel = UILabel()
|
||||
contentWarningLabel.text = "Content Warning"
|
||||
contentWarningLabel.font = UIFont(descriptor: .preferredFontDescriptor(withTextStyle: .callout).withSymbolicTraits(.traitBold)!, size: 0)
|
||||
contentWarningLabel.textColor = .tintColor
|
||||
contentWarningLabel.translatesAutoresizingMaskIntoConstraints = false
|
||||
addSubview(contentWarningLabel)
|
||||
|
||||
let contentLabel = UILabel()
|
||||
contentLabel.text = "Content"
|
||||
contentLabel.font = UIFont(descriptor: .preferredFontDescriptor(withTextStyle: .callout).withSymbolicTraits(.traitBold)!, size: 0)
|
||||
contentLabel.textColor = .tintColor
|
||||
contentLabel.translatesAutoresizingMaskIntoConstraints = false
|
||||
addSubview(contentLabel)
|
||||
|
||||
let fakeDateLabel = UILabel()
|
||||
fakeDateLabel.text = "07/03/2022, 02:31 PM"
|
||||
fakeDateLabel.translatesAutoresizingMaskIntoConstraints = false
|
||||
fakeDateLabel.font = .monospacedDigitSystemFont(ofSize: 17, weight: .regular)
|
||||
fakeDateLabel.isHidden = true
|
||||
addSubview(fakeDateLabel)
|
||||
|
||||
let separator = UIView()
|
||||
separator.backgroundColor = .separator
|
||||
separator.translatesAutoresizingMaskIntoConstraints = false
|
||||
addSubview(separator)
|
||||
|
||||
NSLayoutConstraint.activate([
|
||||
dateLabel.leadingAnchor.constraint(equalToSystemSpacingAfter: leadingAnchor, multiplier: 2),
|
||||
dateLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
|
||||
|
||||
fakeDateLabel.leadingAnchor.constraint(equalToSystemSpacingAfter: leadingAnchor, multiplier: 2),
|
||||
fakeDateLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
|
||||
contentWarningLabel.leadingAnchor.constraint(equalToSystemSpacingAfter: fakeDateLabel.trailingAnchor, multiplier: 2),
|
||||
contentWarningLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
|
||||
|
||||
contentLabel.leadingAnchor.constraint(equalToSystemSpacingAfter: centerXAnchor, multiplier: 1),
|
||||
contentLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
|
||||
|
||||
separator.leadingAnchor.constraint(equalTo: leadingAnchor),
|
||||
separator.trailingAnchor.constraint(equalTo: trailingAnchor),
|
||||
separator.bottomAnchor.constraint(equalTo: bottomAnchor),
|
||||
separator.heightAnchor.constraint(equalToConstant: 0.5),
|
||||
|
||||
heightAnchor.constraint(greaterThanOrEqualToConstant: 32),
|
||||
])
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
//
|
||||
// StatusTableRowCollectionViewCell.swift
|
||||
// MastoSearchMobile
|
||||
//
|
||||
// Created by Shadowfacts on 7/3/22.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
import MastoSearchCore
|
||||
import SwiftSoup
|
||||
|
||||
class StatusTableRowCollectionViewCell: UICollectionViewCell {
|
||||
|
||||
// private static let formatter: DateFormatter = {
|
||||
// let f = DateFormatter()
|
||||
// f.locale = .current
|
||||
// f.setLocalizedDateFormatFromTemplate("yyyy-MM-dd hh:mm a")
|
||||
// return f
|
||||
// }()
|
||||
private static let dateStyle: Date.FormatStyle = {
|
||||
Date.FormatStyle()
|
||||
.year(.extended(minimumLength: 4))
|
||||
.month(.twoDigits)
|
||||
.day(.twoDigits)
|
||||
.hour(.twoDigits(amPM: .abbreviated))
|
||||
.minute(.twoDigits)
|
||||
}()
|
||||
|
||||
private let dateLabel = UILabel()
|
||||
private let contentWarningLabel = UILabel()
|
||||
private let contentLabel = UILabel()
|
||||
|
||||
override init(frame: CGRect) {
|
||||
super.init(frame: frame)
|
||||
|
||||
dateLabel.translatesAutoresizingMaskIntoConstraints = false
|
||||
dateLabel.font = .monospacedDigitSystemFont(ofSize: 17, weight: .regular)
|
||||
addSubview(dateLabel)
|
||||
|
||||
contentWarningLabel.translatesAutoresizingMaskIntoConstraints = false
|
||||
addSubview(contentWarningLabel)
|
||||
|
||||
let spacer = UIView()
|
||||
spacer.translatesAutoresizingMaskIntoConstraints = false
|
||||
addSubview(spacer)
|
||||
|
||||
contentLabel.translatesAutoresizingMaskIntoConstraints = false
|
||||
contentLabel.font = .preferredFont(forTextStyle: .callout)
|
||||
contentLabel.numberOfLines = 2
|
||||
addSubview(contentLabel)
|
||||
|
||||
NSLayoutConstraint.activate([
|
||||
dateLabel.leadingAnchor.constraint(equalToSystemSpacingAfter: leadingAnchor, multiplier: 2),
|
||||
dateLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
|
||||
|
||||
contentWarningLabel.leadingAnchor.constraint(equalToSystemSpacingAfter: dateLabel.trailingAnchor, multiplier: 2),
|
||||
contentWarningLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
|
||||
|
||||
spacer.leadingAnchor.constraint(equalTo: contentWarningLabel.trailingAnchor),
|
||||
centerXAnchor.constraint(equalToSystemSpacingAfter: spacer.trailingAnchor, multiplier: 1),
|
||||
|
||||
contentLabel.leadingAnchor.constraint(equalToSystemSpacingAfter: centerXAnchor, multiplier: 1),
|
||||
contentLabel.trailingAnchor.constraint(equalTo: trailingAnchor),
|
||||
contentLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
|
||||
contentLabel.topAnchor.constraint(greaterThanOrEqualTo: topAnchor, constant: 4),
|
||||
bottomAnchor.constraint(greaterThanOrEqualTo: contentLabel.bottomAnchor, constant: 4),
|
||||
|
||||
heightAnchor.constraint(greaterThanOrEqualToConstant: 32),
|
||||
])
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
func updateUI(status: Status) {
|
||||
dateLabel.text = status.published.formatted(StatusTableRowCollectionViewCell.dateStyle)
|
||||
contentWarningLabel.text = status.summary ?? ""
|
||||
let doc = try! SwiftSoup.parseBodyFragment(status.content)
|
||||
contentLabel.text = try! doc.body()!.text()
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
//
|
||||
// UIColor+App.swift
|
||||
// MastoSearchMobile
|
||||
//
|
||||
// Created by Shadowfacts on 7/3/22.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
extension UIColor {
|
||||
static let alternatingTableRow = UIColor { traitCollection in
|
||||
if traitCollection.userInterfaceStyle == .dark {
|
||||
return .alternatingTableRowDark
|
||||
} else {
|
||||
return .alternatingTableRowLight
|
||||
}
|
||||
}
|
||||
|
||||
private static let alternatingTableRowDark = UIColor(white: 0.1, alpha: 1)
|
||||
private static let alternatingTableRowLight = UIColor(white: 0.95, alpha: 1)
|
||||
}
|
@ -0,0 +1,214 @@
|
||||
//
|
||||
// ViewController.swift
|
||||
// MastoSearchMobile
|
||||
//
|
||||
// Created by Shadowfacts on 7/3/22.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
import MastoSearchCore
|
||||
import Combine
|
||||
import SafariServices
|
||||
|
||||
class ViewController: UIViewController {
|
||||
|
||||
private let searchQueue = DispatchQueue(label: "Search", qos: .userInitiated)
|
||||
|
||||
private var collectionView: UICollectionView!
|
||||
private var dataSource: UICollectionViewDiffableDataSource<Section, Item>!
|
||||
|
||||
private var searchQuerySubject = PassthroughSubject<String, Never>()
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
|
||||
view.backgroundColor = .systemBackground
|
||||
|
||||
navigationItem.title = "MastoSearch"
|
||||
navigationItem.leadingItemGroups = [
|
||||
UIBarButtonItemGroup(barButtonItems: [
|
||||
UIBarButtonItem(title: "Account", menu: createAccountMenu()),
|
||||
UIBarButtonItem(title: "Import", style: .plain, target: self, action: #selector(importPressed)),
|
||||
], representativeItem: nil)
|
||||
]
|
||||
let searchController = UISearchController(searchResultsController: nil)
|
||||
searchController.searchResultsUpdater = self
|
||||
navigationItem.searchController = searchController
|
||||
|
||||
var config = UICollectionLayoutListConfiguration(appearance: .plain)
|
||||
config.headerMode = .supplementary
|
||||
config.itemSeparatorHandler = { indexPath, config in
|
||||
if indexPath.row == 0 {
|
||||
var config = config
|
||||
config.topSeparatorVisibility = .hidden
|
||||
return config
|
||||
} else {
|
||||
return config
|
||||
}
|
||||
}
|
||||
let layout = UICollectionViewCompositionalLayout.list(using: config)
|
||||
collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout)
|
||||
collectionView.translatesAutoresizingMaskIntoConstraints = false
|
||||
collectionView.allowsMultipleSelection = true
|
||||
collectionView.delegate = self
|
||||
view.addSubview(collectionView)
|
||||
NSLayoutConstraint.activate([
|
||||
collectionView.topAnchor.constraint(equalTo: view.topAnchor),
|
||||
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
|
||||
collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
||||
collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
||||
])
|
||||
|
||||
let header = UICollectionView.SupplementaryRegistration<StatusTableHeaderView>(elementKind: UICollectionView.elementKindSectionHeader) { supplementaryView, elementKind, indexPath in
|
||||
}
|
||||
let cell = UICollectionView.CellRegistration<StatusTableRowCollectionViewCell, Status> { cell, indexPath, status in
|
||||
cell.updateUI(status: status)
|
||||
cell.backgroundColor = indexPath.row % 2 == 0 ? .alternatingTableRow : .systemBackground
|
||||
}
|
||||
|
||||
dataSource = UICollectionViewDiffableDataSource(collectionView: collectionView) { collectionView, indexPath, item in
|
||||
collectionView.dequeueConfiguredReusableCell(using: cell, for: indexPath, item: item.status)
|
||||
}
|
||||
dataSource.supplementaryViewProvider = { collectionView, elementKind, indexPath in
|
||||
guard elementKind == UICollectionView.elementKindSectionHeader else {
|
||||
return nil
|
||||
}
|
||||
return collectionView.dequeueConfiguredReusableSupplementary(using: header, for: indexPath)
|
||||
}
|
||||
|
||||
searchQuerySubject
|
||||
.debounce(for: .milliseconds(100), scheduler: DispatchQueue.main)
|
||||
.sink { [unowned self] query in
|
||||
self.updateStatuses(query: query)
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
|
||||
updateStatuses(query: "")
|
||||
}
|
||||
|
||||
private func updateStatuses(query: String) {
|
||||
searchQueue.async {
|
||||
if query.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
|
||||
DatabaseController.shared.getStatuses(sortDescriptor: NSSortDescriptor(key: "published", ascending: false)) { seq in
|
||||
self.applyUpdate(statuses: seq)
|
||||
}
|
||||
} else {
|
||||
DatabaseController.shared.getStatuses(query: query, sortDescriptor: NSSortDescriptor(key: "published", ascending: false)) { seq in
|
||||
self.applyUpdate(statuses: seq)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func applyUpdate(statuses: StatusSequence) {
|
||||
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
|
||||
snapshot.appendSections([.statuses])
|
||||
snapshot.appendItems(statuses.map { Item(status: $0) })
|
||||
DispatchQueue.main.async {
|
||||
self.dataSource.apply(snapshot, animatingDifferences: false)
|
||||
}
|
||||
}
|
||||
|
||||
private func createAccountMenu() -> UIMenu {
|
||||
if let account = LocalData.account {
|
||||
return UIMenu(children: [
|
||||
UIAction(title: "Logged in to \(account.instanceURL.host!)", attributes: .disabled, handler: { _ in }),
|
||||
UIAction(title: "Log out", attributes: .destructive, handler: { [unowned self] _ in
|
||||
self.logout()
|
||||
}),
|
||||
])
|
||||
} else {
|
||||
return UIMenu(children: [
|
||||
UIAction(title: "Log in...", handler: { [unowned self] _ in
|
||||
self.login()
|
||||
}),
|
||||
])
|
||||
}
|
||||
}
|
||||
|
||||
private func login() {
|
||||
|
||||
}
|
||||
|
||||
private func logout() {
|
||||
|
||||
}
|
||||
|
||||
@objc private func importPressed() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension ViewController {
|
||||
enum Section {
|
||||
case statuses
|
||||
}
|
||||
struct Item: Equatable, Hashable {
|
||||
let status: Status
|
||||
|
||||
static func ==(lhs: Item, rhs: Item) -> Bool {
|
||||
return lhs.status.url == rhs.status.url
|
||||
}
|
||||
|
||||
func hash(into hasher: inout Hasher) {
|
||||
hasher.combine(status.url)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension ViewController: UISearchResultsUpdating {
|
||||
func updateSearchResults(for searchController: UISearchController) {
|
||||
searchQuerySubject.send(searchController.searchBar.text ?? "")
|
||||
}
|
||||
}
|
||||
|
||||
extension ViewController: UICollectionViewDelegate {
|
||||
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
||||
guard let status = dataSource.itemIdentifier(for: indexPath)?.status else {
|
||||
return
|
||||
}
|
||||
present(SFSafariViewController(url: URL(string: status.url)!), animated: true)
|
||||
}
|
||||
|
||||
func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemsAt indexPaths: [IndexPath], point: CGPoint) -> UIContextMenuConfiguration? {
|
||||
let statuses = indexPaths.compactMap { dataSource.itemIdentifier(for: $0)?.status }
|
||||
switch statuses.count {
|
||||
case 0:
|
||||
return nil
|
||||
case 1:
|
||||
let url = URL(string: statuses.first!.url)!
|
||||
return UIContextMenuConfiguration {
|
||||
SFSafariViewController(url: url)
|
||||
} actionProvider: { _ in
|
||||
UIMenu(children: [
|
||||
UIAction(title: "Open in Safari", image: UIImage(systemName: "safari"), handler: { [unowned self] _ in
|
||||
self.present(SFSafariViewController(url: url), animated: true)
|
||||
}),
|
||||
UIAction(title: "Copy URL", image: UIImage(systemName: "list.bullet.clipboard"), handler: { _ in
|
||||
UIPasteboard.general.url = url
|
||||
})
|
||||
])
|
||||
}
|
||||
default:
|
||||
return UIContextMenuConfiguration(actionProvider: { _ in
|
||||
UIMenu(children: [
|
||||
UIAction(title: "Copy URLs", image: UIImage(systemName: "list.bullet.clipboard"), handler: { _ in
|
||||
UIPasteboard.general.urls = statuses.map { URL(string: $0.url)! }
|
||||
})
|
||||
])
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func collectionView(_ collectionView: UICollectionView, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) {
|
||||
if let viewController = animator.previewViewController,
|
||||
viewController is SFSafariViewController {
|
||||
animator.preferredCommitStyle = .pop
|
||||
animator.addCompletion {
|
||||
self.present(viewController, animated: true)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue