Skip to content

Commit

Permalink
fix: start duplicate server on mobile when the app is closed by swipi…
Browse files Browse the repository at this point in the history
  • Loading branch information
monkeyWie authored Jan 31, 2024
1 parent 1aa96e1 commit 84d054b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
10 changes: 10 additions & 0 deletions ui/flutter/lib/database/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:hive/hive.dart';
import 'entity.dart';

const String _startConfig = 'startConfig';
const String _lastRunningConfig = 'lastRunningConfig';
const String _windowState = 'windowState';
const String _bookmark = 'bookmark';
const String _createHistory = 'createHistory';
Expand Down Expand Up @@ -53,6 +54,15 @@ class Database {
_startConfig, (json) => StartConfigEntity.fromJson(json));
}

void saveLastRunningConfig(StartConfigEntity entity) {
save<StartConfigEntity>(_lastRunningConfig, entity);
}

StartConfigEntity? getLastRunningConfig() {
return get<StartConfigEntity>(
_lastRunningConfig, (json) => StartConfigEntity.fromJson(json));
}

/// Patch non-null fields with the original value
void saveWindowState(WindowStateEntity entity) {
final state = getWindowState();
Expand Down
21 changes: 19 additions & 2 deletions ui/flutter/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_foreground_task/flutter_foreground_task.dart';
import 'package:get/get.dart';
import 'package:window_manager/window_manager.dart';

Expand All @@ -8,6 +9,7 @@ import 'app/modules/app/controllers/app_controller.dart';
import 'app/modules/app/views/app_view.dart';
import 'core/libgopeed_boot.dart';
import 'database/database.dart';
import 'database/entity.dart';
import 'i18n/message.dart';
import 'util/locale_manager.dart';
import 'util/log_util.dart';
Expand Down Expand Up @@ -52,8 +54,23 @@ Future<void> init() async {
try {
await controller.loadStartConfig();
final startCfg = controller.startConfig.value;
controller.runningPort.value = await LibgopeedBoot.instance.start(startCfg);
api.init(startCfg.network, controller.runningAddress(), startCfg.apiToken);
// When the app is closed by swiping on the mobile, the backend server is actually still running, don't need to start again.
final isRunning =
Util.isMobile() && (await FlutterForegroundTask.isRunningService);
if (!isRunning) {
controller.runningPort.value =
await LibgopeedBoot.instance.start(startCfg);
api.init(
startCfg.network, controller.runningAddress(), startCfg.apiToken);
Database.instance.saveLastRunningConfig(StartConfigEntity(
network: startCfg.network,
address: controller.runningAddress(),
apiToken: startCfg.apiToken));
} else {
final lastRunningConfig = Database.instance.getLastRunningConfig()!;
api.init(lastRunningConfig.network, lastRunningConfig.address,
lastRunningConfig.apiToken);
}
} catch (e) {
logger.e("libgopeed init fail", e);
}
Expand Down

0 comments on commit 84d054b

Please sign in to comment.