Merge pull request #5784 from BlueWallet/watch

FIX: Apple Watch app not working #5470
This commit is contained in:
GLaDOS 2023-10-31 08:20:16 +00:00 committed by GitHub
commit 7c9dd99183
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 75 additions and 53 deletions

3
App.js
View file

@ -26,7 +26,6 @@ import loc from './loc';
import { BlueDefaultTheme, BlueDarkTheme } from './components/themes';
import InitRoot from './Navigation';
import BlueClipboard from './blue_modules/clipboard';
import { isDesktop } from './blue_modules/environment';
import { BlueStorageContext } from './blue_modules/storage-context';
import WatchConnectivity from './WatchConnectivity';
import DeviceQuickActions from './class/quick-actions';
@ -387,8 +386,8 @@ const App = () => {
<InitRoot />
<Notifications onProcessNotifications={processPushNotifications} />
</NavigationContainer>
{walletsInitialized && !isDesktop && <WatchConnectivity />}
</View>
<WatchConnectivity />
<DeviceQuickActions />
<Biometric />
<WidgetCommunication />

View file

@ -1,11 +1,11 @@
import { useContext, useEffect, useRef } from 'react';
import React, { useContext, useEffect, useRef } from 'react';
import {
updateApplicationContext,
watchEvents,
useReachability,
useInstalled,
usePaired,
transferCurrentComplicationUserInfo,
transferUserInfo,
} from 'react-native-watch-connectivity';
import { Chain } from './models/bitcoinUnits';
import loc, { formatBalance, transactionTimeToReadable } from './loc';
@ -18,14 +18,13 @@ function WatchConnectivity() {
const { walletsInitialized, wallets, fetchWalletTransactions, saveToDisk, txMetadata, preferredFiatCurrency } =
useContext(BlueStorageContext);
const isReachable = useReachability();
const isPaired = usePaired();
const isInstalled = useInstalled(); // true | false
const messagesListenerActive = useRef(false);
const lastPreferredCurrency = useRef(FiatUnit.USD.endPointKey);
useEffect(() => {
let messagesListener = () => {};
if (isPaired && isInstalled && isReachable && walletsInitialized && messagesListenerActive.current === false) {
if (isInstalled && isReachable && walletsInitialized && messagesListenerActive.current === false) {
messagesListener = watchEvents.addListener('message', handleMessages);
messagesListenerActive.current = true;
} else {
@ -37,14 +36,26 @@ function WatchConnectivity() {
messagesListenerActive.current = false;
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [walletsInitialized, isPaired, isReachable, isInstalled]);
}, [walletsInitialized, isReachable, isInstalled]);
useEffect(() => {
if (isPaired && isInstalled && isReachable && walletsInitialized) {
sendWalletsToWatch();
console.warn(isInstalled, isReachable, walletsInitialized);
if (isInstalled && walletsInitialized) {
constructWalletsToSendToWatch().then(walletsToProcess => {
console.warn(walletsToProcess);
if (walletsToProcess) {
if (isReachable) {
transferUserInfo(walletsToProcess);
console.warn('sent info to watch transferUserInfo');
} else {
updateApplicationContext(walletsToProcess);
console.warn('sent info to watch context');
}
}
});
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [walletsInitialized, wallets, isPaired, isReachable, isInstalled]);
}, [walletsInitialized, wallets, isReachable, isInstalled]);
useEffect(() => {
updateApplicationContext({ isWalletsInitialized: walletsInitialized, randomID: Math.floor(Math.random() * 11) });
@ -78,8 +89,11 @@ function WatchConnectivity() {
reply({});
});
} else if (message.message === 'sendApplicationContext') {
sendWalletsToWatch();
reply({});
constructWalletsToSendToWatch().then(walletsToProcess => {
if (walletsToProcess) {
updateApplicationContext(walletsToProcess);
}
});
} else if (message.message === 'fetchTransactions') {
fetchWalletTransactions()
.then(() => saveToDisk())
@ -116,7 +130,7 @@ function WatchConnectivity() {
}
};
const sendWalletsToWatch = async () => {
const constructWalletsToSendToWatch = async () => {
if (!Array.isArray(wallets)) {
console.log('No Wallets set to sync with Watch app. Exiting...');
return;
@ -218,10 +232,10 @@ function WatchConnectivity() {
}
walletsToProcess.push(walletInformation);
}
updateApplicationContext({ wallets: walletsToProcess, randomID: Math.floor(Math.random() * 11) });
return { wallets: walletsToProcess, randomID: Math.floor(Math.random() * 11) };
};
return null;
return <></>;
}
export default WatchConnectivity;

View file

@ -47,7 +47,7 @@
<key>WidgetsExtension.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>147</integer>
<integer>129</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>

View file

@ -10,7 +10,6 @@
#import <RNCPushNotificationIOS.h>
#import "EventEmitter.h"
#import <React/RCTRootView.h>
#import <WatchConnectivity/WatchConnectivity.h>
@interface AppDelegate() <UNUserNotificationCenterDelegate>
@ -103,9 +102,6 @@
}
- (void)applicationWillTerminate:(UIApplication *)application {
if ([[WCSession defaultSession] isReachable]) {
[WCSession.defaultSession updateApplicationContext:@{@"isWalletsInitialized": @NO} error:nil];
}
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.io.bluewallet.bluewallet"];
[defaults removeObjectForKey:@"onUserActivityOpen"];
}

View file

@ -14,17 +14,29 @@ class InterfaceController: WKInterfaceController {
@IBOutlet weak var walletsTable: WKInterfaceTable!
@IBOutlet weak var noWalletsAvailableLabel: WKInterfaceLabel!
override func awake(withContext context: Any?) {
super.awake(withContext: context)
if let contextUnwrapped = context as? [String: Any] {
WatchDataSource.shared.processData(data: contextUnwrapped)
}
if WCSession.isSupported() {
print("Activating watch session")
WCSession.default.delegate = self
WCSession.default.activate()
}
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
if (WatchDataSource.shared.wallets.isEmpty) {
noWalletsAvailableLabel.setHidden(false)
} else {
processWalletsTable()
}
NotificationCenter.default.addObserver(self, selector: #selector(processWalletsTable), name: WatchDataSource.NotificationName.dataUpdated, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(processWalletsTable), name: WatchDataSource.NotificationName.dataUpdated, object: nil)
}
@objc private func processWalletsTable() {
@ -50,3 +62,31 @@ class InterfaceController: WKInterfaceController {
}
}
extension InterfaceController: WCSessionDelegate {
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
WatchDataSource.shared.processData(data: applicationContext)
}
func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any] = [:]) {
WatchDataSource.shared.processData(data: userInfo)
}
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
WatchDataSource.shared.companionWalletsInitialized = activationState == .activated
if activationState == .activated {
WatchDataSource.shared.processWalletsData(walletsInfo: WCSession.default.applicationContext)
}
}
func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
WatchDataSource.shared.processData(data: message)
}
override func didDeactivate() {
WCSession.default.activate()
}
}

View file

@ -10,7 +10,7 @@
import Foundation
import WatchConnectivity
class WatchDataSource: NSObject, WCSessionDelegate {
class WatchDataSource: NSObject {
struct NotificationName {
static let dataUpdated = Notification.Name(rawValue: "Notification.WalletDataSource.Updated")
}
@ -30,11 +30,6 @@ class WatchDataSource: NSObject, WCSessionDelegate {
wallets = walletData
WatchDataSource.postDataUpdatedNotification()
}
if WCSession.isSupported() {
print("Activating watch session")
WCSession.default.delegate = self
WCSession.default.activate()
}
}
func processWalletsData(walletsInfo: [String: Any]) {
@ -100,14 +95,7 @@ class WatchDataSource: NSObject, WCSessionDelegate {
}
}
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
processData(data: applicationContext)
}
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) {
processData(data: applicationContext)
}
func processData(data: [String: Any]) {
if let preferredFiatCurrency = data["preferredFiatCurrency"] as? String, let preferredFiatCurrencyUnit = fiatUnit(currency: preferredFiatCurrency) {
@ -122,19 +110,4 @@ class WatchDataSource: NSObject, WCSessionDelegate {
}
}
func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any] = [:]) {
processData(data: userInfo)
}
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
if activationState == .activated {
WCSession.default.sendMessage(["message" : "sendApplicationContext"], replyHandler: { (replyData) in
}) { (error) in
print(error)
}
} else {
WatchDataSource.shared.companionWalletsInitialized = false
}
}
}

View file

@ -40,7 +40,7 @@ class ViewQRCodefaceController: WKInterfaceController {
DispatchQueue.main.async {
guard let cgImage = EFQRCode.generate(
content: passedContext) else {
for: passedContext) else {
return
}
let image = UIImage(cgImage: cgImage)