diff --git a/lib/main.dart b/lib/main.dart index 9e69cb73..275b3ba4 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -18,7 +18,7 @@ import 'package:provider/provider.dart'; import 'package:sentry_flutter/sentry_flutter.dart'; import 'package:shared_preferences/shared_preferences.dart'; -Future main() async { +void main() async { WidgetsFlutterBinding.ensureInitialized(); final prefs = await SharedPreferences.getInstance(); @@ -37,12 +37,14 @@ Future 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. diff --git a/lib/screens/home/home.dart b/lib/screens/home/home.dart index 24e71e8d..db0eaf3f 100644 --- a/lib/screens/home/home.dart +++ b/lib/screens/home/home.dart @@ -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(); + State createState() => _HomeState(); +} +class _HomeState extends State { + late final _authStore = context.read(); + + 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(); + } } diff --git a/lib/screens/home/stores/home_store.dart b/lib/screens/home/stores/home_store.dart index 96d63af8..7b88eaee 100644 --- a/lib/screens/home/stores/home_store.dart +++ b/lib/screens/home/stores/home_store.dart @@ -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(); }