2022-02-14 18:40:41 +02:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:hive/hive.dart';
|
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
|
|
import 'package:immich_mobile/constants/hive_box.dart';
|
|
|
|
import 'package:immich_mobile/modules/login/providers/authentication.provider.dart';
|
2022-06-22 07:23:35 +02:00
|
|
|
import 'package:immich_mobile/shared/providers/asset.provider.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
|
|
|
|
2022-11-27 22:34:19 +02:00
|
|
|
class WebsocketState {
|
2022-02-14 18:40:41 +02:00
|
|
|
final Socket? socket;
|
|
|
|
final bool isConnected;
|
|
|
|
|
2022-11-27 22:34:19 +02:00
|
|
|
WebsocketState({
|
2022-02-14 18:40:41 +02:00
|
|
|
this.socket,
|
|
|
|
required this.isConnected,
|
|
|
|
});
|
|
|
|
|
2022-11-27 22:34:19 +02:00
|
|
|
WebsocketState copyWith({
|
2022-02-14 18:40:41 +02:00
|
|
|
Socket? socket,
|
|
|
|
bool? isConnected,
|
|
|
|
}) {
|
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,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@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)
|
2022-11-27 22:34:19 +02:00
|
|
|
: super(WebsocketState(socket: null, isConnected: false));
|
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;
|
|
|
|
|
|
|
|
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) {
|
|
|
|
var accessToken = Hive.box(userInfoBox).get(accessTokenKey);
|
|
|
|
try {
|
2023-01-19 17:45:37 +02:00
|
|
|
var endpoint = Uri.parse(Hive.box(userInfoBox).get(serverEndpointKey));
|
|
|
|
|
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");
|
2022-11-27 22:34:19 +02:00
|
|
|
state = WebsocketState(isConnected: true, socket: socket);
|
2022-02-14 18:40:41 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.onDisconnect((_) {
|
2022-11-28 22:14:22 +02:00
|
|
|
debugPrint("Disconnect to Websocket Connection");
|
2022-11-27 22:34:19 +02:00
|
|
|
state = WebsocketState(isConnected: false, socket: null);
|
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");
|
|
|
|
state = WebsocketState(isConnected: false, socket: null);
|
2022-02-14 18:40:41 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('on_upload_success', (data) {
|
|
|
|
var jsonString = jsonDecode(data.toString());
|
2022-07-13 14:23:48 +02:00
|
|
|
AssetResponseDto? newAsset = AssetResponseDto.fromJson(jsonString);
|
|
|
|
|
|
|
|
if (newAsset != null) {
|
|
|
|
ref.watch(assetProvider.notifier).onNewAssetUploaded(newAsset);
|
|
|
|
}
|
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) {
|
2022-11-27 22:34:19 +02:00
|
|
|
state = WebsocketState(isConnected: false, socket: null);
|
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");
|
2022-04-03 19:31:45 +02:00
|
|
|
state.socket?.on('on_upload_success', (data) {
|
|
|
|
var jsonString = jsonDecode(data.toString());
|
2022-07-13 14:23:48 +02:00
|
|
|
AssetResponseDto? newAsset = AssetResponseDto.fromJson(jsonString);
|
|
|
|
|
|
|
|
if (newAsset != null) {
|
|
|
|
ref.watch(assetProvider.notifier).onNewAssetUploaded(newAsset);
|
|
|
|
}
|
2022-04-03 19:31:45 +02:00
|
|
|
});
|
|
|
|
}
|
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);
|
|
|
|
});
|