- RuleUsage + UsageLedger: per-rule, per-day minutes/opens in app-group defaults (monotonic minutes, incrementing opens, midnight rollover by day-keying); MockUsageLedger for tests and seeded UI scenarios - usage-aware status: a limit rule whose daily budget is spent on an enabled day is active (blocking) until next midnight; Hard Mode gating and app-list locking honor it; soft unblock pauses until midnight - RuleEnforcer shields spent limit rules (always Block mode) while the app runs - new Usage section under Blocked Apps: '18m of 45m used today · 27m left', '2 of 5 opens today · 3 opens left', 'Blocked until tomorrow' - new 'limits' seed scenario + UI tests
159 lines
5.5 KiB
Swift
159 lines
5.5 KiB
Swift
//
|
|
// AppListPickerSheet.swift
|
|
// OpenAppLock
|
|
//
|
|
|
|
import SwiftData
|
|
import SwiftUI
|
|
|
|
/// Chooses the app list a rule uses: saved lists (tap to select), an Edit
|
|
/// affordance per list, and a "New List" flow. Lists are persisted directly,
|
|
/// independent of any rule, so creating one here never depends on the rule
|
|
/// draft being committed.
|
|
struct AppListPickerSheet: View {
|
|
@Binding var selected: AppList?
|
|
|
|
@Environment(\.dismiss) private var dismiss
|
|
@Environment(\.modelContext) private var modelContext
|
|
@Environment(RuleEnforcer.self) private var enforcer
|
|
@Query(sort: \AppList.createdAt) private var lists: [AppList]
|
|
@Query private var rules: [BlockingRule]
|
|
|
|
@State private var editingList: AppList?
|
|
@State private var creatingList = false
|
|
@State private var deletionBlocked = false
|
|
|
|
/// While any hard-mode rule is actively blocking (by the clock or a spent
|
|
/// limit budget), lists are read-only — editing one would be a back door
|
|
/// out of the hard block.
|
|
private var listsLocked: Bool {
|
|
!RulePolicy.canEditAppLists(rules: rules, usageFor: { enforcer.usage(for: $0) })
|
|
}
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
List {
|
|
Section {
|
|
if lists.isEmpty {
|
|
Text("No app lists yet. Create one to choose which apps this rule affects.")
|
|
.foregroundStyle(.secondary)
|
|
.accessibilityIdentifier("emptyAppListsLabel")
|
|
} else {
|
|
ForEach(lists) { list in
|
|
listRow(list)
|
|
}
|
|
}
|
|
} header: {
|
|
Text("Your App Lists").textCase(nil)
|
|
} footer: {
|
|
if listsLocked {
|
|
Label(
|
|
"Hard Mode is on — app lists are locked until the block ends.",
|
|
systemImage: "lock.fill"
|
|
)
|
|
.accessibilityElement(children: .combine)
|
|
.accessibilityIdentifier("appListsLockedNotice")
|
|
}
|
|
}
|
|
Section {
|
|
Button {
|
|
creatingList = true
|
|
} label: {
|
|
Label("New List", systemImage: "plus")
|
|
}
|
|
.accessibilityIdentifier("newAppListButton")
|
|
}
|
|
}
|
|
.navigationTitle("App List")
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
.toolbar {
|
|
ToolbarItem(placement: .cancellationAction) {
|
|
Button("Close", systemImage: "xmark") {
|
|
dismiss()
|
|
}
|
|
.accessibilityIdentifier("closeAppListPickerButton")
|
|
}
|
|
}
|
|
.navigationDestination(isPresented: $creatingList) {
|
|
AppListEditorView(list: nil) { created in
|
|
selected = created
|
|
creatingList = false
|
|
}
|
|
}
|
|
.navigationDestination(item: $editingList) { list in
|
|
AppListEditorView(list: list) { _ in
|
|
editingList = nil
|
|
}
|
|
}
|
|
.alert("This list is in use", isPresented: $deletionBlocked) {
|
|
Button("OK", role: .cancel) {}
|
|
} message: {
|
|
Text("Remove it from the rules that use it before deleting.")
|
|
}
|
|
}
|
|
}
|
|
|
|
private func listRow(_ list: AppList) -> some View {
|
|
HStack {
|
|
Button {
|
|
selected = list
|
|
dismiss()
|
|
} label: {
|
|
HStack {
|
|
Image(systemName: isSelected(list) ? "checkmark.circle.fill" : "circle")
|
|
.foregroundStyle(
|
|
isSelected(list) ? AnyShapeStyle(.tint) : AnyShapeStyle(Color.secondary)
|
|
)
|
|
.frame(width: 28)
|
|
VStack(alignment: .leading, spacing: 2) {
|
|
Text(list.name)
|
|
.foregroundStyle(Color.primary)
|
|
Text(list.appCountLabel)
|
|
.font(.caption)
|
|
.foregroundStyle(Color.secondary)
|
|
}
|
|
}
|
|
}
|
|
.accessibilityIdentifier("appListRow-\(list.name)")
|
|
Spacer()
|
|
if !listsLocked {
|
|
Button("Edit") {
|
|
editingList = list
|
|
}
|
|
.font(.subheadline)
|
|
.accessibilityIdentifier("editAppListButton-\(list.name)")
|
|
}
|
|
}
|
|
.buttonStyle(.borderless)
|
|
.swipeActions {
|
|
if !listsLocked {
|
|
Button("Delete", role: .destructive) {
|
|
delete(list)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private func isSelected(_ list: AppList) -> Bool {
|
|
selected?.id == list.id
|
|
}
|
|
|
|
private func delete(_ list: AppList) {
|
|
guard !AppList.isInUse(list, context: modelContext) else {
|
|
deletionBlocked = true
|
|
return
|
|
}
|
|
if isSelected(list) {
|
|
selected = nil
|
|
}
|
|
modelContext.delete(list)
|
|
}
|
|
}
|
|
|
|
extension AppList {
|
|
/// "4 Apps" / "1 App" label shared by the picker, editor, and detail rows.
|
|
var appCountLabel: String {
|
|
selectionCount == 1 ? "1 App" : "\(selectionCount) Apps"
|
|
}
|
|
}
|