Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Random UI/UX cleanup #200

Merged
merged 2 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions DLPrototype/Extensions/DispatchQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@
import Foundation

extension DispatchQueue {
static func with<T>(delay: Double = 0.0, background: (()->T?)? = nil, completion: ((T?) -> Void)? = nil) {
DispatchQueue.global(qos: .background).async {
let bg = background?()
if let completion = completion {
DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: {
completion(bg)
})
}
}
}

static func with<T>(delay: Double = 0.0, background: (()->[T]?)? = nil, completion: (([T]?) -> Void)? = nil) {
DispatchQueue.global(qos: .background).async {
let bg = background?()
Expand Down
2 changes: 1 addition & 1 deletion DLPrototype/MainMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct MainMenu: Commands {
}
.keyboardShortcut("j", modifiers: [.command, .shift])
}

Divider()
Menu("Timeline navigation") {
Button("Previous day") {nav.session.date -= 86400}
Expand Down
174 changes: 165 additions & 9 deletions DLPrototype/Utils/Navigation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ public class Navigation: Identifiable, ObservableObject {
@Published public var title: String? = ""
@Published public var pageId: UUID? = UUID()
@Published public var session: Session = Session()
@Published public var planning: Planning = Planning(moc: PersistenceController.shared.container.viewContext)
@Published public var planning: PlanningState = PlanningState(moc: PersistenceController.shared.container.viewContext)
@Published public var saved: Bool = false
@Published public var state: State = State()
@Published public var history: History = History()

public func pageTitle() -> String {
if title!.isEmpty {
Expand All @@ -101,18 +104,26 @@ public class Navigation: Identifiable, ObservableObject {
}

public func setView(_ newView: AnyView) -> Void {
view = nil
view = newView
// view = AnyView(WidgetLoading())
// state.on(.complete, { _ in
view = newView
// })
// if state.phase == .complete {
//
// } else {
//
// }
}

public func setParent(_ newParent: Page) -> Void {
parent = nil
parent = newParent
}

public func setSidebar(_ newView: AnyView) -> Void {
sidebar = nil
sidebar = newView
public func setSidebar(_ newView: AnyView?) -> Void {
if let view = newView {
sidebar = view
}
}

public func setTitle(_ newTitle: String) -> Void {
Expand All @@ -130,13 +141,36 @@ public class Navigation: Identifiable, ObservableObject {
inspector = newInspector
}

public func save() -> Void {
self.saved = true

DispatchQueue.main.asyncAfter(deadline: .now() + 0.75) {
self.saved = false
}
}

// @TODO: this doesn't really work yet
public func to(_ page: Page) -> Void {
let hp = self.history.get(page: page)

self.setId()
self.setView(hp.view)
self.setParent(page)
self.setSidebar(hp.sidebar)
self.setTitle(hp.title)

self.history.push(hp: hp)
}

public func reset() -> Void {
parent = .dashboard
view = nil
sidebar = nil
setId()
inspector = nil
session = Session()
state = State()
history = History()
}
}

Expand All @@ -148,10 +182,132 @@ extension Navigation {
var plan: Plan?
var date: Date = Date()
var idate: IdentifiableDay = IdentifiableDay()
var gif: Planning.GlobalInterfaceFilter = .normal
var gif: PlanningState.GlobalInterfaceFilter = .normal
var search: Search = Search(moc: PersistenceController.shared.container.viewContext)
var toolbar: Toolbar = Toolbar()
}

public struct State {
private var phase: Phase = .ready
private var hierarchy: [Phase] = [.ready, .transitioning, .complete]

mutating func advance() -> Phase {
if let index = hierarchy.firstIndex(of: self.phase) {
// let x = hierarchy.indices
print("DERPO advance.phase.index=\(index)")
if hierarchy.indices.count <= index {
var hIndex = hierarchy.indices[index]
hIndex += 1
self.phase = hierarchy[hIndex]
// let next = Int(hIndex += 1)
} else {
self.phase = .ready
}
}

return .error
}

mutating func on(_ phase: Phase, _ callback: (_ phase: Phase) -> Void) -> Void {
if phase == self.phase {
let nextPhase = advance()
callback(nextPhase)
let _ = advance()
}
}

mutating func set(_ phase: Phase) -> Void {
self.phase = phase
}

mutating func get() -> Phase {
return self.phase
}

enum Phase {
case ready, transitioning, complete, error
}
}

public struct History {
// How far back you can go by clicking "back". Max: 10
var recent: [HistoryPage] = []

private let defaultHistoryPage: HistoryPage = HistoryPage(
page: .dashboard,
view: AnyView(Dashboard()),
sidebar: AnyView(DashboardSidebar()),
title: "Dashboard"
)

public let all: [HistoryPage] = [
HistoryPage(page: .dashboard, view: AnyView(Dashboard()), sidebar: AnyView(DashboardSidebar()), title: "Dashboard"),
HistoryPage(page: .planning, view: AnyView(Planning()), sidebar: AnyView(DefaultPlanningSidebar()), title: "Planning"),
HistoryPage(page: .today, view: AnyView(Today()), sidebar: AnyView(TodaySidebar()), title: "Today"),
HistoryPage(page: .companies, view: AnyView(CompanyDashboard()), sidebar: AnyView(DefaultCompanySidebar()), title: "Companies & Projects"),
HistoryPage(page: .jobs, view: AnyView(JobDashboard()), sidebar: AnyView(JobDashboardSidebar()), title: "Jobs"),
HistoryPage(page: .notes, view: AnyView(NoteDashboard()), sidebar: AnyView(NoteDashboardSidebar()), title: "Notes"),
HistoryPage(page: .tasks, view: AnyView(TaskDashboard()), sidebar: AnyView(TaskDashboardSidebar()), title: "Tasks"),
]

/// A single page representing a page the user navigated to
public struct HistoryPage {
var id: UUID = UUID()
var page: Page
var view: AnyView
var sidebar: AnyView
var title: String
}
}
}

extension Navigation.History {
func get(page: Page) -> HistoryPage {
return all.first(where: {$0.page == page}) ?? defaultHistoryPage
}

mutating func push(hp: HistoryPage) -> Void {
recent.append(hp)

if recent.count > 10 {
let _ = recent.popLast()
}
}

func previous() -> HistoryPage? {
print("DERPO recent=\(recent.count)")
var index = recent.endIndex
index -= 1

if index <= 10 {
print("DERPO previousIndex=\(index)")

if recent.indices.contains(index) {
return recent[index]
}
} else {
index = recent.startIndex
}

return nil
}

func next() -> HistoryPage? {
var index = recent.endIndex
index -= 1

if index <= 10 {
print("DERPO nextIndex=\(index)")

if recent.indices.contains(index) {
return recent[index]
}
} else {
index = recent.endIndex
}

return nil
}
}

extension Navigation.Session {
Expand Down Expand Up @@ -199,7 +355,7 @@ extension Navigation.Session.Search {
}

extension Navigation {
public struct Planning {
public struct PlanningState {
var id: UUID = UUID()
var jobs: Set<Job> = []
var tasks: Set<LogTask> = []
Expand Down Expand Up @@ -319,7 +475,7 @@ extension Navigation {
}
}

extension Navigation.Planning {
extension Navigation.PlanningState {
enum GlobalInterfaceFilter {
case normal, focus
}
Expand Down
8 changes: 8 additions & 0 deletions DLPrototype/Views/Dashboard/Dashboard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ struct Dashboard: View {
VStack(alignment: .leading, spacing: 0) {
FindDashboard(searching: $searching)
FancyDivider()
// Button("State test") {
// print("DERPO state.phase=\(nav.state.get())")
//// nav.state.set(.transitioning)
// nav.state.advance()
// print("DERPO state.phase=\(nav.state.get())")
// nav.state.advance()
// print("DERPO state.phase=\(nav.state.get())")
// }

if !searching {
Widgets()
Expand Down
Loading