🐛 Fix realm view won't show channels

This commit is contained in:
LittleSheep 2024-09-18 13:03:40 +08:00
parent 01aaa5455e
commit 6a0f42cdc9
2 changed files with 18 additions and 6 deletions

View File

@ -89,13 +89,13 @@ class ChannelProvider extends GetxController {
return resp; return resp;
} }
Future<Response> listAvailableChannel({String realm = 'global'}) async { Future<Response> listAvailableChannel({String scope = 'global'}) async {
final AuthProvider auth = Get.find(); final AuthProvider auth = Get.find();
if (auth.isAuthorized.isFalse) throw const UnauthorizedException(); if (auth.isAuthorized.isFalse) throw const UnauthorizedException();
final client = await auth.configureClient('messaging'); final client = await auth.configureClient('messaging');
final resp = await client.get('/channels/$realm/me/available'); final resp = await client.get('/channels/$scope/me/available');
if (resp.statusCode != 200) { if (resp.statusCode != 200) {
throw RequestException(resp); throw RequestException(resp);
} }

View File

@ -34,7 +34,7 @@ class _RealmViewScreenState extends State<RealmViewScreen> {
final List<Channel> _channels = List.empty(growable: true); final List<Channel> _channels = List.empty(growable: true);
getRealm({String? overrideAlias}) async { getRealm({String? overrideAlias}) async {
final RealmProvider provider = Get.find(); final RealmProvider realm = Get.find();
setState(() => _isBusy = true); setState(() => _isBusy = true);
@ -43,7 +43,7 @@ class _RealmViewScreenState extends State<RealmViewScreen> {
} }
try { try {
final resp = await provider.getRealm(_overrideAlias ?? widget.alias); final resp = await realm.getRealm(_overrideAlias ?? widget.alias);
setState(() => _realm = Realm.fromJson(resp.body)); setState(() => _realm = Realm.fromJson(resp.body));
} catch (e) { } catch (e) {
context.showErrorDialog(e); context.showErrorDialog(e);
@ -55,14 +55,26 @@ class _RealmViewScreenState extends State<RealmViewScreen> {
getChannels() async { getChannels() async {
setState(() => _isBusy = true); setState(() => _isBusy = true);
final ChannelProvider provider = Get.find(); final ChannelProvider channel = Get.find();
final resp = await provider.listChannel(scope: _realm!.alias); final resp = await channel.listChannel(scope: _realm!.alias);
final availableResp = await channel.listAvailableChannel(
scope: _realm!.alias,
);
final Set<int> channelIdx = {};
setState(() { setState(() {
_channels.clear(); _channels.clear();
_channels.addAll( _channels.addAll(
resp.body.map((e) => Channel.fromJson(e)).toList().cast<Channel>(), resp.body.map((e) => Channel.fromJson(e)).toList().cast<Channel>(),
); );
_channels.addAll(
availableResp.body
.map((e) => Channel.fromJson(e))
.toList()
.cast<Channel>(),
);
_channels.retainWhere((x) => channelIdx.add(x.id));
}); });
setState(() => _isBusy = false); setState(() => _isBusy = false);