|
1 | | -part of 'worker.dart'; |
| 1 | +import 'package:fl_lib/fl_lib.dart'; |
| 2 | +import 'package:server_box/core/utils/jump_chain.dart'; |
| 3 | +import 'package:server_box/core/utils/refresh_interval.dart'; |
| 4 | +import 'package:server_box/core/utils/server.dart'; |
| 5 | +import 'package:server_box/data/model/server/server_private_info.dart'; |
| 6 | +import 'package:server_box/data/res/default.dart'; |
| 7 | +import 'package:server_box/data/res/store.dart'; |
2 | 8 |
|
3 | 9 | class SftpReq { |
4 | 10 | final Spi spi; |
@@ -87,106 +93,4 @@ class SftpTransferProgress { |
87 | 93 | }); |
88 | 94 | } |
89 | 95 |
|
90 | | -class SftpReqStatus { |
91 | | - final int id; |
92 | | - final SftpReq req; |
93 | | - final void Function() notifyListeners; |
94 | | - late SftpWorker worker; |
95 | | - final Completer? completer; |
96 | | - |
97 | | - String get fileName => req.localPath.split(Pfs.seperator).last; |
98 | | - |
99 | | - // status of the download |
100 | | - double? progress; |
101 | | - double? speedBytesPerSecond; |
102 | | - int? transferredBytes; |
103 | | - DateTime? _speedSampleTime; |
104 | | - int _speedSampleBytes = 0; |
105 | | - SftpWorkerStatus? status; |
106 | | - int? size; |
107 | | - Exception? error; |
108 | | - Duration? spentTime; |
109 | | - |
110 | | - SftpReqStatus({ |
111 | | - required this.req, |
112 | | - required this.notifyListeners, |
113 | | - this.completer, |
114 | | - }) : id = DateTime.now().microsecondsSinceEpoch { |
115 | | - worker = SftpWorker(onNotify: onNotify, req: req)..init(); |
116 | | - } |
117 | | - |
118 | | - @override |
119 | | - bool operator ==(Object other) => other is SftpReqStatus && id == other.id; |
120 | | - |
121 | | - @override |
122 | | - int get hashCode => id ^ super.hashCode; |
123 | | - |
124 | | - void dispose() { |
125 | | - worker._dispose(); |
126 | | - completer?.complete(true); |
127 | | - } |
128 | | - |
129 | | - void onNotify(dynamic event) { |
130 | | - var shouldDispose = false; |
131 | | - switch (event) { |
132 | | - case final SftpWorkerStatus val: |
133 | | - status = val; |
134 | | - if (status == SftpWorkerStatus.finished) { |
135 | | - dispose(); |
136 | | - } |
137 | | - break; |
138 | | - case final double val: |
139 | | - progress = val; |
140 | | - break; |
141 | | - case final SftpTransferProgress val: |
142 | | - progress = val.percent; |
143 | | - transferredBytes = val.transferredBytes; |
144 | | - _initSpeedSampleIfNeeded(val.transferredBytes); |
145 | | - break; |
146 | | - case final int val: |
147 | | - size = val; |
148 | | - break; |
149 | | - case final Duration d: |
150 | | - spentTime = d; |
151 | | - break; |
152 | | - default: |
153 | | - error = Exception('sftp worker event: $event'); |
154 | | - Loggers.app.warning(error); |
155 | | - shouldDispose = true; |
156 | | - } |
157 | | - notifyListeners(); |
158 | | - if (shouldDispose) dispose(); |
159 | | - } |
160 | | - |
161 | | - void _initSpeedSampleIfNeeded(int transferredBytes) { |
162 | | - if (_speedSampleTime != null) return; |
163 | | - _speedSampleTime = DateTime.now(); |
164 | | - _speedSampleBytes = transferredBytes; |
165 | | - } |
166 | | - |
167 | | - bool refreshSpeed([DateTime? now]) { |
168 | | - if (status != SftpWorkerStatus.loading) return false; |
169 | | - |
170 | | - final sampleTime = now ?? DateTime.now(); |
171 | | - final sampleBytes = transferredBytes ?? 0; |
172 | | - final lastSampleTime = _speedSampleTime; |
173 | | - if (lastSampleTime == null) { |
174 | | - _speedSampleTime = sampleTime; |
175 | | - _speedSampleBytes = sampleBytes; |
176 | | - return false; |
177 | | - } |
178 | | - |
179 | | - final elapsedMs = sampleTime.difference(lastSampleTime).inMilliseconds; |
180 | | - if (elapsedMs <= 0) return false; |
181 | | - |
182 | | - final speed = (sampleBytes - _speedSampleBytes) * 1000 / elapsedMs; |
183 | | - _speedSampleTime = sampleTime; |
184 | | - _speedSampleBytes = sampleBytes; |
185 | | - |
186 | | - if (speedBytesPerSecond == speed) return false; |
187 | | - speedBytesPerSecond = speed; |
188 | | - return true; |
189 | | - } |
190 | | -} |
191 | | - |
192 | 96 | enum SftpWorkerStatus { preparing, sshConnectted, loading, finished } |
0 commit comments