|
...
|
...
|
@@ -38,7 +38,18 @@ Future<void> healthBackgroundMain() async { |
|
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
|
|
const channel = MethodChannel('doublefeel_flutter_health_background_channel');
|
|
|
|
|
|
|
|
await AppBootstrap.init();
|
|
|
|
await _healthBackgroundLog(channel, 'entry.start');
|
|
|
|
try {
|
|
|
|
await _healthBackgroundLog(channel, 'bootstrap.start');
|
|
|
|
await AppBootstrap.init();
|
|
|
|
await _healthBackgroundLog(channel, 'bootstrap.success');
|
|
|
|
} catch (error, stackTrace) {
|
|
|
|
await _healthBackgroundLog(
|
|
|
|
channel,
|
|
|
|
'bootstrap.failed error=$error stack=$stackTrace',
|
|
|
|
);
|
|
|
|
rethrow;
|
|
|
|
}
|
|
|
|
|
|
|
|
channel.setMethodCallHandler((call) async {
|
|
|
|
switch (call.method) {
|
|
...
|
...
|
@@ -53,6 +64,7 @@ Future<void> healthBackgroundMain() async { |
|
|
|
await channel.invokeMethod('healthBackgroundTaskReady', {
|
|
|
|
'timestamp': DateTime.now().millisecondsSinceEpoch ~/ 1000,
|
|
|
|
});
|
|
|
|
await _healthBackgroundLog(channel, 'entry.readySent');
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _runPendingHealthTasks(
|
|
...
|
...
|
@@ -60,6 +72,10 @@ Future<void> _runPendingHealthTasks( |
|
|
|
Object? arguments,
|
|
|
|
) async {
|
|
|
|
final tasks = _backgroundHealthTasksFromArguments(arguments);
|
|
|
|
await _healthBackgroundLog(
|
|
|
|
channel,
|
|
|
|
'tasks.received count=${tasks.length} ids=${tasks.map((e) => e.taskId).join(',')}',
|
|
|
|
);
|
|
|
|
if (tasks.isEmpty) return;
|
|
|
|
|
|
|
|
for (final task in tasks) {
|
|
...
|
...
|
@@ -71,6 +87,10 @@ Future<void> _runPendingHealthTasks( |
|
|
|
final userPrefs = Get.find<UserPreferencesStorage>();
|
|
|
|
final userId = userPrefs.preferences.value.meUserInfo?.id ?? 0;
|
|
|
|
final token = userPrefs.accessToken;
|
|
|
|
await _healthBackgroundLog(
|
|
|
|
channel,
|
|
|
|
'login.check userId=$userId hasToken=${token.isNotEmpty}',
|
|
|
|
);
|
|
|
|
if (userId <= 0 || token.isEmpty) {
|
|
|
|
for (final task in tasks) {
|
|
|
|
await channel.invokeMethod('healthBackgroundTaskFinished', {
|
|
...
|
...
|
@@ -84,7 +104,9 @@ Future<void> _runPendingHealthTasks( |
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await _healthBackgroundLog(channel, 'calculate.start');
|
|
|
|
await Get.find<HealthRawDataCoreService>().startCoreCaculate();
|
|
|
|
await _healthBackgroundLog(channel, 'calculate.success');
|
|
|
|
for (final task in tasks) {
|
|
|
|
await channel.invokeMethod('healthBackgroundTaskFinished', {
|
|
|
|
'taskId': task.taskId,
|
|
...
|
...
|
@@ -92,6 +114,7 @@ Future<void> _runPendingHealthTasks( |
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
await _healthBackgroundLog(channel, 'calculate.failed error=$error');
|
|
|
|
for (final task in tasks) {
|
|
|
|
await channel.invokeMethod('healthBackgroundTaskFinished', {
|
|
|
|
'taskId': task.taskId,
|
|
...
|
...
|
@@ -103,6 +126,15 @@ Future<void> _runPendingHealthTasks( |
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _healthBackgroundLog(MethodChannel channel, String message) async {
|
|
|
|
try {
|
|
|
|
await channel.invokeMethod('healthBackgroundLog', {
|
|
|
|
'message': message,
|
|
|
|
'timestamp': DateTime.now().millisecondsSinceEpoch ~/ 1000,
|
|
|
|
});
|
|
|
|
} catch (_) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
List<_BackgroundHealthTask> _backgroundHealthTasksFromArguments(
|
|
|
|
Object? arguments,
|
|
|
|
) {
|
...
|
...
|
|