Skip to content

Commit 81cf50c

Browse files
committed
Fix the ddi bugs
1 parent 50fdf33 commit 81cf50c

2 files changed

Lines changed: 41 additions & 4 deletions

File tree

StikJIT/StikJITApp.swift

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ class TunnelManager: ObservableObject {
233233
}
234234
VPNLogger.shared.log("VPN status updated: \(self.tunnelStatus.rawValue)")
235235
if connectionStatus == .connected && heartbeatStartPending {
236-
startHeartbeatInBackground()
236+
startHeartbeatInBackground(showErrorUI: heartbeatPendingShowUI)
237237
}
238238
}
239239
}
@@ -502,6 +502,7 @@ class DNSChecker: ObservableObject {
502502
var pubHeartBeat = false
503503
private var heartbeatStartPending = false
504504
private var heartbeatStartInProgress = false
505+
private var heartbeatPendingShowUI = true
505506

506507
@main
507508
struct HeartbeatApp: App {
@@ -516,6 +517,7 @@ struct HeartbeatApp: App {
516517
@StateObject private var mount = MountingProgress.shared
517518
@StateObject private var themeExpansionManager = ThemeExpansionManager()
518519
@Environment(\.scenePhase) private var scenePhase // Observe scene lifecycle
520+
@State private var shouldAttemptHeartbeatRestart = false
519521

520522
init() {
521523
registerAdvancedOptionsDefault()
@@ -565,6 +567,20 @@ struct HeartbeatApp: App {
565567
}
566568
}
567569

570+
private func handleScenePhaseChange(_ newPhase: ScenePhase) {
571+
switch newPhase {
572+
case .background:
573+
shouldAttemptHeartbeatRestart = true
574+
case .active:
575+
if shouldAttemptHeartbeatRestart {
576+
shouldAttemptHeartbeatRestart = false
577+
startHeartbeatInBackground(showErrorUI: false)
578+
}
579+
default:
580+
break
581+
}
582+
}
583+
568584
private var globalAccent: Color {
569585
themeExpansionManager.resolvedAccentColor(from: customAccentColorHex)
570586
}
@@ -629,6 +645,9 @@ struct HeartbeatApp: App {
629645
HeartbeatApp.updateUIKitTint(customHex: newHex,
630646
hasAccess: themeExpansionManager.hasThemeExpansion)
631647
}
648+
.onChange(of: scenePhase) { newPhase in
649+
handleScenePhaseChange(newPhase)
650+
}
632651
.sheet(isPresented: $showWelcomeSheet) {
633652
WelcomeSheetView {
634653
// When the user taps "Continue", mark the app as launched and start the VPN if allowed.
@@ -753,12 +772,13 @@ func isPairing() -> Bool {
753772
return true
754773
}
755774

756-
func startHeartbeatInBackground(requireVPNConnection: Bool? = nil) {
775+
func startHeartbeatInBackground(requireVPNConnection: Bool? = nil, showErrorUI: Bool = true) {
757776
assert(Thread.isMainThread, "startHeartbeatInBackground must be called on the main thread")
758777
let pairingFileURL = URL.documentsDirectory.appendingPathComponent("pairingFile.plist")
759778

760779
guard FileManager.default.fileExists(atPath: pairingFileURL.path) else {
761780
heartbeatStartPending = false
781+
heartbeatPendingShowUI = true
762782
return
763783
}
764784

@@ -768,6 +788,7 @@ func startHeartbeatInBackground(requireVPNConnection: Bool? = nil) {
768788
if !heartbeatStartPending {
769789
print("Heartbeat start deferred until VPN connects")
770790
}
791+
heartbeatPendingShowUI = showErrorUI
771792
heartbeatStartPending = true
772793
return
773794
}
@@ -777,6 +798,7 @@ func startHeartbeatInBackground(requireVPNConnection: Bool? = nil) {
777798
}
778799

779800
heartbeatStartPending = false
801+
heartbeatPendingShowUI = true
780802
heartbeatStartInProgress = true
781803

782804
DispatchQueue.global(qos: .userInteractive).async {
@@ -798,6 +820,7 @@ func startHeartbeatInBackground(requireVPNConnection: Bool? = nil) {
798820
let err2 = error as NSError
799821
let code = err2.code
800822
print("Error: \(error.localizedDescription) (Code: \(code))")
823+
guard showErrorUI else { return }
801824
DispatchQueue.main.async {
802825
if code == -9 {
803826
do {

StikJIT/Views/HomeView.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ struct HomeView: View {
7676
@State private var isSchedulingInitialSetup = false
7777
@AppStorage("cachedAppNamesData") private var cachedAppNamesData: Data?
7878
@AppStorage("autoStartVPN") private var autoStartVPN = true
79+
@State private var lastDDIIndicatorStatus: StartupIndicatorStatus = .idle
7980

8081
@AppStorage("appTheme") private var appThemeRaw: String = AppTheme.system.rawValue
8182
@Environment(\.themeExpansionManager) private var themeExpansion
@@ -195,6 +196,7 @@ struct HomeView: View {
195196
hasAutoStartedConnectionCheck = true
196197
runConnectionDiagnostics(autoStart: true)
197198
}
199+
lastDDIIndicatorStatus = ddiIndicatorStatus
198200
NotificationCenter.default.addObserver(
199201
forName: NSNotification.Name("ShowPairingFilePicker"),
200202
object: nil,
@@ -212,6 +214,9 @@ struct HomeView: View {
212214
refreshBackground()
213215
checkPairingFileExists()
214216
heartbeatOK = pubHeartBeat
217+
if !mounting.coolisMounted {
218+
MountingProgress.shared.checkforMounted()
219+
}
215220
}
216221
.onChange(of: pairingFileExists) { _, newValue in
217222
if newValue {
@@ -233,6 +238,9 @@ struct HomeView: View {
233238
loadAppListIfNeeded()
234239
syncFavoriteAppNamesWithCache()
235240
}
241+
.onChange(of: ddiIndicatorStatus) { _, newStatus in
242+
handleDDIIndicatorChange(newStatus)
243+
}
236244
.onChange(of: recentApps) { _, _ in
237245
loadAppListIfNeeded()
238246
}
@@ -269,6 +277,7 @@ struct HomeView: View {
269277
isImportingFile = false
270278
pairingFileIsValid = true
271279
withAnimation { showPairingFileMessage = true }
280+
MountingProgress.shared.checkforMounted()
272281
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
273282
withAnimation { showPairingFileMessage = false }
274283
}
@@ -651,6 +660,12 @@ struct HomeView: View {
651660
isCellularActive = false
652661
}
653662

663+
private func handleDDIIndicatorChange(_ newStatus: StartupIndicatorStatus) {
664+
defer { lastDDIIndicatorStatus = newStatus }
665+
guard lastDDIIndicatorStatus == .success, newStatus == .warning else { return }
666+
startHeartbeatInBackground(showErrorUI: false)
667+
}
668+
654669
private var pairingStatusDescription: String {
655670
if isValidatingPairingFile { return "Validating pairing file…" }
656671
if pairingFileExists {
@@ -1178,8 +1193,7 @@ struct HomeView: View {
11781193
refreshBackground()
11791194
checkPairingFileExists()
11801195
loadAppListIfNeeded()
1181-
1182-
if tunnel.tunnelStatus == .connected {
1196+
if tunnel.tunnelStatus == .connected || !requiresLoopbackVPN {
11831197
MountingProgress.shared.checkforMounted()
11841198
}
11851199
}

0 commit comments

Comments
 (0)