From 90a7432bf9de1f969df7857b9aa539303558be2c Mon Sep 17 00:00:00 2001
From: OmarHatem <omarh.ismail1@gmail.com>
Date: Thu, 26 Jan 2023 02:05:48 +0200
Subject: [PATCH 1/4] - Remove legacy migration from macos - Remove wake lock
 native code and just use the ready made package

---
 ios/CakeWallet/wakeLock.swift                 | 20 ------
 ios/Runner.xcodeproj/project.pbxproj          |  2 -
 lib/di.dart                                   |  3 -
 lib/entities/wake_lock.dart                   | 21 ------
 .../on_wallet_sync_status_change.dart         |  9 +--
 macos/Flutter/GeneratedPluginRegistrant.swift |  2 +
 macos/Runner/AppDelegate.swift                | 68 -------------------
 pubspec_base.yaml                             |  1 +
 8 files changed, 6 insertions(+), 120 deletions(-)
 delete mode 100644 ios/CakeWallet/wakeLock.swift
 delete mode 100644 lib/entities/wake_lock.dart

diff --git a/ios/CakeWallet/wakeLock.swift b/ios/CakeWallet/wakeLock.swift
deleted file mode 100644
index 35f23eafa..000000000
--- a/ios/CakeWallet/wakeLock.swift
+++ /dev/null
@@ -1,20 +0,0 @@
-//
-//  wakeLock.swift
-//  Runner
-//
-//  Created by Godwin Asuquo on 1/21/22.
-//
-
-import Foundation
-import UIKit
-
-func enableWakeScreen() -> Bool{
-    UIApplication.shared.isIdleTimerDisabled = true
-
-    return true
-}
-
-func disableWakeScreen() -> Bool{
-    UIApplication.shared.isIdleTimerDisabled = false
-    return true
-}
diff --git a/ios/Runner.xcodeproj/project.pbxproj b/ios/Runner.xcodeproj/project.pbxproj
index 8d62665c3..81b8dfcc7 100644
--- a/ios/Runner.xcodeproj/project.pbxproj
+++ b/ios/Runner.xcodeproj/project.pbxproj
@@ -12,7 +12,6 @@
 		1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; };
 		20ED0868E1BD7E12278C0CB3 /* Pods_Runner.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B26E3F56D69167FBB1DC160A /* Pods_Runner.framework */; };
 		3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; };
-		5AFFEBFD279AD49C00F906A4 /* wakeLock.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5AFFEBFC279AD49C00F906A4 /* wakeLock.swift */; };
 		74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74858FAE1ED2DC5600515810 /* AppDelegate.swift */; };
 		97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; };
 		97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FD1CF9000F007C117D /* Assets.xcassets */; };
@@ -280,7 +279,6 @@
 			files = (
 				74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */,
 				1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */,
-				5AFFEBFD279AD49C00F906A4 /* wakeLock.swift in Sources */,
 				0C9D68C9264854B60011B691 /* secRandom.swift in Sources */,
 				0C44A71A2518EF8000B570ED /* decrypt.swift in Sources */,
 			);
diff --git a/lib/di.dart b/lib/di.dart
index 1acdad2dd..fac4d634a 100644
--- a/lib/di.dart
+++ b/lib/di.dart
@@ -1,6 +1,5 @@
 import 'package:cake_wallet/core/yat_service.dart';
 import 'package:cake_wallet/entities/parse_address_from_domain.dart';
-import 'package:cake_wallet/entities/wake_lock.dart';
 import 'package:cake_wallet/ionia/ionia_anypay.dart';
 import 'package:cake_wallet/ionia/ionia_gift_card.dart';
 import 'package:cake_wallet/ionia/ionia_tip.dart';
@@ -704,8 +703,6 @@ Future setup(
             param1: item, param2: unspentCoinsListViewModel));
   });
 
