2022-02-14 18:40:41 +02:00
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
|
|
import 'package:immich_mobile/modules/login/providers/authentication.provider.dart';
|
2023-02-04 22:42:42 +02:00
|
|
|
import 'package:immich_mobile/shared/models/asset.dart';
|
2023-03-23 03:36:44 +02:00
|
|
|
import 'package:immich_mobile/shared/models/store.dart';
|
2022-06-22 07:23:35 +02:00
|
|
|
import 'package:immich_mobile/shared/providers/asset.provider.dart';
|
2023-10-06 09:01:14 +02:00
|
|
|
import 'package:immich_mobile/shared/providers/server_info.provider.dart';
|
2023-10-16 20:01:38 +02:00
|
|
|
import 'package:immich_mobile/shared/services/sync.service.dart';
|
|
|
|
import 'package:immich_mobile/utils/debounce.dart';
|
2022-11-27 22:34:19 +02:00
|
|
|
import 'package:logging/logging.dart';
|
2022-07-13 14:23:48 +02:00
|
|
|
import 'package:openapi/api.dart';
|
2022-06-22 07:23:35 +02:00
|
|
|
import 'package:socket_io_client/socket_io_client.dart';
|
2022-02-14 18:40:41 +02:00
|
|
|
|
2023-10-16 20:01:38 +02:00
|
|
|
enum PendingAction {
|
|
|
|
assetDelete,
|
|
|
|
}
|
|
|
|
|
|
|
|
class PendingChange {
|
|
|
|
final PendingAction action;
|
|
|
|
final dynamic value;
|
|
|
|
|
|
|
|
const PendingChange(this.action, this.value);
|
|
|
|
}
|
|
|
|
|
2022-11-27 22:34:19 +02:00
|
|
|
class WebsocketState {
|
2022-02-14 18:40:41 +02:00
|
|
|
final Socket? socket;
|
|
|
|
final bool isConnected;
|
2023-10-16 20:01:38 +02:00
|
|
|
final List<PendingChange> pendingChanges;
|
2022-02-14 18:40:41 +02:00
|
|
|
|
2022-11-27 22:34:19 +02:00
|
|
|
WebsocketState({
|
2022-02-14 18:40:41 +02:00
|
|
|
this.socket,
|
|
|
|
required this.isConnected,
|
2023-10-16 20:01:38 +02:00
|
|
|
required this.pendingChanges,
|
2022-02-14 18:40:41 +02:00
|
|
|
});
|
|
|
|
|
2022-11-27 22:34:19 +02:00
|
|
|
WebsocketState copyWith({
|
2022-02-14 18:40:41 +02:00
|
|
|
Socket? socket,
|
|
|
|
bool? isConnected,
|
2023-10-16 20:01:38 +02:00
|
|
|
List<PendingChange>? pendingChanges,
|
2022-02-14 18:40:41 +02:00
|
|
|
}) {
|
2022-11-27 22:34:19 +02:00
|
|
|
return WebsocketState(
|
2022-02-14 18:40:41 +02:00
|
|
|
socket: socket ?? this.socket,
|
|
|
|
isConnected: isConnected ?? this.isConnected,
|
2023-10-16 20:01:38 +02:00
|
|
|
pendingChanges: pendingChanges ?? this.pendingChanges,
|
2022-02-14 18:40:41 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2022-06-25 22:12:47 +02:00
|
|
|
String toString() =>
|
2022-11-27 22:34:19 +02:00
|
|
|
'WebsocketState(socket: $socket, isConnected: $isConnected)';
|
2022-02-14 18:40:41 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
bool operator ==(Object other) {
|
|
|
|
if (identical(this, other)) return true;
|
|
|
|
|
2022-11-27 22:34:19 +02:00
|
|
|
return other is WebsocketState &&
|
2022-06-25 22:12:47 +02:00
|
|
|
other.socket == socket &&
|
|
|
|
other.isConnected == isConnected;
|
2022-02-14 18:40:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
int get hashCode => socket.hashCode ^ isConnected.hashCode;
|
|
|
|
}
|
|
|
|
|
2022-11-27 22:34:19 +02:00
|
|
|
class WebsocketNotifier extends StateNotifier<WebsocketState> {
|
2022-06-25 22:12:47 +02:00
|
|
|
WebsocketNotifier(this.ref)
|
2023-10-16 20:01:38 +02:00
|
|
|
: super(
|
|
|
|
WebsocketState(socket: null, isConnected: false, pendingChanges: []),
|
|
|
|
) {
|
|
|
|
debounce = Debounce(
|
|
|
|
const Duration(milliseconds: 500),
|
|
|
|
);
|
|
|
|
}
|
2022-02-14 18:40:41 +02:00
|
|
|
|
2022-11-27 22:34:19 +02:00
|
|
|
final log = Logger('WebsocketNotifier');
|
2022-02-14 18:40:41 +02:00
|
|
|
final Ref ref;
|
2023-10-16 20:01:38 +02:00
|
|
|
late final Debounce debounce;
|
2022-02-14 18:40:41 +02:00
|
|
|
|
|
|
|
connect() {
|
2022-02-14 22:42:06 +02:00
|
|
|
var authenticationState = ref.read(authenticationProvider);
|
2022-02-14 18:40:41 +02:00
|
|
|
|
|
|
|
if (authenticationState.isAuthenticated) {
|
2023-03-23 03:36:44 +02:00
|
|
|
final accessToken = Store.get(StoreKey.accessToken);
|
2022-02-14 18:40:41 +02:00
|
|
|
try {
|
2023-03-23 03:36:44 +02:00
|
|
|
final endpoint = Uri.parse(Store.get(StoreKey.serverEndpoint));
|
2023-01-19 17:45:37 +02:00
|
|
|
|
2022-11-28 22:14:22 +02:00
|
|
|
debugPrint("Attempting to connect to websocket");
|
2022-11-27 22:34:19 +02:00
|
|
|
// Configure socket transports must be specified
|
2022-02-14 18:40:41 +02:00
|
|
|
Socket socket = io(
|
2023-01-19 17:45:37 +02:00
|
|
|
endpoint.origin,
|
2022-02-14 18:40:41 +02:00
|
|
|
OptionBuilder()
|
2023-01-19 17:45:37 +02:00
|
|
|
.setPath("${endpoint.path}/socket.io")
|
2022-02-14 18:40:41 +02:00
|
|
|
.setTransports(['websocket'])
|
|
|
|
.enableReconnection()
|
|
|
|
.enableForceNew()
|
|
|
|
.enableForceNewConnection()
|
|
|
|
.enableAutoConnect()
|
|
|
|
.setExtraHeaders({"Authorization": "Bearer $accessToken"})
|
|
|
|
.build(),
|
|
|
|
);
|
|
|
|
|
|
|
|
socket.onConnect((_) {
|
2022-11-28 22:14:22 +02:00
|
|
|
debugPrint("Established Websocket Connection");
|
2023-10-16 20:01:38 +02:00
|
|
|
state = WebsocketState(
|
|
|
|
isConnected: true,
|
|
|
|
socket: socket,
|
|
|
|
pendingChanges: state.pendingChanges,
|
|
|
|
);
|
2022-02-14 18:40:41 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.onDisconnect((_) {
|
2022-11-28 22:14:22 +02:00
|
|
|
debugPrint("Disconnect to Websocket Connection");
|
2023-10-16 20:01:38 +02:00
|
|
|
state = WebsocketState(
|
|
|
|
isConnected: false,
|
|
|
|
socket: null,
|
|
|
|
pendingChanges: state.pendingChanges,
|
|
|
|
);
|
2022-02-14 18:40:41 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('error', (errorMessage) {
|
2022-11-27 22:34:19 +02:00
|
|
|
log.severe("Websocket Error - $errorMessage");
|
2023-10-16 20:01:38 +02:00
|
|
|
state = WebsocketState(
|
|
|
|
isConnected: false,
|
|
|
|
socket: null,
|
|
|
|
pendingChanges: state.pendingChanges,
|
|
|
|
);
|
2022-02-14 18:40:41 +02:00
|
|
|
});
|
|
|
|
|
2023-02-04 22:42:42 +02:00
|
|
|
socket.on('on_upload_success', _handleOnUploadSuccess);
|
2023-10-06 09:01:14 +02:00
|
|
|
socket.on('on_config_update', _handleOnConfigUpdate);
|
2023-10-16 20:01:38 +02:00
|
|
|
socket.on('on_asset_delete', _handleOnAssetDelete);
|
|
|
|
socket.on('on_asset_trash', _handleServerUpdates);
|
|
|
|
socket.on('on_asset_restore', _handleServerUpdates);
|
2023-10-22 04:38:07 +02:00
|
|
|
socket.on('on_asset_update', _handleServerUpdates);
|
2022-02-14 18:40:41 +02:00
|
|
|
} catch (e) {
|
|
|
|
debugPrint("[WEBSOCKET] Catch Websocket Error - ${e.toString()}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnect() {
|
2022-11-28 22:14:22 +02:00
|
|
|
debugPrint("Attempting to disconnect from websocket");
|
2022-11-27 22:34:19 +02:00
|
|
|
|
2022-02-14 18:40:41 +02:00
|
|
|
var socket = state.socket?.disconnect();
|
2022-07-01 03:08:49 +02:00
|
|
|
|
|
|
|
if (socket?.disconnected == true) {
|
2023-10-16 20:01:38 +02:00
|
|
|
state = WebsocketState(
|
|
|
|
isConnected: false,
|
|
|
|
socket: null,
|
|
|
|
pendingChanges: state.pendingChanges,
|
|
|
|
);
|
2022-02-14 18:40:41 +02:00
|
|
|
}
|
|
|
|
}
|
2022-04-03 19:31:45 +02:00
|
|
|
|
|
|
|
stopListenToEvent(String eventName) {
|
2022-11-28 22:14:22 +02:00
|
|
|
debugPrint("Stop listening to event $eventName");
|
2022-04-03 19:31:45 +02:00
|
|
|
state.socket?.off(eventName);
|
|
|
|
}
|
|
|
|
|
|
|
|
listenUploadEvent() {
|
2022-11-28 22:14:22 +02:00
|
|
|
debugPrint("Start listening to event on_upload_success");
|
2023-02-04 22:42:42 +02:00
|
|
|
state.socket?.on('on_upload_success', _handleOnUploadSuccess);
|
|
|
|
}
|
2022-07-13 14:23:48 +02:00
|
|
|
|
2023-10-16 20:01:38 +02:00
|
|
|
addPendingChange(PendingAction action, dynamic value) {
|
|
|
|
state = state.copyWith(
|
|
|
|
pendingChanges: [...state.pendingChanges, PendingChange(action, value)],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
handlePendingChanges() {
|
|
|
|
final deleteChanges = state.pendingChanges
|
|
|
|
.where((c) => c.action == PendingAction.assetDelete)
|
|
|
|
.toList();
|
|
|
|
if (deleteChanges.isNotEmpty) {
|
2023-10-24 17:05:42 +02:00
|
|
|
List<String> remoteIds =
|
|
|
|
deleteChanges.map((a) => a.value.toString()).toList();
|
2023-10-16 20:01:38 +02:00
|
|
|
ref.read(syncServiceProvider).handleRemoteAssetRemoval(remoteIds);
|
|
|
|
state = state.copyWith(
|
|
|
|
pendingChanges: state.pendingChanges
|
|
|
|
.where((c) => c.action != PendingAction.assetDelete)
|
|
|
|
.toList(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-04 22:42:42 +02:00
|
|
|
_handleOnUploadSuccess(dynamic data) {
|
2023-10-24 17:05:42 +02:00
|
|
|
final dto = AssetResponseDto.fromJson(data);
|
2023-02-04 22:42:42 +02:00
|
|
|
if (dto != null) {
|
|
|
|
final newAsset = Asset.remote(dto);
|
|
|
|
ref.watch(assetProvider.notifier).onNewAssetUploaded(newAsset);
|
|
|
|
}
|
2022-04-03 19:31:45 +02:00
|
|
|
}
|
2023-10-06 09:01:14 +02:00
|
|
|
|
2023-10-24 17:05:42 +02:00
|
|
|
_handleOnConfigUpdate(dynamic _) {
|
2023-10-06 09:01:14 +02:00
|
|
|
ref.read(serverInfoProvider.notifier).getServerFeatures();
|
|
|
|
ref.read(serverInfoProvider.notifier).getServerConfig();
|
|
|
|
}
|
2023-10-16 20:01:38 +02:00
|
|
|
|
|
|
|
// Refresh updated assets
|
2023-10-24 17:05:42 +02:00
|
|
|
_handleServerUpdates(dynamic _) {
|
2023-10-16 20:01:38 +02:00
|
|
|
ref.read(assetProvider.notifier).getAllAsset();
|
|
|
|
}
|
|
|
|
|
|
|
|
_handleOnAssetDelete(dynamic data) {
|
|
|
|
addPendingChange(PendingAction.assetDelete, data);
|
|
|
|
debounce(handlePendingChanges);
|
|
|
|
}
|
2022-02-14 18:40:41 +02:00
|
|
|
}
|
|
|
|
|
2022-06-25 22:12:47 +02:00
|
|
|
final websocketProvider =
|
2022-11-27 22:34:19 +02:00
|
|
|
StateNotifierProvider<WebsocketNotifier, WebsocketState>((ref) {
|
2022-02-14 18:40:41 +02:00
|
|
|
return WebsocketNotifier(ref);
|
|
|
|
});
|