Skip to content

Commit

Permalink
#1008 opening app from launcher shows home page only when exited by b…
Browse files Browse the repository at this point in the history
…ack button
  • Loading branch information
deckerst committed Jun 8, 2024
1 parent 1578d2d commit 27cf1bf
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 81 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file.

### Changed

- opening app from launcher shows home page only when exited by back button
- Screen saver: black background, consistent with slideshow
- upgraded Flutter to stable v3.22.2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,10 @@ open class MainActivity : FlutterFragmentActivity() {
open fun extractIntentData(intent: Intent?): FieldMap {
when (val action = intent?.action) {
Intent.ACTION_MAIN -> {
val fields = hashMapOf<String, Any?>(
INTENT_DATA_KEY_LAUNCHER to intent.hasCategory(Intent.CATEGORY_LAUNCHER),
INTENT_DATA_KEY_SAFE_MODE to intent.getBooleanExtra(EXTRA_KEY_SAFE_MODE, false),
)
val fields = HashMap<String, Any?>()
if (intent.getBooleanExtra(EXTRA_KEY_SAFE_MODE, false)) {
fields[INTENT_DATA_KEY_SAFE_MODE] = true
}
intent.getStringExtra(EXTRA_KEY_PAGE)?.let { page ->
val filters = extractFiltersFromIntent(intent)
fields[INTENT_DATA_KEY_PAGE] = page
Expand Down Expand Up @@ -497,7 +497,6 @@ open class MainActivity : FlutterFragmentActivity() {
const val INTENT_DATA_KEY_ACTION = "action"
const val INTENT_DATA_KEY_ALLOW_MULTIPLE = "allowMultiple"
const val INTENT_DATA_KEY_FILTERS = "filters"
const val INTENT_DATA_KEY_LAUNCHER = "launcher"
const val INTENT_DATA_KEY_MIME_TYPE = "mimeType"
const val INTENT_DATA_KEY_PAGE = "page"
const val INTENT_DATA_KEY_QUERY = "query"
Expand Down
174 changes: 98 additions & 76 deletions lib/widgets/aves_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import 'package:aves/theme/themes.dart';
import 'package:aves/widgets/collection/collection_grid.dart';
import 'package:aves/widgets/collection/collection_page.dart';
import 'package:aves/widgets/common/basic/scaffold.dart';
import 'package:aves/widgets/common/behaviour/pop/scope.dart';
import 'package:aves/widgets/common/behaviour/route_tracker.dart';
import 'package:aves/widgets/common/behaviour/routes.dart';
import 'package:aves/widgets/common/extensions/build_context.dart';
Expand Down Expand Up @@ -178,6 +179,7 @@ class _AvesAppState extends State<AvesApp> with WidgetsBindingObserver {
static const defaultPageTransitionsBuilder = FadeUpwardsPageTransitionsBuilder();
static final GlobalKey<NavigatorState> _navigatorKey = GlobalKey(debugLabel: 'app-navigator');
static ScreenBrightness? _screenBrightness;
static bool _exitedMainByPop = false;

@override
void initState() {
Expand Down Expand Up @@ -224,83 +226,91 @@ class _AvesAppState extends State<AvesApp> with WidgetsBindingObserver {
DurationsProvider(),
HighlightInfoProvider(),
],
child: OverlaySupport(
child: FutureBuilder<void>(
future: _appSetup,
builder: (context, snapshot) {
final initialized = !snapshot.hasError && snapshot.connectionState == ConnectionState.done;
if (initialized) {
AvesApp.showSystemUI();
}
final home = initialized
? _getFirstPage(intentData: widget.debugIntentData)
: AvesScaffold(
body: snapshot.hasError ? _buildError(snapshot.error!) : const SizedBox(),
);
return Selector<Settings, (Locale?, AvesThemeBrightness, bool)>(
selector: (context, s) => (
s.locale,
s.initialized ? s.themeBrightness : SettingsDefaults.themeBrightness,
s.initialized ? s.enableDynamicColor : SettingsDefaults.enableDynamicColor,
),
builder: (context, s, child) {
final (settingsLocale, themeBrightness, enableDynamicColor) = s;
return DynamicColorBuilder(
builder: (lightScheme, darkScheme) {
const defaultAccent = AvesColorsData.defaultAccent;
Color lightAccent = defaultAccent, darkAccent = defaultAccent;
if (enableDynamicColor) {
lightAccent = lightScheme?.primary ?? lightAccent;
darkAccent = darkScheme?.primary ?? darkAccent;
}
final lightTheme = Themes.lightTheme(lightAccent, initialized);
final darkTheme = themeBrightness == AvesThemeBrightness.black ? Themes.blackTheme(darkAccent, initialized) : Themes.darkTheme(darkAccent, initialized);
return Shortcuts(
shortcuts: {
// handle Android TV remote `select` button (KEYCODE_DPAD_CENTER)
// the following keys are already handled by default:
// KEYCODE_ENTER, KEYCODE_BUTTON_A, KEYCODE_NUMPAD_ENTER
LogicalKeySet(LogicalKeyboardKey.select): const ActivateIntent(),
},
child: Builder(
builder: (context) {
return MediaQuery(
data: MediaQuery.of(context).copyWith(
// disable accessible navigation, as it impacts snack bar action timer
// for all users of apps registered as accessibility services,
// even though they are not for accessibility purposes (like TalkBack is)
accessibleNavigation: false,
),
child: MaterialApp(
navigatorKey: _navigatorKey,
home: home,
navigatorObservers: _navigatorObservers,
builder: (context, child) => _decorateAppChild(
context: context,
initialized: initialized,
child: child,
),
onGenerateTitle: (context) => context.l10n.appName,
theme: lightTheme,
darkTheme: darkTheme,
themeMode: themeBrightness.appThemeMode,
locale: settingsLocale,
localizationsDelegates: const [
...AppLocalizations.localizationsDelegates,
...LocalizationsNn.delegates,
],
supportedLocales: AvesApp.supportedLocales,
scrollBehavior: AvesScrollBehavior(),
),
);
},
),
child: NotificationListener<PopExitNotification>(
onNotification: (notification) {
if (_appModeNotifier.value == AppMode.main) {
_exitedMainByPop = true;
}
return true;
},
child: OverlaySupport(
child: FutureBuilder<void>(
future: _appSetup,
builder: (context, snapshot) {
final initialized = !snapshot.hasError && snapshot.connectionState == ConnectionState.done;
if (initialized) {
AvesApp.showSystemUI();
}
final home = initialized
? _getFirstPage(intentData: widget.debugIntentData)
: AvesScaffold(
body: snapshot.hasError ? _buildError(snapshot.error!) : const SizedBox(),
);
},
);
},
);
},
return Selector<Settings, (Locale?, AvesThemeBrightness, bool)>(
selector: (context, s) => (
s.locale,
s.initialized ? s.themeBrightness : SettingsDefaults.themeBrightness,
s.initialized ? s.enableDynamicColor : SettingsDefaults.enableDynamicColor,
),
builder: (context, s, child) {
final (settingsLocale, themeBrightness, enableDynamicColor) = s;
return DynamicColorBuilder(
builder: (lightScheme, darkScheme) {
const defaultAccent = AvesColorsData.defaultAccent;
Color lightAccent = defaultAccent, darkAccent = defaultAccent;
if (enableDynamicColor) {
lightAccent = lightScheme?.primary ?? lightAccent;
darkAccent = darkScheme?.primary ?? darkAccent;
}
final lightTheme = Themes.lightTheme(lightAccent, initialized);
final darkTheme = themeBrightness == AvesThemeBrightness.black ? Themes.blackTheme(darkAccent, initialized) : Themes.darkTheme(darkAccent, initialized);
return Shortcuts(
shortcuts: {
// handle Android TV remote `select` button (KEYCODE_DPAD_CENTER)
// the following keys are already handled by default:
// KEYCODE_ENTER, KEYCODE_BUTTON_A, KEYCODE_NUMPAD_ENTER
LogicalKeySet(LogicalKeyboardKey.select): const ActivateIntent(),
},
child: Builder(
builder: (context) {
return MediaQuery(
data: MediaQuery.of(context).copyWith(
// disable accessible navigation, as it impacts snack bar action timer
// for all users of apps registered as accessibility services,
// even though they are not for accessibility purposes (like TalkBack is)
accessibleNavigation: false,
),
child: MaterialApp(
navigatorKey: _navigatorKey,
home: home,
navigatorObservers: _navigatorObservers,
builder: (context, child) => _decorateAppChild(
context: context,
initialized: initialized,
child: child,
),
onGenerateTitle: (context) => context.l10n.appName,
theme: lightTheme,
darkTheme: darkTheme,
themeMode: themeBrightness.appThemeMode,
locale: settingsLocale,
localizationsDelegates: const [
...AppLocalizations.localizationsDelegates,
...LocalizationsNn.delegates,
],
supportedLocales: AvesApp.supportedLocales,
scrollBehavior: AvesScrollBehavior(),
),
);
},
),
);
},
);
},
);
},
),
),
),
);
Expand Down Expand Up @@ -615,6 +625,18 @@ class _AvesAppState extends State<AvesApp> with WidgetsBindingObserver {

void _onNewIntent(Map? intentData) {
reportService.log('New intent data=$intentData');

if (_appModeNotifier.value == AppMode.main) {
// do not reset when relaunching the app, except when exiting by pop
final shouldReset = _exitedMainByPop;
_exitedMainByPop = false;

if (!shouldReset && (intentData ?? {}).isEmpty) {
reportService.log('Relaunch');
return;
}
}

_navigatorKey.currentState!.pushReplacement(DirectMaterialPageRoute(
settings: const RouteSettings(name: HomePage.routeName),
builder: (_) => _getFirstPage(intentData: intentData),
Expand Down
5 changes: 5 additions & 0 deletions lib/widgets/common/behaviour/pop/scope.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:aves/services/common/services.dart';
import 'package:aves/widgets/aves_app.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';

Expand Down Expand Up @@ -28,6 +29,7 @@ class AvesPopScope extends StatelessWidget {
} else {
// exit
reportService.log('Exit by pop');
PopExitNotification().dispatch(context);
SystemNavigator.pop();
}
}
Expand All @@ -36,3 +38,6 @@ class AvesPopScope extends StatelessWidget {
);
}
}

@immutable
class PopExitNotification extends Notification {}

0 comments on commit 27cf1bf

Please sign in to comment.