-  getIt.registerFactory(() => WakeLock());
-
   getIt.registerFactory(() => YatService());
 
   getIt.registerFactory(() => AddressResolver(yatService: getIt.get<YatService>(),
diff --git a/lib/entities/wake_lock.dart b/lib/entities/wake_lock.dart
deleted file mode 100644
index 99acc65ee..000000000
--- a/lib/entities/wake_lock.dart
+++ /dev/null
@@ -1,21 +0,0 @@
-import 'package:flutter/services.dart';
-
-class WakeLock {
-  static const _utils = const MethodChannel('com.cake_wallet/native_utils');
-
-  Future<void> enableWake() async {
-    try {
-      await _utils.invokeMethod<bool>('enableWakeScreen');
-    } on PlatformException catch (_) {
-      print('Failed enabling screen wakelock');
-    }
-  }
-
-  Future<void> disableWake() async {
-    try {
-      await _utils.invokeMethod<bool>('disableWakeScreen');
-    } on PlatformException catch (_) {
-      print('Failed enabling screen wakelock');
-    }
-  }
-}
diff --git a/lib/reactions/on_wallet_sync_status_change.dart b/lib/reactions/on_wallet_sync_status_change.dart
index 68bd4b3c2..767bfd7e8 100644
--- a/lib/reactions/on_wallet_sync_status_change.dart
+++ b/lib/reactions/on_wallet_sync_status_change.dart
@@ -1,6 +1,4 @@
-import 'package:cake_wallet/di.dart';
 import 'package:cake_wallet/entities/update_haven_rate.dart';
-import 'package:cake_wallet/entities/wake_lock.dart';
 import 'package:cake_wallet/store/dashboard/fiat_conversion_store.dart';
 import 'package:cw_core/wallet_type.dart';
 import 'package:mobx/mobx.dart';
@@ -9,7 +7,7 @@ import 'package:cw_core/wallet_base.dart';
 import 'package:cw_core/balance.dart';
 import 'package:cw_core/transaction_info.dart';
 import 'package:cw_core/sync_status.dart';
-import 'package:flutter/services.dart';
+import 'package:wakelock/wakelock.dart';
 
 ReactionDisposer? _onWalletSyncStatusChangeReaction;
 
@@ -17,7 +15,6 @@ void startWalletSyncStatusChangeReaction(
     WalletBase<Balance, TransactionHistoryBase<TransactionInfo>,
             TransactionInfo> wallet,
     FiatConversionStore fiatConversionStore) {
-  final _wakeLock = getIt.get<WakeLock>();
   _onWalletSyncStatusChangeReaction?.reaction.dispose();
   _onWalletSyncStatusChangeReaction =
       reaction((_) => wallet.syncStatus, (SyncStatus status) async {
@@ -30,10 +27,10 @@ void startWalletSyncStatusChangeReaction(
         }
       }
       if (status is SyncingSyncStatus) {
-        await _wakeLock.enableWake();
+        await Wakelock.enable();
       }
       if (status is SyncedSyncStatus || status is FailedSyncStatus) {
-        await _wakeLock.disableWake();
+        await Wakelock.disable();
       }
     } catch(e) {
       print(e.toString());
diff --git a/macos/Flutter/GeneratedPluginRegistrant.swift b/macos/Flutter/GeneratedPluginRegistrant.swift
index 068b087fe..745087d97 100644
--- a/macos/Flutter/GeneratedPluginRegistrant.swift
+++ b/macos/Flutter/GeneratedPluginRegistrant.swift
@@ -16,6 +16,7 @@ import platform_device_id_macos
 import share_plus_macos
 import shared_preferences_foundation
 import url_launcher_macos
+import wakelock_macos
 
 func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
   ConnectivityPlugin.register(with: registry.registrar(forPlugin: "ConnectivityPlugin"))
@@ -29,4 +30,5 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
   SharePlusMacosPlugin.register(with: registry.registrar(forPlugin: "SharePlusMacosPlugin"))
   SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
   UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
+  WakelockMacosPlugin.register(with: registry.registrar(forPlugin: "WakelockMacosPlugin"))
 }
diff --git a/macos/Runner/AppDelegate.swift b/macos/Runner/AppDelegate.swift
index def2979dd..0c8973175 100644
--- a/macos/Runner/AppDelegate.swift
+++ b/macos/Runner/AppDelegate.swift
@@ -8,54 +8,8 @@ class AppDelegate: FlutterAppDelegate {
         return true
     }
 
-    var assertionID: IOPMAssertionID = 0
-
     override func applicationDidFinishLaunching(_ notification: Notification) {
         let controller : FlutterViewController = mainFlutterWindow?.contentViewController as! FlutterViewController
-        let legacyMigrationChannel = FlutterMethodChannel(
-            name: "com.cakewallet.cakewallet/legacy_wallet_migration",
-            binaryMessenger: controller.engine.binaryMessenger)
-        legacyMigrationChannel.setMethodCallHandler({
-            (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
-
-            switch call.method {
-            case "decrypt":
-                guard let args = call.arguments as? Dictionary<String, Any>,
-                      let data = args["bytes"] as? FlutterStandardTypedData,
-                      let key = args["key"] as? String,
-                      let salt = args["salt"] as? String else {
-                    result(nil)
-                    return
-                }
-
-                let content = decrypt(data: data.data, key: key, salt: salt)
-                result(content)
-            case "read_user_defaults":
-                guard let args = call.arguments as? Dictionary<String, Any>,
-                      let key = args["key"] as? String,
-                      let type = args["type"] as? String else {
-                    result(nil)
-                    return
-                }
-
-                var value: Any?
-
-                switch (type) {
-                case "string":
-                    value = UserDefaults.standard.string(forKey: key)
-                case "int":
-                    value = UserDefaults.standard.integer(forKey: key)
-                case "bool":
-                    value = UserDefaults.standard.bool(forKey: key)
-                default:
-                    break
-                }
-
-                result(value)
-            default:
-                result(FlutterMethodNotImplemented)
-            }
-        })
 
         let utilsChannel = FlutterMethodChannel(
             name: "com.cake_wallet/native_utils",
@@ -70,32 +24,10 @@ class AppDelegate: FlutterAppDelegate {
                 }
 
                 result(secRandom(count: count))
-            case "getUnstoppableDomainAddress":
-                // Not supported on macos
-                result(nil)
-            case "enableWakeScreen":
-                result(self?.enableWakeScreen())
-
-            case "disableWakeScreen":
-                result(self?.disableWakeScreen())
 
             default:
                 result(FlutterMethodNotImplemented)
             }
         })
     }
-
-
-    func enableWakeScreen(reason: String = "Disabling display sleep") -> Bool{
-          return IOPMAssertionCreateWithName( kIOPMAssertionTypeNoDisplaySleep as CFString,
-                                IOPMAssertionLevel(kIOPMAssertionLevelOn),
-                                reason as CFString,
-                                &assertionID) == kIOReturnSuccess
-    }
-
-    func disableWakeScreen() -> Bool{
-        IOPMAssertionRelease(assertionID)
-
-        return true
-    }
 }
diff --git a/pubspec_base.yaml b/pubspec_base.yaml
index 4bd308abd..6b44fdfac 100644
--- a/pubspec_base.yaml
+++ b/pubspec_base.yaml
@@ -61,6 +61,7 @@ dependencies:
   permission_handler: ^10.0.0
   device_display_brightness: ^0.0.6
   platform_device_id: ^1.0.1
+  wakelock: ^0.6.2
   cake_backup:
     git:
       url: https://github.com/cake-tech/cake_backup.git

From 409c13a77a65aee1eeafbef4b7a8f21fb98c65e0 Mon Sep 17 00:00:00 2001
From: OmarHatem <omarh.ismail1@gmail.com>
Date: Thu, 26 Jan 2023 02:25:28 +0200
Subject: [PATCH 2/4] Remove wake lock native code and just use the ready made
 package

---
 ios/Runner/AppDelegate.swift | 5 -----
 1 file changed, 5 deletions(-)

diff --git a/ios/Runner/AppDelegate.swift b/ios/Runner/AppDelegate.swift
index c4d460e08..cdc7071e8 100644
--- a/ios/Runner/AppDelegate.swift
+++ b/ios/Runner/AppDelegate.swift
@@ -96,11 +96,6 @@ import UnstoppableDomainsResolution
                     
                     result(address)
                 }
