Skip to content
Closed
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
1 change: 1 addition & 0 deletions Normal/Models/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ final class Settings {
var blockAllPreventsAppDelete: Bool = true
var defaultTab: AppTab?
var hideDonateButton: Bool = false
var hideBlockedApps: Bool = false
var showTimedUnblockLiveActivity: Bool = false
var enableCustomDomains: Bool = false

Expand Down
18 changes: 18 additions & 0 deletions Normal/Views/Home/BlockedItemVisibility.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Foundation

/// Decides which entries of a status list stay visible when the user has opted to
/// hide whatever is currently blocked. Generic over the item so the rules can be
/// tested without Screen Time tokens, which cannot be built outside the system.
struct BlockedItemVisibility {
let hideBlocked: Bool

func visible<Item>(_ items: [Item], isBlocked: (Item) -> Bool) -> [Item] {
guard hideBlocked else { return items }
return items.filter { !isBlocked($0) }
}

func hiddenCount<Item>(_ items: [Item], isBlocked: (Item) -> Bool) -> Int {
guard hideBlocked else { return 0 }
return items.filter(isBlocked).count
}
}
60 changes: 53 additions & 7 deletions Normal/Views/Home/StatusDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,41 @@ struct StatusDetailView: View {
ScheduleSummary(schedules: schedules)
}

private var visibility: BlockedItemVisibility {
BlockedItemVisibility(hideBlocked: allSettings.first?.hideBlockedApps ?? false)
}

private var appKinds: [SelectedTokenKind] {
selection.applicationTokens.sortedStably.map(SelectedTokenKind.application)
}

private var websiteKinds: [SelectedTokenKind] {
selection.webDomainTokens.sortedStably.map(SelectedTokenKind.webDomain)
}

private var categoryKinds: [SelectedTokenKind] {
selection.categoryTokens.sortedStably.map(SelectedTokenKind.category)
}

// Custom domains are filtered as a group: the shield store tracks them as one
// filter list, so they follow the overall status like their rows already do.
private var areCustomDomainsBlocked: Bool { overallStatus != .none }

private var visibleCustomDomains: [String] {
visibility.visible(customDomains) { _ in areCustomDomainsBlocked }
}

private var hiddenCount: Int {
visibility.hiddenCount(appKinds + websiteKinds + categoryKinds) { screenTimeService.isShielded($0) }
+ visibility.hiddenCount(customDomains) { _ in areCustomDomainsBlocked }
}

var body: some View {
List {
overallSection
tokenSection("Apps", kinds: selection.applicationTokens.sortedStably.map(SelectedTokenKind.application))
tokenSection("Websites", kinds: selection.webDomainTokens.sortedStably.map(SelectedTokenKind.webDomain))
tokenSection("Categories", kinds: selection.categoryTokens.sortedStably.map(SelectedTokenKind.category))
tokenSection("Apps", kinds: appKinds)
tokenSection("Websites", kinds: websiteKinds)
tokenSection("Categories", kinds: categoryKinds)
customDomainsSection
if !schedules.isEmpty { schedulesSection }
}
Expand All @@ -50,14 +79,30 @@ struct StatusDetailView: View {
.foregroundStyle(.secondary)
}
}
if hiddenCount > 0 { hiddenNote }
}
}

private var hiddenNote: some View {
Label {
if hiddenCount == 1 {
Text("1 blocked item hidden")
} else {
Text("\(hiddenCount) blocked items hidden")
}
} icon: {
Image(systemName: "eye.slash")
}
.font(.caption)
.foregroundStyle(.secondary)
.accessibilityIdentifier("status.hiddenNote")
}

@ViewBuilder
private var customDomainsSection: some View {
if !customDomains.isEmpty {
if !visibleCustomDomains.isEmpty {
Section("Custom Domains") {
ForEach(customDomains, id: \.self) { domain in
ForEach(visibleCustomDomains, id: \.self) { domain in
HStack {
Label(domain, systemImage: "globe")
.labelStyle(.titleAndIcon)
Expand All @@ -77,9 +122,10 @@ struct StatusDetailView: View {

@ViewBuilder
private func tokenSection(_ title: LocalizedStringKey, kinds: [SelectedTokenKind]) -> some View {
if !kinds.isEmpty {
let shown = visibility.visible(kinds) { screenTimeService.isShielded($0) }
if !shown.isEmpty {
Section(title) {
ForEach(kinds, id: \.self, content: tokenRow)
ForEach(shown, id: \.self, content: tokenRow)
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion Normal/Views/Settings/GeneralSettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,16 @@ struct GeneralSettingsView: View {
"Hide Donate Button",
isOn: Bindable(settings).hideDonateButton
)

Toggle(
"Hide Blocked Apps",
isOn: Bindable(settings).hideBlockedApps
)
.accessibilityIdentifier("settings.hideBlockedAppsToggle")
} header: {
Text("Navigation & Appearance")
} footer: {
Text("Default Page opens when you launch the app. Hiding the Donate button hides it from the main toolbar.")
Text("Default Page opens when you launch the app. Hiding the Donate button hides it from the main toolbar. Hiding blocked apps keeps apps, websites and categories out of the Status list while they are blocked.")
}
}

Expand Down
1 change: 1 addition & 0 deletions NormalTests/Models/SettingsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ struct SettingsTests {
#expect(s.blockAllPreventsAppDelete == true)
#expect(s.hasCompletedOnboarding == false)
#expect(s.defaultTab == nil)
#expect(s.hideBlockedApps == false)
}
}
55 changes: 55 additions & 0 deletions NormalTests/Views/BlockedItemVisibilityTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import Foundation
@testable import Normal
import Testing

@MainActor
struct BlockedItemVisibilityTests {
private static let blocked: Set<String> = ["mail", "news"]

private func visible(_ items: [String], hideBlocked: Bool) -> [String] {
BlockedItemVisibility(hideBlocked: hideBlocked).visible(items) { Self.blocked.contains($0) }
}

private func hiddenCount(_ items: [String], hideBlocked: Bool) -> Int {
BlockedItemVisibility(hideBlocked: hideBlocked).hiddenCount(items) { Self.blocked.contains($0) }
}

@Test func showsEverythingWhenDisabled() {
let items = ["mail", "maps", "news"]
#expect(visible(items, hideBlocked: false) == items)
}

@Test func hidesBlockedItemsWhenEnabled() {
#expect(visible(["mail", "maps", "news"], hideBlocked: true) == ["maps"])
}

@Test func preservesOrderOfRemainingItems() {
let items = ["maps", "mail", "phone", "news", "photos"]
#expect(visible(items, hideBlocked: true) == ["maps", "phone", "photos"])
}

@Test func hidesNothingWhenNoItemIsBlocked() {
let items = ["maps", "phone"]
#expect(visible(items, hideBlocked: true) == items)
#expect(hiddenCount(items, hideBlocked: true) == 0)
}

@Test func hidesEverythingWhenAllItemsAreBlocked() {
let items = ["mail", "news"]
#expect(visible(items, hideBlocked: true).isEmpty)
#expect(hiddenCount(items, hideBlocked: true) == 2)
}

@Test func hiddenCountIsZeroWhenDisabled() {
#expect(hiddenCount(["mail", "news"], hideBlocked: false) == 0)
}

@Test func hiddenCountCountsOnlyBlockedItems() {
#expect(hiddenCount(["mail", "maps", "news"], hideBlocked: true) == 2)
}

@Test func emptyInputStaysEmpty() {
#expect(visible([], hideBlocked: true).isEmpty)
#expect(hiddenCount([], hideBlocked: true) == 0)
}
}