Skip to content
Open
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
78 changes: 78 additions & 0 deletions BDKSwiftExampleWallet/App/BDKSwiftExampleWalletApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@
//

import BitcoinDevKit
import BackgroundTasks
import SwiftUI

@main
struct BDKSwiftExampleWalletApp: App {
@AppStorage("isOnboarding") var isOnboarding: Bool = true
@State private var navigationPath = NavigationPath()
@State private var refreshTrigger = UUID()
@Environment(\.scenePhase) private var scenePhase

init() {
BackgroundCBFSyncTask.register()
BackgroundCBFSyncTask.schedule()
}

var body: some Scene {
WindowGroup {
Expand All @@ -30,6 +37,11 @@ struct BDKSwiftExampleWalletApp: App {
BDKClient.live.setNeedsFullScan(true)
navigationPath = NavigationPath()
}
.onChange(of: scenePhase) { _, newValue in
if newValue == .background {
BackgroundCBFSyncTask.schedule()
}
}
}
}
}
Expand All @@ -42,3 +54,69 @@ extension BDKSwiftExampleWalletApp {
return (try? KeyClient.live.getBackupInfo()) != nil
}
}

private enum BackgroundCBFSyncTask {
static let identifier = "com.bitcoindevkit.bdkswiftexamplewallet.cbf-sync"
private static let minimumInterval: TimeInterval = 60 * 60 * 24 * 7

static func register() {
BGTaskScheduler.shared.register(
forTaskWithIdentifier: identifier,
using: nil
) { task in
guard let processingTask = task as? BGProcessingTask else {
task.setTaskCompleted(success: false)
return
}
handle(processingTask)
}
}

static func schedule() {
let request = BGProcessingTaskRequest(identifier: identifier)
request.requiresNetworkConnectivity = true
request.requiresExternalPower = true
request.earliestBeginDate = Date(timeIntervalSinceNow: minimumInterval)

do {
try BGTaskScheduler.shared.submit(request)
} catch {
print("[BackgroundCBF] Failed to schedule task: \(error)")
}
}

private static func handle(_ task: BGProcessingTask) {
schedule()

let syncTask = Task.detached(priority: .background) {
do {
try await runKyotoSync()
task.setTaskCompleted(success: true)
} catch {
print("[BackgroundCBF] Background sync failed: \(error)")
task.setTaskCompleted(success: false)
}
}

task.expirationHandler = {
syncTask.cancel()
}
}

private static func runKyotoSync() async throws {
let bdkClient = BDKClient.live

guard (try? bdkClient.getBackupInfo()) != nil else {
return
}

try bdkClient.loadWallet()

guard bdkClient.getClientType() == .kyoto else {
return
}

let inspector = WalletSyncScriptInspector(updateProgress: { _, _ in })
try await bdkClient.syncWithInspector(inspector)
}
}
8 changes: 8 additions & 0 deletions BDKSwiftExampleWallet/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>BGTaskSchedulerPermittedIdentifiers</key>
<array>
<string>com.bitcoindevkit.bdkswiftexamplewallet.cbf-sync</string>
</array>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>NSPasteboardUsageDescription</key>
<string>"To allow users to copy and paste text between the app and other apps"</string>
<key>UIBackgroundModes</key>
<array>
<string>processing</string>
</array>
</dict>
</plist>
2 changes: 1 addition & 1 deletion BDKSwiftExampleWallet/Service/Key Service/KeyService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ private struct KeyService {
let keychain = Keychain(service: "com.matthewramsden.bdkswiftexamplewallet.testservice")
.label(Bundle.main.displayName)
.synchronizable(false)
.accessibility(.whenUnlocked)
.accessibility(.afterFirstUnlock)
self.keychain = keychain
}

Expand Down