2023-11-09 18:10:56 +02:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
2024-02-24 05:38:57 +02:00
|
|
|
import 'package:immich_mobile/extensions/response_extensions.dart';
|
2023-08-27 07:07:35 +02:00
|
|
|
import 'package:immich_mobile/modules/map/models/map_state.model.dart';
|
|
|
|
import 'package:immich_mobile/modules/settings/providers/app_settings.provider.dart';
|
|
|
|
import 'package:immich_mobile/modules/settings/services/app_settings.service.dart';
|
2023-11-09 18:10:56 +02:00
|
|
|
import 'package:immich_mobile/shared/providers/api.provider.dart';
|
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
import 'package:openapi/api.dart';
|
2024-01-15 17:26:13 +02:00
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
2023-08-27 07:07:35 +02:00
|
|
|
|
2024-01-15 17:26:13 +02:00
|
|
|
part 'map_state.provider.g.dart';
|
2023-11-09 18:10:56 +02:00
|
|
|
|
2024-01-15 17:26:13 +02:00
|
|
|
@Riverpod(keepAlive: true)
|
|
|
|
class MapStateNotifier extends _$MapStateNotifier {
|
|
|
|
final _log = Logger("MapStateNotifier");
|
2023-11-09 18:10:56 +02:00
|
|
|
|
2024-01-15 17:26:13 +02:00
|
|
|
@override
|
|
|
|
MapState build() {
|
|
|
|
final appSettingsProvider = ref.read(appSettingsServiceProvider);
|
2023-08-27 07:07:35 +02:00
|
|
|
|
2024-01-15 17:26:13 +02:00
|
|
|
// Fetch and save the Style JSONs
|
|
|
|
loadStyles();
|
|
|
|
return MapState(
|
|
|
|
themeMode: ThemeMode.values[
|
|
|
|
appSettingsProvider.getSetting<int>(AppSettingsEnum.mapThemeMode)],
|
|
|
|
showFavoriteOnly: appSettingsProvider
|
|
|
|
.getSetting<bool>(AppSettingsEnum.mapShowFavoriteOnly),
|
|
|
|
includeArchived: appSettingsProvider
|
|
|
|
.getSetting<bool>(AppSettingsEnum.mapIncludeArchived),
|
|
|
|
relativeTime:
|
|
|
|
appSettingsProvider.getSetting<int>(AppSettingsEnum.mapRelativeDate),
|
2023-08-27 07:07:35 +02:00
|
|
|
);
|
2023-11-09 18:10:56 +02:00
|
|
|
}
|
|
|
|
|
2024-01-15 17:26:13 +02:00
|
|
|
void loadStyles() async {
|
|
|
|
final documents = (await getApplicationDocumentsDirectory()).path;
|
2023-11-09 18:10:56 +02:00
|
|
|
|
2024-01-15 17:26:13 +02:00
|
|
|
// Set to loading
|
|
|
|
state = state.copyWith(lightStyleFetched: const AsyncLoading());
|
|
|
|
|
|
|
|
// Fetch and save light theme
|
|
|
|
final lightResponse = await ref
|
|
|
|
.read(apiServiceProvider)
|
|
|
|
.systemConfigApi
|
|
|
|
.getMapStyleWithHttpInfo(MapTheme.light);
|
|
|
|
|
|
|
|
if (lightResponse.statusCode >= HttpStatus.badRequest) {
|
|
|
|
state = state.copyWith(
|
|
|
|
lightStyleFetched: AsyncError(lightResponse.body, StackTrace.current),
|
|
|
|
);
|
|
|
|
_log.severe(
|
2024-02-24 05:38:57 +02:00
|
|
|
"Cannot fetch map light style",
|
|
|
|
lightResponse.toLoggerString(),
|
2024-01-15 17:26:13 +02:00
|
|
|
);
|
2023-11-09 18:10:56 +02:00
|
|
|
return;
|
|
|
|
}
|
2024-01-15 17:26:13 +02:00
|
|
|
|
|
|
|
final lightJSON = lightResponse.body;
|
|
|
|
final lightFile = await File("$documents/map-style-light.json")
|
|
|
|
.writeAsString(lightJSON, flush: true);
|
|
|
|
|
|
|
|
// Update state with path
|
|
|
|
state =
|
|
|
|
state.copyWith(lightStyleFetched: AsyncData(lightFile.absolute.path));
|
|
|
|
|
|
|
|
// Set to loading
|
|
|
|
state = state.copyWith(darkStyleFetched: const AsyncLoading());
|
|
|
|
|
|
|
|
// Fetch and save dark theme
|
|
|
|
final darkResponse = await ref
|
|
|
|
.read(apiServiceProvider)
|
|
|
|
.systemConfigApi
|
|
|
|
.getMapStyleWithHttpInfo(MapTheme.dark);
|
|
|
|
|
|
|
|
if (darkResponse.statusCode >= HttpStatus.badRequest) {
|
|
|
|
state = state.copyWith(
|
|
|
|
darkStyleFetched: AsyncError(darkResponse.body, StackTrace.current),
|
|
|
|
);
|
2024-02-24 05:38:57 +02:00
|
|
|
_log.severe("Cannot fetch map dark style", darkResponse.toLoggerString());
|
2023-11-09 18:10:56 +02:00
|
|
|
return;
|
|
|
|
}
|
2024-01-15 17:26:13 +02:00
|
|
|
|
|
|
|
final darkJSON = darkResponse.body;
|
|
|
|
final darkFile = await File("$documents/map-style-dark.json")
|
|
|
|
.writeAsString(darkJSON, flush: true);
|
|
|
|
|
|
|
|
// Update state with path
|
|
|
|
state = state.copyWith(darkStyleFetched: AsyncData(darkFile.absolute.path));
|
|
|
|
}
|
|
|
|
|
|
|
|
void switchTheme(ThemeMode mode) {
|
|
|
|
ref.read(appSettingsServiceProvider).setSetting(
|
|
|
|
AppSettingsEnum.mapThemeMode,
|
|
|
|
mode.index,
|
|
|
|
);
|
|
|
|
state = state.copyWith(themeMode: mode);
|
2023-08-27 07:07:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void switchFavoriteOnly(bool isFavoriteOnly) {
|
2024-01-15 17:26:13 +02:00
|
|
|
ref.read(appSettingsServiceProvider).setSetting(
|
|
|
|
AppSettingsEnum.mapShowFavoriteOnly,
|
|
|
|
isFavoriteOnly,
|
|
|
|
);
|
|
|
|
state = state.copyWith(
|
|
|
|
showFavoriteOnly: isFavoriteOnly,
|
|
|
|
shouldRefetchMarkers: true,
|
2023-08-27 07:07:35 +02:00
|
|
|
);
|
2024-01-15 17:26:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void setRefetchMarkers(bool shouldRefetch) {
|
|
|
|
state = state.copyWith(shouldRefetchMarkers: shouldRefetch);
|
2023-08-27 07:07:35 +02:00
|
|
|
}
|
|
|
|
|
2023-10-04 15:51:07 +02:00
|
|
|
void switchIncludeArchived(bool isIncludeArchived) {
|
2024-01-15 17:26:13 +02:00
|
|
|
ref.read(appSettingsServiceProvider).setSetting(
|
|
|
|
AppSettingsEnum.mapIncludeArchived,
|
|
|
|
isIncludeArchived,
|
|
|
|
);
|
|
|
|
state = state.copyWith(
|
|
|
|
includeArchived: isIncludeArchived,
|
|
|
|
shouldRefetchMarkers: true,
|
2023-10-04 15:51:07 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-27 07:07:35 +02:00
|
|
|
void setRelativeTime(int relativeTime) {
|
2024-01-15 17:26:13 +02:00
|
|
|
ref.read(appSettingsServiceProvider).setSetting(
|
|
|
|
AppSettingsEnum.mapRelativeDate,
|
|
|
|
relativeTime,
|
|
|
|
);
|
|
|
|
state = state.copyWith(
|
|
|
|
relativeTime: relativeTime,
|
|
|
|
shouldRefetchMarkers: true,
|
2023-08-27 07:07:35 +02:00
|
|
|
);
|
2023-11-09 18:10:56 +02:00
|
|
|
}
|
2023-08-27 07:07:35 +02:00
|
|
|
}
|