2022-06-29 01:06:33 -04:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:flutter/widgets.dart';
|
2022-08-31 14:11:45 -04:00
|
|
|
import 'package:frosty/screens/settings/stores/auth_store.dart';
|
2022-01-08 09:34:54 -05:00
|
|
|
import 'package:mobx/mobx.dart';
|
2022-05-12 04:58:03 -04:00
|
|
|
|
2022-01-08 09:34:54 -05:00
|
|
|
part 'home_store.g.dart';
|
|
|
|
|
|
2022-06-29 01:06:33 -04:00
|
|
|
/// The home store that handles scrolling and navigating between tabs on the home screen.
|
2022-05-12 04:58:03 -04:00
|
|
|
class HomeStore = HomeStoreBase with _$HomeStore;
|
2022-01-08 09:34:54 -05:00
|
|
|
|
2022-05-12 04:58:03 -04:00
|
|
|
abstract class HomeStoreBase with Store {
|
2022-01-25 22:06:01 -05:00
|
|
|
final AuthStore authStore;
|
|
|
|
|
|
2022-06-29 01:06:33 -04:00
|
|
|
/// 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(),
|
|
|
|
|
];
|
2022-01-25 22:06:01 -05:00
|
|
|
|
2022-06-29 06:48:52 -04:00
|
|
|
/// The scroll controller for controlling the scroll to top on the search section.
|
|
|
|
|
final searchScrollController = ScrollController();
|
|
|
|
|
|
2022-06-29 01:06:33 -04:00
|
|
|
/// 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;
|
2022-05-12 04:58:03 -04:00
|
|
|
HomeStoreBase({required this.authStore}) {
|
2022-06-29 01:06:33 -04:00
|
|
|
// 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.
|
2022-01-25 22:06:01 -05:00
|
|
|
_disposeReaction = reaction(
|
|
|
|
|
(_) => authStore.isLoggedIn,
|
|
|
|
|
(_) => _selectedIndex = 0,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-08 09:34:54 -05:00
|
|
|
@action
|
|
|
|
|
void handleTap(int index) {
|
2022-06-29 01:06:33 -04:00
|
|
|
// If tapping a different tab, set the new index to show.
|
2022-01-08 09:34:54 -05:00
|
|
|
if (index != _selectedIndex) {
|
|
|
|
|
_selectedIndex = index;
|
2022-06-29 01:06:33 -04:00
|
|
|
} else {
|
2023-10-22 18:38:32 -04:00
|
|
|
const duration = Duration(milliseconds: 300);
|
2022-06-29 06:48:52 -04:00
|
|
|
|
|
|
|
|
// Use different logic if logged in/out since there will be one less tab when logged out.
|
|
|
|
|
if (authStore.isLoggedIn) {
|
2023-01-18 05:34:54 -05:00
|
|
|
if (index == 0 && followedScrollController.hasClients) {
|
2022-06-29 06:48:52 -04:00
|
|
|
// If on the followed tab and tapping the followed tab, scroll to the top.
|
|
|
|
|
followedScrollController.animateTo(
|
|
|
|
|
0.0,
|
|
|
|
|
duration: duration,
|
2023-01-18 05:34:54 -05:00
|
|
|
curve: Curves.easeOut,
|
2022-06-29 06:48:52 -04:00
|
|
|
);
|
2023-04-29 11:34:28 -04:00
|
|
|
} else if (index == 1 &&
|
|
|
|
|
topSectionScrollControllers[topSectionCurrentIndex].hasClients) {
|
2022-06-29 06:48:52 -04:00
|
|
|
// 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,
|
2023-01-18 05:34:54 -05:00
|
|
|
curve: Curves.easeOut,
|
2022-06-29 06:48:52 -04:00
|
|
|
);
|
2023-01-18 05:34:54 -05:00
|
|
|
} else if (searchScrollController.hasClients) {
|
2022-06-29 06:48:52 -04:00
|
|
|
// If on the search tab and tapping the search tab, scroll to the top.
|
|
|
|
|
searchScrollController.animateTo(
|
|
|
|
|
0.0,
|
|
|
|
|
duration: duration,
|
2023-01-18 05:34:54 -05:00
|
|
|
curve: Curves.easeOut,
|
2022-06-29 06:48:52 -04:00
|
|
|
);
|
|
|
|
|
}
|
2022-06-29 01:06:33 -04:00
|
|
|
} else {
|
2023-04-29 11:34:28 -04:00
|
|
|
if (index == 0 &&
|
|
|
|
|
topSectionScrollControllers[topSectionCurrentIndex].hasClients) {
|
2022-06-29 06:48:52 -04:00
|
|
|
// 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,
|
2023-01-18 05:34:54 -05:00
|
|
|
curve: Curves.easeOut,
|
2022-06-29 06:48:52 -04:00
|
|
|
);
|
2023-01-18 05:34:54 -05:00
|
|
|
} else if (searchScrollController.hasClients) {
|
2022-06-29 06:48:52 -04:00
|
|
|
// If on the search tab and tapping the search tab, scroll to the top.
|
|
|
|
|
searchScrollController.animateTo(
|
|
|
|
|
0.0,
|
|
|
|
|
duration: duration,
|
2023-01-18 05:34:54 -05:00
|
|
|
curve: Curves.easeOut,
|
2022-06-29 06:48:52 -04:00
|
|
|
);
|
|
|
|
|
}
|
2022-06-29 01:06:33 -04:00
|
|
|
}
|
2022-01-08 09:34:54 -05:00
|
|
|
}
|
|
|
|
|
}
|
2022-01-25 22:06:01 -05:00
|
|
|
|
2022-06-29 01:06:33 -04:00
|
|
|
void dispose() {
|
|
|
|
|
_disposeReaction();
|
|
|
|
|
|
|
|
|
|
followedScrollController.dispose();
|
|
|
|
|
|
|
|
|
|
for (final controller in topSectionScrollControllers) {
|
|
|
|
|
controller.dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-08 09:34:54 -05:00
|
|
|
}
|