Skip to content

Commit

Permalink
Merge pull request #42 from WalletConnect/#41-register-background-task
Browse files Browse the repository at this point in the history
register background task
  • Loading branch information
llbartekll authored Jan 25, 2022
2 parents 496f6a8 + 6794a6f commit 673318a
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 17 deletions.
1 change: 1 addition & 0 deletions Example/ExampleApp/Proposer/ProposerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ final class ProposerViewController: UIViewController {
proposerView.tableView.delegate = self

client.delegate = self
client.logger.setLogging(level: .debug)
}

@objc func copyURI() {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Relayer/WakuNetworkRelay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public final class WakuNetworkRelay {
private func acknowledgeSubscription(requestId: Int64) {
let response = JSONRPCResponse(id: requestId, result: AnyCodable(true))
let responseJson = try! response.json()
try? jsonRpcSubscriptionsHistory.resolve(response: JsonRpcResponseTypes.response(response))
_ = try? jsonRpcSubscriptionsHistory.resolve(response: JsonRpcResponseTypes.response(response))
dispatcher.send(responseJson) { [weak self] error in
if let error = error {
self?.logger.debug("Failed to Respond for request id: \(requestId), error: \(error)")
Expand Down
3 changes: 2 additions & 1 deletion Sources/WalletConnect/Engine/PairingEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Foundation
import Combine
import WalletConnectUtils


final class PairingEngine {

var onApprovalAcknowledgement: ((Pairing) -> Void)?
Expand Down Expand Up @@ -163,7 +164,7 @@ final class PairingEngine {
}
}
}

private func setUpWCRequestHandling() {
wcSubscriber.onReceivePayload = { [unowned self] subscriptionPayload in
let requestId = subscriptionPayload.wcRequest.id
Expand Down
2 changes: 1 addition & 1 deletion Sources/WalletConnect/Engine/SessionEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ final class SessionEngine {
if let error = error {
logger.error(error)
} else {
try? sequencesStore.setSequence(session)
sequencesStore.setSequence(session)
onSessionUpgrade?(session.topic, newPermissions)
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/WalletConnect/Relay/WalletConnectRelay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ class WalletConnectRelay: WalletConnectRelaying {

func respond(topic: String, response: JsonRpcResponseTypes, completion: @escaping ((Error?)->())) {
do {
try jsonRpcHistory.resolve(response: response)
_ = try jsonRpcHistory.resolve(response: response)
let message = try jsonRpcSerialiser.serialise(topic: topic, encodable: response.value)
logger.debug("Responding....topic: \(topic)")
networkRelayer.publish(topic: topic, payload: message) { [weak self] error in
networkRelayer.publish(topic: topic, payload: message) { error in
completion(error)
}
} catch WalletConnectError.internal(.jsonRpcDuplicateDetected) {
Expand Down
35 changes: 23 additions & 12 deletions Sources/WalletConnect/WalletConnectClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ public final class WalletConnectClient {
private let secureStorage: SecureStorage
private let pairingQueue = DispatchQueue(label: "com.walletconnect.sdk.client.pairing", qos: .userInitiated)
private let history: JsonRpcHistory

#if os(iOS)
private var backgroundTaskID: UIBackgroundTaskIdentifier = .invalid
#endif

// MARK: - Initializers

/// Initializes and returns newly created WalletConnect Client Instance. Establishes a network connection with the relay
Expand Down Expand Up @@ -68,8 +71,25 @@ public final class WalletConnectClient {
self.sessionEngine = SessionEngine(relay: relay, crypto: crypto, subscriber: WCSubscriber(relay: relay, logger: logger), sequencesStore: sessionSequencesStore, isController: isController, metadata: metadata, logger: logger)
setUpEnginesCallbacks()
subscribeNotificationCenter()
registerBackgroundTask()
}

func registerBackgroundTask() {
#if os(iOS)
self.backgroundTaskID = UIApplication.shared.beginBackgroundTask (withName: "Finish Network Tasks") { [weak self] in
self?.endBackgroundTask()
}
#endif
}

func endBackgroundTask() {
#if os(iOS)
wakuRelay.disconnect(closeCode: .goingAway)
print("Background task ended.")
UIApplication.shared.endBackgroundTask(backgroundTaskID)
backgroundTaskID = .invalid
#endif
}
deinit {
unsubscribeNotificationCenter()
}
Expand Down Expand Up @@ -291,11 +311,6 @@ public final class WalletConnectClient {

private func subscribeNotificationCenter() {
#if os(iOS)
NotificationCenter.default.addObserver(
self,
selector: #selector(appDidEnterBackground),
name: UIApplication.didEnterBackgroundNotification,
object: nil)
NotificationCenter.default.addObserver(
self,
selector: #selector(appWillEnterForeground),
Expand All @@ -306,18 +321,14 @@ public final class WalletConnectClient {

private func unsubscribeNotificationCenter() {
#if os(iOS)
NotificationCenter.default.removeObserver(self, name: UIApplication.didEnterBackgroundNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIApplication.willEnterForegroundNotification, object: nil)
#endif
}

@objc
private func appWillEnterForeground() {
wakuRelay.connect()
registerBackgroundTask()
}

@objc
private func appDidEnterBackground() {
wakuRelay.disconnect(closeCode: .goingAway)
}

}

0 comments on commit 673318a

Please sign in to comment.