Skip to content

Instantly share code, notes, and snippets.

@tsavo-at-pieces
Created June 9, 2023 20:10
Show Gist options
  • Save tsavo-at-pieces/5fef796bd5fdaf504e83186da1d22a47 to your computer and use it in GitHub Desktop.
Save tsavo-at-pieces/5fef796bd5fdaf504e83186da1d22a47 to your computer and use it in GitHub Desktop.
This Dart code defines a class for handling automatic updates in a Windows application, including checking for updates, downloading and installing them, and emitting events for update status. This code defines a AutoUpdater class that handles updates and events for updating, including error handling or checking. It also includes methods to set t…

Dart Auto Updater Class

Preview:
import 'dart:async';
import 'dart:io';

import 'package:squirrel_windows/src/logger.dart';
import 'package:squirrel_windows/src/squirrel_windows_api.dart';

class AutoUpdater {
  final StreamController<Error> _onError = StreamController<Error>.broadcast();
  final StreamController<void> _onCheckingForUpdate = StreamController<void>.broadcast();
  final StreamController<void> _onUpdateNotAvailable = StreamController<void>.broadcast();
  final StreamController<void> _onUpdateAvailable = StreamController<void>.broadcast();
  final StreamController<UpdateDownloadedEvent> _onUpdateDownloaded =
      StreamController<UpdateDownloadedEvent>.broadcast();

  bool updateAvailable = false;
  String? updateURL;

  Stream<Error> get onError => _onError.stream;

  Stream<void> get onCheckingForUpdate => _onCheckingForUpdate.stream;

  Stream<void> get onUpdateNotAvailable => _onUpdateNotAvailable.stream;

  Stream<void> get onUpdateAvailable => _onUpdateAvailable.stream;

  Stream<UpdateDownloadedEvent> get onUpdateDownloaded => _onUpdateDownloaded.stream;

  void quitAndInstall() {
    if (!updateAvailable) {
      _emitError(Error.safeToString('No update available, can\'t quit and install'));
      return;
    }
    processStart();
    exit(0);
  }

  String? getFeedURL() {
    return updateURL;
  }

  void setFeedURL(dynamic options) {
    String? newUpdateURL;
    if (options is Map) {
      if (options['url'] is String) {
        newUpdateURL = options['url'];
      } else {
        throw ArgumentError('Expected options object to contain a \'url\' string property in setFeedUrl call');
      }
    } else if (options is String) {
      newUpdateURL = options;
    } else {
      throw ArgumentError('Expected an options object with a \'url\' property to be provided');
    }
    updateURL = newUpdateURL;
  }

  void checkForUpdates() {
    final String? url = updateURL;
    if (url == null) {
      _emitError(Error.safeToString('Update URL is not set'));
      return;
    }
    if (!supported()) {
      _emitError(Error.safeToString('Can not find Squirrel'));
      return;
    }
    _onCheckingForUpdate.add(null);

    checkForUpdate(url, (error, update) {
      if (error != null) {
        logger.d(error);
        _emitError(error);
        return;
      }
      if (update == null) {
        logger.d('Update Not Available');
        _onUpdateNotAvailable.add(null);
        return;
      }

      updateAvailable = true;

      _onUpdateAvailable.add(null);

      updateApp(url, (error) {
        if (error != null) {
          logger.d(error);
          _emitError(error);
          return;
        }

        final releaseNotes = update['releaseNotes'];

        final version = update['version'];

        /// Date is not available on Windows, so fake it.
        final DateTime date = DateTime.now();
        logger.d(
            'Before  _onUpdateDownloaded.add(UpdateDownloadedEvent(releaseNotes, version, date, updateURL, quitAndInstall));');

        _onUpdateDownloaded.add(UpdateDownloadedEvent(releaseNotes, version, date, updateURL, quitAndInstall));

        logger.d(
            'After _onUpdateDownloaded.add(UpdateDownloadedEvent(releaseNotes, version, date, updateURL, quitAndInstall));');
      });
    });
  }

  void _emitError(dynamic error) {
    logger.d(error);
    _onError.add(Error.safeToString(error) as Error);
  }
}

class UpdateDownloadedEvent {
  final String? releaseNotes;
  final String? version;
  final DateTime date;
  final String? updateURL;
  final Function() quitAndInstall;

  UpdateDownloadedEvent(this.releaseNotes, this.version, this.date, this.updateURL, this.quitAndInstall);
}

final AutoUpdater autoUpdater = AutoUpdater();
Associated Context
Type Code Snippet ( .dart )
Associated Tags flutter AutoUpdater Class UpdateDownloadedEvent Object Stream Update checking Framework:Dart StreamController Interface flutter-animation Set URL Parameter Error Handling Dart SDK Update installation StreamController updateURL Method flutter-layout AutoUpdater class Squirrel Windows API Functionality testing Debugging Error handling UpdateDownloadedEvent class setFeedURL Function Dart programming language
💡 Smart Description This Dart code defines a class for handling automatic updates in a Windows application, including checking for updates, downloading and installing them, and emitting events for update status.
This code defines a AutoUpdater class that handles updates and events for updating, including error handling or checking. It also includes methods to set the URL of an object with options provided in SetFeedURL call
🔎 Suggested Searches Dart AutoUpdater class with Squirrel Windows API
StreamController usage in Dart AutoUpdater
How to check for updates in Dart using Squirrel Windows API
Dart AutoUpdater class with quitAndInstall method
UpdateDownloadedEvent class in Dart AutoUpdater
Squirrel Windows AutoUpdater class
StreamController and StreamController for UpdateDownloadedEvent in Squirrel windows API
How to use StreamController with SetFeedURL method eact
Using streamController onUpdateNotAvailable eventeact
Setting feed URL using Options object
Related Links https://dart.dev/guides/language/language-tour
https://dart.dev/guides/libraries
https://api.dart.dev/stable/2.14.0/dart-async/StreamController-class.html
https://dart.dev/tools/sdk
https://api.dart.dev/stable/2.14.0/dart-io/Process-class.html
https://github.com/Squirrel/Squirrel.Windows
https://api.dart.dev/stable/2.14.0/dart-io/exit.html
Related People Tsavo Knott, Nathan Courtney, Nathan Courtney
Sensitive Information No Sensitive Information Detected
Shareable Link https://tsavo.pieces.cloud/?p=3ccd4787f9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment