demo of status change not working

This commit is contained in:
talksik
2023-11-12 13:38:54 -08:00
parent 0ddab63129
commit ed3b523cd4
13 changed files with 581 additions and 16 deletions
+96 -14
View File
@@ -1,11 +1,16 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:stream_chat/stream_chat.dart';
// DEBUG NOTE: made this global for easier access just for demo purposes
late StreamChatClient client;
Future<void> main() async {
/// Create a new instance of [StreamChatClient]
/// by passing the apikey obtained from your project dashboard.
final client = StreamChatClient(
'b67pax5b2wdq',
client = StreamChatClient(
'kjt8387d892u',
logLevel: Level.INFO,
);
@@ -14,14 +19,15 @@ Future<void> main() async {
/// Please see the following for more information:
/// https://getstream.io/chat/docs/ios_user_setup_and_tokens/
await client.connectUser(
User(
id: 'cool-shadow-7',
name: 'Cool Shadow',
image:
'https://getstream.io/random_png/?id=cool-shadow-7&amp;name=Cool+shadow',
),
'''eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiY29vbC1zaGFkb3ctNyJ9.gkOlCRb1qgy4joHPaxFwPOdXcGvSPvp6QY0S4mpRkVo''',
);
User(
id: 'cool-shadow-7',
name: 'Cool Shadow',
// DEBUG NOTE: setting initial state to 0 (focus mode)
extraData: {'state': 0},
image:
'https://getstream.io/random_png/?id=cool-shadow-7&amp;name=Cool+shadow',
),
"eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiY2M5ZGUwMTctY2E5Mi00ODliLWFlY2MtZDEzYTQwMDUyYzk3In0.qPjnrWzyQBNF_tpFkVNayFmzArIXj42EVQKoo-sFmB4");
/// Creates a channel using the type `messaging` and `godevs`.
/// Channels are containers for holding messages between different members. To
@@ -62,12 +68,13 @@ class StreamExample extends StatelessWidget {
Widget build(BuildContext context) => MaterialApp(
title: 'Stream Chat Dart Example',
home: HomeScreen(channel: channel),
debugShowCheckedModeBanner: false,
);
}
/// Main screen of our application. The layout is comprised of an [AppBar]
/// containing the channel name and a [MessageView] displaying recent messages.
class HomeScreen extends StatelessWidget {
class HomeScreen extends StatefulWidget {
/// [HomeScreen] is constructed using the [Channel] we defined earlier.
const HomeScreen({
super.key,
@@ -77,12 +84,87 @@ class HomeScreen extends StatelessWidget {
/// Channel object containing the [Channel.id] we'd like to observe.
final Channel channel;
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
int? _state = null; // DEBUG NOTE: 0 => focus mode, 1 => ready
bool _isOnline = false;
StreamSubscription<OwnUser?>? _subscription;
@override
void dispose() {
_subscription?.cancel();
super.dispose();
}
@override
void initState() {
// DEBUG NOTE: I'm getting the initial local user info
final initialState = client.state.currentUser?.extraData['state'] as int?;
debugPrint('initialState: ${initialState.toString()}');
setState(() {
_state = initialState;
_isOnline = client.state.currentUser?.online ?? false;
});
// DEBUG NOTE: Im listening to changes to the local user
_subscription = client.state.currentUserStream.listen((user) {
debugPrint(
'is user online ? ${user?.online}'); // <----- DEBUG NOTE: this is what returns false after goReady()
final newState = user?.extraData['state'] as int?;
debugPrint('new user state : ${newState.toString()}');
setState(() {
_state = newState;
_isOnline = user?.online ?? false;
});
});
}
// DEBUG NOTE: test of switching user's state from focus mode to online
void goReady() {
client.updateUser(
client.state.currentUser!.copyWith(
// trying to force online state to stay true
// tried with this commented and without
online: true,
extraData: {
'state': 1,
},
),
);
}
Widget get userGlobalState {
if (_state == null) {
return Text("null");
}
if (_state == 1) {
return Text("status: ready | online: $_isOnline");
} else {
// DEBUG NOTE: this is what shows initially
return Text("status: focus mode | online: $_isOnline");
}
}
@override
Widget build(BuildContext context) {
final messages = channel.state!.messagesStream;
final messages = widget.channel.state!.messagesStream;
return Scaffold(
appBar: AppBar(
title: Text('Channel: ${channel.id}'),
title: Text('Channel: ${widget.channel.id}'),
leading: userGlobalState,
actions: [
if (_state == 0)
IconButton(
onPressed: goReady,
icon: Icon(Icons.circle, color: Colors.purple))
],
),
body: SafeArea(
child: StreamBuilder<List<Message>?>(
@@ -94,7 +176,7 @@ class HomeScreen extends StatelessWidget {
if (snapshot.hasData && snapshot.data != null) {
return MessageView(
messages: snapshot.data!.reversed.toList(),
channel: channel,
channel: widget.channel,
);
} else if (snapshot.hasError) {
return const Center(