fluffychat/lib/pages/chat_list/stories_header.dart

236 lines
7.7 KiB
Dart
Raw Normal View History

2021-12-24 14:18:09 +01:00
//@dart=2.12
import 'package:flutter/material.dart';
import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:future_loading_dialog/future_loading_dialog.dart';
import 'package:matrix/matrix.dart';
import 'package:vrouter/vrouter.dart';
import 'package:fluffychat/config/app_config.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions.dart/client_stories_extension.dart';
import 'package:fluffychat/widgets/avatar.dart';
import 'package:fluffychat/widgets/matrix.dart';
2021-12-25 18:47:36 +01:00
enum ContextualRoomAction {
mute,
unmute,
leave,
}
2021-12-24 14:18:09 +01:00
class StoriesHeader extends StatelessWidget {
const StoriesHeader({Key? key}) : super(key: key);
void _addToStoryAction(BuildContext context) =>
VRouter.of(context).to('/stories/create');
void _goToStoryAction(BuildContext context, String roomId) =>
VRouter.of(context).toSegments(['stories', roomId]);
void _contextualActions(BuildContext context, Room room) async {
final action = await showModalActionSheet<ContextualRoomAction>(
2021-12-25 14:07:48 +01:00
cancelLabel: L10n.of(context)!.cancel,
2021-12-24 14:18:09 +01:00
context: context,
actions: [
if (room.pushRuleState != PushRuleState.notify)
SheetAction(
label: L10n.of(context)!.unmuteChat,
key: ContextualRoomAction.unmute,
icon: Icons.notifications_outlined,
)
else
SheetAction(
label: L10n.of(context)!.muteChat,
key: ContextualRoomAction.mute,
icon: Icons.notifications_off_outlined,
),
SheetAction(
label: L10n.of(context)!.unsubscribeStories,
key: ContextualRoomAction.leave,
icon: Icons.unsubscribe_outlined,
isDestructiveAction: true,
),
],
);
if (action == null) return;
switch (action) {
case ContextualRoomAction.mute:
await showFutureLoadingDialog(
context: context,
future: () => room.setPushRuleState(PushRuleState.dontNotify),
);
break;
case ContextualRoomAction.unmute:
await showFutureLoadingDialog(
context: context,
future: () => room.setPushRuleState(PushRuleState.notify),
);
break;
case ContextualRoomAction.leave:
await showFutureLoadingDialog(
context: context,
future: () => room.leave(),
);
break;
}
}
@override
Widget build(BuildContext context) {
final client = Matrix.of(context).client;
return StreamBuilder<Object>(
stream: client.onSync.stream
.where((syncUpdate) => syncUpdate.hasRoomUpdate),
builder: (context, snapshot) {
if (client.storiesRooms.isEmpty && client.contacts.isEmpty) {
return Container();
}
2021-12-25 18:47:36 +01:00
if (client.storiesRooms.isEmpty ||
Matrix.of(context).shareContent != null) {
2021-12-24 14:18:09 +01:00
return ListTile(
leading: CircleAvatar(
radius: Avatar.defaultSize / 2,
backgroundColor: Theme.of(context).colorScheme.surface,
foregroundColor: Theme.of(context).textTheme.bodyText1?.color,
child: const Icon(Icons.add),
),
title: const Text('Add to story'),
onTap: () => _addToStoryAction(context),
);
}
return SizedBox(
2021-12-25 18:47:36 +01:00
height: 98,
2021-12-24 14:18:09 +01:00
child: ListView(
padding: const EdgeInsets.symmetric(horizontal: 2),
scrollDirection: Axis.horizontal,
children: [
_StoryButton(
2021-12-25 18:47:36 +01:00
label: L10n.of(context)!.yourStory,
2021-12-24 14:18:09 +01:00
onPressed: () => _addToStoryAction(context),
child: const Icon(Icons.add),
),
...client.storiesRooms.map(
2021-12-25 14:07:48 +01:00
(room) => Opacity(
opacity: room.hasPosts ? 1 : 0.5,
child: _StoryButton(
label: room.creatorDisplayname,
child: Avatar(
mxContent: room
.getState(EventTypes.RoomCreate)!
.sender
.avatarUrl,
name: room.creatorDisplayname,
2021-12-25 18:47:36 +01:00
size: 100,
2021-12-25 14:07:48 +01:00
),
unread: room.notificationCount > 0 ||
room.membership == Membership.invite,
onPressed: () => room.hasPosts
? _goToStoryAction(context, room.id)
: _contextualActions(context, room),
onLongPressed: () => _contextualActions(context, room),
2021-12-24 14:18:09 +01:00
),
),
),
],
),
);
});
}
}
extension on Room {
String get creatorDisplayname =>
getState(EventTypes.RoomCreate)!.sender.calcDisplayname();
2021-12-25 14:07:48 +01:00
bool get hasPosts {
final lastEvent = this.lastEvent;
if (lastEvent == null) return false;
if (lastEvent.type != EventTypes.Message) return false;
if (DateTime.now().difference(lastEvent.originServerTs).inHours >
ClientStoriesExtension.lifeTimeInHours) {
return false;
}
return true;
}
2021-12-24 14:18:09 +01:00
}
class _StoryButton extends StatelessWidget {
final Widget child;
final String label;
final void Function() onPressed;
final void Function()? onLongPressed;
final bool unread;
const _StoryButton({
required this.child,
required this.label,
required this.onPressed,
this.unread = false,
this.onLongPressed,
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return SizedBox(
2021-12-25 18:47:36 +01:00
width: 78,
2021-12-24 14:18:09 +01:00
child: InkWell(
borderRadius: BorderRadius.circular(AppConfig.borderRadius),
onTap: onPressed,
onLongPress: onLongPressed,
child: Padding(
2021-12-25 18:47:36 +01:00
padding: const EdgeInsets.symmetric(horizontal: 4),
2021-12-24 14:18:09 +01:00
child: Column(
children: [
const SizedBox(height: 8),
Container(
padding: const EdgeInsets.all(2),
decoration: BoxDecoration(
gradient: unread
? const LinearGradient(
colors: [
Colors.red,
Colors.purple,
Colors.orange,
],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
)
: null,
2021-12-25 18:47:36 +01:00
color: unread ? null : Theme.of(context).dividerColor,
2021-12-24 14:18:09 +01:00
borderRadius: BorderRadius.circular(Avatar.defaultSize),
),
2021-12-25 18:47:36 +01:00
child: Material(
color: Theme.of(context).colorScheme.surface,
borderRadius: BorderRadius.circular(Avatar.defaultSize),
child: Padding(
padding: const EdgeInsets.all(2.0),
child: CircleAvatar(
radius: 30,
backgroundColor: Theme.of(context).colorScheme.surface,
foregroundColor:
Theme.of(context).textTheme.bodyText1?.color,
child: child,
),
),
2021-12-24 14:18:09 +01:00
),
),
const SizedBox(height: 8),
Text(
label,
maxLines: 1,
textAlign: TextAlign.center,
2021-12-25 18:47:36 +01:00
style: TextStyle(
2021-12-24 14:18:09 +01:00
fontSize: 12,
2021-12-25 18:47:36 +01:00
fontWeight: unread ? FontWeight.bold : null,
2021-12-24 14:18:09 +01:00
),
),
],
),
),
),
);
}
}