Skip to content

Commit

Permalink
Add title and color support to OctopusDelegate
Browse files Browse the repository at this point in the history
  • Loading branch information
PlugFox committed Dec 22, 2023
1 parent c1db8e6 commit 282e50e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 37 deletions.
23 changes: 22 additions & 1 deletion lib/src/controller/delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:developer' as developer;

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:meta/meta.dart';
import 'package:octopus/src/controller/guard.dart';
import 'package:octopus/src/controller/octopus.dart';
Expand All @@ -26,7 +27,7 @@ typedef NotFoundBuilder = Widget Function(
/// {@nodoc}
@internal
final class OctopusDelegate extends RouterDelegate<OctopusState>
with ChangeNotifier {
with ChangeNotifier, _TitleMixin {
/// Octopus delegate.
/// {@nodoc}
OctopusDelegate({
Expand Down Expand Up @@ -57,6 +58,8 @@ final class OctopusDelegate extends RouterDelegate<OctopusState>
_setConfiguration(initialState);
// Clear extra storage when processing completed.
_$stateChangeQueue.addCompleteListener(_onIdleState);
// Update title & color
_updateTitle(routes[currentConfiguration.children.lastOrNull?.name]);
}

final _OctopusStateObserver _stateObserver;
Expand Down Expand Up @@ -385,6 +388,7 @@ final class OctopusDelegate extends RouterDelegate<OctopusState>
final result = StateUtil.normalize(newConfiguration);

if (_stateObserver._changeState(result)) {
_updateTitle(routes[result.children.lastOrNull?.name]);
notifyListeners(); // Notify listeners if the state changed
}
},
Expand All @@ -402,6 +406,23 @@ final class OctopusDelegate extends RouterDelegate<OctopusState>
}
}

mixin _TitleMixin {
String? _$lastTitle;
Color? _$lastColor;
// Update title & color
void _updateTitle(OctopusRoute? route) {
final title = route?.title;
final color = route?.color;
if (title == _$lastTitle && _$lastColor == color) return;
SystemChrome.setApplicationSwitcherDescription(
ApplicationSwitcherDescription(
label: _$lastTitle = title,
primaryColor: (_$lastColor = color)?.value,
),
).ignore();
}
}

/// Octopus state observer.
abstract interface class OctopusStateObserver
implements ValueListenable<OctopusState$Immutable> {
Expand Down
38 changes: 21 additions & 17 deletions lib/src/controller/information_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ class OctopusInformationProvider extends RouteInformationProvider
return;
} */

fine('routerReportsNewRouteInformation(${routeInformation.uri}, '
'${routeInformation.state})');

if (routeInformation is OctopusRouteInformation) {
if (routeInformation.intention == OctopusStateIntention.cancel) return;
if (routeInformation.intention == OctopusStateIntention.neglect) return;
Expand All @@ -103,24 +106,24 @@ class OctopusInformationProvider extends RouteInformationProvider
case OctopusRouteInformation info
when info.intention == OctopusStateIntention.navigate:
replace = false;
default:
switch (type) {
case RouteInformationReportingType.none:
if (_valueInEngine.uri == routeInformation.uri) {
if (identical(_valueInEngine.state, routeInformation.state)) {
replace = true;
} else {
final hashA = jenkinsHash(_valueInEngine.state);
final hashB = jenkinsHash(routeInformation.state);
if (hashA == hashB) replace = true;
}
}
if (replace) return; // Avoid adding a new history entry.
case RouteInformationReportingType.neglect:
replace = true;
case RouteInformationReportingType.navigate:
replace = false;
}

switch (type) {
case RouteInformationReportingType.none:
if (_valueInEngine.uri == routeInformation.uri) {
replace = true;
if (identical(_valueInEngine.state, routeInformation.state)) {
return;
} else {
final hashA = jenkinsHash(_valueInEngine.state);
final hashB = jenkinsHash(routeInformation.state);
if (hashA == hashB) return;
}
}
case RouteInformationReportingType.neglect:
replace = true;
case RouteInformationReportingType.navigate:
replace = false;
}

// If the route is different from the current route, then update the engine.
Expand Down Expand Up @@ -187,6 +190,7 @@ class OctopusInformationProvider extends RouteInformationProvider
// then handle it as a pop operation.
if (!routeInformation.uri.path.startsWith('/'))
return popUri(routeInformation);

_value = RouteInformation(
uri: uri,
state: null,
Expand Down
4 changes: 3 additions & 1 deletion lib/src/state/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -725,10 +725,12 @@ mixin OctopusRoute {
/// e.g. my-page
String get name;

// TODO(plugfox): implement title builder for active route
/// Title of this route.
String? get title => null;

/// Color of this route.
Color? get color => null;

/// Build [Widget] for this route using [BuildContext] and [OctopusNode].
///
/// Use [OctopusNode] to access current route information,
Expand Down
22 changes: 4 additions & 18 deletions lib/src/util/js/system_navigator_util_js.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,8 @@ import 'package:octopus/src/util/logs.dart';
@internal
void $pushState(Object? data, String? title, Uri? url) {
fine('pushState($url)');
//SystemNavigator.selectSingleEntryHistory();
/* SystemNavigator.selectMultiEntryHistory().whenComplete(() {
SystemNavigator.routeInformationUpdated(
uri: url,
state: data,
replace: false,
);
}); */
SystemNavigator.selectMultiEntryHistory().ignore();
//SystemNavigator.selectMultiEntryHistory().ignore();
SystemNavigator.selectSingleEntryHistory().ignore();
SystemNavigator.routeInformationUpdated(
uri: url,
state: data,
Expand All @@ -38,15 +31,8 @@ void $replaceState(
Uri? url,
) {
fine('replaceState($url)');
//SystemNavigator.selectSingleEntryHistory();
/* SystemNavigator.selectMultiEntryHistory().whenComplete(() {
SystemNavigator.routeInformationUpdated(
uri: url,
state: data,
replace: true,
);
}); */
SystemNavigator.selectMultiEntryHistory().ignore();
//SystemNavigator.selectMultiEntryHistory().ignore();
SystemNavigator.selectSingleEntryHistory().ignore();
SystemNavigator.routeInformationUpdated(
uri: url,
state: data,
Expand Down

0 comments on commit 282e50e

Please sign in to comment.