Skip to content

Commit

Permalink
feat: add badge for insecure items
Browse files Browse the repository at this point in the history
Signed-off-by: 82Flex <82flex@gmail.com>
  • Loading branch information
Lessica committed Jan 28, 2024
1 parent b2d34e6 commit d88329a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions Reveil/DataModels/Presets/SecurityPresets.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ struct SecurityPresets: Codable {
"com.apple.security.network.client",
"com.apple.private.security.container-required",
"beta-reports-active",
"aps-environment",
]

var secureMainBundleIdentifiers: Set<String> = [
Expand Down
37 changes: 37 additions & 0 deletions Reveil/Pages/DashboardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
//

import SwiftUI
#if canImport(UserNotifications)
import UserNotifications
#endif

struct DashboardView: View, GlobalTimerObserver {
let id = UUID()
Expand All @@ -15,6 +18,28 @@ struct DashboardView: View, GlobalTimerObserver {
@ObservedObject private var securityModel = Security.shared
@State private var isNavigationLinkActive = false

private static var isUserNotificationAuthorizationRequested = false
private static var isUserNotificationAuthorizationRequestGranted = false

@available(iOS 16.0, *)
private func updateBadgeCount() -> Void {
#if canImport(UserNotifications)
if !Self.isUserNotificationAuthorizationRequested {
UNUserNotificationCenter.current().requestAuthorization(options: [.badge]) { succeed, error in
if succeed {
let securityModelPresented: Bool = PinStorage.shared.isPinned(forKey: .Security)
UNUserNotificationCenter.current().setBadgeCount(securityModelPresented ? Security.shared.numberOfInsecureChecks : 0)
}
Self.isUserNotificationAuthorizationRequestGranted = succeed
Self.isUserNotificationAuthorizationRequested = true
}
} else if (Self.isUserNotificationAuthorizationRequestGranted) {
let securityModelPresented: Bool = PinStorage.shared.isPinned(forKey: .Security)
UNUserNotificationCenter.current().setBadgeCount(securityModelPresented ? Security.shared.numberOfInsecureChecks : 0)
}
#endif
}

var body: some View {
ScrollView(.vertical) {
VStack {
Expand Down Expand Up @@ -44,11 +69,23 @@ struct DashboardView: View, GlobalTimerObserver {
.padding()
}
.onAppear {
if #available(iOS 16.0, *) {
updateBadgeCount()
}
GlobalTimer.shared.addObserver(self)
}
.onDisappear {
GlobalTimer.shared.removeObserver(self)
}
.onReceive(securityModel.$isLoading) { isLoading in
if #available(iOS 16.0, *) {
if !isLoading {
updateBadgeCount()
}
} else {
// Fallback on earlier versions
}
}
}

@ViewBuilder
Expand Down
2 changes: 2 additions & 0 deletions Reveil/Reveil.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,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>aps-environment</key>
<string>development</string>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.network.client</key>
Expand Down
9 changes: 8 additions & 1 deletion Reveil/ViewModels/Security.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,24 @@ final class Security: ObservableObject, StaticEntryProvider, Explainable {

@Published var isLoading: Bool
@Published var isInsecure: Bool
@Published var numberOfInsecureChecks: Int

private init() {
basicEntries = []
isLoading = false
isInsecure = false
numberOfInsecureChecks = 0
reloadData()
}

private func hasInsecureCheck(_ checks: [SecurityCheck]) -> Bool {
checks.first { $0.isFailed } != nil
}

private func countNumberOfInsecureChecks(_ checks: [SecurityCheck]) -> Int {
checks.filter { $0.isFailed }.count
}

func reloadData() {
guard !isLoading else {
return
Expand All @@ -71,10 +77,11 @@ final class Security: ObservableObject, StaticEntryProvider, Explainable {
let groupedEntries = Dictionary(grouping: performedEntries) { $0.children?.isEmpty ?? true }
let allEntries = (groupedEntries[false] ?? []) + (groupedEntries[true] ?? [])

DispatchQueue.main.async {
DispatchQueue.main.async { [unowned self] in
self.basicEntries.removeAll(keepingCapacity: true)
self.basicEntries.append(contentsOf: allEntries)
self.isInsecure = self.hasInsecureCheck(performedCases)
self.numberOfInsecureChecks = self.countNumberOfInsecureChecks(performedCases)
self.isLoading = false
}
}
Expand Down

0 comments on commit d88329a

Please sign in to comment.