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

553 lines
18 KiB
Dart
Raw Normal View History

2021-02-16 22:39:46 +01:00
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:lemmy_api_client/v2.dart';
2021-02-18 10:21:10 +01:00
import '../util/extensions/api.dart';
import '../util/extensions/datetime.dart';
import '../util/goto.dart';
import '../widgets/avatar.dart';
2021-02-19 20:06:01 +01:00
import '../widgets/bottom_safe.dart';
2021-02-18 10:21:10 +01:00
2021-02-16 22:39:46 +01:00
class ModlogPage extends HookWidget {
final String instanceHost;
final String name;
final int communityId;
const ModlogPage.forInstance({
@required this.instanceHost,
}) : assert(instanceHost != null),
communityId = null,
name = instanceHost;
const ModlogPage.forCommunity({
@required this.instanceHost,
@required this.communityId,
@required String communityName,
}) : assert(instanceHost != null),
assert(communityId != null),
assert(communityName != null),
name = '!$communityName';
@override
Widget build(BuildContext context) {
2021-02-17 00:40:26 +01:00
final page = useState(1);
// will be set true when a fetch returns 0 results
final isDone = useState(false);
final modlogFut = useMemoized(
() => LemmyApiV2(instanceHost).run(
GetModlog(
communityId: communityId,
page: page.value,
),
),
[page.value],
);
return Scaffold(
appBar: AppBar(title: Text("$name's modlog")),
2021-02-20 17:00:36 +01:00
body: LayoutBuilder(
builder: (context, constraints) => SingleChildScrollView(
child: ConstrainedBox(
constraints: BoxConstraints(minHeight: constraints.maxHeight),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const SizedBox.shrink(),
FutureBuilder<Modlog>(
key: ValueKey(modlogFut),
future: modlogFut,
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {
return Center(
child: Text('Error: ${snapshot.error?.toString()}'));
}
final modlog = snapshot.data;
2021-02-16 22:39:46 +01:00
2021-02-20 17:00:36 +01:00
if (modlog.added.length +
modlog.addedToCommunity.length +
modlog.banned.length +
modlog.bannedFromCommunity.length +
modlog.lockedPosts.length +
modlog.removedComments.length +
modlog.removedCommunities.length +
modlog.removedPosts.length +
modlog.stickiedPosts.length ==
0) {
WidgetsBinding.instance
.addPostFrameCallback((_) => isDone.value = true);
2021-02-16 22:39:46 +01:00
2021-02-20 17:00:36 +01:00
return const Center(child: Text('no more logs to show'));
}
2021-02-17 00:40:26 +01:00
2021-02-20 17:00:36 +01:00
return _ModlogTable(modlog: modlog);
},
),
Column(
children: [
Padding(
padding: const EdgeInsets.all(8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
TextButton(
onPressed:
page.value != 1 ? () => page.value-- : null,
child: const Icon(Icons.skip_previous),
),
TextButton(
onPressed: isDone.value ? null : () => page.value++,
child: const Icon(Icons.skip_next),
),
],
),
),
const BottomSafe(),
],
),
],
2021-02-19 20:06:01 +01:00
),
2021-02-20 17:00:36 +01:00
),
2021-02-17 00:40:26 +01:00
),
),
);
}
}
class _ModlogTable extends StatelessWidget {
const _ModlogTable({Key key, @required this.modlog})
: assert(modlog != null),
super(key: key);
final Modlog modlog;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
2021-02-16 22:39:46 +01:00
2021-02-18 10:21:10 +01:00
InlineSpan user(UserSafe user) => TextSpan(
children: [
WidgetSpan(
child: Avatar(
url: user.avatar,
noBlank: true,
radius: 10,
),
),
TextSpan(
text: ' ${user.displayName}',
style: TextStyle(color: theme.accentColor),
recognizer: TapGestureRecognizer()
..onTap = () => goToUser.byId(
context,
user.instanceHost,
user.id,
),
),
],
);
InlineSpan community(CommunitySafe community) => TextSpan(
children: [
WidgetSpan(
child: Avatar(
url: community.icon,
noBlank: true,
radius: 10,
),
),
TextSpan(
text: ' !${community.name}',
style: TextStyle(color: theme.accentColor),
recognizer: TapGestureRecognizer()
..onTap = () => goToCommunity.byId(
context,
community.instanceHost,
community.id,
),
),
],
);
2021-02-16 22:39:46 +01:00
final modlogEntries = [
for (final removedPost in modlog.removedPosts)
_ModlogEntry.fromModRemovePostView(
removedPost,
RichText(
text: TextSpan(
children: [
if (removedPost.modRemovePost.removed)
const TextSpan(text: 'removed')
else
const TextSpan(text: 'restored'),
const TextSpan(text: ' post '),
TextSpan(
text: '"${removedPost.post.name}"',
style: TextStyle(color: theme.accentColor),
recognizer: TapGestureRecognizer()
..onTap = () => goToPost(
context,
removedPost.instanceHost,
removedPost.post.id,
),
),
],
style: TextStyle(color: theme.colorScheme.onSurface),
),
),
),
for (final lockedPost in modlog.lockedPosts)
_ModlogEntry.fromModLockPostView(
lockedPost,
RichText(
text: TextSpan(
children: [
if (lockedPost.modLockPost.locked)
const TextSpan(text: 'locked')
else
const TextSpan(text: 'unlocked'),
const TextSpan(text: ' post '),
TextSpan(
text: '"${lockedPost.post.name}"',
style: TextStyle(color: theme.accentColor),
recognizer: TapGestureRecognizer()
..onTap = () => goToPost(
context,
lockedPost.instanceHost,
lockedPost.post.id,
),
),
],
style: TextStyle(color: theme.colorScheme.onSurface),
),
),
),
for (final stickiedPost in modlog.stickiedPosts)
_ModlogEntry.fromModStickyPostView(
stickiedPost,
RichText(
text: TextSpan(
children: [
if (stickiedPost.modStickyPost.stickied)
const TextSpan(text: 'stickied')
else
const TextSpan(text: 'unstickied'),
const TextSpan(text: ' post '),
TextSpan(
text: '"${stickiedPost.post.name}"',
style: TextStyle(color: theme.accentColor),
recognizer: TapGestureRecognizer()
..onTap = () => goToPost(
context,
stickiedPost.instanceHost,
stickiedPost.post.id,
),
),
],
style: TextStyle(color: theme.colorScheme.onSurface),
),
),
),
for (final removedComment in modlog.removedComments)
_ModlogEntry.fromModRemoveCommentView(
removedComment,
RichText(
text: TextSpan(
children: [
if (removedComment.modRemoveComment.removed)
const TextSpan(text: 'removed')
else
const TextSpan(text: 'restored'),
2021-02-18 10:21:10 +01:00
const TextSpan(text: ' comment '),
2021-02-16 22:39:46 +01:00
TextSpan(
2021-02-18 10:21:10 +01:00
text:
'"${removedComment.comment.content.replaceAll('\n', ' ')}"',
2021-02-16 22:39:46 +01:00
style: TextStyle(color: theme.accentColor),
recognizer: TapGestureRecognizer()
..onTap = () => goToPost(
context,
removedComment.instanceHost,
removedComment.post.id,
),
),
2021-02-18 10:21:10 +01:00
const TextSpan(text: ' by '),
user(removedComment.commenter),
2021-02-16 22:39:46 +01:00
],
style: TextStyle(color: theme.colorScheme.onSurface),
),
),
),
for (final removedCommunity in modlog.removedCommunities)
_ModlogEntry.fromModRemoveCommunityView(
removedCommunity,
RichText(
text: TextSpan(
children: [
if (removedCommunity.modRemoveCommunity.removed)
const TextSpan(text: 'removed')
else
const TextSpan(text: 'restored'),
const TextSpan(text: ' community '),
2021-02-18 10:21:10 +01:00
community(removedCommunity.community),
2021-02-16 22:39:46 +01:00
],
style: TextStyle(color: theme.colorScheme.onSurface),
),
),
),
for (final bannedFromCommunity in modlog.bannedFromCommunity)
_ModlogEntry.fromModBanFromCommunityView(
bannedFromCommunity,
RichText(
text: TextSpan(
children: [
if (bannedFromCommunity.modBanFromCommunity.banned)
2021-02-18 10:21:10 +01:00
const TextSpan(text: 'banned ')
2021-02-16 22:39:46 +01:00
else
2021-02-18 10:21:10 +01:00
const TextSpan(text: 'unbanned '),
user(bannedFromCommunity.bannedUser),
2021-02-16 22:39:46 +01:00
const TextSpan(text: ' from community '),
2021-02-18 10:21:10 +01:00
community(bannedFromCommunity.community),
2021-02-16 22:39:46 +01:00
],
style: TextStyle(color: theme.colorScheme.onSurface),
),
),
),
for (final banned in modlog.banned)
_ModlogEntry.fromModBanView(
banned,
RichText(
text: TextSpan(
children: [
if (banned.modBan.banned)
2021-02-18 10:21:10 +01:00
const TextSpan(text: 'banned ')
2021-02-16 22:39:46 +01:00
else
2021-02-18 10:21:10 +01:00
const TextSpan(text: 'unbanned '),
user(banned.bannedUser),
2021-02-16 22:39:46 +01:00
],
style: TextStyle(color: theme.colorScheme.onSurface),
),
),
),
for (final addedToCommunity in modlog.addedToCommunity)
_ModlogEntry.fromModAddCommunityView(
addedToCommunity,
RichText(
text: TextSpan(
children: [
if (addedToCommunity.modAddCommunity.removed)
2021-02-18 10:21:10 +01:00
const TextSpan(text: 'removed ')
2021-02-16 22:39:46 +01:00
else
2021-02-18 10:21:10 +01:00
const TextSpan(text: 'appointed '),
user(addedToCommunity.moddedUser),
const TextSpan(text: ' as mod of '),
community(addedToCommunity.community),
2021-02-16 22:39:46 +01:00
],
style: TextStyle(color: theme.colorScheme.onSurface),
),
),
),
for (final added in modlog.added)
_ModlogEntry.fromModAddView(
added,
RichText(
text: TextSpan(
children: [
if (added.modAdd.removed)
2021-02-18 10:21:10 +01:00
const TextSpan(text: 'removed ')
2021-02-16 22:39:46 +01:00
else
2021-02-18 10:21:10 +01:00
const TextSpan(text: 'apointed '),
user(added.moddedUser),
const TextSpan(text: ' as admin'),
2021-02-16 22:39:46 +01:00
],
style: TextStyle(color: theme.colorScheme.onSurface),
),
),
),
]..sort((a, b) => b.when.compareTo(a.when));
2021-02-17 00:40:26 +01:00
return SingleChildScrollView(
2021-02-18 13:14:03 +01:00
padding: const EdgeInsets.all(8),
2021-02-17 00:40:26 +01:00
scrollDirection: Axis.horizontal,
2021-02-18 13:14:03 +01:00
child: SizedBox(
width: 1000,
child: Table(
border: TableBorder.all(color: theme.colorScheme.onSurface),
2021-02-18 13:14:03 +01:00
columnWidths: const {
0: FixedColumnWidth(80),
1: FixedColumnWidth(200),
2: FlexColumnWidth(),
3: FixedColumnWidth(200),
},
children: [
const TableRow(
children: [
Center(child: Text('when')),
Center(child: Text('mod')),
Center(child: Text('action')),
Center(child: Text('reason')),
],
),
for (final modlogEntry in modlogEntries) modlogEntry.build(context)
],
),
2021-02-16 22:39:46 +01:00
),
);
}
}
class _ModlogEntry {
final DateTime when;
final UserSafe mod;
final Widget action;
final String reason;
const _ModlogEntry({
@required this.when,
@required this.mod,
@required this.action,
this.reason,
}) : assert(when != null),
assert(mod != null),
assert(action != null);
_ModlogEntry.fromModRemovePostView(
ModRemovePostView removedPost,
Widget action,
) : this(
when: removedPost.modRemovePost.when,
mod: removedPost.moderator,
action: action,
reason: removedPost.modRemovePost.reason,
);
_ModlogEntry.fromModLockPostView(
ModLockPostView lockedPost,
Widget action,
) : this(
when: lockedPost.modLockPost.when,
mod: lockedPost.moderator,
action: action,
);
_ModlogEntry.fromModStickyPostView(
ModStickyPostView stickiedPost,
Widget action,
) : this(
when: stickiedPost.modStickyPost.when,
mod: stickiedPost.moderator,
action: action,
);
_ModlogEntry.fromModRemoveCommentView(
ModRemoveCommentView removedComment,
Widget action,
) : this(
when: removedComment.modRemoveComment.when,
mod: removedComment.moderator,
action: action,
reason: removedComment.modRemoveComment.reason,
);
_ModlogEntry.fromModRemoveCommunityView(
ModRemoveCommunityView removedCommunity,
Widget action,
) : this(
when: removedCommunity.modRemoveCommunity.when,
mod: removedCommunity.moderator,
action: action,
reason: removedCommunity.modRemoveCommunity.reason,
);
_ModlogEntry.fromModBanFromCommunityView(
ModBanFromCommunityView bannedFromCommunity,
Widget action,
) : this(
when: bannedFromCommunity.modBanFromCommunity.when,
mod: bannedFromCommunity.moderator,
action: action,
reason: bannedFromCommunity.modBanFromCommunity.reason,
);
_ModlogEntry.fromModBanView(
ModBanView banned,
Widget action,
) : this(
when: banned.modBan.when,
mod: banned.moderator,
action: action,
reason: banned.modBan.reason,
);
_ModlogEntry.fromModAddCommunityView(
ModAddCommunityView addedToCommunity,
Widget action,
) : this(
when: addedToCommunity.modAddCommunity.when,
mod: addedToCommunity.moderator,
action: action,
);
_ModlogEntry.fromModAddView(
ModAddView added,
Widget action,
) : this(
when: added.modAdd.when,
mod: added.moderator,
action: action,
);
TableRow build(BuildContext context) => TableRow(
children: [
2021-02-19 20:06:01 +01:00
GestureDetector(
onTap: () {
showDialog(
context: context,
builder: (context) => Dialog(
child: Padding(
padding: const EdgeInsets.all(8),
child: Center(
heightFactor: 1,
child: Text(when.toString()),
),
),
),
);
},
child: Center(child: Text(when.fancyShort)),
),
2021-02-18 10:24:33 +01:00
GestureDetector(
onTap: () => goToUser.byId(
context,
mod.instanceHost,
mod.id,
),
child: Row(
children: [
Avatar(
url: mod.avatar,
noBlank: true,
radius: 10,
),
2021-02-19 20:06:01 +01:00
Text(
' ${mod.displayName}',
style: TextStyle(color: Theme.of(context).accentColor),
),
2021-02-18 10:24:33 +01:00
],
),
2021-02-16 22:39:46 +01:00
),
action,
2021-02-18 17:13:20 +01:00
if (reason == null) const Center(child: Text('-')) else Text(reason),
2021-02-16 22:39:46 +01:00
]
.map(
(widget) => Padding(
padding: const EdgeInsets.all(8),
child: widget,
),
)
.toList(),
);
}