frosty/lib/screens/home/home_store.dart

108 lines
3.7 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:frosty/screens/settings/stores/auth_store.dart';
import 'package:mobx/mobx.dart';
part 'home_store.g.dart';
/// The home store that handles scrolling and navigating between tabs on the home screen.
class HomeStore = HomeStoreBase with _$HomeStore;
abstract class HomeStoreBase with Store {
final AuthStore authStore;
/// The scroll controller for controlling the scroll to top on the followed section.
final followedScrollController = ScrollController();
/// The scroll controllers for controlling the scroll to top on the top section.
/// One controller is for the top streams tab and the other is for the top categories tab.
final topSectionScrollControllers = [
ScrollController(),
ScrollController(),
];
/// The scroll controller for controlling the scroll to top on the search section.
final searchScrollController = ScrollController();
/// The current index of the top section tab. Changes when switching between the streams and categories tabs.
var topSectionCurrentIndex = 0;
/// The current selected index/tab of the bottom navigation bar.
@readonly
var _selectedIndex = 0;
late final ReactionDisposer _disposeReaction;
HomeStoreBase({required this.authStore}) {
// Setup a reaction that will return to the first tab when logging in/out.
// This will prevent an out of range index error when last visiting the search screen and logging out.
_disposeReaction = reaction(
(_) => authStore.isLoggedIn,
(_) => _selectedIndex = 0,
);
}
@action
void handleTap(int index) {
// If tapping a different tab, set the new index to show.
if (index != _selectedIndex) {
_selectedIndex = index;
} else {
Update to Material 3/You and other changes/fixes (#280) * Use adaptive refresh indicators * Add new message count indicator in chat * Sync pubspec.lock * Initial migration to material 3 * Restore default bottom sheet style * Remove FrostyListTile and reset styles * Use "Clear" button for all search history * Add `require_trailing_commas` lint rule * Exclude generated files from linter * Use ListView for safe area support in bottom sheet * Add initial dark theme * Make sizing and units consistent * Update onbaording spacings & copy * Migrate to default material 3 buttons * Add headers to chat details bottom sheet * Simplify token log in method * Use bottom sheet for account options * Remove `AnimateScale` widget * Tweak search paddings * Update message spacing divisions * Upgrade packages * Update message spacing divisions * Regenerate splash screens * Update Inter & add google fonts * Add initial material 3 themes * Use adaptive alert dialogs * Revert to oultined inputs * Fix tooltip message * Revert add google_fonts * Improve themes and various styling * Convert categories grid to list * Tweak stream card thumbnail radii * Tweak settings headers * Left align floating snackbars * Tweak search header padding * Fix profile pics occasionally not rounded * Use profile pic for settings button if logged in * Redesign sleep timer * Use divider theme and reduce thickness * Use 0 letter spacing for tab bar titles * Redesign category streams list * Add border to tooltips * Increase category card font weight * Use outlined icon btton for chat user modal * Reduce profile pic radius in search results * Fix scrolll to top button safe area * Add support for reply threads * Reduce tooltip and snackbar border thickness * Rebuild reply threads on new messages * Upgrade packages (iOS) * Reduce font weight for all chat alerts * Use tabbars for pageview * Add dividers everywhere * Revert "Add dividers everywhere" This reverts commit 5f0d349fdbb0afad313cfb88e8c359714b3ea87a. * Remove fill from inputs * Add dividers to settings * Use segmented button for settings list * Add scrollbar to lists with scrollcontroller * Redesign badge/emote tooltip * Remove outline from chat user modal buttons * Remove divider from reply thread * Use tap for chatters list and increase font weight * Constrain emote tooltip width
2023-10-22 18:38:32 -04:00
const duration = Duration(milliseconds: 300);
// Use different logic if logged in/out since there will be one less tab when logged out.
if (authStore.isLoggedIn) {
if (index == 0 && followedScrollController.hasClients) {
// If on the followed tab and tapping the followed tab, scroll to the top.
followedScrollController.animateTo(
0.0,
duration: duration,
curve: Curves.easeOut,
);
} else if (index == 1 &&
topSectionScrollControllers[topSectionCurrentIndex].hasClients) {
// If on the top section, scroll to the top of the tab based on the current top tab.
topSectionScrollControllers[topSectionCurrentIndex].animateTo(
0.0,
duration: duration,
curve: Curves.easeOut,
);
} else if (searchScrollController.hasClients) {
// If on the search tab and tapping the search tab, scroll to the top.
searchScrollController.animateTo(
0.0,
duration: duration,
curve: Curves.easeOut,
);
}
} else {
if (index == 0 &&
topSectionScrollControllers[topSectionCurrentIndex].hasClients) {
// If on the top section, scroll to the top of the tab based on the current top tab.
topSectionScrollControllers[topSectionCurrentIndex].animateTo(
0.0,
duration: duration,
curve: Curves.easeOut,
);
} else if (searchScrollController.hasClients) {
// If on the search tab and tapping the search tab, scroll to the top.
searchScrollController.animateTo(
0.0,
duration: duration,
curve: Curves.easeOut,
);
}
}
}
}
void dispose() {
_disposeReaction();
followedScrollController.dispose();
for (final controller in topSectionScrollControllers) {
controller.dispose();
}
}
}