lemmur-app-android/lib/pages/inbox.dart

399 lines
14 KiB
Dart
Raw Normal View History

2021-02-24 20:52:18 +01:00
import 'dart:math' show pi;
2020-10-04 21:51:38 +02:00
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
2021-04-05 20:14:39 +02:00
import 'package:lemmy_api_client/v3.dart';
2021-02-24 20:52:18 +01:00
import 'package:matrix4_transform/matrix4_transform.dart';
import '../hooks/delayed_loading.dart';
import '../hooks/infinite_scroll.dart';
import '../hooks/stores.dart';
2021-03-01 14:21:45 +01:00
import '../l10n/l10n.dart';
2021-02-24 20:52:18 +01:00
import '../util/delayed_action.dart';
import '../util/extensions/api.dart';
import '../util/goto.dart';
2021-09-11 01:04:15 +02:00
import '../util/icons.dart';
2021-02-24 20:52:18 +01:00
import '../widgets/bottom_modal.dart';
2021-10-21 14:40:28 +02:00
import '../widgets/cached_network_image.dart';
import '../widgets/comment/comment.dart';
2021-02-24 20:52:18 +01:00
import '../widgets/infinite_scroll.dart';
import '../widgets/info_table_popup.dart';
import '../widgets/markdown_mode_icon.dart';
import '../widgets/markdown_text.dart';
import '../widgets/radio_picker.dart';
import '../widgets/sortable_infinite_list.dart';
import '../widgets/tile_action.dart';
import 'write_message.dart';
2020-10-04 21:51:38 +02:00
class InboxPage extends HookWidget {
2021-01-03 19:43:39 +01:00
const InboxPage();
2020-10-04 21:51:38 +02:00
@override
2021-02-24 20:52:18 +01:00
Widget build(BuildContext context) {
final accStore = useAccountsStore();
final selected = useState(accStore.defaultInstanceHost);
final theme = Theme.of(context);
final isc = useInfiniteScrollController();
final unreadOnly = useState(true);
if (accStore.hasNoAccount) {
return Scaffold(
2020-10-06 12:06:29 +02:00
appBar: AppBar(),
2021-02-24 20:52:18 +01:00
body: const Center(child: Text('no accounts added')),
);
}
final selectedInstance = selected.value!;
2021-02-24 20:52:18 +01:00
toggleUnreadOnly() {
unreadOnly.value = !unreadOnly.value;
isc.clear();
}
return DefaultTabController(
length: 3,
child: Scaffold(
appBar: AppBar(
title: RadioPicker<String>(
onChanged: (val) {
selected.value = val;
isc.clear();
},
2021-03-03 12:31:36 +01:00
title: 'select instance',
groupValue: selectedInstance,
2021-02-24 20:52:18 +01:00
buttonBuilder: (context, displayString, onPressed) => TextButton(
style: TextButton.styleFrom(
padding: const EdgeInsets.symmetric(horizontal: 15),
),
onPressed: onPressed,
child: Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Flexible(
child: Text(
displayString,
style: theme.appBarTheme.titleTextStyle,
2021-02-24 20:52:18 +01:00
overflow: TextOverflow.fade,
softWrap: false,
),
),
const Icon(Icons.arrow_drop_down),
],
),
),
values: accStore.loggedInInstances.toList(),
),
actions: [
IconButton(
icon: Icon(unreadOnly.value ? Icons.mail : Icons.mail_outline),
onPressed: toggleUnreadOnly,
tooltip: unreadOnly.value ? 'show all' : 'show only unread',
)
],
2021-03-01 14:21:45 +01:00
bottom: TabBar(
2021-02-24 20:52:18 +01:00
tabs: [
Tab(text: L10n.of(context).replies),
Tab(text: L10n.of(context).mentions),
Tab(text: L10n.of(context).messages),
2021-02-24 20:52:18 +01:00
],
),
),
body: TabBarView(
children: [
SortableInfiniteList<CommentView>(
noItems: const Text('no replies'),
controller: isc,
defaultSort: SortType.new_,
fetcher: (page, batchSize, sortType) =>
LemmyApiV3(selectedInstance).run(GetReplies(
2021-04-11 18:27:22 +02:00
auth: accStore.defaultUserDataFor(selectedInstance)!.jwt.raw,
2021-02-24 20:52:18 +01:00
sort: sortType,
limit: batchSize,
page: page,
unreadOnly: unreadOnly.value,
)),
itemBuilder: (cv) => CommentWidget.fromCommentView(
cv,
canBeMarkedAsRead: true,
hideOnRead: unreadOnly.value,
),
2021-04-22 21:08:30 +02:00
uniqueProp: (item) => item.comment.apId,
2021-02-24 20:52:18 +01:00
),
2021-04-05 20:14:39 +02:00
SortableInfiniteList<PersonMentionView>(
2021-02-24 20:52:18 +01:00
noItems: const Text('no mentions'),
controller: isc,
defaultSort: SortType.new_,
fetcher: (page, batchSize, sortType) =>
LemmyApiV3(selectedInstance).run(GetPersonMentions(
2021-04-11 18:27:22 +02:00
auth: accStore.defaultUserDataFor(selectedInstance)!.jwt.raw,
2021-02-24 20:52:18 +01:00
sort: sortType,
limit: batchSize,
page: page,
unreadOnly: unreadOnly.value,
)),
2021-04-05 20:14:39 +02:00
itemBuilder: (umv) => CommentWidget.fromPersonMentionView(
2021-02-24 20:52:18 +01:00
umv,
hideOnRead: unreadOnly.value,
),
2021-04-06 17:52:10 +02:00
uniqueProp: (item) => item.personMention.id,
2021-02-24 20:52:18 +01:00
),
InfiniteScroll<PrivateMessageView>(
noItems: const Padding(
padding: EdgeInsets.only(top: 60),
child: Text('no messages'),
),
controller: isc,
fetcher: (page, batchSize) => LemmyApiV3(selectedInstance).run(
2021-02-24 20:52:18 +01:00
GetPrivateMessages(
2021-04-11 18:27:22 +02:00
auth: accStore.defaultUserDataFor(selectedInstance)!.jwt.raw,
2021-02-24 20:52:18 +01:00
limit: batchSize,
page: page,
unreadOnly: unreadOnly.value,
),
),
itemBuilder: (mv) => PrivateMessageTile(
privateMessageView: mv,
hideOnRead: unreadOnly.value,
),
2021-04-22 21:08:30 +02:00
uniqueProp: (item) => item.privateMessage.apId,
2021-02-24 20:52:18 +01:00
),
],
),
),
);
}
}
class PrivateMessageTile extends HookWidget {
final PrivateMessageView privateMessageView;
final bool hideOnRead;
const PrivateMessageTile({
required this.privateMessageView,
2021-02-24 20:52:18 +01:00
this.hideOnRead = false,
});
2021-02-24 20:52:18 +01:00
static const double _iconSize = 16;
@override
Widget build(BuildContext context) {
final accStore = useAccountsStore();
final theme = Theme.of(context);
final pmv = useState(privateMessageView);
final raw = useState(false);
final selectable = useState(false);
final deleted = useState(pmv.value.privateMessage.deleted);
final deleteDelayed = useDelayedLoading(const Duration(milliseconds: 250));
final read = useState(pmv.value.privateMessage.read);
final readDelayed = useDelayedLoading(const Duration(milliseconds: 200));
final toMe = useMemoized(() =>
pmv.value.recipient.originInstanceHost == pmv.value.instanceHost &&
pmv.value.recipient.id ==
2021-04-11 18:27:22 +02:00
accStore.defaultUserDataFor(pmv.value.instanceHost)?.userId);
2021-02-24 20:52:18 +01:00
final otherSide =
useMemoized(() => toMe ? pmv.value.creator : pmv.value.recipient);
void showMoreMenu() {
showBottomModal(
context: context,
builder: (context) {
pop() => Navigator.of(context).pop();
return Column(
children: [
ListTile(
title: Text(raw.value ? 'Show fancy' : 'Show raw'),
leading: markdownModeIcon(fancy: !raw.value),
onTap: () {
raw.value = !raw.value;
pop();
},
),
ListTile(
title: Text('Make ${selectable.value ? 'un' : ''}selectable'),
leading: Icon(
selectable.value ? Icons.assignment : Icons.content_cut),
onTap: () {
selectable.value = !selectable.value;
pop();
},
),
ListTile(
title: const Text('Nerd stuff'),
leading: const Icon(Icons.info_outline),
onTap: () {
pop();
showInfoTablePopup(
context: context, table: pmv.value.toJson());
},
),
],
);
},
);
}
handleDelete() => delayedAction<PrivateMessageView>(
context: context,
delayedLoading: deleteDelayed,
instanceHost: pmv.value.instanceHost,
query: DeletePrivateMessage(
privateMessageId: pmv.value.privateMessage.id,
2021-04-11 18:27:22 +02:00
auth: accStore.defaultUserDataFor(pmv.value.instanceHost)!.jwt.raw,
2021-02-24 20:52:18 +01:00
deleted: !deleted.value,
),
onSuccess: (val) => deleted.value = val.privateMessage.deleted,
);
handleRead() => delayedAction<PrivateMessageView>(
context: context,
delayedLoading: readDelayed,
instanceHost: pmv.value.instanceHost,
query: MarkPrivateMessageAsRead(
privateMessageId: pmv.value.privateMessage.id,
2021-04-11 18:27:22 +02:00
auth: accStore.defaultUserDataFor(pmv.value.instanceHost)!.jwt.raw,
2021-02-24 20:52:18 +01:00
read: !read.value,
),
// TODO: add notification for notifying parent list
onSuccess: (val) => read.value = val.privateMessage.read,
);
if (hideOnRead && read.value) {
return const SizedBox.shrink();
}
final body = raw.value
? selectable.value
? SelectableText(pmv.value.privateMessage.content)
: Text(pmv.value.privateMessage.content)
: MarkdownText(
pmv.value.privateMessage.content,
instanceHost: pmv.value.instanceHost,
selectable: selectable.value,
);
return Padding(
padding: const EdgeInsets.all(8),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
2020-10-06 12:06:29 +02:00
children: [
Text(
'${toMe ? L10n.of(context).from : L10n.of(context).to} ',
style: TextStyle(color: theme.textTheme.caption?.color),
2021-02-24 20:52:18 +01:00
),
InkWell(
borderRadius: BorderRadius.circular(10),
2021-04-05 20:14:39 +02:00
onTap: () => goToUser.fromPersonSafe(context, otherSide),
2021-02-24 20:52:18 +01:00
child: Row(
children: [
if (otherSide.avatar != null)
Padding(
padding: const EdgeInsets.only(right: 5),
child: CachedNetworkImage(
imageUrl: otherSide.avatar!,
2021-02-24 20:52:18 +01:00
height: 20,
width: 20,
imageBuilder: (context, imageProvider) => Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
fit: BoxFit.cover,
image: imageProvider,
),
),
),
2021-10-21 14:40:28 +02:00
errorBuilder: (_, ___) => const SizedBox.shrink(),
2021-02-24 20:52:18 +01:00
),
),
Text(
2021-04-29 11:38:28 +02:00
otherSide.originPreferredName,
style: TextStyle(color: theme.colorScheme.secondary),
2021-02-24 20:52:18 +01:00
),
],
),
),
const Spacer(),
if (pmv.value.privateMessage.updated != null) const Text('🖊 '),
Text(pmv.value.privateMessage.updated?.timeago(context) ??
pmv.value.privateMessage.published.timeago(context)),
2021-02-24 20:52:18 +01:00
const SizedBox(width: 5),
Transform(
transform: Matrix4Transform()
.rotateByCenter((toMe ? -1 : 1) * pi / 2,
const Size(_iconSize, _iconSize))
.flipVertically(
origin: const Offset(_iconSize / 2, _iconSize / 2))
.matrix4,
child: const Opacity(
opacity: 0.8,
child: Icon(Icons.reply, size: _iconSize),
),
2020-10-06 12:06:29 +02:00
)
],
),
2021-02-24 20:52:18 +01:00
const SizedBox(height: 5),
if (pmv.value.privateMessage.deleted)
2021-03-09 08:53:26 +01:00
Text(
L10n.of(context).deleted_by_creator,
2021-03-09 08:53:26 +01:00
style: const TextStyle(fontStyle: FontStyle.italic),
)
2021-02-24 20:52:18 +01:00
else
body,
Row(children: [
const Spacer(),
TileAction(
icon: moreIcon,
onPressed: showMoreMenu,
tooltip: L10n.of(context).more,
2021-02-24 20:52:18 +01:00
),
if (toMe) ...[
TileAction(
iconColor: read.value ? theme.colorScheme.secondary : null,
2021-02-24 20:52:18 +01:00
icon: Icons.check,
tooltip: L10n.of(context).mark_as_read,
2021-02-24 20:52:18 +01:00
onPressed: handleRead,
delayedLoading: readDelayed,
),
TileAction(
icon: Icons.reply,
tooltip: L10n.of(context).reply,
2021-02-24 20:52:18 +01:00
onPressed: () {
Navigator.of(context).push(
WriteMessagePage.sendRoute(
instanceHost: pmv.value.instanceHost,
recipient: otherSide,
),
);
2021-02-24 20:52:18 +01:00
},
)
] else ...[
TileAction(
icon: Icons.edit,
tooltip: L10n.of(context).edit,
2021-02-24 20:52:18 +01:00
onPressed: () async {
final val = await Navigator.of(context)
.push(WriteMessagePage.editRoute(pmv.value));
2021-02-25 21:23:54 +01:00
if (val != null) pmv.value = val;
2021-02-24 20:52:18 +01:00
},
),
TileAction(
delayedLoading: deleteDelayed,
icon: deleted.value ? Icons.restore : Icons.delete,
2021-03-01 14:21:45 +01:00
tooltip: deleted.value
? L10n.of(context).restore
: L10n.of(context).delete,
2021-02-24 20:52:18 +01:00
onPressed: handleDelete,
),
]
]),
const Divider(),
],
),
);
}
2020-10-04 21:51:38 +02:00
}