From 2662f6173ebe46c92f39fbb590791dab134250be Mon Sep 17 00:00:00 2001 From: mahesh jamdade Date: Fri, 5 Jan 2024 09:13:23 +0530 Subject: [PATCH] fix ui not loading --- lib/base_home.dart | 171 ++++++++++---------- lib/controller/app_controller.dart | 89 ++++++---- lib/controller/dashboard_controller.dart | 11 ++ lib/main.dart | 14 +- lib/navbar/dashboard/dashboard.dart | 35 ++-- lib/navbar/profile/settings.dart | 9 +- lib/services/services/settings_service.dart | 13 -- lib/utils/app_utils.dart | 22 +++ lib/widgets/whats_new.dart | 6 +- 9 files changed, 212 insertions(+), 158 deletions(-) create mode 100644 lib/utils/app_utils.dart diff --git a/lib/base_home.dart b/lib/base_home.dart index bc96a5c..9a48b5f 100644 --- a/lib/base_home.dart +++ b/lib/base_home.dart @@ -71,7 +71,7 @@ class _AdaptiveLayoutState extends ConsumerState { } try { final appController = ref.read(appProvider); - + final appUtility = ref.read(appUtilityProvider); final packageInfo = await PackageInfo.fromPlatform(); final String appVersion = packageInfo.version; final int appBuildNumber = int.parse(packageInfo.buildNumber); @@ -84,9 +84,9 @@ class _AdaptiveLayoutState extends ConsumerState { await remoteConfig.fetchAndActivate(); final version = remoteConfig.getString('${Constants.VERSION_KEY}'); final buildNumber = remoteConfig.getInt('${Constants.BUILD_NUMBER_KEY}'); - - final oldVersion = appController.version.split(' ')[0]; - final oldBuildNumber = int.parse(appController.version.split(' ')[1]); + final storedVersion = appUtility.getVersion(); + final oldVersion = storedVersion.split(' ')[0]; + final oldBuildNumber = int.parse(storedVersion.split(' ')[1]); if (appVersion != version || buildNumber > appBuildNumber) { ref .read(appProvider.notifier) @@ -99,14 +99,15 @@ class _AdaptiveLayoutState extends ConsumerState { if (oldVersion != version || oldBuildNumber < buildNumber) { Navigate.push( context, - WhatsNew(), + WhatsNew( + showFullChangelog: false, + ), transitionType: TransitionType.btt, ); - ref.read(appProvider.notifier).copyWith(appController.copyWith( - showFAB: true, - extended: true, - hasUpdate: false, - version: '$appVersion $appBuildNumber')); + + appUtility.setAppVersion( + '$version $buildNumber', + ); } } } catch (_) { @@ -231,85 +232,89 @@ class _AdaptiveLayoutState extends ConsumerState { } } - return Scaffold( - resizeToAvoidBottomInset: false, - floatingActionButton: appController.hasUpdate ? null : _buildFab(), - body: Stack( - children: [ - NavbarRouter( - errorBuilder: (context) { - return const Center(child: Text('Error 404')); - }, - type: NavbarType.floating, - onBackButtonPressed: (isExiting) { - if (isExiting) { - newTime = DateTime.now(); - final int difference = newTime.difference(oldTime).inMilliseconds; - oldTime = newTime; - if (difference < 1000) { - hideToast(); - return isExiting; - } else { - showToast('Press again to exit'); - return false; - } - } else { - return isExiting; - } - }, - shouldPopToBaseRoute: true, - isDesktop: !SizeUtils.isMobile, - // destinationAnimationCurve: Curves.fastOutSlowIn, - destinationAnimationDuration: SizeUtils.isDesktop ? 0 : 0, - onCurrentTabClicked: () { - exploreController.scrollToIndex = 0; - }, - onChanged: (x) async { - ref.read(appProvider.notifier).copyWith(appController.copyWith( - index: x, showFAB: x < 2 && user!.isLoggedIn, extended: true)); + return AnimatedBuilder( + animation: dashboardController, + builder: (context, child) { + return Scaffold( + resizeToAvoidBottomInset: false, + floatingActionButton: appController.hasUpdate ? null : _buildFab(), + body: Stack( + children: [ + NavbarRouter( + errorBuilder: (context) { + return const Center(child: Text('Error 404')); + }, + type: NavbarType.floating, + onBackButtonPressed: (isExiting) { + if (isExiting) { + newTime = DateTime.now(); + final int difference = newTime.difference(oldTime).inMilliseconds; + oldTime = newTime; + if (difference < 1000) { + hideToast(); + return isExiting; + } else { + showToast('Press again to exit'); + return false; + } + } else { + return isExiting; + } + }, + shouldPopToBaseRoute: true, + isDesktop: !SizeUtils.isMobile, + // destinationAnimationCurve: Curves.fastOutSlowIn, + destinationAnimationDuration: SizeUtils.isDesktop ? 0 : 0, + onCurrentTabClicked: () { + exploreController.scrollToIndex = 0; + }, + onChanged: (x) async { + ref.read(appProvider.notifier).copyWith(appController.copyWith( + index: x, showFAB: x < 2 && user!.isLoggedIn, extended: true)); - /// Simulate DragGesture on pageView - final pageController = exploreController.pageController; - if (EXPLORE_INDEX == x && SizeUtils.isMobile) { - if (pageController.hasClients) { - if (exploreController.shouldShowScrollAnimation) { - Future.delayed(Duration(seconds: 3), () async { - if (NavbarNotifier.currentIndex == EXPLORE_INDEX) { - exploreController.showScrollAnimation(); + /// Simulate DragGesture on pageView + final pageController = exploreController.pageController; + if (EXPLORE_INDEX == x && SizeUtils.isMobile) { + if (pageController.hasClients) { + if (exploreController.shouldShowScrollAnimation) { + Future.delayed(Duration(seconds: 3), () async { + if (NavbarNotifier.currentIndex == EXPLORE_INDEX) { + exploreController.showScrollAnimation(); + } + }); + } } - }); - } - } - } - }, - decoration: FloatingNavbarDecoration( - height: kNavbarHeight * 1.2, - backgroundColor: SizeUtils.isDesktop - ? colorScheme.surfaceVariant - : colorScheme.scrim.withOpacity(0.2), - margin: EdgeInsets.zero, - showSelectedLabels: false, - borderRadius: BorderRadius.zero, - // backgroundColor: (colorScheme.surfaceVariant.withOpacity(0.4)), - ), - destinations: [ - for (int i = 0; i < items.length; i++) - DestinationRouter( - navbarItem: items[i], + } + }, + decoration: FloatingNavbarDecoration( + height: kNavbarHeight * 1.2, + backgroundColor: SizeUtils.isDesktop + ? colorScheme.surfaceVariant + : colorScheme.scrim.withOpacity(0.2), + margin: EdgeInsets.zero, + showSelectedLabels: false, + borderRadius: BorderRadius.zero, + // backgroundColor: (colorScheme.surfaceVariant.withOpacity(0.4)), + ), destinations: [ - for (int j = 0; j < _routes[i]!.keys.length; j++) - Destination( - route: _routes[i]!.keys.elementAt(j), - widget: _routes[i]!.values.elementAt(j), + for (int i = 0; i < items.length; i++) + DestinationRouter( + navbarItem: items[i], + destinations: [ + for (int j = 0; j < _routes[i]!.keys.length; j++) + Destination( + route: _routes[i]!.keys.elementAt(j), + widget: _routes[i]!.values.elementAt(j), + ), + ], + initialRoute: _routes[i]!.keys.elementAt(0), ), ], - initialRoute: _routes[i]!.keys.elementAt(0), ), - ], - ), - ], - ), - ); + ], + ), + ); + }); } } diff --git a/lib/controller/app_controller.dart b/lib/controller/app_controller.dart index 0997ed6..2a14e06 100644 --- a/lib/controller/app_controller.dart +++ b/lib/controller/app_controller.dart @@ -2,6 +2,7 @@ import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:vocabhub/services/services.dart'; @immutable class AppController { @@ -10,29 +11,30 @@ class AppController { final bool extended; final bool hasUpdate; final String version; - - AppController({ - this.index = 0, - this.showFAB = true, - this.extended = true, - this.hasUpdate = false, - this.version = '1.0.0 1', - }); - - AppController copyWith({ - int? index, - bool? showFAB, - bool? extended, - bool? hasUpdate, - String? version, - }) { + final String oldVersion; + + AppController( + {this.index = 0, + this.showFAB = true, + this.extended = true, + this.hasUpdate = false, + this.version = '1.0.0 1', + this.oldVersion = '1.0.0 1'}); + + AppController copyWith( + {int? index, + bool? showFAB, + bool? extended, + bool? hasUpdate, + String? version, + String? oldVersion}) { return AppController( - index: index ?? this.index, - showFAB: showFAB ?? this.showFAB, - extended: extended ?? this.extended, - hasUpdate: hasUpdate ?? this.hasUpdate, - version: version ?? this.version, - ); + index: index ?? this.index, + showFAB: showFAB ?? this.showFAB, + extended: extended ?? this.extended, + hasUpdate: hasUpdate ?? this.hasUpdate, + version: version ?? this.version, + oldVersion: oldVersion ?? this.oldVersion); } Map toMap() { @@ -43,17 +45,18 @@ class AppController { result.addAll({'extended': extended}); result.addAll({'hasUpdate': hasUpdate}); result.addAll({'version': version}); + result.addAll({'oldVersion': oldVersion}); return result; } factory AppController.fromMap(Map map) { return AppController( - index: map['index']?.toInt() ?? 0, - showFAB: map['showFAB'] ?? false, - extended: map['extended'] ?? false, - hasUpdate: map['hasUpdate'] ?? false, - version: map['version'] ?? '1.0.0 1', - ); + index: map['index']?.toInt() ?? 0, + showFAB: map['showFAB'] ?? false, + extended: map['extended'] ?? false, + hasUpdate: map['hasUpdate'] ?? false, + version: map['version'] ?? '1.0.0 1', + oldVersion: map['oldVersion'] ?? '1.0.0 1'); } String toJson() => json.encode(toMap()); @@ -62,7 +65,7 @@ class AppController { @override String toString() { - return 'AppController(index: $index, showFAB: $showFAB, extended: $extended, hasUpdate: $hasUpdate, version: $version)'; + return 'AppController(index: $index, showFAB: $showFAB, extended: $extended, hasUpdate: $hasUpdate, version: $version oldVersion: $oldVersion)'; } @override @@ -74,7 +77,8 @@ class AppController { other.showFAB == showFAB && other.extended == extended && other.hasUpdate == hasUpdate && - other.version == version; + other.version == version && + other.oldVersion == oldVersion; } @override @@ -83,13 +87,24 @@ class AppController { showFAB.hashCode ^ extended.hashCode ^ hasUpdate.hashCode ^ - version.hashCode; + version.hashCode ^ + oldVersion.hashCode; } } class AppNotifier extends StateNotifier { - AppNotifier(super.state); - + AppNotifier(this.ref) + : super(AppController( + extended: true, + index: 0, + showFAB: true, + hasUpdate: false, + oldVersion: "1.0.0 1", + version: "1.0.0 1")) { + final oldVer = ref.read(appUtilityProvider).getVersion(); + state.copyWith(oldVersion: oldVer); + } + Ref ref; void setIndex(int index) { state = state.copyWith(index: index); } @@ -110,12 +125,18 @@ class AppNotifier extends StateNotifier { state = state.copyWith(version: version); } + void setOldVersion(String oldVersion) { + state = state.copyWith(oldVersion: oldVersion); + ref.read(appUtilityProvider).setAppVersion(oldVersion); + } + void copyWith(AppController appController) { state = state.copyWith( index: appController.index, showFAB: appController.showFAB, extended: appController.extended, hasUpdate: appController.hasUpdate, - version: appController.version); + version: appController.version, + oldVersion: appController.oldVersion); } } diff --git a/lib/controller/dashboard_controller.dart b/lib/controller/dashboard_controller.dart index 36d9b21..fd0c6fe 100644 --- a/lib/controller/dashboard_controller.dart +++ b/lib/controller/dashboard_controller.dart @@ -4,6 +4,15 @@ import 'package:vocabhub/services/services.dart'; import 'package:vocabhub/services/services/service_base.dart'; class DashboardController extends ChangeNotifier with ServiceBase { + bool _isLoading = true; + + bool get isLoading => _isLoading; + + set isLoading(bool isLoading) { + _isLoading = isLoading; + notifyListeners(); + } + Word? _wordOfTheDay; late final DashboardService _dashboardService; final List _words = []; @@ -46,6 +55,7 @@ class DashboardController extends ChangeNotifier with ServiceBase { @override Future initService() async { try { + isLoading = true; if (_wordOfTheDay == null) { _wordOfTheDay = Word.init(); } @@ -53,6 +63,7 @@ class DashboardController extends ChangeNotifier with ServiceBase { await _dashboardService.initService(); _wordOfTheDay = await getLastPublishedWord(); words = await _dashboardService.getWords(); + isLoading = false; } catch (e) { rethrow; } diff --git a/lib/main.dart b/lib/main.dart index de8bf14..69e77e1 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -26,6 +26,7 @@ import 'package:vocabhub/services/appstate.dart'; import 'package:vocabhub/services/services.dart'; import 'package:vocabhub/themes/theme_utils.dart'; import 'package:vocabhub/themes/vocabtheme_controller.dart'; +import 'package:vocabhub/utils/app_utils.dart'; import 'package:vocabhub/utils/firebase_options.dart'; import 'package:vocabhub/utils/logger.dart'; import 'package:vocabhub/widgets/whats_new.dart'; @@ -37,13 +38,7 @@ final userNotifierProvider = Provider((ref) { }); final dashBoardNotifier = Provider((ref) => DashboardController()); -final appProvider = - StateNotifierProvider((ref) => AppNotifier(AppController( - extended: true, - index: 0, - showFAB: true, - hasUpdate: false, - ))); +final appProvider = StateNotifierProvider(AppNotifier.new); final appThemeProvider = StateNotifierProvider(VocabThemeNotifier.new); @@ -59,6 +54,11 @@ final themeUtilityProvider = Provider((ref) { return ThemeUtility(sharedPreferences: sharedPrefs); }); +final appUtilityProvider = Provider((ref) { + final sharedPrefs = ref.watch(sharedPreferencesProvider); + return AppUtility(sharedPreferences: sharedPrefs); +}); + Future main() async { WidgetsFlutterBinding.ensureInitialized(); final sharedPreferences = await SharedPreferences.getInstance(); diff --git a/lib/navbar/dashboard/dashboard.dart b/lib/navbar/dashboard/dashboard.dart index 32706e3..926b97f 100644 --- a/lib/navbar/dashboard/dashboard.dart +++ b/lib/navbar/dashboard/dashboard.dart @@ -103,21 +103,26 @@ class _DashboardState extends ConsumerState { errorMessage: response.message, ); } - return ResponsiveBuilder( - desktopBuilder: (context) => DashboardDesktop(), - mobileBuilder: (context) { - if (response.state == RequestState.active) { - return LoadingWidget(); - } - return RefreshIndicator(onRefresh: () async { - await publishWordOfTheDay(isRefresh: true); - }, child: DashboardMobile( - onRefresh: () async { - await publishWordOfTheDay(isRefresh: true); - }, - )); - }, - ); + return AnimatedBuilder( + animation: dashboardController, + builder: (context, child) { + return ResponsiveBuilder( + desktopBuilder: (context) => DashboardDesktop(), + mobileBuilder: (context) { + if (response.state == RequestState.active || + dashboardController.isLoading) { + return LoadingWidget(); + } + return RefreshIndicator(onRefresh: () async { + await publishWordOfTheDay(isRefresh: true); + }, child: DashboardMobile( + onRefresh: () async { + await publishWordOfTheDay(isRefresh: true); + }, + )); + }, + ); + }); })); } } diff --git a/lib/navbar/profile/settings.dart b/lib/navbar/profile/settings.dart index b9317a2..d023b64 100644 --- a/lib/navbar/profile/settings.dart +++ b/lib/navbar/profile/settings.dart @@ -103,8 +103,7 @@ class _SettingsPageMobileState extends ConsumerState { user.setUser(userNew); } }); - final appController = ref.read(appProvider); - + final oldVersion = ref.read(appProvider).oldVersion; return Material( color: Colors.transparent, child: ListView( @@ -127,10 +126,10 @@ class _SettingsPageMobileState extends ConsumerState { if (snapshot.data == null) return SizedBox.shrink(); final String appVersion = snapshot.data!.version; final int appBuildNumber = int.parse(snapshot.data!.buildNumber); - final oldVersion = appController.version.split(' ')[0]; - final oldBuildNumber = int.parse(appController.version.split(' ')[1]); + final version = oldVersion.split(' ')[0]; + final buildNumber = int.parse(oldVersion.split(' ')[1]); - if (appVersion != oldVersion || oldBuildNumber < appBuildNumber) { + if (appVersion != version || buildNumber < appBuildNumber) { return settingTile( 'Whats New', title: RichText( diff --git a/lib/services/services/settings_service.dart b/lib/services/services/settings_service.dart index 4a53ad4..a8d08c0 100644 --- a/lib/services/services/settings_service.dart +++ b/lib/services/services/settings_service.dart @@ -1,13 +1,9 @@ -import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:vocabhub/controller/explore_controller.dart'; import 'package:vocabhub/services/services/service_base.dart'; -import 'package:vocabhub/themes/vocab_theme.dart'; class SettingsService extends ServiceBase { late SharedPreferences _sharedPreferences; - final String kThemeKey = 'kThemeKey'; - final String kThemeSeedKey = 'kThemeSeedKey'; final String kRatedOnPlaystore = 'kRatedOnPlaystore'; final String kLastRatedDate = 'kLastRatedDate'; final String kOnboardedKey = 'kOnboardedKey'; @@ -60,15 +56,6 @@ class SettingsService extends ServiceBase { } } - void setThemeSeed(Color color) { - _sharedPreferences.setInt(kThemeSeedKey, color.value); - } - - Color getThemeSeed() { - final int color = _sharedPreferences.getInt(kThemeSeedKey) ?? VocabTheme.colorSeeds[1].value; - return Color(color); - } - Future setRatedOnPlaystore(bool value) async { await _sharedPreferences.setBool(kRatedOnPlaystore, value); diff --git a/lib/utils/app_utils.dart b/lib/utils/app_utils.dart new file mode 100644 index 0000000..1b9ba01 --- /dev/null +++ b/lib/utils/app_utils.dart @@ -0,0 +1,22 @@ +import 'package:shared_preferences/shared_preferences.dart'; + +class AppUtility { + AppUtility({ + required this.sharedPreferences, + }); + + final SharedPreferences sharedPreferences; + + final String kOldAppVersionKey = 'kOldAppVersionKey'; + + void setAppVersion(String value) { + sharedPreferences.setString(kOldAppVersionKey, value); + } + + /// Returns the last stored app version + /// This is used to check if the app version has changed + /// inorder to show the changelog + String getVersion() { + return sharedPreferences.getString(kOldAppVersionKey) ?? '1.0.0 1'; + } +} diff --git a/lib/widgets/whats_new.dart b/lib/widgets/whats_new.dart index 07d58c5..b6c4e2a 100644 --- a/lib/widgets/whats_new.dart +++ b/lib/widgets/whats_new.dart @@ -7,6 +7,10 @@ import 'package:vocabhub/utils/utils.dart'; import 'package:vocabhub/widgets/widgets.dart'; class WhatsNew extends StatefulWidget { + final bool showFullChangelog; + + WhatsNew({this.showFullChangelog = true}); + static const route = '/whats-new'; @override _WhatsNewState createState() => _WhatsNewState(); @@ -68,7 +72,7 @@ class _WhatsNewState extends State { return LoadingWidget(); } return ListView.builder( - itemCount: snapshot.data!.length, + itemCount: widget.showFullChangelog ? snapshot.data!.length : 1, itemBuilder: (context, index) { final release = snapshot.data![index]; return Column(