Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing different Upgrader instances #410

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 47 additions & 24 deletions lib/src/upgrade_alert.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,29 +90,53 @@ class UpgradeAlertState extends State<UpgradeAlert> {
/// Is the alert dialog being displayed right now?
bool displayed = false;

late Upgrader _upgrader;

@override
void initState() {
super.initState();
widget.upgrader.initialize();
_initUpgrader();
}

@override
void dispose() {
_upgrader.dispose();
super.dispose();
}

@override
void didUpdateWidget(covariant UpgradeAlert oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.upgrader.minAppVersion != oldWidget.upgrader.minAppVersion) {
oldWidget.upgrader.dispose();
_initUpgrader();
}
}

void _initUpgrader() {
_upgrader = widget.upgrader;
_upgrader.initialize();
}

/// Describes the part of the user interface represented by this widget.
@override
Widget build(BuildContext context) {
if (widget.upgrader.state.debugLogging) {
if (_upgrader.state.debugLogging) {
print('upgrader: build UpgradeAlert');
}

return StreamBuilder(
initialData: widget.upgrader.state,
stream: widget.upgrader.stateStream,
// The key is necessary until https://github.com/flutter/flutter/issues/64916 is fixed.
key: ValueKey(_upgrader.hashCode),
initialData: _upgrader.state,
stream: _upgrader.stateStream,
builder: (BuildContext context, AsyncSnapshot<UpgraderState> snapshot) {
if ((snapshot.connectionState == ConnectionState.waiting ||
snapshot.connectionState == ConnectionState.active) &&
snapshot.data != null) {
final upgraderState = snapshot.data!;
if (upgraderState.versionInfo != null) {
if (widget.upgrader.state.debugLogging) {
if (_upgrader.state.debugLogging) {
print("upgrader: need to evaluate version");
}

Expand All @@ -132,23 +156,23 @@ class UpgradeAlertState extends State<UpgradeAlert> {

/// Will show the alert dialog when it should be dispalyed.
void checkVersion({required BuildContext context}) {
final shouldDisplay = widget.upgrader.shouldDisplayUpgrade();
if (widget.upgrader.state.debugLogging) {
final shouldDisplay = _upgrader.shouldDisplayUpgrade();
if (_upgrader.state.debugLogging) {
print('upgrader: shouldDisplayReleaseNotes: $shouldDisplayReleaseNotes');
}
if (shouldDisplay) {
displayed = true;
final appMessages = widget.upgrader.determineMessages(context);
final appMessages = _upgrader.determineMessages(context);

Future.delayed(Duration.zero, () {
showTheDialog(
key: widget.dialogKey ?? const Key('upgrader_alert_dialog'),
// ignore: use_build_context_synchronously
context: context,
title: appMessages.message(UpgraderMessage.title),
message: widget.upgrader.body(appMessages),
message: _upgrader.body(appMessages),
releaseNotes:
shouldDisplayReleaseNotes ? widget.upgrader.releaseNotes : null,
shouldDisplayReleaseNotes ? _upgrader.releaseNotes : null,
barrierDismissible: widget.barrierDismissible,
messages: appMessages,
);
Expand All @@ -157,15 +181,15 @@ class UpgradeAlertState extends State<UpgradeAlert> {
}

void onUserIgnored(BuildContext context, bool shouldPop) {
if (widget.upgrader.state.debugLogging) {
if (_upgrader.state.debugLogging) {
print('upgrader: button tapped: ignore');
}

// If this callback has been provided, call it.
final doProcess = widget.onIgnore?.call() ?? true;

if (doProcess) {
widget.upgrader.saveIgnored();
_upgrader.saveIgnored();
}

if (shouldPop) {
Expand All @@ -174,7 +198,7 @@ class UpgradeAlertState extends State<UpgradeAlert> {
}

void onUserLater(BuildContext context, bool shouldPop) {
if (widget.upgrader.state.debugLogging) {
if (_upgrader.state.debugLogging) {
print('upgrader: button tapped: later');
}

Expand All @@ -187,15 +211,15 @@ class UpgradeAlertState extends State<UpgradeAlert> {
}

void onUserUpdated(BuildContext context, bool shouldPop) {
if (widget.upgrader.state.debugLogging) {
if (_upgrader.state.debugLogging) {
print('upgrader: button tapped: update now');
}

// If this callback has been provided, call it.
final doProcess = widget.onUpdate?.call() ?? true;

if (doProcess) {
widget.upgrader.sendUserToAppStore();
_upgrader.sendUserToAppStore();
}

if (shouldPop) {
Expand All @@ -209,8 +233,7 @@ class UpgradeAlertState extends State<UpgradeAlert> {
}

bool get shouldDisplayReleaseNotes =>
widget.showReleaseNotes &&
(widget.upgrader.releaseNotes?.isNotEmpty ?? false);
widget.showReleaseNotes && (_upgrader.releaseNotes?.isNotEmpty ?? false);

/// Show the alert dialog.
void showTheDialog({
Expand All @@ -222,14 +245,14 @@ class UpgradeAlertState extends State<UpgradeAlert> {
required bool barrierDismissible,
required UpgraderMessages messages,
}) {
if (widget.upgrader.state.debugLogging) {
if (_upgrader.state.debugLogging) {
print('upgrader: showTheDialog title: $title');
print('upgrader: showTheDialog message: $message');
print('upgrader: showTheDialog releaseNotes: $releaseNotes');
}

// Save the date/time as the last time alerted.
widget.upgrader.saveLastAlerted();
_upgrader.saveLastAlerted();

showDialog(
barrierDismissible: barrierDismissible,
Expand All @@ -238,7 +261,7 @@ class UpgradeAlertState extends State<UpgradeAlert> {
return PopScope(
canPop: onCanPop(),
onPopInvokedWithResult: (didPop, result) {
if (widget.upgrader.state.debugLogging) {
if (_upgrader.state.debugLogging) {
print('upgrader: showTheDialog onPopInvoked: $didPop');
}
},
Expand All @@ -259,12 +282,12 @@ class UpgradeAlertState extends State<UpgradeAlert> {
/// Determines if the dialog blocks the current route from being popped.
/// Will return the result from [shouldPopScope] if it is not null, otherwise it will return false.
bool onCanPop() {
if (widget.upgrader.state.debugLogging) {
if (_upgrader.state.debugLogging) {
print('upgrader: onCanPop called');
}
if (widget.shouldPopScope != null) {
final should = widget.shouldPopScope!();
if (widget.upgrader.state.debugLogging) {
if (_upgrader.state.debugLogging) {
print('upgrader: shouldPopScope=$should');
}
return should;
Expand All @@ -283,7 +306,7 @@ class UpgradeAlertState extends State<UpgradeAlert> {
UpgraderMessages messages) {
// If installed version is below minimum app version, or is a critical update,
// disable ignore and later buttons.
final isBlocked = widget.upgrader.blocked();
final isBlocked = _upgrader.blocked();
final showIgnore = isBlocked ? false : widget.showIgnore;
final showLater = isBlocked ? false : widget.showLater;

Expand Down Expand Up @@ -340,7 +363,7 @@ class UpgradeAlertState extends State<UpgradeAlert> {
cupertino: cupertino,
text: messages.message(UpgraderMessage.buttonTitleUpdate),
context: context,
onPressed: () => onUserUpdated(context, !widget.upgrader.blocked()),
onPressed: () => onUserUpdated(context, !_upgrader.blocked()),
isDefaultAction: true,
),
];
Expand Down
Loading