2022-07-17 17:08:51 +02:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
import 'package:animations/animations.dart';
|
|
|
|
import 'package:async/async.dart';
|
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
|
|
|
import 'package:matrix/matrix.dart';
|
|
|
|
|
|
|
|
import 'package:fluffychat/pages/chat_list/search_title.dart';
|
|
|
|
import 'package:fluffychat/widgets/matrix.dart';
|
|
|
|
import 'recommended_room_list_item.dart';
|
|
|
|
|
|
|
|
class SpacesHierarchyProposals extends StatefulWidget {
|
|
|
|
static final Map<String, AsyncCache<GetSpaceHierarchyResponse?>> _cache = {};
|
|
|
|
|
|
|
|
final String? space;
|
|
|
|
final String? query;
|
|
|
|
|
|
|
|
const SpacesHierarchyProposals({
|
|
|
|
Key? key,
|
|
|
|
required this.space,
|
|
|
|
this.query,
|
|
|
|
}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<SpacesHierarchyProposals> createState() =>
|
|
|
|
_SpacesHierarchyProposalsState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _SpacesHierarchyProposalsState extends State<SpacesHierarchyProposals> {
|
|
|
|
@override
|
|
|
|
void didUpdateWidget(covariant SpacesHierarchyProposals oldWidget) {
|
|
|
|
if (oldWidget.space != widget.space || oldWidget.query != widget.query) {
|
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
super.didUpdateWidget(oldWidget);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
// check for recommended rooms in case the active space is a [SpaceSpacesEntry]
|
|
|
|
if (widget.space != null) {
|
|
|
|
final client = Matrix.of(context).client;
|
|
|
|
|
|
|
|
final cache = SpacesHierarchyProposals._cache[widget.space!] ??=
|
|
|
|
AsyncCache<GetSpaceHierarchyResponse?>(const Duration(minutes: 15));
|
|
|
|
|
|
|
|
/// additionally saving the future's state in the completer in order to
|
|
|
|
/// display the loading indicator when refreshing as a [FutureBuilder] is
|
|
|
|
/// a [StatefulWidget].
|
|
|
|
final completer = Completer();
|
|
|
|
final future = cache.fetch(() => client.getSpaceHierarchy(
|
|
|
|
widget.space!,
|
|
|
|
suggestedOnly: true,
|
|
|
|
maxDepth: 1,
|
|
|
|
));
|
|
|
|
future.then(completer.complete);
|
|
|
|
|
|
|
|
return FutureBuilder<GetSpaceHierarchyResponse?>(
|
|
|
|
future: future,
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
Widget child;
|
|
|
|
if (snapshot.hasData) {
|
2022-08-02 16:02:03 +02:00
|
|
|
final thereWereRooms = snapshot.data!.rooms.isNotEmpty;
|
2022-07-17 17:08:51 +02:00
|
|
|
final rooms = snapshot.data!.rooms.where(
|
|
|
|
(element) =>
|
|
|
|
element.roomId != widget.space &&
|
|
|
|
// filtering in case a query is given
|
|
|
|
(widget.query != null
|
|
|
|
? (element.name?.contains(widget.query!) ?? false) ||
|
|
|
|
(element.topic?.contains(widget.query!) ?? false)
|
|
|
|
// in case not, just leave it...
|
|
|
|
: true) &&
|
|
|
|
client.rooms
|
2022-08-02 16:02:03 +02:00
|
|
|
.every((knownRoom) => element.roomId != knownRoom.id),
|
2022-07-17 17:08:51 +02:00
|
|
|
);
|
2022-08-02 16:02:03 +02:00
|
|
|
if (rooms.isEmpty && !thereWereRooms) {
|
|
|
|
child = const ListTile(key: ValueKey(false));
|
|
|
|
}
|
2022-07-17 17:08:51 +02:00
|
|
|
child = Column(
|
|
|
|
key: ValueKey(widget.space),
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
SearchTitle(
|
|
|
|
title: L10n.of(context)!.suggestedRooms,
|
|
|
|
icon: const Icon(Icons.auto_awesome_outlined),
|
|
|
|
trailing: completer.isCompleted
|
|
|
|
? const Icon(
|
|
|
|
Icons.refresh_outlined,
|
|
|
|
size: 16,
|
|
|
|
)
|
|
|
|
: const SizedBox(
|
|
|
|
width: 16,
|
|
|
|
height: 16,
|
|
|
|
child: CircularProgressIndicator.adaptive(
|
|
|
|
strokeWidth: 1,
|
|
|
|
),
|
|
|
|
),
|
2022-07-24 19:02:14 +02:00
|
|
|
onTap: _refreshRooms,
|
2022-07-17 17:08:51 +02:00
|
|
|
),
|
2022-08-02 16:02:03 +02:00
|
|
|
if (rooms.isEmpty && thereWereRooms)
|
|
|
|
ListTile(
|
|
|
|
leading: const Icon(Icons.info),
|
|
|
|
title: Text(L10n.of(context)!.allSuggestedRoomsJoined),
|
|
|
|
),
|
2022-07-17 17:08:51 +02:00
|
|
|
...rooms.map(
|
|
|
|
(e) => RecommendedRoomListItem(
|
|
|
|
room: e,
|
2022-07-24 19:02:14 +02:00
|
|
|
onRoomJoined: _refreshRooms,
|
2022-07-17 17:08:51 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
child = Column(
|
|
|
|
key: const ValueKey(null),
|
2022-07-24 19:02:14 +02:00
|
|
|
children: [
|
|
|
|
if (!snapshot.hasError) const LinearProgressIndicator(),
|
|
|
|
const ListTile(),
|
2022-07-17 17:08:51 +02:00
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return PageTransitionSwitcher(
|
|
|
|
// prevent the animation from re-building on dependency change
|
|
|
|
key: ValueKey(widget.space),
|
|
|
|
transitionBuilder: (
|
|
|
|
Widget child,
|
|
|
|
Animation<double> primaryAnimation,
|
|
|
|
Animation<double> secondaryAnimation,
|
|
|
|
) {
|
|
|
|
return SharedAxisTransition(
|
|
|
|
animation: primaryAnimation,
|
|
|
|
secondaryAnimation: secondaryAnimation,
|
|
|
|
transitionType: SharedAxisTransitionType.scaled,
|
|
|
|
child: child,
|
|
|
|
fillColor: Colors.transparent,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
layoutBuilder: (children) => Stack(
|
|
|
|
alignment: Alignment.topCenter,
|
|
|
|
children: children,
|
|
|
|
),
|
|
|
|
child: child,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return Container();
|
|
|
|
}
|
|
|
|
}
|
2022-07-24 19:02:14 +02:00
|
|
|
|
|
|
|
void _refreshRooms() => setState(
|
|
|
|
() => SpacesHierarchyProposals._cache[widget.space!]!.invalidate(),
|
|
|
|
);
|
2022-07-17 17:08:51 +02:00
|
|
|
}
|