Skip to content

Commit

Permalink
feat: Do not pause after disconnects
Browse files Browse the repository at this point in the history
  • Loading branch information
hhanh00 committed Nov 30, 2024
1 parent 91d8807 commit 06d6c1a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/pages/accounts/rescan.dart
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class _RewindState extends State<RewindPage> {
if (!confirmed) return;
WarpApi.rewindTo(aa.coin, height);
Future(() async {
syncStatus2.sync(true);
syncStatus2.sync();
});
GoRouter.of(context).pop();
}
Expand Down
4 changes: 2 additions & 2 deletions lib/pages/main/sync_status.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class SyncStatusState extends State<SyncStatusWidget> {
case 0:
return '$syncedHeight / $latestHeight';
case 1:
final m = syncStatus2.isRescan ? s.rescan : s.catchup;
final m = syncStatus2.rescanning ? s.rescan : s.catchup;
return '$m $percent %';
case 2:
return remaining != null ? '$remaining...' : '';
Expand Down Expand Up @@ -104,7 +104,7 @@ class SyncStatusState extends State<SyncStatusWidget> {
} else {
if (syncStatus2.paused)
syncStatus2.setPause(false);
Future(() => syncStatus2.sync(false));
Future(() => syncStatus2.sync());
}
}
}
20 changes: 14 additions & 6 deletions lib/pages/more/vote.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:gap/gap.dart';
import 'package:http/http.dart' as http;
import 'package:go_router/go_router.dart';
import 'package:settings_ui/settings_ui.dart';
Expand Down Expand Up @@ -222,6 +223,7 @@ class VoteCandidateState extends State<VoteCandidatePage>

@override
Widget build(BuildContext context) {
final t = Theme.of(context);
final e = widget.vote.election;
final candidates = e.candidates
.asMap()
Expand All @@ -240,12 +242,18 @@ class VoteCandidateState extends State<VoteCandidatePage>
child: Padding(
padding: EdgeInsets.fromLTRB(8, 0, 8, 0),
child: FormBuilder(
child: FormBuilderRadioGroup(
name: 'candidates',
initialValue: candidate,
orientation: OptionsOrientation.vertical,
options: candidates,
onChanged: (v) => setState(() => candidate = v!),
child: Column(
children: [
Text(e.question, style: t.textTheme.headlineSmall),
Gap(16),
FormBuilderRadioGroup(
name: 'candidates',
initialValue: candidate,
orientation: OptionsOrientation.vertical,
options: candidates,
onChanged: (v) => setState(() => candidate = v!),
),
],
),
),
),
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/splash.dart
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ void backgroundSyncDispatcher() {
if (!appStore.initialized) return;
Workmanager().executeTask((task, inputData) async {
logger.i("Native called background task: $task");
await syncStatus2.sync(false, auto: true);
await syncStatus2.sync(auto: true);
return true;
});
}
26 changes: 16 additions & 10 deletions lib/store2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ Timer? syncTimer;
Future<void> startAutoSync() async {
if (syncTimer == null) {
await syncStatus2.update();
await syncStatus2.sync(false, auto: true);
await syncStatus2.sync(auto: true);
syncTimer = Timer.periodic(Duration(seconds: 15), (timer) {
syncStatus2.sync(false, auto: true);
syncStatus2.sync(auto: true);
aa.updateDivisified();
});
}
Expand All @@ -65,7 +65,6 @@ class SyncStatus2 = _SyncStatus2 with _$SyncStatus2;

abstract class _SyncStatus2 with Store {
int startSyncedHeight = 0;
bool isRescan = false;
ETA eta = ETA();

@observable
Expand All @@ -86,6 +85,8 @@ abstract class _SyncStatus2 with Store {
@observable
bool paused = false;

bool rescanning = false;

@observable
int downloadedSize = 0;

Expand Down Expand Up @@ -117,7 +118,7 @@ abstract class _SyncStatus2 with Store {

@action
void reset() {
isRescan = false;
rescanning = false;
syncedHeight = WarpApi.getDbHeight(aa.coin).height;
syncing = false;
paused = false;
Expand All @@ -139,22 +140,27 @@ abstract class _SyncStatus2 with Store {
}

@action
Future<void> sync(bool rescan, {bool auto = false}) async {
logger.d('R/A/P/S $rescan $auto $paused $syncing');
Future<void> sync({bool auto = false}) async {
logger.d('A/P/S $auto $paused $syncing');
if (paused) return;
if (syncing) return;
try {
await update();
final lh = latestHeight;
if (lh == null) return;
// don't auto sync more than 1 month of data
if (!rescan && auto && lh - syncedHeight > 30 * 24 * 60 * 4 / 5) {
if (!rescanning && auto && lh - syncedHeight > 30 * 24 * 60 * 4 / 5) {
paused = true;
return;
}
if (isSynced) return;
if (isSynced) {
rescanning = false;
return;
}
// user started/resumed a manual scan
if (!auto)
rescanning = true;
syncing = true;
isRescan = rescan;
_updateSyncedHeight();
startSyncedHeight = syncedHeight;
eta.begin(latestHeight!);
Expand Down Expand Up @@ -212,7 +218,7 @@ abstract class _SyncStatus2 with Store {
WarpApi.rescanFrom(aa.coin, height);
_updateSyncedHeight();
paused = false;
await sync(true);
await sync();
}

@action
Expand Down

0 comments on commit 06d6c1a

Please sign in to comment.