Fix home index reset on build & logout exception
This commit is contained in:
parent
2a2201b099
commit
892f249114
3 changed files with 40 additions and 12 deletions
|
|
@ -18,7 +18,7 @@ import 'package:provider/provider.dart';
|
|||
import 'package:sentry_flutter/sentry_flutter.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
Future<void> main() async {
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
|
|
@ -37,12 +37,14 @@ Future<void> main() async {
|
|||
// With the shared preferences instance, obtain the existing user settings if it exists.
|
||||
// If default settings don't exist, use an empty JSON string to use the default values.
|
||||
final userSettings = prefs.getString('settings') ?? '{}';
|
||||
|
||||
// Initialize a settings store from the settings JSON string.
|
||||
final settingsStore = SettingsStore.fromJson(jsonDecode(userSettings));
|
||||
|
||||
// Create a MobX reaction that will save the settings on disk every time they are changed.
|
||||
autorun((_) => prefs.setString('settings', jsonEncode(settingsStore)));
|
||||
|
||||
// Initialize Sentry for crash reporting if enabled.
|
||||
if (settingsStore.sendCrashLogs) await SentryFlutter.init((options) => options.tracesSampleRate = sampleRate);
|
||||
|
||||
/// Initialize API services with a common client.
|
||||
|
|
|
|||
|
|
@ -10,14 +10,20 @@ import 'package:frosty/screens/settings/settings.dart';
|
|||
import 'package:frosty/screens/settings/stores/settings_store.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class Home extends StatelessWidget {
|
||||
class Home extends StatefulWidget {
|
||||
const Home({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final homeStore = HomeStore();
|
||||
final authStore = context.read<AuthStore>();
|
||||
State<Home> createState() => _HomeState();
|
||||
}
|
||||
|
||||
class _HomeState extends State<Home> {
|
||||
late final _authStore = context.read<AuthStore>();
|
||||
|
||||
late final _homeStore = HomeStore(authStore: _authStore);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return GestureDetector(
|
||||
onTap: FocusScope.of(context).unfocus,
|
||||
child: Scaffold(
|
||||
|
|
@ -26,12 +32,12 @@ class Home extends StatelessWidget {
|
|||
title: Observer(
|
||||
builder: (_) {
|
||||
final titles = [
|
||||
if (authStore.isLoggedIn) 'Followed Streams',
|
||||
if (_authStore.isLoggedIn) 'Followed Streams',
|
||||
'Top',
|
||||
'Search',
|
||||
];
|
||||
|
||||
return Text(titles[homeStore.selectedIndex]);
|
||||
return Text(titles[_homeStore.selectedIndex]);
|
||||
},
|
||||
),
|
||||
actions: [
|
||||
|
|
@ -50,9 +56,9 @@ class Home extends StatelessWidget {
|
|||
body: SafeArea(
|
||||
child: Observer(
|
||||
builder: (_) => IndexedStack(
|
||||
index: homeStore.selectedIndex,
|
||||
index: _homeStore.selectedIndex,
|
||||
children: [
|
||||
if (authStore.isLoggedIn) const StreamsList(listType: ListType.followed),
|
||||
if (_authStore.isLoggedIn) const StreamsList(listType: ListType.followed),
|
||||
const TopSection(),
|
||||
const Search(),
|
||||
],
|
||||
|
|
@ -62,7 +68,7 @@ class Home extends StatelessWidget {
|
|||
bottomNavigationBar: Observer(
|
||||
builder: (_) => BottomNavigationBar(
|
||||
items: [
|
||||
if (authStore.isLoggedIn)
|
||||
if (_authStore.isLoggedIn)
|
||||
const BottomNavigationBarItem(
|
||||
icon: Icon(Icons.favorite),
|
||||
label: 'Followed',
|
||||
|
|
@ -76,11 +82,17 @@ class Home extends StatelessWidget {
|
|||
label: 'Search',
|
||||
),
|
||||
],
|
||||
currentIndex: homeStore.selectedIndex,
|
||||
onTap: homeStore.handleTap,
|
||||
currentIndex: _homeStore.selectedIndex,
|
||||
onTap: _homeStore.handleTap,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_homeStore.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,21 @@
|
|||
import 'package:frosty/core/auth/auth_store.dart';
|
||||
import 'package:mobx/mobx.dart';
|
||||
part 'home_store.g.dart';
|
||||
|
||||
class HomeStore = _HomeStoreBase with _$HomeStore;
|
||||
|
||||
abstract class _HomeStoreBase with Store {
|
||||
final AuthStore authStore;
|
||||
|
||||
late final ReactionDisposer _disposeReaction;
|
||||
|
||||
_HomeStoreBase({required this.authStore}) {
|
||||
_disposeReaction = reaction(
|
||||
(_) => authStore.isLoggedIn,
|
||||
(_) => _selectedIndex = 0,
|
||||
);
|
||||
}
|
||||
|
||||
@readonly
|
||||
var _selectedIndex = 0;
|
||||
|
||||
|
|
@ -13,4 +25,6 @@ abstract class _HomeStoreBase with Store {
|
|||
_selectedIndex = index;
|
||||
}
|
||||
}
|
||||
|
||||
void dispose() => _disposeReaction();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue