Skip to content

Commit

Permalink
Fixed issue #312 when multiple instances of UpgradeAlert or UpgradeCa…
Browse files Browse the repository at this point in the history
…rd were used and the stream had already been listened to.
  • Loading branch information
larryaasen committed Jul 26, 2023
1 parent db563d2 commit 6bd0a4b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 8.1.0-alpha.1

- Fixed issue #312 when multiple instances of UpgradeAlert or UpgradeCard were used and the stream had already been listened to.

## 8.0.0

- Added support for checking for updates every time the app resumes from the background. (#272)
Expand Down
29 changes: 29 additions & 0 deletions example/lib/main_multiple.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2023 Larry Aasen. All rights reserved.

import 'package:flutter/material.dart';
import 'package:upgrader/upgrader.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();

// Only call clearSavedSettings() during testing to reset internal values.
await Upgrader.clearSavedSettings(); // REMOVE this for release builds

runApp(MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Upgrader Example - Multiple',
home: UpgradeAlert(
child: Scaffold(
appBar: AppBar(title: Text('Upgrader Example - Multiple')),
body: Center(child: UpgradeAlert(child: Text('Checking...'))),
)),
);
}
}
1 change: 1 addition & 0 deletions lib/src/upgrade_alert.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class UpgradeAlert extends UpgradeBase {
}

return StreamBuilder(
initialData: state.widget.upgrader.evaluationReady,
stream: state.widget.upgrader.evaluationStream,
builder:
(BuildContext context, AsyncSnapshot<UpgraderEvaluateNeed> snapshot) {
Expand Down
1 change: 1 addition & 0 deletions lib/src/upgrade_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class UpgradeCard extends UpgradeBase {
}

return StreamBuilder(
initialData: state.widget.upgrader.evaluationReady,
stream: state.widget.upgrader.evaluationStream,
builder: (BuildContext context,
AsyncSnapshot<UpgraderEvaluateNeed> snapshot) {
Expand Down
13 changes: 10 additions & 3 deletions lib/src/upgrader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,14 @@ class Upgrader with WidgetsBindingObserver {
/// Track the initialization future so that [initialize] can be called multiple times.
Future<bool>? _futureInit;

/// A stream that provides a series of values each time an evaluation should be performed.
/// A stream that provides a new value each time an evaluation should be performed.
/// The values will always be null or true.
final _streamController = StreamController<UpgraderEvaluateNeed>();
Stream<UpgraderEvaluateNeed> get evaluationStream => _streamController.stream;
final _streamController = StreamController<UpgraderEvaluateNeed>.broadcast();

/// An evaluation should be performed.
bool get evaluationReady => _evaluationReady;
bool _evaluationReady = false;

final notInitializedExceptionMessage =
'initialize() not called. Must be called first.';
Expand Down Expand Up @@ -266,6 +270,8 @@ class Upgrader with WidgetsBindingObserver {
// Add an observer of application events.
WidgetsBinding.instance.addObserver(this);

_evaluationReady = true;

/// Trigger the stream to indicate an evaluation should be performed.
/// The value will always be true.
_streamController.add(true);
Expand Down Expand Up @@ -457,7 +463,8 @@ class Upgrader with WidgetsBindingObserver {
return msg;
}

/// Only called by [UpgradeAlert].
/// Will show the alert dialog when it should be dispalyed.
/// Only called by [UpgradeAlert] and not used by [UpgradeCard].
void checkVersion({required BuildContext context}) {
if (!_displayed) {
final shouldDisplay = shouldDisplayUpgrade();
Expand Down

0 comments on commit 6bd0a4b

Please sign in to comment.