Skip to content

Commit

Permalink
create release repository to manage new release version
Browse files Browse the repository at this point in the history
  • Loading branch information
zenoxs committed May 5, 2022
1 parent c410e35 commit 7884e47
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 33 deletions.
41 changes: 18 additions & 23 deletions lib/guards/new_release_guard/bloc/new_release_cubit.dart
Original file line number Diff line number Diff line change
@@ -1,44 +1,39 @@
import 'package:appcenter_companion/repositories/repositories.dart';
import 'package:bloc/bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:version/version.dart';

part 'new_release_cubit.freezed.dart';

part 'new_release_state.dart';

class NewReleaseCubit extends Cubit<NewReleaseState> {
NewReleaseCubit(GitHubRepository gitHubRepository)
: _githubRepository = gitHubRepository,
NewReleaseCubit(ReleaseRepository releaseRepository)
: _releaseRepository = releaseRepository,
super(NewReleaseState.initial()) {
_checkForNewRelease();
}

final GitHubRepository _githubRepository;
final ReleaseRepository _releaseRepository;

void ignoreCurrentRelease({bool ignored = false}) {
emit(
state.maybeMap(
availableVersion: (value) =>
value.copyWith(currentReleaseIgnored: ignored),
orElse: () => state,
),
);
Future<void> ignoreCurrentRelease({bool ignored = false}) async {
final currentState = state;
if (currentState is NewReleaseStateAvailableVersion) {
await _releaseRepository.ignoreRelease(
version: currentState.version,
ignored: ignored,
);
emit(
currentState.copyWith(currentReleaseIgnored: ignored),
);
}
}

Future<void> _checkForNewRelease() async {
final latestRelease = await _githubRepository.getLatestRelease();
//final latestVersion = Version.parse(latestRelease.tagName);
final latestVersion = Version.parse('1.8.0');
final packageInfo = await PackageInfo.fromPlatform();
final currentVersion =
Version.parse('${packageInfo.version}+${packageInfo.buildNumber}');
if (currentVersion.compareTo(latestVersion) < 0) {
final newVersion = await _releaseRepository.newVersionAvailable();
if (newVersion != null) {
emit(
NewReleaseState.availableVersion(
version: latestRelease.tagName,
url: latestRelease.htmlUrl,
version: newVersion.version,
downloadUrl: newVersion.downloadUrl,
),
);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/guards/new_release_guard/bloc/new_release_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class NewReleaseState with _$NewReleaseState {

factory NewReleaseState.availableVersion({
required String version,
required String url,
required String downloadUrl,
@Default(false) bool currentReleaseIgnored,
}) = NewReleaseStateAvailableVersion;
}
2 changes: 1 addition & 1 deletion lib/guards/new_release_guard/new_release_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class NewReleaseDialog extends StatelessWidget {
FilledButton(
child: const Text('Update'),
onPressed: () {
launchUrlString(state.url);
launchUrlString(state.downloadUrl);
Navigator.of(context).pop();
},
),
Expand Down
4 changes: 2 additions & 2 deletions lib/guards/new_release_guard/new_release_guard.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:appcenter_companion/repositories/github_repository.dart';
import 'package:appcenter_companion/repositories/repositories.dart';
import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

Expand All @@ -15,7 +15,7 @@ class NewReleaseGuard extends StatefulWidget {
}

class _NewReleaseGuardState extends State<NewReleaseGuard> {
late final cubit = NewReleaseCubit(context.read<GitHubRepository>());
late final cubit = NewReleaseCubit(context.read<ReleaseRepository>());

@override
Widget build(BuildContext context) {
Expand Down
4 changes: 4 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,16 @@ Future<void> main() async {
final GitHubRepository gitHubRepository = GitHubRepository(
environment: environment,
);
final ReleaseRepository releaseRepository = ReleaseRepository(
githubRepository: gitHubRepository,
);

runApp(
MultiRepositoryProvider(
providers: [
RepositoryProvider.value(value: environment),
RepositoryProvider.value(value: store),
RepositoryProvider.value(value: releaseRepository),
RepositoryProvider.value(value: authenticationRepository),
RepositoryProvider.value(value: gitHubRepository),
RepositoryProvider.value(value: appcenterHttp),
Expand Down
8 changes: 4 additions & 4 deletions lib/repositories/authentication_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:fluent_ui/fluent_ui.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:rxdart/rxdart.dart';

import 'storage_repository.dart';
import 'secure_storage_repository.dart';

part 'authentication_repository.freezed.dart';
part 'authentication_repository.g.dart';
Expand Down Expand Up @@ -45,13 +45,13 @@ extension AuthenticationHelpers on AuthenticationState {
}

class AuthenticationRepository {
final StorageRepository _storage;
final SecureStorageRepository _storage;
final AppcenterHttp _http;

AuthenticationRepository({
StorageRepository? storage,
SecureStorageRepository? storage,
required AppcenterHttp http,
}) : _storage = storage ?? StorageRepository(),
}) : _storage = storage ?? SecureStorageRepository(),
_http = http {
_restoreAuth().then(
(value) =>
Expand Down
1 change: 1 addition & 0 deletions lib/repositories/models/models.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'new_release.dart';
11 changes: 11 additions & 0 deletions lib/repositories/models/new_release.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:freezed_annotation/freezed_annotation.dart';

part 'new_release.freezed.dart';

@freezed
class NewRelease with _$NewRelease {
const factory NewRelease({
required String version,
required String downloadUrl,
}) = _NewRelease;
}
62 changes: 62 additions & 0 deletions lib/repositories/release_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import 'package:appcenter_companion/repositories/github_repository.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:version/version.dart';

import 'models/new_release.dart';

class ReleaseRepository {
ReleaseRepository({
required GitHubRepository githubRepository,
}) : _githubRepository = githubRepository;

final GitHubRepository _githubRepository;

Future<SharedPreferences> get _sharedPreferences =>
SharedPreferences.getInstance();

Future<List<String>> get _ignoredVersions => _sharedPreferences
.then((prefs) => prefs.getStringList(_keyIgnoredVersions) ?? []);

Future<bool> _setIgnoredVersions(List<String> ignoredVersions) =>
_sharedPreferences.then(
(prefs) => prefs.setStringList(_keyIgnoredVersions, ignoredVersions),
);

final _keyIgnoredVersions = 'ignoredVersions';

Future<NewRelease?> newVersionAvailable() async {
final latestRelease = await _githubRepository.getLatestRelease();
final latestVersion = Version.parse(latestRelease.tagName);
final ignoredVersions = await _ignoredVersions;

if (ignoredVersions.contains(latestVersion.toString())) {
return null;
}

final packageInfo = await PackageInfo.fromPlatform();
final currentVersion =
Version.parse('${packageInfo.version}+${packageInfo.buildNumber}');
if (currentVersion.compareTo(latestVersion) < 0) {
return NewRelease(
version: latestVersion.toString(),
downloadUrl: latestRelease.htmlUrl,
);
}
return null;
}

Future<void> ignoreRelease({
bool ignored = true,
required String version,
}) async {
final ignoredVersions = await _ignoredVersions;
if (ignored && !ignoredVersions.contains(version)) {
ignoredVersions.add(version);
}
if (!ignored && ignoredVersions.contains(version)) {
ignoredVersions.remove(version);
}
await _setIgnoredVersions(ignoredVersions);
}
}
2 changes: 2 additions & 0 deletions lib/repositories/repositories.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ export 'bundled_application_repository.dart';
export 'dto/dto.dart';
export 'entities/entities.dart';
export 'github_repository.dart';
export 'models/models.dart';
export 'release_repository.dart';
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import 'dart:io';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:shared_preferences/shared_preferences.dart';

class StorageRepository {
StorageRepository() {
class SecureStorageRepository {
SecureStorageRepository() {
// Can't use secure storage on mac os if the app not signed, which will required a developer program account
_useSecureStorage = !Platform.isMacOS;
}
Expand Down

0 comments on commit 7884e47

Please sign in to comment.