mirror of
https://github.com/immich-app/immich.git
synced 2025-11-24 17:30:43 +09:00
fix(mobile): better app state handling (#4989)
* fix(mobile): better app state handling * watch -> read --------- Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
This commit is contained in:
committed by
GitHub
parent
291159e7fc
commit
38983838fd
@@ -63,21 +63,19 @@ class WebsocketState {
|
||||
}
|
||||
|
||||
class WebsocketNotifier extends StateNotifier<WebsocketState> {
|
||||
WebsocketNotifier(this.ref)
|
||||
WebsocketNotifier(this._ref)
|
||||
: super(
|
||||
WebsocketState(socket: null, isConnected: false, pendingChanges: []),
|
||||
) {
|
||||
debounce = Debounce(
|
||||
const Duration(milliseconds: 500),
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
final log = Logger('WebsocketNotifier');
|
||||
final Ref ref;
|
||||
late final Debounce debounce;
|
||||
final _log = Logger('WebsocketNotifier');
|
||||
final Ref _ref;
|
||||
final Debounce _debounce = Debounce(const Duration(milliseconds: 500));
|
||||
|
||||
connect() {
|
||||
var authenticationState = ref.read(authenticationProvider);
|
||||
/// Connects websocket to server unless already connected
|
||||
void connect() {
|
||||
if (state.isConnected) return;
|
||||
final authenticationState = _ref.read(authenticationProvider);
|
||||
|
||||
if (authenticationState.isAuthenticated) {
|
||||
final accessToken = Store.get(StoreKey.accessToken);
|
||||
@@ -118,7 +116,7 @@ class WebsocketNotifier extends StateNotifier<WebsocketState> {
|
||||
});
|
||||
|
||||
socket.on('error', (errorMessage) {
|
||||
log.severe("Websocket Error - $errorMessage");
|
||||
_log.severe("Websocket Error - $errorMessage");
|
||||
state = WebsocketState(
|
||||
isConnected: false,
|
||||
socket: null,
|
||||
@@ -138,7 +136,7 @@ class WebsocketNotifier extends StateNotifier<WebsocketState> {
|
||||
}
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
void disconnect() {
|
||||
debugPrint("Attempting to disconnect from websocket");
|
||||
|
||||
var socket = state.socket?.disconnect();
|
||||
@@ -152,30 +150,30 @@ class WebsocketNotifier extends StateNotifier<WebsocketState> {
|
||||
}
|
||||
}
|
||||
|
||||
stopListenToEvent(String eventName) {
|
||||
void stopListenToEvent(String eventName) {
|
||||
debugPrint("Stop listening to event $eventName");
|
||||
state.socket?.off(eventName);
|
||||
}
|
||||
|
||||
listenUploadEvent() {
|
||||
void listenUploadEvent() {
|
||||
debugPrint("Start listening to event on_upload_success");
|
||||
state.socket?.on('on_upload_success', _handleOnUploadSuccess);
|
||||
}
|
||||
|
||||
addPendingChange(PendingAction action, dynamic value) {
|
||||
void addPendingChange(PendingAction action, dynamic value) {
|
||||
state = state.copyWith(
|
||||
pendingChanges: [...state.pendingChanges, PendingChange(action, value)],
|
||||
);
|
||||
}
|
||||
|
||||
handlePendingChanges() {
|
||||
void handlePendingChanges() {
|
||||
final deleteChanges = state.pendingChanges
|
||||
.where((c) => c.action == PendingAction.assetDelete)
|
||||
.toList();
|
||||
if (deleteChanges.isNotEmpty) {
|
||||
List<String> remoteIds =
|
||||
deleteChanges.map((a) => a.value.toString()).toList();
|
||||
ref.read(syncServiceProvider).handleRemoteAssetRemoval(remoteIds);
|
||||
_ref.read(syncServiceProvider).handleRemoteAssetRemoval(remoteIds);
|
||||
state = state.copyWith(
|
||||
pendingChanges: state.pendingChanges
|
||||
.where((c) => c.action != PendingAction.assetDelete)
|
||||
@@ -184,27 +182,27 @@ class WebsocketNotifier extends StateNotifier<WebsocketState> {
|
||||
}
|
||||
}
|
||||
|
||||
_handleOnUploadSuccess(dynamic data) {
|
||||
void _handleOnUploadSuccess(dynamic data) {
|
||||
final dto = AssetResponseDto.fromJson(data);
|
||||
if (dto != null) {
|
||||
final newAsset = Asset.remote(dto);
|
||||
ref.watch(assetProvider.notifier).onNewAssetUploaded(newAsset);
|
||||
_ref.watch(assetProvider.notifier).onNewAssetUploaded(newAsset);
|
||||
}
|
||||
}
|
||||
|
||||
_handleOnConfigUpdate(dynamic _) {
|
||||
ref.read(serverInfoProvider.notifier).getServerFeatures();
|
||||
ref.read(serverInfoProvider.notifier).getServerConfig();
|
||||
void _handleOnConfigUpdate(dynamic _) {
|
||||
_ref.read(serverInfoProvider.notifier).getServerFeatures();
|
||||
_ref.read(serverInfoProvider.notifier).getServerConfig();
|
||||
}
|
||||
|
||||
// Refresh updated assets
|
||||
_handleServerUpdates(dynamic _) {
|
||||
ref.read(assetProvider.notifier).getAllAsset();
|
||||
void _handleServerUpdates(dynamic _) {
|
||||
_ref.read(assetProvider.notifier).getAllAsset();
|
||||
}
|
||||
|
||||
_handleOnAssetDelete(dynamic data) {
|
||||
void _handleOnAssetDelete(dynamic data) {
|
||||
addPendingChange(PendingAction.assetDelete, data);
|
||||
debounce(handlePendingChanges);
|
||||
_debounce(handlePendingChanges);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user