This repository has been archived by the owner on Sep 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #156 from MXCzkEVM/dapp_hooks
DApp hooks & WiFi hooks
- Loading branch information
Showing
31 changed files
with
1,428 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export 'dapp_hooks_service.dart'; | ||
export 'notifications_service.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import 'package:background_fetch/background_fetch.dart'; | ||
import 'package:datadashwallet/core/core.dart'; | ||
import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||
import 'package:mxc_logic/mxc_logic.dart'; | ||
|
||
class DAppHooksService { | ||
@pragma( | ||
'vm:entry-point') // Mandatory if the App is obfuscated or using Flutter 3.1+ | ||
static void dappHooksServiceCallBackDispatcher(HeadlessTask task) async { | ||
String taskId = task.taskId; | ||
bool isTimeout = task.timeout; | ||
if (isTimeout) { | ||
// This task has exceeded its allowed running-time. | ||
// You must stop what you're doing and immediately .finish(taskId) | ||
print("[BackgroundFetch] Headless task timed-out: $taskId"); | ||
BackgroundFetch.finish(taskId); | ||
return; | ||
} | ||
dappHooksServiceCallBackDispatcherForeground(taskId); | ||
} | ||
|
||
static void dappHooksServiceCallBackDispatcherForeground( | ||
String taskId) async { | ||
try { | ||
await loadProviders(); | ||
|
||
final container = ProviderContainer(); | ||
final authUseCase = container.read(authUseCaseProvider); | ||
final chainConfigurationUseCase = | ||
container.read(chainConfigurationUseCaseProvider); | ||
final accountUseCase = container.read(accountUseCaseProvider); | ||
// final backgroundFetchConfigUseCase = | ||
// container.read(backgroundFetchConfigUseCaseProvider); | ||
final dAppHooksUseCase = container.read(dAppHooksUseCaseProvider); | ||
|
||
final selectedNetwork = | ||
chainConfigurationUseCase.getCurrentNetworkWithoutRefresh(); | ||
DAppHooksModel dappHooksData = dAppHooksUseCase.dappHooksData.value; | ||
final chainId = selectedNetwork.chainId; | ||
|
||
final isLoggedIn = authUseCase.loggedIn; | ||
final account = accountUseCase.account.value; | ||
final serviceEnabled = dappHooksData.enabled; | ||
final wifiHooksEnabled = dappHooksData.wifiHooks.enabled; | ||
|
||
// Make sure user is logged in | ||
if (isLoggedIn && Config.isMxcChains(chainId) && serviceEnabled) { | ||
AXSNotification().setupFlutterNotifications(shouldInitFirebase: false); | ||
|
||
if (wifiHooksEnabled) { | ||
await dAppHooksUseCase.sendWifiInfo( | ||
account!, | ||
); | ||
} | ||
|
||
dAppHooksUseCase.updateItem(dappHooksData); | ||
BackgroundFetch.finish(taskId); | ||
} else { | ||
// terminate background fetch | ||
BackgroundFetch.stop(taskId); | ||
} | ||
} catch (e) { | ||
BackgroundFetch.finish(taskId); | ||
} | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
lib/core/src/background_process/notifications_service.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import 'package:background_fetch/background_fetch.dart'; | ||
import 'package:datadashwallet/core/core.dart'; | ||
import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||
import 'package:mxc_logic/mxc_logic.dart'; | ||
|
||
class NotificationsService { | ||
@pragma( | ||
'vm:entry-point') // Mandatory if the App is obfuscated or using Flutter 3.1+ | ||
static void callbackDispatcher(HeadlessTask task) async { | ||
String taskId = task.taskId; | ||
bool isTimeout = task.timeout; | ||
if (isTimeout) { | ||
// This task has exceeded its allowed running-time. | ||
// You must stop what you're doing and immediately .finish(taskId) | ||
print("[BackgroundFetch] Headless task timed-out: $taskId"); | ||
BackgroundFetch.finish(taskId); | ||
return; | ||
} | ||
callbackDispatcherForeGround(taskId); | ||
} | ||
|
||
// Foreground | ||
static void callbackDispatcherForeGround(String taskId) async { | ||
try { | ||
await loadProviders(); | ||
|
||
final container = ProviderContainer(); | ||
final authUseCase = container.read(authUseCaseProvider); | ||
final chainConfigurationUseCase = | ||
container.read(chainConfigurationUseCaseProvider); | ||
final accountUseCase = container.read(accountUseCaseProvider); | ||
final backgroundFetchConfigUseCase = | ||
container.read(backgroundFetchConfigUseCaseProvider); | ||
|
||
final selectedNetwork = | ||
chainConfigurationUseCase.getCurrentNetworkWithoutRefresh(); | ||
PeriodicalCallData periodicalCallData = | ||
backgroundFetchConfigUseCase.periodicalCallData.value; | ||
final chainId = selectedNetwork.chainId; | ||
|
||
final isLoggedIn = authUseCase.loggedIn; | ||
final account = accountUseCase.account.value; | ||
final lowBalanceLimit = periodicalCallData.lowBalanceLimit; | ||
final expectedTransactionFee = periodicalCallData.expectedTransactionFee; | ||
final lowBalanceLimitEnabled = periodicalCallData.lowBalanceLimitEnabled; | ||
final expectedTransactionFeeEnabled = | ||
periodicalCallData.expectedTransactionFeeEnabled; | ||
final lastEpoch = periodicalCallData.lasEpoch; | ||
final expectedEpochOccurrence = | ||
periodicalCallData.expectedEpochOccurrence; | ||
final expectedEpochOccurrenceEnabled = | ||
periodicalCallData.expectedEpochOccurrenceEnabled; | ||
final serviceEnabled = periodicalCallData.serviceEnabled; | ||
|
||
// Make sure user is logged in | ||
if (isLoggedIn && Config.isMxcChains(chainId) && serviceEnabled) { | ||
AXSNotification().setupFlutterNotifications(shouldInitFirebase: false); | ||
|
||
if (lowBalanceLimitEnabled) { | ||
await backgroundFetchConfigUseCase.checkLowBalance( | ||
account!, lowBalanceLimit); | ||
} | ||
|
||
if (expectedTransactionFeeEnabled) { | ||
await backgroundFetchConfigUseCase | ||
.checkTransactionFee(expectedTransactionFee); | ||
} | ||
|
||
if (expectedEpochOccurrenceEnabled) { | ||
periodicalCallData = | ||
await backgroundFetchConfigUseCase.checkEpochOccur( | ||
periodicalCallData, | ||
lastEpoch, | ||
expectedEpochOccurrence, | ||
chainId); | ||
} | ||
|
||
backgroundFetchConfigUseCase.updateItem(periodicalCallData); | ||
BackgroundFetch.finish(taskId); | ||
} else { | ||
// terminate background fetch | ||
BackgroundFetch.stop(taskId); | ||
} | ||
} catch (e) { | ||
BackgroundFetch.finish(taskId); | ||
} | ||
} | ||
} |
Oops, something went wrong.