Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions lib/src/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,28 @@ class App extends StatefulWidget {

class _AppState extends State<App> {
int _navIndex = 0;
final PageController _pageController = PageController();
Key _feedKey = UniqueKey();
Key _exploreKey = UniqueKey();
Key _accountKey = UniqueKey();

void _changeNav(int newIndex) {
setState(() {
_navIndex = newIndex;
});
_pageController.jumpToPage(_navIndex);
}

@override
Widget build(BuildContext context) {
final appController = context.watch<AppController>();
appController.refreshState = () {
setState(() {
_feedKey = UniqueKey();
_exploreKey = UniqueKey();
_accountKey = UniqueKey();
});
};

final intlLocale =
intl_locale.Locale.tryParse(appController.profile.appLanguage);
Expand Down Expand Up @@ -189,12 +201,16 @@ class _AppState extends State<App> {
width: 1,
),
Expanded(
child: const [
FeedScreen(),
ExploreScreen(),
AccountScreen(),
SettingsScreen(),
][_navIndex]),
child: PageView(
controller: _pageController,
children: [
FeedScreen(key: _feedKey),
ExploreScreen(key: _exploreKey),
AccountScreen(key: _accountKey),
SettingsScreen(),
],
)
),
]),
);
},
Expand Down
5 changes: 5 additions & 0 deletions lib/src/controller/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ class AppController with ChangeNotifier {
late Map<String, FilterList> _filterLists;
Map<String, FilterList> get filterLists => _filterLists;

late Function refreshState;

Future<void> init() async {
refreshState = (){};
final mainProfileTemp = await _mainProfileRecord.get(db) as String?;
if (mainProfileTemp != null) {
_mainProfile = mainProfileTemp;
Expand Down Expand Up @@ -134,6 +137,7 @@ class AppController with ChangeNotifier {
_builtProfile = ProfileRequired.fromOptional(
(await getProfile(_mainProfile)).merge(_selectedProfileValue),
);
refreshState();
}

Future<void> updateProfile(ProfileOptional value) async {
Expand Down Expand Up @@ -346,6 +350,7 @@ class AppController with ChangeNotifier {
magazineMentionCache.clear();

notifyListeners();
refreshState();

await _selectedAccountRecord.put(db, _selectedAccount);
}
Expand Down
7 changes: 6 additions & 1 deletion lib/src/screens/account/account_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ class AccountScreen extends StatefulWidget {
State<AccountScreen> createState() => _AccountScreenState();
}

class _AccountScreenState extends State<AccountScreen> {
class _AccountScreenState extends State<AccountScreen> with AutomaticKeepAliveClientMixin<AccountScreen> {

@override
bool get wantKeepAlive => true;

@override
Widget build(BuildContext context) {
super.build(context);
return whenLoggedIn(
context,
context.read<AppController>().serverSoftware == ServerSoftware.lemmy
Expand Down
6 changes: 5 additions & 1 deletion lib/src/screens/account/messages/messages_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ class MessagesScreen extends StatefulWidget {
State<MessagesScreen> createState() => _MessagesScreenState();
}

class _MessagesScreenState extends State<MessagesScreen> {
class _MessagesScreenState extends State<MessagesScreen> with AutomaticKeepAliveClientMixin<MessagesScreen> {
final PagingController<String, MessageThreadModel> _pagingController =
PagingController(firstPageKey: '');

@override
bool get wantKeepAlive => true;

@override
void initState() {
super.initState();
Expand Down Expand Up @@ -51,6 +54,7 @@ class _MessagesScreenState extends State<MessagesScreen> {

@override
Widget build(BuildContext context) {
super.build(context);
return RefreshIndicator(
onRefresh: () => Future.sync(
() => _pagingController.refresh(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ class NotificationsScreen extends StatefulWidget {
State<NotificationsScreen> createState() => _NotificationsScreenState();
}

class _NotificationsScreenState extends State<NotificationsScreen> {
class _NotificationsScreenState extends State<NotificationsScreen> with AutomaticKeepAliveClientMixin<NotificationsScreen> {
NotificationsFilter filter = NotificationsFilter.all;

final PagingController<String, NotificationModel> _pagingController =
PagingController(firstPageKey: '');

@override
bool get wantKeepAlive => true;

@override
void initState() {
super.initState();
Expand Down Expand Up @@ -62,6 +65,7 @@ class _NotificationsScreenState extends State<NotificationsScreen> {

@override
Widget build(BuildContext context) {
super.build(context);
final currentNotificationFilter =
notificationFilterSelect(context).getOption(filter);

Expand Down
6 changes: 5 additions & 1 deletion lib/src/screens/account/self_feed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ class SelfFeed extends StatefulWidget {
State<SelfFeed> createState() => _SelfFeedState();
}

class _SelfFeedState extends State<SelfFeed> {
class _SelfFeedState extends State<SelfFeed> with AutomaticKeepAliveClientMixin<SelfFeed> {
UserModel? _meUser;

@override
bool get wantKeepAlive => true;

@override
void initState() {
super.initState();
Expand All @@ -33,6 +36,7 @@ class _SelfFeedState extends State<SelfFeed> {

@override
Widget build(BuildContext context) {
super.build(context);
if (_meUser == null) {
return const LoadingTemplate();
}
Expand Down
6 changes: 5 additions & 1 deletion lib/src/screens/explore/explore_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ExploreScreen extends StatefulWidget {
State<ExploreScreen> createState() => _ExploreScreenState();
}

class _ExploreScreenState extends State<ExploreScreen> {
class _ExploreScreenState extends State<ExploreScreen> with AutomaticKeepAliveClientMixin<ExploreScreen> {
String search = '';
final searchDebounce = Debouncer(duration: const Duration(milliseconds: 500));

Expand All @@ -32,6 +32,9 @@ class _ExploreScreenState extends State<ExploreScreen> {
final PagingController<String, dynamic> _pagingController =
PagingController(firstPageKey: '');

@override
bool get wantKeepAlive => true;

@override
void initState() {
super.initState();
Expand Down Expand Up @@ -132,6 +135,7 @@ class _ExploreScreenState extends State<ExploreScreen> {

@override
Widget build(BuildContext context) {
super.build(context);
const chipPadding = EdgeInsets.symmetric(vertical: 6, horizontal: 4);

final currentExploreSort = exploreSortSelection(context).getOption(sort);
Expand Down
6 changes: 5 additions & 1 deletion lib/src/screens/explore/user_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -466,10 +466,13 @@ class UserScreenBody extends StatefulWidget {
State<UserScreenBody> createState() => _UserScreenBodyState();
}

class _UserScreenBodyState extends State<UserScreenBody> {
class _UserScreenBodyState extends State<UserScreenBody> with AutomaticKeepAliveClientMixin<UserScreenBody> {
final PagingController<String, dynamic> _pagingController =
PagingController(firstPageKey: '');

@override
bool get wantKeepAlive => true;

@override
void didUpdateWidget(covariant oldWidget) {
super.didUpdateWidget(oldWidget);
Expand Down Expand Up @@ -577,6 +580,7 @@ class _UserScreenBodyState extends State<UserScreenBody> {

@override
Widget build(BuildContext context) {
super.build(context);
return RefreshIndicator(
onRefresh: () => Future.sync(() => _pagingController.refresh()),
child: CustomScrollView(
Expand Down
12 changes: 10 additions & 2 deletions lib/src/screens/feed/feed_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,16 @@ class FeedScreen extends StatefulWidget {
State<FeedScreen> createState() => _FeedScreenState();
}

class _FeedScreenState extends State<FeedScreen> {
class _FeedScreenState extends State<FeedScreen> with AutomaticKeepAliveClientMixin<FeedScreen> {
final _fabKey = GlobalKey<FloatingMenuState>();
final List<GlobalKey<_FeedScreenBodyState>> _feedKeyList = [];
late FeedSource _filter;
late PostType _mode;
FeedSort? _sort;

@override
bool get wantKeepAlive => true;

_getFeedKey(int index) {
while (index >= _feedKeyList.length) {
_feedKeyList.add(GlobalKey());
Expand Down Expand Up @@ -73,6 +76,7 @@ class _FeedScreenState extends State<FeedScreen> {

@override
Widget build(BuildContext context) {
super.build(context);
final sort = _sort ?? _defaultSortFromMode(_mode);

final currentFeedModeOption = feedTypeSelect(context).getOption(_mode);
Expand Down Expand Up @@ -574,7 +578,7 @@ class FeedScreenBody extends StatefulWidget {
State<FeedScreenBody> createState() => _FeedScreenBodyState();
}

class _FeedScreenBodyState extends State<FeedScreenBody> {
class _FeedScreenBodyState extends State<FeedScreenBody> with AutomaticKeepAliveClientMixin<FeedScreenBody> {
final _pagingController =
PagingController<String, PostModel>(firstPageKey: '');
final _scrollController = ScrollController();
Expand All @@ -590,6 +594,9 @@ class _FeedScreenBodyState extends State<FeedScreenBody> {
_pagingController.addPageRequestListener(_fetchPage);
}

@override
bool get wantKeepAlive => true;

Future<void> _fetchPage(String pageKey) async {
if (pageKey.isEmpty) _filterListWarnings.clear();

Expand Down Expand Up @@ -700,6 +707,7 @@ class _FeedScreenBodyState extends State<FeedScreenBody> {

@override
Widget build(BuildContext context) {
super.build(context);
return RefreshIndicator(
onRefresh: () => Future.sync(
() => _pagingController.refresh(),
Expand Down
5 changes: 3 additions & 2 deletions lib/src/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,14 @@ class DefaultTabControllerListener extends StatefulWidget {

class _DefaultTabControllerListenerState
extends State<DefaultTabControllerListener> {
late final void Function()? _listener;
void Function()? _listener;
TabController? _tabController;

@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!mounted) return;
final tabController = DefaultTabController.of(context);
_listener = () {
if (tabController.indexIsChanging) {
Expand All @@ -228,7 +229,7 @@ class _DefaultTabControllerListenerState
@override
void dispose() {
if (_listener != null && _tabController != null) {
_tabController!.removeListener(_listener);
_tabController!.removeListener(_listener!);
}
super.dispose();
}
Expand Down