2024-04-16 14:29:58 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
import 'package:solian/models/channel.dart';
|
|
|
|
import 'package:solian/providers/auth.dart';
|
2024-04-17 15:00:53 +00:00
|
|
|
import 'package:solian/router.dart';
|
2024-04-16 14:29:58 +00:00
|
|
|
import 'package:solian/utils/service_url.dart';
|
2024-05-03 05:39:52 +00:00
|
|
|
import 'package:solian/utils/theme.dart';
|
2024-04-19 11:36:03 +00:00
|
|
|
import 'package:solian/widgets/chat/chat_new.dart';
|
2024-04-29 12:22:06 +00:00
|
|
|
import 'package:solian/widgets/exts.dart';
|
2024-05-03 05:39:52 +00:00
|
|
|
import 'package:solian/widgets/scaffold.dart';
|
2024-04-16 14:29:58 +00:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2024-04-25 13:33:53 +00:00
|
|
|
import 'package:solian/widgets/notification_notifier.dart';
|
2024-04-19 10:02:53 +00:00
|
|
|
import 'package:solian/widgets/signin_required.dart';
|
2024-04-16 14:29:58 +00:00
|
|
|
|
2024-05-03 05:39:52 +00:00
|
|
|
class ChatListScreen extends StatelessWidget {
|
|
|
|
const ChatListScreen({super.key});
|
2024-05-01 16:49:38 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-05-03 05:39:52 +00:00
|
|
|
return IndentScaffold(
|
2024-05-01 16:49:38 +00:00
|
|
|
title: AppLocalizations.of(context)!.chat,
|
2024-05-03 05:39:52 +00:00
|
|
|
appBarActions: const [NotificationButton()],
|
|
|
|
fixedAppBarColor: SolianTheme.isLargeScreen(context),
|
2024-05-05 15:01:08 +00:00
|
|
|
child: const ChatListWidget(),
|
2024-05-01 16:49:38 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-03 05:39:52 +00:00
|
|
|
class ChatListWidget extends StatefulWidget {
|
2024-05-05 15:01:08 +00:00
|
|
|
final String? realm;
|
2024-05-01 16:49:38 +00:00
|
|
|
|
2024-05-05 15:01:08 +00:00
|
|
|
const ChatListWidget({super.key, this.realm});
|
2024-05-01 16:49:38 +00:00
|
|
|
|
|
|
|
@override
|
2024-05-03 05:39:52 +00:00
|
|
|
State<ChatListWidget> createState() => _ChatListWidgetState();
|
2024-05-01 16:49:38 +00:00
|
|
|
}
|
|
|
|
|
2024-05-03 05:39:52 +00:00
|
|
|
class _ChatListWidgetState extends State<ChatListWidget> {
|
2024-04-16 14:29:58 +00:00
|
|
|
List<Channel> _channels = List.empty();
|
|
|
|
|
2024-04-19 16:01:48 +00:00
|
|
|
Future<void> fetchChannels() async {
|
2024-04-16 14:29:58 +00:00
|
|
|
final auth = context.read<AuthProvider>();
|
|
|
|
if (!await auth.isAuthorized()) return;
|
|
|
|
|
2024-05-05 15:01:08 +00:00
|
|
|
Uri uri;
|
|
|
|
if (widget.realm == null) {
|
|
|
|
uri = getRequestUri('messaging', '/api/channels/global/me/available');
|
|
|
|
} else {
|
|
|
|
uri = getRequestUri('messaging', '/api/channels/${widget.realm}');
|
|
|
|
}
|
2024-04-16 14:29:58 +00:00
|
|
|
|
|
|
|
var res = await auth.client!.get(uri);
|
|
|
|
if (res.statusCode == 200) {
|
|
|
|
final result = jsonDecode(utf8.decode(res.bodyBytes)) as List<dynamic>;
|
|
|
|
setState(() {
|
|
|
|
_channels = result.map((x) => Channel.fromJson(x)).toList();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
var message = utf8.decode(res.bodyBytes);
|
2024-04-29 12:22:06 +00:00
|
|
|
context.showErrorDialog(message);
|
2024-04-16 14:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-19 11:36:03 +00:00
|
|
|
void viewNewChatAction() {
|
|
|
|
showModalBottomSheet(
|
|
|
|
context: context,
|
2024-05-05 15:01:08 +00:00
|
|
|
builder: (context) => ChatNewAction(
|
|
|
|
onUpdate: () => fetchChannels(),
|
|
|
|
realm: widget.realm,
|
|
|
|
),
|
2024-04-19 11:36:03 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-04-16 14:29:58 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
Future.delayed(Duration.zero, () {
|
2024-04-19 16:01:48 +00:00
|
|
|
fetchChannels();
|
2024-04-16 14:29:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-04-19 10:02:53 +00:00
|
|
|
final auth = context.read<AuthProvider>();
|
|
|
|
|
2024-05-01 16:49:38 +00:00
|
|
|
return Scaffold(
|
2024-04-19 11:36:03 +00:00
|
|
|
floatingActionButton: FutureBuilder(
|
|
|
|
future: auth.isAuthorized(),
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.hasData && snapshot.data!) {
|
|
|
|
return FloatingActionButton.extended(
|
|
|
|
icon: const Icon(Icons.add),
|
|
|
|
label: Text(AppLocalizations.of(context)!.chatNew),
|
|
|
|
onPressed: () => viewNewChatAction(),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return Container();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
2024-05-01 16:49:38 +00:00
|
|
|
body: FutureBuilder(
|
|
|
|
future: auth.isAuthorized(),
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (!snapshot.hasData || !snapshot.data!) {
|
|
|
|
return const SignInRequiredScreen();
|
|
|
|
}
|
|
|
|
|
|
|
|
return RefreshIndicator(
|
|
|
|
onRefresh: () => fetchChannels(),
|
|
|
|
child: ListView.builder(
|
|
|
|
itemCount: _channels.length,
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
final element = _channels[index];
|
|
|
|
return ListTile(
|
|
|
|
leading: const CircleAvatar(
|
|
|
|
backgroundColor: Colors.indigo,
|
|
|
|
child: Icon(Icons.tag, color: Colors.white),
|
|
|
|
),
|
|
|
|
title: Text(element.name),
|
|
|
|
subtitle: Text(element.description),
|
2024-05-02 04:16:01 +00:00
|
|
|
onTap: () async {
|
2024-05-05 15:01:08 +00:00
|
|
|
String? result;
|
|
|
|
if (SolianRouter.currentRoute.name == 'chat.channel') {
|
|
|
|
result = await SolianRouter.router.pushReplacementNamed(
|
|
|
|
widget.realm == null ? 'chat.channel' : 'realms.chat.channel',
|
|
|
|
pathParameters: {
|
|
|
|
'channel': element.alias,
|
|
|
|
...(widget.realm == null ? {} : {'realm': widget.realm!}),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
result = await SolianRouter.router.pushNamed(
|
|
|
|
widget.realm == null ? 'chat.channel' : 'realms.chat.channel',
|
|
|
|
pathParameters: {
|
|
|
|
'channel': element.alias,
|
|
|
|
...(widget.realm == null ? {} : {'realm': widget.realm!}),
|
|
|
|
},
|
|
|
|
);
|
2024-05-02 04:16:01 +00:00
|
|
|
}
|
|
|
|
switch (result) {
|
|
|
|
case 'refresh':
|
|
|
|
fetchChannels();
|
|
|
|
}
|
|
|
|
},
|
2024-05-01 16:49:38 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
2024-04-16 14:29:58 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|