server_info.provider.dart 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import 'package:hooks_riverpod/hooks_riverpod.dart';
  2. import 'package:immich_mobile/shared/models/server_info_state.model.dart';
  3. import 'package:immich_mobile/shared/services/server_info.service.dart';
  4. import 'package:openapi/api.dart';
  5. import 'package:package_info_plus/package_info_plus.dart';
  6. class ServerInfoNotifier extends StateNotifier<ServerInfoState> {
  7. ServerInfoNotifier(this._serverInfoService)
  8. : super(
  9. ServerInfoState(
  10. serverVersion: ServerVersionReponseDto(
  11. major: 0,
  12. patch_: 0,
  13. minor: 0,
  14. build: 0,
  15. ),
  16. isVersionMismatch: false,
  17. versionMismatchErrorMessage: "",
  18. ),
  19. );
  20. final ServerInfoService _serverInfoService;
  21. getServerVersion() async {
  22. ServerVersionReponseDto? serverVersion =
  23. await _serverInfoService.getServerVersion();
  24. if (serverVersion == null) {
  25. state = state.copyWith(
  26. isVersionMismatch: true,
  27. versionMismatchErrorMessage:
  28. "Server is out of date. Some functionalities might not working correctly. Download and rebuild server",
  29. );
  30. return;
  31. }
  32. state = state.copyWith(serverVersion: serverVersion);
  33. var packageInfo = await PackageInfo.fromPlatform();
  34. Map<String, int> appVersion = _getDetailVersion(packageInfo.version);
  35. if (appVersion["major"]! > serverVersion.major) {
  36. state = state.copyWith(
  37. isVersionMismatch: true,
  38. versionMismatchErrorMessage:
  39. "Server is out of date in major version. Some functionalities might not work correctly. Download and rebuild server",
  40. );
  41. return;
  42. }
  43. if (appVersion["minor"]! > serverVersion.minor) {
  44. state = state.copyWith(
  45. isVersionMismatch: true,
  46. versionMismatchErrorMessage:
  47. "Server is out of date in minor version. Some functionalities might not work correctly. Consider download and rebuild server",
  48. );
  49. return;
  50. }
  51. state = state.copyWith(
  52. isVersionMismatch: false,
  53. versionMismatchErrorMessage: "",
  54. );
  55. }
  56. Map<String, int> _getDetailVersion(String version) {
  57. List<String> detail = version.split(".");
  58. var major = detail[0];
  59. var minor = detail[1];
  60. var patch = detail[2];
  61. return {
  62. "major": int.parse(major),
  63. "minor": int.parse(minor),
  64. "patch": int.parse(patch),
  65. };
  66. }
  67. }
  68. final serverInfoProvider =
  69. StateNotifierProvider<ServerInfoNotifier, ServerInfoState>((ref) {
  70. return ServerInfoNotifier(ref.watch(serverInfoServiceProvider));
  71. });