IceCubesApp/Packages/Env/Sources/Env/Routeur.swift
2022-12-24 13:41:25 +01:00

58 lines
1.5 KiB
Swift

import Foundation
import SwiftUI
import Models
public enum RouteurDestinations: Hashable {
case accountDetail(id: String)
case accountDetailWithAccount(account: Account)
case statusDetail(id: String)
case hashTag(tag: String, account: String?)
case followers(id: String)
case following(id: String)
case favouritedBy(id: String)
case rebloggedBy(id: String)
}
public enum SheetDestinations: Identifiable {
case statusEditor(replyToStatus: String?)
public var id: String {
switch self {
case .statusEditor:
return "statusEditor"
}
}
}
public class RouterPath: ObservableObject {
@Published public var path: [RouteurDestinations] = []
@Published public var presentedSheet: SheetDestinations?
public init() {}
public func navigate(to: RouteurDestinations) {
path.append(to)
}
public func handleStatus(status: AnyStatus, url: URL) -> OpenURLAction.Result {
if url.pathComponents.contains(where: { $0 == "tags" }),
let tag = url.pathComponents.last {
navigate(to: .hashTag(tag: tag, account: nil))
return .handled
} else if let mention = status.mentions.first(where: { $0.url == url }) {
navigate(to: .accountDetail(id: mention.id))
return .handled
}
return .systemAction
}
public func handle(url: URL) -> OpenURLAction.Result {
if url.pathComponents.contains(where: { $0 == "tags" }),
let tag = url.pathComponents.last {
navigate(to: .hashTag(tag: tag, account: nil))
return .handled
}
return .systemAction
}
}