Add update checking

This commit is contained in:
problematicconsumer
2023-07-27 18:03:41 +03:30
parent f9545df308
commit 429f1aadf0
13 changed files with 383 additions and 22 deletions

View File

@@ -2,8 +2,11 @@ import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:hiddify/core/core_providers.dart';
import 'package:hiddify/domain/constants.dart';
import 'package:hiddify/domain/failures.dart';
import 'package:hiddify/features/common/new_version_dialog.dart';
import 'package:hiddify/features/common/runtime_details.dart';
import 'package:hiddify/gen/assets.gen.dart';
import 'package:hiddify/utils/alerts.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:recase/recase.dart';
import 'package:url_launcher/url_launcher.dart';
@@ -14,7 +17,36 @@ class AboutPage extends HookConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final t = ref.watch(translationsProvider);
final details = ref.watch(runtimeDetailsNotifierProvider);
final appVersion = ref.watch(appVersionProvider);
final isCheckingForUpdate = ref.watch(
runtimeDetailsNotifierProvider.select(
(value) => value.maybeWhen(
data: (data) => data.latestVersion.isLoading,
orElse: () => false,
),
),
);
ref.listen(
runtimeDetailsNotifierProvider,
(_, next) async {
if (next case AsyncData(:final value)) {
switch (value.latestVersion) {
case AsyncError(:final error):
CustomToast.error(t.presentError(error)).show(context);
default:
if (value.newVersionAvailable) {
await NewVersionDialog(
value.appVersion,
value.latestVersion.value!,
canIgnore: false,
).show(context);
}
}
}
},
);
return Scaffold(
body: CustomScrollView(
@@ -22,7 +54,7 @@ class AboutPage extends HookConsumerWidget {
SliverAppBar(
title: Text(t.about.pageTitle.titleCase),
),
...switch (details) {
...switch (appVersion) {
AsyncData(:final value) => [
SliverToBoxAdapter(
child: Padding(
@@ -77,6 +109,18 @@ class AboutPage extends HookConsumerWidget {
),
ListTile(
title: Text(t.about.checkForUpdate.sentenceCase),
trailing: isCheckingForUpdate
? const SizedBox(
width: 24,
height: 24,
child: CircularProgressIndicator(),
)
: const Icon(Icons.update),
onTap: () async {
await ref
.read(runtimeDetailsNotifierProvider.notifier)
.checkForUpdates();
},
),
],
),

View File

@@ -0,0 +1,99 @@
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:go_router/go_router.dart';
import 'package:hiddify/core/core_providers.dart';
import 'package:hiddify/domain/app/app.dart';
import 'package:hiddify/domain/constants.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:recase/recase.dart';
import 'package:url_launcher/url_launcher.dart';
// TODO add release notes
class NewVersionDialog extends HookConsumerWidget {
const NewVersionDialog(
this.currentVersion,
this.newVersion, {
super.key,
this.canIgnore = true,
});
final InstalledVersionInfo currentVersion;
final RemoteVersionInfo newVersion;
final bool canIgnore;
Future<void> show(BuildContext context) {
return showDialog(
context: context,
builder: (context) => this,
);
}
@override
Widget build(BuildContext context, WidgetRef ref) {
final t = ref.watch(translationsProvider);
final theme = Theme.of(context);
return AlertDialog(
title: Text(t.appUpdate.dialogTitle.titleCase),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(t.appUpdate.updateMsg),
const Gap(8),
Text.rich(
TextSpan(
children: [
TextSpan(
text: "${t.appUpdate.currentVersionLbl}: ",
style: theme.textTheme.bodySmall,
),
TextSpan(
text: currentVersion.fullVersion,
style: theme.textTheme.labelMedium,
),
],
),
),
Text.rich(
TextSpan(
children: [
TextSpan(
text: "${t.appUpdate.newVersionLbl}: ",
style: theme.textTheme.bodySmall,
),
TextSpan(
text: newVersion.fullVersion,
style: theme.textTheme.labelMedium,
),
],
),
),
],
),
actions: [
if (canIgnore)
TextButton(
onPressed: () {
// TODO add prefs for ignoring version
context.pop();
},
child: Text(t.appUpdate.ignoreBtnTxt.titleCase),
),
TextButton(
onPressed: context.pop,
child: Text(t.appUpdate.laterBtnTxt.titleCase),
),
TextButton(
onPressed: () async {
await launchUrl(
Uri.parse(Constants.githubLatestReleaseUrl),
mode: LaunchMode.externalApplication,
);
},
child: Text(t.appUpdate.updateNowBtnTxt.titleCase),
),
],
);
}
}

View File

@@ -1,39 +1,78 @@
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:hiddify/data/data_providers.dart';
import 'package:hiddify/domain/app/app.dart';
import 'package:hiddify/utils/utils.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'runtime_details.freezed.dart';
part 'runtime_details.g.dart';
// TODO implement clash version
// TODO add clash version
@Riverpod(keepAlive: true)
class RuntimeDetailsNotifier extends _$RuntimeDetailsNotifier with AppLogger {
@override
Future<RuntimeDetails> build() async {
final packageInfo = await PackageInfo.fromPlatform();
return RuntimeDetails(
version: packageInfo.version,
buildNumber: packageInfo.buildNumber,
installerStore: packageInfo.installerStore,
clashVersion: "",
);
final appVersion = await ref
.watch(updateRepositoryProvider)
.getCurrentVersion()
.getOrElse((l) => throw l)
.run();
return RuntimeDetails(appVersion: appVersion);
}
Future<void> checkForUpdates() async {
if (state case AsyncData(:final value)) {
switch (value.latestVersion) {
case AsyncLoading():
return;
default:
loggy.debug("checking for updates");
state =
AsyncData(value.copyWith(latestVersion: const AsyncLoading()));
// TODO use prefs
const includePreReleases = true;
await ref
.read(updateRepositoryProvider)
.getLatestVersion(includePreReleases: includePreReleases)
.match(
(l) {
loggy.warning("failed to get latest version, $l");
state = AsyncData(
value.copyWith(
latestVersion: AsyncError(l, StackTrace.current),
),
);
},
(r) {
state = AsyncData(
value.copyWith(latestVersion: AsyncData(r)),
);
},
).run();
}
}
}
}
@Riverpod(keepAlive: true)
AsyncValue<InstalledVersionInfo> appVersion(AppVersionRef ref) => ref.watch(
runtimeDetailsNotifierProvider
.select((value) => value.whenData((value) => value.appVersion)),
);
@freezed
class RuntimeDetails with _$RuntimeDetails {
const RuntimeDetails._();
const factory RuntimeDetails({
required String version,
required String buildNumber,
String? installerStore,
required String clashVersion,
required InstalledVersionInfo appVersion,
@Default(AsyncData(null)) AsyncValue<RemoteVersionInfo?> latestVersion,
}) = _RuntimeDetails;
String get fullVersion => version + buildNumber;
factory RuntimeDetails.fromJson(Map<String, dynamic> json) =>
_$RuntimeDetailsFromJson(json);
bool get newVersionAvailable => latestVersion.maybeWhen(
data: (data) =>
data != null &&
data.fullVersion.compareTo(this.appVersion.fullVersion) > 0,
orElse: () => false,
);
}

View File

@@ -98,7 +98,7 @@ class AppVersionLabel extends HookConsumerWidget {
final theme = Theme.of(context);
final version = ref.watch(
runtimeDetailsNotifierProvider.select(
appVersionProvider.select(
(value) => switch (value) {
AsyncData(:final value) => value.fullVersion,
_ => "",