feat(mobile): Auto switching server URLs (#14437)
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/providers/album/album.provider.dart';
|
||||
import 'package:immich_mobile/services/background.service.dart';
|
||||
@@ -35,7 +36,7 @@ class AppLifeCycleNotifier extends StateNotifier<AppLifeCycleEnum> {
|
||||
return state;
|
||||
}
|
||||
|
||||
void handleAppResume() {
|
||||
void handleAppResume() async {
|
||||
state = AppLifeCycleEnum.resumed;
|
||||
|
||||
// no need to resume because app was never really paused
|
||||
@@ -46,32 +47,49 @@ class AppLifeCycleNotifier extends StateNotifier<AppLifeCycleEnum> {
|
||||
|
||||
// Needs to be logged in
|
||||
if (isAuthenticated) {
|
||||
// switch endpoint if needed
|
||||
final endpoint =
|
||||
await _ref.read(authProvider.notifier).setOpenApiServiceEndpoint();
|
||||
if (kDebugMode) {
|
||||
debugPrint("Using server URL: $endpoint");
|
||||
}
|
||||
|
||||
final permission = _ref.watch(galleryPermissionNotifier);
|
||||
if (permission.isGranted || permission.isLimited) {
|
||||
_ref.read(backupProvider.notifier).resumeBackup();
|
||||
_ref.read(backgroundServiceProvider).resumeServiceIfEnabled();
|
||||
await _ref.read(backupProvider.notifier).resumeBackup();
|
||||
await _ref.read(backgroundServiceProvider).resumeServiceIfEnabled();
|
||||
}
|
||||
_ref.read(serverInfoProvider.notifier).getServerVersion();
|
||||
|
||||
await _ref.read(serverInfoProvider.notifier).getServerVersion();
|
||||
|
||||
switch (_ref.read(tabProvider)) {
|
||||
case TabEnum.home:
|
||||
_ref.read(assetProvider.notifier).getAllAsset();
|
||||
await _ref.read(assetProvider.notifier).getAllAsset();
|
||||
break;
|
||||
case TabEnum.search:
|
||||
// nothing to do
|
||||
// nothing to do
|
||||
break;
|
||||
|
||||
case TabEnum.albums:
|
||||
_ref.read(albumProvider.notifier).refreshRemoteAlbums();
|
||||
await _ref.read(albumProvider.notifier).refreshRemoteAlbums();
|
||||
break;
|
||||
case TabEnum.library:
|
||||
// nothing to do
|
||||
// nothing to do
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_ref.read(websocketProvider.notifier).connect();
|
||||
|
||||
_ref
|
||||
await _ref
|
||||
.read(notificationPermissionProvider.notifier)
|
||||
.getNotificationPermission();
|
||||
_ref.read(galleryPermissionNotifier.notifier).getGalleryPermissionStatus();
|
||||
|
||||
_ref.read(iOSBackgroundSettingsProvider.notifier).refresh();
|
||||
await _ref
|
||||
.read(galleryPermissionNotifier.notifier)
|
||||
.getGalleryPermissionStatus();
|
||||
|
||||
await _ref.read(iOSBackgroundSettingsProvider.notifier).refresh();
|
||||
|
||||
_ref.invalidate(memoryFutureProvider);
|
||||
}
|
||||
|
||||
@@ -45,6 +45,17 @@ class AuthNotifier extends StateNotifier<AuthState> {
|
||||
return _authService.validateServerUrl(url);
|
||||
}
|
||||
|
||||
/// Validating the url is the alternative connecting server url without
|
||||
/// saving the infomation to the local database
|
||||
Future<bool> validateAuxilaryServerUrl(String url) async {
|
||||
try {
|
||||
final validEndpoint = await _apiService.resolveEndpoint(url);
|
||||
return await _authService.validateAuxilaryServerUrl(validEndpoint);
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<LoginResponse> login(String email, String password) async {
|
||||
final response = await _authService.login(email, password);
|
||||
await saveAuthInfo(accessToken: response.accessToken);
|
||||
@@ -161,4 +172,34 @@ class AuthNotifier extends StateNotifier<AuthState> {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Future<void> saveWifiName(String wifiName) {
|
||||
return Store.put(StoreKey.preferredWifiName, wifiName);
|
||||
}
|
||||
|
||||
Future<void> saveLocalEndpoint(String url) {
|
||||
return Store.put(StoreKey.localEndpoint, url);
|
||||
}
|
||||
|
||||
String? getSavedWifiName() {
|
||||
return Store.tryGet(StoreKey.preferredWifiName);
|
||||
}
|
||||
|
||||
String? getSavedLocalEndpoint() {
|
||||
return Store.tryGet(StoreKey.localEndpoint);
|
||||
}
|
||||
|
||||
/// Returns the current server endpoint (with /api) URL from the store
|
||||
String? getServerEndpoint() {
|
||||
return Store.tryGet(StoreKey.serverEndpoint);
|
||||
}
|
||||
|
||||
/// Returns the current server URL (input by the user) from the store
|
||||
String? getServerUrl() {
|
||||
return Store.tryGet(StoreKey.serverUrl);
|
||||
}
|
||||
|
||||
Future<String?> setOpenApiServiceEndpoint() {
|
||||
return _authService.setOpenApiServiceEndpoint();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:immich_mobile/services/network.service.dart';
|
||||
|
||||
final networkProvider = StateNotifierProvider<NetworkNotifier, String>((ref) {
|
||||
return NetworkNotifier(
|
||||
ref.watch(networkServiceProvider),
|
||||
);
|
||||
});
|
||||
|
||||
class NetworkNotifier extends StateNotifier<String> {
|
||||
final NetworkService _networkService;
|
||||
|
||||
NetworkNotifier(this._networkService) : super('');
|
||||
|
||||
Future<String?> getWifiName() {
|
||||
return _networkService.getWifiName();
|
||||
}
|
||||
|
||||
Future<bool> getWifiReadPermission() {
|
||||
return _networkService.getLocationWhenInUserPermission();
|
||||
}
|
||||
|
||||
Future<bool> getWifiReadBackgroundPermission() {
|
||||
return _networkService.getLocationAlwaysPermission();
|
||||
}
|
||||
|
||||
Future<bool> requestWifiReadPermission() {
|
||||
return _networkService.requestLocationWhenInUsePermission();
|
||||
}
|
||||
|
||||
Future<bool> requestWifiReadBackgroundPermission() {
|
||||
return _networkService.requestLocationAlwaysPermission();
|
||||
}
|
||||
|
||||
Future<bool> openSettings() {
|
||||
return _networkService.openSettings();
|
||||
}
|
||||
}
|
||||
@@ -59,7 +59,7 @@ class ServerInfoNotifier extends StateNotifier<ServerInfo> {
|
||||
await getServerConfig();
|
||||
}
|
||||
|
||||
getServerVersion() async {
|
||||
Future<void> getServerVersion() async {
|
||||
try {
|
||||
final serverVersion = await _serverInfoService.getServerVersion();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user