add stay awake swift support

Godwin Asuquo committed Jan 21, 2022 at 14:02 UTC f021dbf6a6b57e8ae1d674a6a0f0b25a6c66f91b
6 files changed +108 -53
ios/CakeWallet/wakeLock.swift new
+20
@@ -0,0 +1,20 @@
1 +//
2 +// wakeLock.swift
3 +// Runner
4 +//
5 +// Created by Godwin Asuquo on 1/21/22.
6 +//
7 +
8 +import Foundation
9 +import UIKit
10 +
11 +func enableWakeScreen() -> String {
12 + UIApplication.shared.isIdleTimerDisabled = true
13 +
14 + return "screen wake turned ON"
15 +}
16 +
17 +func disableWakeScreen() -> String{
18 + UIApplication.shared.isIdleTimerDisabled = false
19 + return "screen wake turned OFF"
20 +}
ios/Runner.xcodeproj/project.pbxproj
+4
@@ -12,6 +12,7 @@
12 1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; };
13 20ED0868E1BD7E12278C0CB3 /* Pods_Runner.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B26E3F56D69167FBB1DC160A /* Pods_Runner.framework */; };
14 3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; };
15 + 5AFFEBFD279AD49C00F906A4 /* wakeLock.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5AFFEBFC279AD49C00F906A4 /* wakeLock.swift */; };
16 74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74858FAE1ED2DC5600515810 /* AppDelegate.swift */; };
17 97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; };
18 97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FD1CF9000F007C117D /* Assets.xcassets */; };
@@ -28,6 +29,7 @@
29 20F67A1B2C2FCB2A3BB048C1 /* Pods-Runner.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.debug.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"; sourceTree = "<group>"; };
30 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = "<group>"; };
31 501EA9286675DC8636978EA4 /* Pods-Runner.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.release.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"; sourceTree = "<group>"; };
32 + 5AFFEBFC279AD49C00F906A4 /* wakeLock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = wakeLock.swift; sourceTree = "<group>"; };
33 61CAA8652B54F23356F7592A /* Pods-Runner.profile.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.profile.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.profile.xcconfig"; sourceTree = "<group>"; };
34 74858FAD1ED2DC5600515810 /* Runner-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Runner-Bridging-Header.h"; sourceTree = "<group>"; };
35 74858FAE1ED2DC5600515810 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
@@ -68,6 +70,7 @@
70 children = (
71 0C44A7192518EF8000B570ED /* decrypt.swift */,
72 0C9D68C8264854B60011B691 /* secRandom.swift */,
73 + 5AFFEBFC279AD49C00F906A4 /* wakeLock.swift */,
74 );
75 path = CakeWallet;
76 sourceTree = "<group>";
@@ -277,6 +280,7 @@
280 files = (
281 74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */,
282 1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */,
283 + 5AFFEBFD279AD49C00F906A4 /* wakeLock.swift in Sources */,
284 0C9D68C9264854B60011B691 /* secRandom.swift in Sources */,
285 0C44A71A2518EF8000B570ED /* decrypt.swift in Sources */,
286 );
ios/Runner/AppDelegate.swift
+6
@@ -96,6 +96,12 @@ import UnstoppableDomainsResolution
96
97 result(address)
98 }
99 + case "enableWakeScreen":
100 + result(enableWakeScreen())
101 +
102 + case "disableWakeScreen":
103 + result(disableWakeScreen())
104 +
105 default:
106 result(FlutterMethodNotImplemented)
107 }
lib/di.dart
+47 -53
@@ -1,3 +1,4 @@
1 +import 'package:cake_wallet/entities/wake_lock.dart';
2 import 'package:cake_wallet/monero/monero.dart';
3 import 'package:cake_wallet/bitcoin/bitcoin.dart';
4 import 'package:cw_core/unspent_coins_info.dart';
@@ -192,8 +193,8 @@ Future setup(
193 getIt.registerSingleton<ExchangeTemplateStore>(
194 ExchangeTemplateStore(templateSource: _exchangeTemplates));
195 getIt.registerSingleton<YatStore>(YatStore(
195 - appStore: getIt.get<AppStore>(),
196 - secureStorage: getIt.get<FlutterSecureStorage>())
196 + appStore: getIt.get<AppStore>(),
197 + secureStorage: getIt.get<FlutterSecureStorage>())
198 ..init());
199
200 final secretStore =
@@ -237,11 +238,9 @@ Future setup(
238 type: type, language: language);
239 });
240
240 - getIt.registerFactory<WalletAddressListViewModel>(
241 - () => WalletAddressListViewModel(
242 - appStore: getIt.get<AppStore>(),
243 - yatStore: getIt.get<YatStore>()
244 - ));
241 + getIt.registerFactory<WalletAddressListViewModel>(() =>
242 + WalletAddressListViewModel(
243 + appStore: getIt.get<AppStore>(), yatStore: getIt.get<YatStore>()));
244
245 getIt.registerFactory(() => BalanceViewModel(
246 appStore: getIt.get<AppStore>(),
@@ -323,8 +322,7 @@ Future setup(
322 getIt.get<AppStore>().wallet,
323 getIt.get<AppStore>().settingsStore,
324 getIt.get<SendTemplateStore>(),
326 - getIt.get<FiatConversionStore>()
327 - ));
325 + getIt.get<FiatConversionStore>()));
326
327 getIt.registerFactory<SendViewModel>(() => SendViewModel(
328 getIt.get<AppStore>().wallet,
@@ -336,9 +334,8 @@ Future setup(
334 getIt.registerFactory(
335 () => SendPage(sendViewModel: getIt.get<SendViewModel>()));
336
339 - getIt.registerFactory(
340 - () => SendTemplatePage(
341 - sendTemplateViewModel: getIt.get<SendTemplateViewModel>()));
337 + getIt.registerFactory(() => SendTemplatePage(
338 + sendTemplateViewModel: getIt.get<SendTemplateViewModel>()));
339
340 getIt.registerFactory(() => WalletListViewModel(
341 _walletInfoSource,
@@ -471,9 +468,11 @@ Future setup(
468 case WalletType.monero:
469 return monero.createMoneroWalletService(_walletInfoSource);
470 case WalletType.bitcoin:
474 - return bitcoin.createBitcoinWalletService(_walletInfoSource, _unspentCoinsInfoSource);
471 + return bitcoin.createBitcoinWalletService(
472 + _walletInfoSource, _unspentCoinsInfoSource);
473 case WalletType.litecoin:
476 - return bitcoin.createLitecoinWalletService(_walletInfoSource, _unspentCoinsInfoSource);
474 + return bitcoin.createLitecoinWalletService(
475 + _walletInfoSource, _unspentCoinsInfoSource);
476 default:
477 return null;
478 }
@@ -507,13 +506,13 @@ Future setup(
506 getIt
507 .registerFactoryParam<TransactionDetailsViewModel, TransactionInfo, void>(
508 (TransactionInfo transactionInfo, _) {
510 - final wallet = getIt.get<AppStore>().wallet;
511 - return TransactionDetailsViewModel(
512 - transactionInfo: transactionInfo,
513 - transactionDescriptionBox: _transactionDescriptionBox,
514 - wallet: wallet,
515 - settingsStore: getIt.get<SettingsStore>());
516 - });
509 + final wallet = getIt.get<AppStore>().wallet;
510 + return TransactionDetailsViewModel(
511 + transactionInfo: transactionInfo,
512 + transactionDescriptionBox: _transactionDescriptionBox,
513 + wallet: wallet,
514 + settingsStore: getIt.get<SettingsStore>());
515 + });
516
517 getIt.registerFactoryParam<TransactionDetailsPage, TransactionInfo, void>(
518 (TransactionInfo transactionInfo, _) => TransactionDetailsPage(
@@ -574,23 +573,21 @@ Future setup(
573 return PreOrderPage(buyViewModel: getIt.get<BuyViewModel>());
574 });
575
577 - getIt.registerFactoryParam<BuyWebViewPage, List, void>(
578 - (List args, _) {
579 - final url = args.first as String;
580 - final buyViewModel = args[1] as BuyViewModel;
576 + getIt.registerFactoryParam<BuyWebViewPage, List, void>((List args, _) {
577 + final url = args.first as String;
578 + final buyViewModel = args[1] as BuyViewModel;
579
582 - return BuyWebViewPage(buyViewModel: buyViewModel,
583 - ordersStore: getIt.get<OrdersStore>(), url: url);
584 - });
580 + return BuyWebViewPage(
581 + buyViewModel: buyViewModel,
582 + ordersStore: getIt.get<OrdersStore>(),
583 + url: url);
584 + });
585
586 - getIt.registerFactoryParam<OrderDetailsViewModel, Order, void>(
587 - (order, _) {
588 - final wallet = getIt.get<AppStore>().wallet;
586 + getIt.registerFactoryParam<OrderDetailsViewModel, Order, void>((order, _) {
587 + final wallet = getIt.get<AppStore>().wallet;
588
590 - return OrderDetailsViewModel(
591 - wallet: wallet,
592 - orderForDetails: order);
593 - });
589 + return OrderDetailsViewModel(wallet: wallet, orderForDetails: order);
590 + });
591
592 getIt.registerFactoryParam<OrderDetailsPage, Order, void>((Order order, _) =>
593 OrderDetailsPage(getIt.get<OrderDetailsViewModel>(param1: order)));
@@ -603,31 +600,28 @@ Future setup(
600 final wallet = getIt.get<AppStore>().wallet;
601
602 return UnspentCoinsListViewModel(
606 - wallet: wallet,
607 - unspentCoinsInfo: _unspentCoinsInfoSource);
603 + wallet: wallet, unspentCoinsInfo: _unspentCoinsInfoSource);
604 });
605
606 getIt.registerFactory(() => UnspentCoinsListPage(
611 - unspentCoinsListViewModel: getIt.get<UnspentCoinsListViewModel>()
612 - ));
607 + unspentCoinsListViewModel: getIt.get<UnspentCoinsListViewModel>()));
608
614 - getIt.registerFactoryParam<UnspentCoinsDetailsViewModel,
615 - UnspentCoinsItem, UnspentCoinsListViewModel>((item, model) =>
616 - UnspentCoinsDetailsViewModel(
617 - unspentCoinsItem: item,
618 - unspentCoinsListViewModel: model));
609 + getIt.registerFactoryParam<UnspentCoinsDetailsViewModel, UnspentCoinsItem,
610 + UnspentCoinsListViewModel>(
611 + (item, model) => UnspentCoinsDetailsViewModel(
612 + unspentCoinsItem: item, unspentCoinsListViewModel: model));
613
614 getIt.registerFactoryParam<UnspentCoinsDetailsPage, List, void>(
621 - (List args, _) {
622 - final item = args.first as UnspentCoinsItem;
623 - final unspentCoinsListViewModel = args[1] as UnspentCoinsListViewModel;
624 -
625 - return UnspentCoinsDetailsPage(
626 - unspentCoinsDetailsViewModel:
627 - getIt.get<UnspentCoinsDetailsViewModel>(
628 - param1: item,
629 - param2: unspentCoinsListViewModel));
615 + (List args, _) {
616 + final item = args.first as UnspentCoinsItem;
617 + final unspentCoinsListViewModel = args[1] as UnspentCoinsListViewModel;
618 +
619 + return UnspentCoinsDetailsPage(
620 + unspentCoinsDetailsViewModel: getIt.get<UnspentCoinsDetailsViewModel>(
621 + param1: item, param2: unspentCoinsListViewModel));
622 });
623
624 + getIt.registerFactory(() => WakeLock());
625 +
626 _isSetupFinished = true;
627 }
lib/entities/wake_lock.dart new
+21
@@ -0,0 +1,21 @@
1 +import 'package:flutter/services.dart';
2 +
3 +class WakeLock {
4 + static const _utils = const MethodChannel('com.cake_wallet/native_utils');
5 +
6 + Future<void> enableWake() async {
7 + try {
8 + await _utils.invokeMethod<String>('enableWakeScreen');
9 + } on PlatformException catch (_) {
10 + print('Failed enabling screen wakelock');
11 + }
12 + }
13 +
14 + Future<void> disableWake() async {
15 + try {
16 + await _utils.invokeMethod<String>('disableWakeScreen');
17 + } on PlatformException catch (_) {
18 + print('Failed enabling screen wakelock');
19 + }
20 + }
21 +}
lib/reactions/on_wallet_sync_status_change.dart
+10
@@ -1,9 +1,12 @@
1 +import 'package:cake_wallet/di.dart';
2 +import 'package:cake_wallet/entities/wake_lock.dart';
3 import 'package:mobx/mobx.dart';
4 import 'package:cw_core/transaction_history.dart';
5 import 'package:cw_core/wallet_base.dart';
6 import 'package:cw_core/balance.dart';
7 import 'package:cw_core/transaction_info.dart';
8 import 'package:cw_core/sync_status.dart';
9 +import 'package:flutter/services.dart';
10
11 ReactionDisposer _onWalletSyncStatusChangeReaction;
12
@@ -11,11 +14,18 @@ void startWalletSyncStatusChangeReaction(
14 WalletBase<Balance, TransactionHistoryBase<TransactionInfo>,
15 TransactionInfo>
16 wallet) {
17 + final WakeLock _wakeLock = getIt.get<WakeLock>();
18 _onWalletSyncStatusChangeReaction?.reaction?.dispose();
19 _onWalletSyncStatusChangeReaction =
20 reaction((_) => wallet.syncStatus, (SyncStatus status) async {
21 if (status is ConnectedSyncStatus) {
22 await wallet.startSync();
23 }
24 + if (status is SyncingSyncStatus) {
25 + await _wakeLock.enableWake();
26 + }
27 + if (status is SyncedSyncStatus || status is FailedSyncStatus) {
28 + await _wakeLock.disableWake();
29 + }
30 });
31 }