mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-12-02 12:14:09 +01:00
fix: Sync bar ETA (#2515)
* fix: Sync bar ETA * fix: Smooth ETA - WIP * fix: Show disconnected status when sync disconnects * feat: Display blocks remaining for the first five seconds and then switch to percentage * Update * fix: Extend duration for showing blocks remaining during sync from 5 to 15 seconds; update connectivity check to handle multiple connectivity results, and handle disconnected state while syncing * feat: Initially display block if the a reconnection is triggered while syncing * fix: More improvements to syncing
This commit is contained in:
@@ -21,6 +21,7 @@ class StartingScanSyncStatus extends SyncStatus {
|
||||
class SyncingSyncStatus extends SyncStatus {
|
||||
SyncingSyncStatus(this.blocksLeft, this.ptc) {
|
||||
updateEtaHistory(blocksLeft);
|
||||
_globalSyncStartTime ??= DateTime.now();
|
||||
}
|
||||
|
||||
double ptc;
|
||||
@@ -32,6 +33,19 @@ class SyncingSyncStatus extends SyncStatus {
|
||||
@override
|
||||
String toString() => '$blocksLeft';
|
||||
|
||||
/// Returns true if we should show blocks remaining instead of percentage
|
||||
/// Shows blocks remaining for the first 15 seconds of syncing
|
||||
bool shouldShowBlocksRemaining() {
|
||||
if (_globalSyncStartTime == null) return true;
|
||||
final elapsed = DateTime.now().difference(_globalSyncStartTime!);
|
||||
return elapsed.inSeconds < 15;
|
||||
}
|
||||
|
||||
/// Reset the global sync start time (call when sync completes or fails)
|
||||
static void resetSyncStartTime() {
|
||||
_globalSyncStartTime = null;
|
||||
}
|
||||
|
||||
factory SyncingSyncStatus.fromHeightValues(int chainTip, int initialSyncHeight, int syncHeight) {
|
||||
final track = chainTip - initialSyncHeight;
|
||||
final diff = track - (chainTip - syncHeight);
|
||||
@@ -45,21 +59,14 @@ class SyncingSyncStatus extends SyncStatus {
|
||||
|
||||
static void updateEtaHistory(int blocksLeft) {
|
||||
blockHistory[DateTime.now()] = blocksLeft;
|
||||
|
||||
// keep only the last 30 entries (gives us better statistical accuracy)
|
||||
while (blockHistory.length > 30) {
|
||||
blockHistory.remove(blockHistory.keys.first);
|
||||
}
|
||||
}
|
||||
|
||||
static Map<DateTime, int> blockHistory = {};
|
||||
static Duration? lastEtaDuration;
|
||||
static const int _minDataPoints = 3;
|
||||
static const int _maxDataAgeMinutes = 2;
|
||||
static DateTime? _globalSyncStartTime;
|
||||
|
||||
String? getFormattedEtaWithPlaceholder() {
|
||||
_cleanOldEntries();
|
||||
|
||||
// If we have enough data, show actual ETA
|
||||
if (blockHistory.length >= _minDataPoints) {
|
||||
final eta = getFormattedEta();
|
||||
@@ -70,11 +77,6 @@ class SyncingSyncStatus extends SyncStatus {
|
||||
return '--:--';
|
||||
}
|
||||
|
||||
void _cleanOldEntries() {
|
||||
final cutoffTime = DateTime.now().subtract(Duration(minutes: _maxDataAgeMinutes));
|
||||
blockHistory.removeWhere((key, value) => key.isBefore(cutoffTime));
|
||||
}
|
||||
|
||||
String? getFormattedEta() {
|
||||
Duration? duration = getEtaDuration();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user