|
| 1 | +import 'dart:convert'; |
| 2 | + |
| 3 | +import 'package:flutter/foundation.dart'; |
| 4 | +import 'package:hive/hive.dart'; |
| 5 | +import 'package:hooks_riverpod/hooks_riverpod.dart'; |
| 6 | +import 'package:immich_mobile/modules/home/providers/asset.provider.dart'; |
| 7 | +import 'package:immich_mobile/shared/models/immich_asset.model.dart'; |
| 8 | +import 'package:socket_io_client/socket_io_client.dart'; |
| 9 | + |
| 10 | +import 'package:immich_mobile/constants/hive_box.dart'; |
| 11 | +import 'package:immich_mobile/modules/login/providers/authentication.provider.dart'; |
| 12 | + |
| 13 | +class WebscoketState { |
| 14 | + final Socket? socket; |
| 15 | + final bool isConnected; |
| 16 | + |
| 17 | + WebscoketState({ |
| 18 | + this.socket, |
| 19 | + required this.isConnected, |
| 20 | + }); |
| 21 | + |
| 22 | + WebscoketState copyWith({ |
| 23 | + Socket? socket, |
| 24 | + bool? isConnected, |
| 25 | + }) { |
| 26 | + return WebscoketState( |
| 27 | + socket: socket ?? this.socket, |
| 28 | + isConnected: isConnected ?? this.isConnected, |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + @override |
| 33 | + String toString() => 'WebscoketState(socket: $socket, isConnected: $isConnected)'; |
| 34 | + |
| 35 | + @override |
| 36 | + bool operator ==(Object other) { |
| 37 | + if (identical(this, other)) return true; |
| 38 | + |
| 39 | + return other is WebscoketState && other.socket == socket && other.isConnected == isConnected; |
| 40 | + } |
| 41 | + |
| 42 | + @override |
| 43 | + int get hashCode => socket.hashCode ^ isConnected.hashCode; |
| 44 | +} |
| 45 | + |
| 46 | +class WebsocketNotifier extends StateNotifier<WebscoketState> { |
| 47 | + WebsocketNotifier(this.ref) : super(WebscoketState(socket: null, isConnected: false)) { |
| 48 | + debugPrint("Init websocket instance"); |
| 49 | + } |
| 50 | + |
| 51 | + final Ref ref; |
| 52 | + |
| 53 | + connect() { |
| 54 | + var authenticationState = ref.watch(authenticationProvider); |
| 55 | + |
| 56 | + if (authenticationState.isAuthenticated) { |
| 57 | + var accessToken = Hive.box(userInfoBox).get(accessTokenKey); |
| 58 | + var endpoint = Hive.box(userInfoBox).get(serverEndpointKey); |
| 59 | + try { |
| 60 | + debugPrint("[WEBSOCKET] Attempting to connect to ws"); |
| 61 | + // Configure socket transports must be sepecified |
| 62 | + Socket socket = io( |
| 63 | + endpoint, |
| 64 | + OptionBuilder() |
| 65 | + .setTransports(['websocket']) |
| 66 | + .enableReconnection() |
| 67 | + .enableForceNew() |
| 68 | + .enableForceNewConnection() |
| 69 | + .enableAutoConnect() |
| 70 | + .setExtraHeaders({"Authorization": "Bearer $accessToken"}) |
| 71 | + .build(), |
| 72 | + ); |
| 73 | + |
| 74 | + socket.onConnect((_) { |
| 75 | + debugPrint("[WEBSOCKET] Established Websocket Connection"); |
| 76 | + state = WebscoketState(isConnected: true, socket: socket); |
| 77 | + }); |
| 78 | + |
| 79 | + socket.onDisconnect((_) { |
| 80 | + debugPrint("[WEBSOCKET] Disconnect to Websocket Connection"); |
| 81 | + state = WebscoketState(isConnected: false, socket: null); |
| 82 | + }); |
| 83 | + |
| 84 | + socket.on('error', (errorMessage) { |
| 85 | + debugPrint("Webcoket Error - $errorMessage"); |
| 86 | + state = WebscoketState(isConnected: false, socket: null); |
| 87 | + }); |
| 88 | + |
| 89 | + socket.on('on_upload_success', (data) { |
| 90 | + var jsonString = jsonDecode(data.toString()); |
| 91 | + ImmichAsset newAsset = ImmichAsset.fromMap(jsonString); |
| 92 | + ref.watch(assetProvider.notifier).onNewAssetUploaded(newAsset); |
| 93 | + }); |
| 94 | + } catch (e) { |
| 95 | + debugPrint("[WEBSOCKET] Catch Websocket Error - ${e.toString()}"); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + disconnect() { |
| 101 | + debugPrint("[WEBSOCKET] Attempting to disconnect"); |
| 102 | + var socket = state.socket?.disconnect(); |
| 103 | + if (socket != null) { |
| 104 | + if (socket.disconnected) { |
| 105 | + state = WebscoketState(isConnected: false, socket: null); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +final websocketProvider = StateNotifierProvider<WebsocketNotifier, WebscoketState>((ref) { |
| 112 | + return WebsocketNotifier(ref); |
| 113 | +}); |
0 commit comments