-            case "enableWakeScreen":
-                result(enableWakeScreen())
-
-            case "disableWakeScreen":
-                result(disableWakeScreen())
 
             default:
                 result(FlutterMethodNotImplemented)

From 132033207e594929b0432df57f335cab0fc4dc35 Mon Sep 17 00:00:00 2001
From: OmarHatem <omarh.ismail1@gmail.com>
Date: Thu, 26 Jan 2023 02:33:15 +0200
Subject: [PATCH 3/4] Remove unstoppable domain from macos since it's not
 supported

---
 macos/Podfile | 2 --
 1 file changed, 2 deletions(-)

diff --git a/macos/Podfile b/macos/Podfile
index 8261d92c2..bb6c2dc2d 100644
--- a/macos/Podfile
+++ b/macos/Podfile
@@ -34,8 +34,6 @@ target 'Runner' do
 
   # Cake Wallet (Legacy)
   pod 'CryptoSwift'
-#   Not supported on macos
-#   pod 'UnstoppableDomainsResolution', '~> 5.0.0'
 end
 
 post_install do |installer|

From adfebe69403dd8d0350f58b89652fea0ff49837c Mon Sep 17 00:00:00 2001
From: OmarHatem <omarh.ismail1@gmail.com>
Date: Thu, 26 Jan 2023 20:53:26 +0200
Subject: [PATCH 4/4] Temporarily fetch unstoppable domains only on mobile

---
 lib/entities/unstoppable_domain_address.dart | 21 +++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/lib/entities/unstoppable_domain_address.dart b/lib/entities/unstoppable_domain_address.dart
index ab94e31fb..531702d38 100644
--- a/lib/entities/unstoppable_domain_address.dart
+++ b/lib/entities/unstoppable_domain_address.dart
@@ -1,3 +1,5 @@
+import 'dart:io';
+
 import 'package:flutter/services.dart';
 
 const channel = MethodChannel('com.cake_wallet/native_utils');
@@ -6,13 +8,18 @@ Future<String> fetchUnstoppableDomainAddress(String domain, String ticker) async
   var address = '';
 
   try {
-    address = await channel.invokeMethod<String>(
-        'getUnstoppableDomainAddress',
-        <String, String> {
-          'domain' : domain,
-          'ticker' : ticker
-        }
-    ) ?? '';
+    if (Platform.isAndroid || Platform.isIOS) {
+      address = await channel.invokeMethod<String>(
+          'getUnstoppableDomainAddress',
+          <String, String> {
+            'domain' : domain,
+            'ticker' : ticker
+          }
+      ) ?? '';
+    } else {
+      // TODO: Integrate with Unstoppable domains resolution API
+      return address;
+    }
   } catch (e) {
     print('Unstoppable domain error: ${e.toString()}');
     address = '';