Skip to content

Commit

Permalink
Migrate modlog to mobx (#303)
Browse files Browse the repository at this point in the history
* Migrate modlog to mobx

* Remove column

* Add MobxProvider and DisposableStore

* Add modlog store tests
  • Loading branch information
shilangyu authored Dec 4, 2021
1 parent 85f1ab8 commit d4d4a5b
Show file tree
Hide file tree
Showing 24 changed files with 910 additions and 610 deletions.
1 change: 1 addition & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
linter:
rules:
- annotate_overrides
- avoid_bool_literals_in_conditional_expressions
- avoid_catching_errors
- avoid_equals_and_hash_code_on_mutable_classes
Expand Down
6 changes: 3 additions & 3 deletions lib/main_common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'l10n/timeago/pl.dart';
import 'pages/log_console/log_console_page_store.dart';
import 'stores/accounts_store.dart';
import 'stores/config_store.dart';
import 'util/mobx_provider.dart';

Future<void> mainCommon(AppConfig appConfig) async {
WidgetsFlutterBinding.ensureInitialized();
Expand All @@ -22,15 +23,14 @@ Future<void> mainCommon(AppConfig appConfig) async {
_setupLogger(appConfig, logConsoleStore);
_setupTimeago();

final configStore = ConfigStore.load(sharedPrefs);
final accountsStore = await AccountsStore.load();

runApp(
MultiProvider(
providers: [
Provider.value(value: configStore),
MobxProvider(create: (context) => ConfigStore.load(sharedPrefs)),
ChangeNotifierProvider.value(value: accountsStore),
Provider.value(value: logConsoleStore),
MobxProvider.value(value: logConsoleStore),
],
child: const MyApp(),
),
Expand Down
3 changes: 2 additions & 1 deletion lib/pages/community/community.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import '../../util/async_store.dart';
import '../../util/async_store_listener.dart';
import '../../util/extensions/api.dart';
import '../../util/icons.dart';
import '../../util/mobx_provider.dart';
import '../../util/observer_consumers.dart';
import '../../util/share.dart';
import '../../widgets/failed_to_load.dart';
Expand Down Expand Up @@ -158,7 +159,7 @@ class CommunityPage extends HookWidget {
static Route _route(String instanceHost, CommunityStore store) {
return MaterialPageRoute(
builder: (context) {
return Provider.value(
return MobxProvider.value(
value: store
..refresh(context
.read<AccountsStore>()
Expand Down
8 changes: 3 additions & 5 deletions lib/pages/community/community_about_tab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import 'package:lemmy_api_client/v3.dart';
import '../../l10n/l10n.dart';
import '../../stores/accounts_store.dart';
import '../../util/extensions/spaced.dart';
import '../../util/goto.dart';
import '../../util/observer_consumers.dart';
import '../../widgets/bottom_safe.dart';
import '../../widgets/markdown_text.dart';
import '../../widgets/pull_to_refresh.dart';
import '../../widgets/user_tile.dart';
import '../modlog_page.dart';
import '../modlog/modlog.dart';
import 'community_store.dart';

class CommmunityAboutTab extends StatelessWidget {
Expand Down Expand Up @@ -99,9 +98,8 @@ class CommmunityAboutTab extends StatelessWidget {
style: Theme.of(context).textTheme.headline6,
),
),
onTap: () => goTo(
context,
(context) => ModlogPage.forCommunity(
onTap: () => Navigator.of(context).push(
ModlogPage.forCommunityRoute(
instanceHost: community.instanceHost,
communityId: community.community.id,
communityName: community.community.name,
Expand Down
4 changes: 3 additions & 1 deletion lib/pages/community/community_more_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:url_launcher/url_launcher.dart' as ul;

import '../../hooks/logged_in_action.dart';
import '../../util/extensions/api.dart';
import '../../util/mobx_provider.dart';
import '../../util/observer_consumers.dart';
import '../../widgets/bottom_modal.dart';
import '../../widgets/info_table_popup.dart';
Expand Down Expand Up @@ -60,9 +61,10 @@ class CommunityMoreMenu extends HookWidget {

static void open(BuildContext context, FullCommunityView fullCommunityView) {
final store = context.read<CommunityStore>();

showBottomModal(
context: context,
builder: (context) => Provider.value(
builder: (context) => MobxProvider.value(
value: store,
child: CommunityMoreMenu(
fullCommunityView: fullCommunityView,
Expand Down
9 changes: 5 additions & 4 deletions lib/pages/full_post/full_post.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import '../../stores/accounts_store.dart';
import '../../util/async_store_listener.dart';
import '../../util/extensions/api.dart';
import '../../util/icons.dart';
import '../../util/mobx_provider.dart';
import '../../util/observer_consumers.dart';
import '../../util/share.dart';
import '../../widgets/failed_to_load.dart';
Expand Down Expand Up @@ -100,7 +101,7 @@ class FullPostPage extends HookWidget {
),
actions: [
IconButton(icon: Icon(shareIcon), onPressed: sharePost),
Provider.value(
MobxProvider.value(
value: postStore,
child: const SavePostButton(),
),
Expand Down Expand Up @@ -143,7 +144,7 @@ class FullPostPage extends HookWidget {
}

static Route route(int id, String instanceHost) => MaterialPageRoute(
builder: (context) => Provider(
builder: (context) => MobxProvider(
create: (context) =>
FullPostStore(instanceHost: instanceHost, postId: id)
..refresh(_tryGetJwt(context, instanceHost)),
Expand All @@ -152,14 +153,14 @@ class FullPostPage extends HookWidget {
);

static Route fromPostViewRoute(PostView postView) => MaterialPageRoute(
builder: (context) => Provider(
builder: (context) => MobxProvider(
create: (context) => FullPostStore.fromPostView(postView)
..refresh(_tryGetJwt(context, postView.instanceHost)),
child: const FullPostPage._(),
),
);
static Route fromPostStoreRoute(PostStore postStore) => MaterialPageRoute(
builder: (context) => Provider(
builder: (context) => MobxProvider(
create: (context) => FullPostStore.fromPostStore(postStore)
..refresh(_tryGetJwt(context, postStore.postView.instanceHost)),
child: const FullPostPage._(),
Expand Down
1 change: 1 addition & 0 deletions lib/pages/home_tab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ class _SelectedList {
this.instanceHost,
});

@override
String toString() =>
'SelectedList(instanceHost: $instanceHost, listingType: $listingType)';
}
7 changes: 3 additions & 4 deletions lib/pages/instance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import '../widgets/reveal_after_scroll.dart';
import '../widgets/sortable_infinite_list.dart';
import '../widgets/user_tile.dart';
import 'communities_list.dart';
import 'modlog_page.dart';
import 'modlog/modlog.dart';
import 'users_list.dart';

/// Displays posts, comments, and general info about the given instance
Expand Down Expand Up @@ -365,9 +365,8 @@ class _AboutTab extends HookWidget {
),
ListTile(
title: Center(child: Text(L10n.of(context).modlog)),
onTap: () => goTo(
context,
(context) => ModlogPage.forInstance(instanceHost: instanceHost),
onTap: () => Navigator.of(context).push(
ModlogPage.forInstanceRoute(instanceHost),
),
),
],
Expand Down
107 changes: 107 additions & 0 deletions lib/pages/modlog/modlog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import 'package:flutter/material.dart';

import '../../l10n/l10n.dart';
import '../../util/mobx_provider.dart';
import '../../util/observer_consumers.dart';
import '../../widgets/bottom_safe.dart';
import '../../widgets/failed_to_load.dart';
import 'modlog_page_store.dart';
import 'modlog_table.dart';

class ModlogPage extends StatelessWidget {
final String name;

const ModlogPage._({required this.name});

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("$name's modlog")),
body: LayoutBuilder(
builder: (context, constraints) => SingleChildScrollView(
child: ConstrainedBox(
constraints: BoxConstraints(minHeight: constraints.maxHeight),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const SizedBox(),
ObserverBuilder<ModlogPageStore>(
builder: (context, store) {
if (!store.hasNextPage) {
return const Center(child: Text('no more logs to show'));
}

return store.modlogState.map(
loading: () => const Center(
child: CircularProgressIndicator.adaptive(),
),
error: (errorTerm) => Center(
child: FailedToLoad(
message: errorTerm.tr(context),
refresh: store.fetchPage,
),
),
data: (modlog) => ModlogTable(modlog: modlog),
);
},
),
Column(
children: [
Padding(
padding: const EdgeInsets.all(8),
child: ObserverBuilder<ModlogPageStore>(
builder: (context, store) => Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
TextButton(
onPressed: store.hasPreviousPage &&
!store.modlogState.isLoading
? store.previousPage
: null,
child: const Icon(Icons.skip_previous),
),
TextButton(
onPressed: store.hasNextPage &&
!store.modlogState.isLoading
? store.nextPage
: null,
child: const Icon(Icons.skip_next),
),
],
),
),
),
const BottomSafe(),
],
),
],
),
),
),
),
);
}

static Route forInstanceRoute(String instanceHost) {
return MaterialPageRoute(
builder: (context) => MobxProvider(
create: (context) => ModlogPageStore(instanceHost)..fetchPage(),
child: ModlogPage._(name: instanceHost),
),
);
}

static Route forCommunityRoute({
required String instanceHost,
required int communityId,
required String communityName,
}) {
return MaterialPageRoute(
builder: (context) => MobxProvider(
create: (context) =>
ModlogPageStore(instanceHost, communityId)..fetchPage(),
child: ModlogPage._(name: '!$communityName'),
),
);
}
}
Loading

0 comments on commit d4d4a5b

Please sign in to comment.