Skip to content

Commit

Permalink
Add global ui reload function (setState) + fix flickering when switch…
Browse files Browse the repository at this point in the history
…ing screens
  • Loading branch information
Hedon-dev committed Feb 5, 2025
1 parent 4309edc commit 8420193
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
39 changes: 32 additions & 7 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ class ViewerAppState extends State<ViewerApp> with WidgetsBindingObserver {
bool updateAvailable = false;
UpdateManager updateManager = UpdateManager();

Future<bool> onboardingCompleted = sharedStorage
.getBool("general_onboarding_completed")
.then((value) => value ?? false);
Future<String> appearanceType = sharedStorage
.getString("appearance_launcher_appearance")
.then((value) => value ?? "Hedon haven");
Future<ThemeMode> themeMode = getThemeMode();

/// This controls whether the preview should be currently blocked
bool blockPreview = false;
int _selectedIndex = 0;
Expand All @@ -86,6 +94,9 @@ class ViewerAppState extends State<ViewerApp> with WidgetsBindingObserver {
@override
void initState() {
super.initState();

initGlobalSetState(setStateMain);

// Hide app preview by default
// The desktops don't support app preview hiding at an OS level
if (Platform.isAndroid || Platform.isIOS) {
Expand Down Expand Up @@ -170,15 +181,31 @@ class ViewerAppState extends State<ViewerApp> with WidgetsBindingObserver {
}

void setStateMain() {
logger.d("setState called from child");
logger.w("Global setState called");

// reload ui vars to force a true reload
onboardingCompleted = sharedStorage
.getBool("general_onboarding_completed")
.then((value) => value ?? false);
appearanceType = sharedStorage
.getString("appearance_launcher_appearance")
.then((value) => value ?? "Hedon haven");
themeMode = getThemeMode();

// Set current screen to home
_selectedIndex = 0;

// Clear navigation stack
materialAppKey.currentState?.popUntil((route) => route.isFirst);

setState(() {});
}

@override
Widget build(BuildContext context) {
return DynamicColorBuilder(builder: (lightColorScheme, darkColorScheme) {
return FutureBuilder<ThemeMode?>(
future: getThemeMode(),
future: themeMode,
builder: (context, snapshot) {
return MaterialApp(
title: "Hedon haven",
Expand All @@ -193,12 +220,11 @@ class ViewerAppState extends State<ViewerApp> with WidgetsBindingObserver {
primarySwatch: Colors.green,
brightness: Brightness.dark),
),
themeMode: snapshot.data ?? ThemeMode.dark,
themeMode: snapshot.data ?? ThemeMode.system,
navigatorKey: materialAppKey,
home: Stack(children: [
FutureBuilder<bool?>(
future:
sharedStorage.getBool("general_onboarding_completed"),
future: onboardingCompleted,
builder: (context, snapshotParent) {
// Don't show anything until the future is done
if (snapshotParent.connectionState ==
Expand All @@ -208,8 +234,7 @@ class ViewerAppState extends State<ViewerApp> with WidgetsBindingObserver {
return !snapshotParent.data!
? WelcomeScreen(setStateMain: setStateMain)
: FutureBuilder<String?>(
future: sharedStorage
.getString("appearance_launcher_appearance"),
future: appearanceType,
builder: (context, snapshot) {
// Don't show anything until the future is done
if (snapshot.connectionState ==
Expand Down
1 change: 1 addition & 0 deletions lib/ui/screens/settings/settings_appearance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class _AppearanceScreenState extends State<AppearanceScreen> {
onSelected: (value) async {
await sharedStorage.setString(
"appearance_theme_mode", value);
globalSetState();
setState(() {});
},
);
Expand Down
15 changes: 14 additions & 1 deletion lib/ui/screens/settings/settings_developer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ class DeveloperScreen extends StatelessWidget {
await setDefaultSettings(true);
PluginManager.discoverAndLoadPlugins();
showToast(
"All settings have been reset", context);
"All settings have been reset. Reloading UI in 2 seconds",
context);
await Future.delayed(const Duration(seconds: 2));
// Reload entire UI
globalSetState();
}),
ListTile(
leading: const Icon(Icons.storage),
Expand Down Expand Up @@ -150,6 +154,15 @@ class DeveloperScreen extends StatelessWidget {
showToast(
"Icon cache has been refreshed", context);
}),
ListTile(
leading: const Icon(Icons.widgets),
title: const Text("Reload entire UI"),
onTap: () async {
showToast(
"Reloading entire UI in 2 seconds", context);
await Future.delayed(const Duration(seconds: 2));
globalSetState();
}),
FutureBuilder<bool?>(
future:
sharedStorage.getBool("general_enable_logging"),
Expand Down
9 changes: 9 additions & 0 deletions lib/utils/global_vars.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ late Logger logger;
late PackageInfo packageInfo;
late http.Client client;

/// Global visual app reload. MUST be set from initGlobalSetState, NOT from the main initGlobalVars()
late void Function() globalSetState;

/// This stores the global setting of whether the preview should be hidden
bool hidePreview = true;

Expand All @@ -27,6 +30,12 @@ Future<void> initGlobalVars() async {
await initHttpClient();
}

// This function is not called by the main initGlobalVars as it has to be set
// from the UI part of main, not the startup part
Future<void> initGlobalSetState(void Function() function) async {
globalSetState = function;
}

Future<void> initSharedStorage() async {
sharedStorage = SharedPreferencesAsync();
}
Expand Down

0 comments on commit 8420193

Please sign in to comment.