feat: added datepicker, better refresh, and lots of fixes

This commit is contained in:
2024-03-17 21:17:09 +01:00
parent 48476f6fe4
commit fa129f17fb
6 changed files with 319 additions and 326 deletions

View File

@ -22,20 +22,30 @@ class UserPathBloc extends Bloc<UserPathEvent, UserPathState> {
SettingsState settingsState;
Option<WebSocketClient> _ws = None;
OwntracksApi get _api => OwntracksApi(
baseUrl: settingsState.url,
username: settingsState.username,
pass: settingsState.password);
OwntracksApi get _api =>
OwntracksApi(baseUrl: settingsState.url, username: settingsState.username, pass: settingsState.password);
UserPathBloc(this.deviceId, this.settingsState)
: super(MainUserPathState(
initialPoints: const IListConst([]),
livePoints: const IListConst([]),
from: DateTime.now().subtract(const Duration(days: 1)),
to: DateTime.now().add(const Duration(days: 365 * 100)))) {
from: DateTime.now().subtract(settingsState.historyTime),
to: DateTime.now())) /*.add(const Duration(days: 365 * 100)))) */ {
on<UserPathLoginDataChanged>((event, emit) {
if (event.newSettings == settingsState) {
print('settings states the same, returning. states:\nold: $settingsState\nnew: ${event.newSettings}');
return;
}
settingsState = event.newSettings;
// TODO: restart live connections
emit(MainUserPathState.copy(
state as MainUserPathState,
from: DateTime.now().subtract(settingsState.historyTime),
));
print('settings states differ, doing full update');
add(UserPathFullUpdate());
});
on<UserPathLiveSubscriptionUpdate>((event, emit) {
@ -51,21 +61,28 @@ class UserPathBloc extends Bloc<UserPathEvent, UserPathState> {
print("fpu");
if (state is MainUserPathState) {
final istate = state as MainUserPathState;
final history = await _api.fetchPointsForDevice(
var history = await _api.fetchPointsForDevice(
user: deviceId.$1,
device: deviceId.$2,
from: istate.from,
to: istate.to,
);
final Result<List<Point>> livePoints =
history.map((ok) => ok.isNotEmpty ? [ok.last] : []);
history = await history.toFutureResult().map((h) async {
if (h.isEmpty) {
final last = await _api.fetchLastPoint(user: deviceId.$1, device: deviceId.$2);
if (last.isOk()) {
return [last.unwrap()];
}
}
return h;
});
final Result<List<Point>> livePoints = history.map((ok) => ok.isNotEmpty ? [ok.last] : []);
emit(MainUserPathState(
initialPoints:
history.expect("Couldn't retrieve path history for $deviceId").lock,
livePoints:
livePoints.expect("Couldn\'t retrieve last (current) point").lock,
initialPoints: history.expect("Couldn't retrieve path history for $deviceId").lock,
livePoints: livePoints.expect("Couldn\'t retrieve last (current) point").lock,
from: istate.from,
to: istate.to));
}