Improve handling of NostrDB when switching apps

There was an issue where profiles on Damus would not load when switching
back and forth between the extension and Damus.

This commit fixes that by closing NostrDB when the extension is backgrounded

Testing
-------

PASS

Device: iPhone 13 Mini
iOS: 17.6.1
Damus: This commit
Steps:
1. Go to a webpage in safari, and open the highlight extension
2. With the highlight extension open, switch apps to Damus (without closing the extension)
3. Make sure profiles can be loaded on Damus

Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
This commit is contained in:
Daniel D’Aquino
2024-08-30 09:57:09 -07:00
parent b43dcd2bc7
commit cf16a9cd10

View File

@@ -20,6 +20,8 @@ struct ShareExtensionView: View {
@State private var selectedTextHeight: CGFloat = .zero
@State private var selectedTextWidth: CGFloat = .zero
@Environment(\.scenePhase) var scenePhase
var body: some View {
VStack(spacing: 15) {
if let state {
@@ -147,6 +149,41 @@ struct ShareExtensionView: View {
self.highlighter_state = .cancelled
}
}
.onChange(of: scenePhase) { (phase: ScenePhase) in
guard let state else { return }
switch phase {
case .background:
print("txn: 📙 HIGHLIGHTER BACKGROUNDED")
Task { @MainActor in
state.ndb.close()
}
break
case .inactive:
print("txn: 📙 HIGHLIGHTER INACTIVE")
break
case .active:
print("txn: 📙 HIGHLIGHTER ACTIVE")
state.pool.ping()
@unknown default:
break
}
}
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { obj in
guard let state else { return }
print("txn: 📙 HIGHLIGHTER ACTIVE NOTIFY")
if state.ndb.reopen() {
print("txn: HIGHLIGHTER NOSTRDB REOPENED")
} else {
print("txn: HIGHLIGHTER NOSTRDB FAILED TO REOPEN closed: \(state.ndb.is_closed)")
}
}
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification)) { obj in
guard let state else { return }
print("txn: 📙 HIGHLIGHTER BACKGROUNDED")
Task { @MainActor in
state.ndb.close()
}
}
}
func loadSharedUrl() {