From cf16a9cd1090156a363fe875d2a7bacf55b6f006 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=E2=80=99Aquino?= Date: Fri, 30 Aug 2024 09:57:09 -0700 Subject: [PATCH] Improve handling of NostrDB when switching apps MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../ActionViewController.swift | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/highlighter action extension/ActionViewController.swift b/highlighter action extension/ActionViewController.swift index a1ef5c46..72aa1ea9 100644 --- a/highlighter action extension/ActionViewController.swift +++ b/highlighter action extension/ActionViewController.swift @@ -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() {