diff --git a/lib/components/chat_component.dart b/lib/components/chat_component.dart index 304953f..5fb623c 100644 --- a/lib/components/chat_component.dart +++ b/lib/components/chat_component.dart @@ -207,7 +207,8 @@ class ChatComponentState extends ConsumerState { IconButton( icon: const Icon(Icons.close), onPressed: () async { - activeChatState.add(null); + ref.read(activeChatStateProvider.notifier).state = + null; }).paddingLTRB(16, 0, 16, 0) ]), ), diff --git a/lib/components/chat_single_contact_item_widget.dart b/lib/components/chat_single_contact_item_widget.dart index 6075194..f9cc102 100644 --- a/lib/components/chat_single_contact_item_widget.dart +++ b/lib/components/chat_single_contact_item_widget.dart @@ -20,7 +20,7 @@ class ChatSingleContactItemWidget extends ConsumerWidget { //final textTheme = theme.textTheme; final scale = theme.extension()!; - final activeChat = ref.watch(activeChatStateProvider).value; + final activeChat = ref.watch(activeChatStateProvider); final remoteConversationRecordKey = proto.TypedKeyProto.fromProto(contact.remoteConversationRecordKey); final selected = activeChat == remoteConversationRecordKey; @@ -69,7 +69,8 @@ class ChatSingleContactItemWidget extends ConsumerWidget { // component is not dragged. child: ListTile( onTap: () async { - activeChatState.add(remoteConversationRecordKey); + ref.read(activeChatStateProvider.notifier).state = + remoteConversationRecordKey; ref.invalidate(fetchChatListProvider); }, title: Text(contact.editedProfile.name), diff --git a/lib/components/signal_strength_meter.dart b/lib/components/signal_strength_meter.dart index 29f4699..3e290d2 100644 --- a/lib/components/signal_strength_meter.dart +++ b/lib/components/signal_strength_meter.dart @@ -15,10 +15,8 @@ class SignalStrengthMeterWidget extends ConsumerWidget { final scale = theme.extension()!; const iconSize = 16.0; - final connState = ref.watch(globalConnectionStateProvider).value; - if (connState == null) { - return const Icon(Icons.signal_cellular_off, size: iconSize); - } + final connState = ref.watch(globalConnectionStateProvider); + late final double value; late final Color color; late final Color inactiveColor; diff --git a/lib/pages/home.dart b/lib/pages/home.dart index 4dd7832..408aa86 100644 --- a/lib/pages/home.dart +++ b/lib/pages/home.dart @@ -31,7 +31,7 @@ class HomePage extends ConsumerStatefulWidget { final contactList = ref.watch(fetchContactListProvider).asData?.value ?? const IListConst([]); - final activeChat = ref.watch(activeChatStateProvider).asData?.value; + final activeChat = ref.watch(activeChatStateProvider); if (activeChat == null) { return const EmptyChatWidget(); } @@ -48,7 +48,7 @@ class HomePage extends ConsumerStatefulWidget { activeChat, ); if (activeChatContactIdx == -1) { - activeChatState.add(null); + ref.read(activeChatStateProvider.notifier).state = null; return const EmptyChatWidget(); } final activeChatContact = contactList[activeChatContactIdx]; diff --git a/lib/processor.dart b/lib/processor.dart index 101cf05..0ea7d60 100644 --- a/lib/processor.dart +++ b/lib/processor.dart @@ -100,7 +100,7 @@ class Processor { } } - globalConnectionState.add(cs); + globalConnectionState.state = cs; } Future processUpdateConfig(VeilidUpdateConfig updateConfig) async { diff --git a/lib/providers/chat.dart b/lib/providers/chat.dart index 17ac439..c4bfc76 100644 --- a/lib/providers/chat.dart +++ b/lib/providers/chat.dart @@ -1,10 +1,10 @@ import 'package:fast_immutable_collections/fast_immutable_collections.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:riverpod_annotation/riverpod_annotation.dart'; import '../proto/proto.dart' as proto; import '../proto/proto.dart' show Chat, ChatType; -import '../tools/tools.dart'; import '../veilid_support/veilid_support.dart'; import 'account.dart'; @@ -72,8 +72,8 @@ Future deleteChat( if (c.remoteConversationKey == remoteConversationKey) { await chatList.tryRemoveItem(i); - if (activeChatState.currentState == remoteConversationRecordKey) { - activeChatState.add(null); + if (activeChatState.state == remoteConversationRecordKey) { + activeChatState.state = null; } return; @@ -113,7 +113,7 @@ Future?> fetchChatList(FetchChatListRef ref) async { } // The selected chat -ExternalStreamState activeChatState = - ExternalStreamState(null); -AutoDisposeStreamProvider activeChatStateProvider = - activeChatState.provider(); +final activeChatState = StateController(null); +final activeChatStateProvider = + AutoDisposeStateNotifierProvider, TypedKey?>( + (ref) => activeChatState); diff --git a/lib/providers/connection_state.dart b/lib/providers/connection_state.dart index 80f08a5..7af589e 100644 --- a/lib/providers/connection_state.dart +++ b/lib/providers/connection_state.dart @@ -1,7 +1,5 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; -import '../tools/tools.dart'; - enum GlobalConnectionState { detached, detaching, @@ -13,7 +11,8 @@ enum GlobalConnectionState { overAttached, } -ExternalStreamState globalConnectionState = - ExternalStreamState(GlobalConnectionState.detached); -AutoDisposeStreamProvider globalConnectionStateProvider = - globalConnectionState.provider(); +final globalConnectionState = + StateController(GlobalConnectionState.detached); +final globalConnectionStateProvider = AutoDisposeStateNotifierProvider< + StateController, + GlobalConnectionState>((ref) => globalConnectionState); diff --git a/lib/providers/conversation.dart b/lib/providers/conversation.dart index f369a21..3d29cd1 100644 --- a/lib/providers/conversation.dart +++ b/lib/providers/conversation.dart @@ -335,7 +335,7 @@ class ActiveConversationMessages extends _$ActiveConversationMessages { FutureOr?> build() async { await eventualVeilid.future; - final activeChat = activeChatState.currentState; + final activeChat = ref.watch(activeChatStateProvider); if (activeChat == null) { return null; } diff --git a/lib/providers/local_accounts.g.dart b/lib/providers/local_accounts.g.dart index 3ad8658..026ddcc 100644 --- a/lib/providers/local_accounts.g.dart +++ b/lib/providers/local_accounts.g.dart @@ -159,7 +159,7 @@ class _FetchLocalAccountProviderElement (origin as FetchLocalAccountProvider).accountMasterRecordKey; } -String _$localAccountsHash() => r'148d98fcd8a61147bb475708d50b9699887c5bec'; +String _$localAccountsHash() => r'f19ec560b585d353219be82bc383b2c091660c53'; /// See also [LocalAccounts]. @ProviderFor(LocalAccounts) diff --git a/lib/providers/logins.g.dart b/lib/providers/logins.g.dart index a003f64..e4eee2e 100644 --- a/lib/providers/logins.g.dart +++ b/lib/providers/logins.g.dart @@ -157,7 +157,7 @@ class _FetchLoginProviderElement (origin as FetchLoginProvider).accountMasterRecordKey; } -String _$loginsHash() => r'41c4630869b474c409b2fb3461dd2a56d9350c7f'; +String _$loginsHash() => r'2660f71bb7903464187a93fba5c07e22041e8c40'; /// See also [Logins]. @ProviderFor(Logins) diff --git a/lib/router/router_notifier.dart b/lib/router/router_notifier.dart index d9ee694..81a591d 100644 --- a/lib/router/router_notifier.dart +++ b/lib/router/router_notifier.dart @@ -29,7 +29,7 @@ class RouterNotifier extends _$RouterNotifier implements Listenable { hasAnyAccount = await ref.watch( localAccountsProvider.selectAsync((data) => data.isNotEmpty), ); - hasActiveChat = ref.watch(activeChatStateProvider).value != null; + hasActiveChat = ref.watch(activeChatStateProvider) != null; // When this notifier's state changes, inform GoRouter ref.listenSelf((_, __) { diff --git a/lib/router/router_notifier.g.dart b/lib/router/router_notifier.g.dart index a08d13e..db5c445 100644 --- a/lib/router/router_notifier.g.dart +++ b/lib/router/router_notifier.g.dart @@ -6,7 +6,7 @@ part of 'router_notifier.dart'; // RiverpodGenerator // ************************************************************************** -String _$routerNotifierHash() => r'8e636edc119d07296a95a5de8a6edadb119154cf'; +String _$routerNotifierHash() => r'f462d13cf63fa23a7a8e6a4edfa63984f32adaf1'; /// See also [RouterNotifier]. @ProviderFor(RouterNotifier) diff --git a/lib/tick.dart b/lib/tick.dart index ebfde43..679a963 100644 --- a/lib/tick.dart +++ b/lib/tick.dart @@ -133,7 +133,7 @@ class BackgroundTickerState extends ConsumerState { } Future _doNewMessageCheck() async { - final activeChat = activeChatState.currentState; + final activeChat = ref.read(activeChatStateProvider); if (activeChat == null) { return; } diff --git a/lib/tools/external_stream_state.dart b/lib/tools/external_stream_state.dart deleted file mode 100644 index ba54b00..0000000 --- a/lib/tools/external_stream_state.dart +++ /dev/null @@ -1,26 +0,0 @@ -import 'dart:async'; -import 'package:flutter_riverpod/flutter_riverpod.dart'; - -// Caches a state value which can be changed from anywhere -// Creates a provider interface that notices when the value changes -class ExternalStreamState { - ExternalStreamState(T initialState) - : currentState = initialState, - streamController = StreamController.broadcast(); - T currentState; - StreamController streamController; - void add(T newState) { - currentState = newState; - streamController.add(newState); - } - - AutoDisposeStreamProvider provider() => - AutoDisposeStreamProvider((ref) async* { - if (await streamController.stream.isEmpty) { - yield currentState; - } - await for (final value in streamController.stream) { - yield value; - } - }); -} diff --git a/lib/tools/tools.dart b/lib/tools/tools.dart index 89cb964..11cb944 100644 --- a/lib/tools/tools.dart +++ b/lib/tools/tools.dart @@ -1,5 +1,4 @@ export 'animations.dart'; -export 'external_stream_state.dart'; export 'loggy.dart'; export 'phono_byte.dart'; export 'radix_generator.dart';