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/models/immich_asset.model.dart';
|
|
|
|
import 'package:immich_mobile/shared/providers/asset.provider.dart';
|
|
|
|
import 'package:socket_io_client/socket_io_client.dart';
|
2022-02-14 18:40:41 +02:00
|
|
|
|
|
|
|
class WebscoketState {
|
|
|
|
final Socket? socket;
|
|
|
|
final bool isConnected;
|
|
|
|
|
|
|
|
WebscoketState({
|
|
|
|
this.socket,
|
|
|
|
required this.isConnected,
|
|
|
|
});
|
|
|
|
|
|
|
|
WebscoketState copyWith({
|
|
|
|
Socket? socket,
|
|
|
|
bool? isConnected,
|
|
|
|
}) {
|
|
|
|
return WebscoketState(
|
|
|
|
socket: socket ?? this.socket,
|
|
|
|
isConnected: isConnected ?? this.isConnected,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2022-06-25 22:12:47 +02:00
|
|
|
String toString() =>
|
|
|
|
'WebscoketState(socket: $socket, isConnected: $isConnected)';
|
2022-02-14 18:40:41 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
bool operator ==(Object other) {
|
|
|
|
if (identical(this, other)) return true;
|
|
|
|
|
2022-06-25 22:12:47 +02:00
|
|
|
return other is WebscoketState &&
|
|
|
|
other.socket == socket &&
|
|
|
|
other.isConnected == isConnected;
|
2022-02-14 18:40:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
int get hashCode => socket.hashCode ^ isConnected.hashCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
class WebsocketNotifier extends StateNotifier<WebscoketState> {
|
2022-06-25 22:12:47 +02:00
|
|
|
WebsocketNotifier(this.ref)
|
|
|
|
: super(WebscoketState(socket: null, isConnected: false)) {
|
2022-02-14 18:40:41 +02:00
|
|
|
debugPrint("Init websocket instance");
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
var endpoint = Hive.box(userInfoBox).get(serverEndpointKey);
|
|
|
|
try {
|
|
|
|
debugPrint("[WEBSOCKET] Attempting to connect to ws");
|
|
|
|
// Configure socket transports must be sepecified
|
|
|
|
Socket socket = io(
|
2022-06-24 05:18:50 +02:00
|
|
|
endpoint.toString().replaceAll('/api', ''),
|
2022-02-14 18:40:41 +02:00
|
|
|
OptionBuilder()
|
2022-06-24 05:18:50 +02:00
|
|
|
.setPath('/api/socket.io')
|
2022-02-14 18:40:41 +02:00
|
|
|
.setTransports(['websocket'])
|
|
|
|
.enableReconnection()
|
|
|
|
.enableForceNew()
|
|
|
|
.enableForceNewConnection()
|
|
|
|
.enableAutoConnect()
|
|
|
|
.setExtraHeaders({"Authorization": "Bearer $accessToken"})
|
|
|
|
.build(),
|
|
|
|
);
|
|
|
|
|
|
|
|
socket.onConnect((_) {
|
|
|
|
debugPrint("[WEBSOCKET] Established Websocket Connection");
|
|
|
|
state = WebscoketState(isConnected: true, socket: socket);
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.onDisconnect((_) {
|
|
|
|
debugPrint("[WEBSOCKET] Disconnect to Websocket Connection");
|
|
|
|
state = WebscoketState(isConnected: false, socket: null);
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('error', (errorMessage) {
|
|
|
|
debugPrint("Webcoket Error - $errorMessage");
|
|
|
|
state = WebscoketState(isConnected: false, socket: null);
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('on_upload_success', (data) {
|
|
|
|
var jsonString = jsonDecode(data.toString());
|
|
|
|
ImmichAsset newAsset = ImmichAsset.fromMap(jsonString);
|
|
|
|
ref.watch(assetProvider.notifier).onNewAssetUploaded(newAsset);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint("[WEBSOCKET] Catch Websocket Error - ${e.toString()}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnect() {
|
|
|
|
debugPrint("[WEBSOCKET] Attempting to disconnect");
|
|
|
|
var socket = state.socket?.disconnect();
|
2022-07-01 03:08:49 +02:00
|
|
|
|
|
|
|
if (socket?.disconnected == true) {
|
|
|
|
state = WebscoketState(isConnected: false, socket: null);
|
2022-02-14 18:40:41 +02:00
|
|
|
}
|
|
|
|
}
|
2022-04-03 19:31:45 +02:00
|
|
|
|
|
|
|
stopListenToEvent(String eventName) {
|
|
|
|
debugPrint("[Websocket] Stop listening to event $eventName");
|
|
|
|
state.socket?.off(eventName);
|
|
|
|
}
|
|
|
|
|
|
|
|
listenUploadEvent() {
|
|
|
|
debugPrint("[Websocket] Start listening to event on_upload_success");
|
|
|
|
state.socket?.on('on_upload_success', (data) {
|
|
|
|
var jsonString = jsonDecode(data.toString());
|
|
|
|
ImmichAsset newAsset = ImmichAsset.fromMap(jsonString);
|
|
|
|
ref.watch(assetProvider.notifier).onNewAssetUploaded(newAsset);
|
|
|
|
});
|
|
|
|
}
|
2022-02-14 18:40:41 +02:00
|
|
|
}
|
|
|
|
|
2022-06-25 22:12:47 +02:00
|
|
|
final websocketProvider =
|
|
|
|
StateNotifierProvider<WebsocketNotifier, WebscoketState>((ref) {
|
2022-02-14 18:40:41 +02:00
|
|
|
return WebsocketNotifier(ref);
|
|
|
|
});
|