Add basic flavors

This commit is contained in:
problematicconsumer
2023-09-12 15:22:58 +03:30
parent ea81be3763
commit f1b0f8ee4b
24 changed files with 271 additions and 293 deletions

View File

@@ -3,8 +3,8 @@ 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/common.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/services/service_providers.dart';
import 'package:hiddify/utils/utils.dart';
@@ -16,33 +16,22 @@ class AboutPage extends HookConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final t = ref.watch(translationsProvider);
final appVersion = ref.watch(appVersionProvider);
final isCheckingForUpdate = ref.watch(
runtimeDetailsNotifierProvider.select(
(value) => value.maybeWhen(
data: (data) => data.latestVersion.isLoading,
orElse: () => false,
),
),
);
final appInfo = ref.watch(appInfoProvider);
final appUpdate = ref.watch(appUpdateNotifierProvider);
ref.listen(
runtimeDetailsNotifierProvider,
appUpdateNotifierProvider,
(_, next) async {
if (next case AsyncData(:final value)) {
switch (value.latestVersion) {
case AsyncError(:final error):
CustomToast.error(t.printError(error)).show(context);
default:
if (value.newVersionAvailable) {
await NewVersionDialog(
value.appVersion,
value.latestVersion.value!,
canIgnore: false,
).show(context);
}
}
switch (next) {
case AsyncData(value: final remoteVersion?):
await NewVersionDialog(
appInfo.version,
remoteVersion,
canIgnore: false,
).show(context);
case AsyncError(:final error):
if (!context.mounted) return;
CustomToast.error(t.printError(error)).show(context);
}
},
);
@@ -53,86 +42,77 @@ class AboutPage extends HookConsumerWidget {
SliverAppBar(
title: Text(t.about.pageTitle),
),
...switch (appVersion) {
AsyncData(:final value) => [
SliverToBoxAdapter(
child: Padding(
padding: const EdgeInsets.all(16),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Assets.images.logo.svg(width: 64, height: 64),
const Gap(16),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
t.general.appTitle,
style: Theme.of(context).textTheme.titleLarge,
),
const Gap(4),
Text(
"${t.about.version} ${value.version} ${value.buildNumber}",
),
],
),
],
),
),
),
SliverList(
delegate: SliverChildListDelegate(
[
ListTile(
title: Text(t.about.sourceCode),
trailing: const Icon(Icons.open_in_new),
onTap: () async {
await UriUtils.tryLaunch(
Uri.parse(Constants.githubUrl),
);
},
SliverToBoxAdapter(
child: Padding(
padding: const EdgeInsets.all(16),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Assets.images.logo.svg(width: 64, height: 64),
const Gap(16),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
t.general.appTitle,
style: Theme.of(context).textTheme.titleLarge,
),
ListTile(
title: Text(t.about.telegramChannel),
trailing: const Icon(Icons.open_in_new),
onTap: () async {
await UriUtils.tryLaunch(
Uri.parse(Constants.telegramChannelUrl),
);
},
),
ListTile(
title: Text(t.about.checkForUpdate),
trailing: isCheckingForUpdate
? const SizedBox(
width: 24,
height: 24,
child: CircularProgressIndicator(),
)
: const Icon(Icons.update),
onTap: () async {
await ref
.read(runtimeDetailsNotifierProvider.notifier)
.checkForUpdates();
},
),
ListTile(
title: Text(t.settings.general.openWorkingDir),
trailing: const Icon(Icons.arrow_outward_outlined),
onTap: () async {
final path = ref
.read(filesEditorServiceProvider)
.workingDir
.uri;
await UriUtils.tryLaunch(path);
},
const Gap(4),
Text(
"${t.about.version} ${appInfo.version}",
),
],
),
],
),
),
),
SliverList(
delegate: SliverChildListDelegate(
[
ListTile(
title: Text(t.about.sourceCode),
trailing: const Icon(Icons.open_in_new),
onTap: () async {
await UriUtils.tryLaunch(
Uri.parse(Constants.githubUrl),
);
},
),
ListTile(
title: Text(t.about.telegramChannel),
trailing: const Icon(Icons.open_in_new),
onTap: () async {
await UriUtils.tryLaunch(
Uri.parse(Constants.telegramChannelUrl),
);
},
),
ListTile(
title: Text(t.about.checkForUpdate),
trailing: appUpdate.isLoading
? const SizedBox(
width: 24,
height: 24,
child: CircularProgressIndicator(),
)
: const Icon(Icons.update),
onTap: () {
ref.invalidate(appUpdateNotifierProvider);
},
),
ListTile(
title: Text(t.settings.general.openWorkingDir),
trailing: const Icon(Icons.arrow_outward_outlined),
onTap: () async {
final path =
ref.read(filesEditorServiceProvider).workingDir.uri;
await UriUtils.tryLaunch(path);
},
),
],
_ => [],
},
),
),
],
),
);

View File

@@ -0,0 +1,33 @@
import 'package:hiddify/core/core_providers.dart';
import 'package:hiddify/data/data_providers.dart';
import 'package:hiddify/domain/app/app.dart';
import 'package:hiddify/utils/utils.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'app_update_notifier.g.dart';
@Riverpod(keepAlive: true)
class AppUpdateNotifier extends _$AppUpdateNotifier with AppLogger {
@override
Future<RemoteVersionInfo?> build() async {
loggy.debug("checking for update");
final currentVersion = ref.watch(appInfoProvider).version;
return ref
.watch(appRepositoryProvider)
.getLatestVersion(includePreReleases: true)
.match(
(l) {
loggy.warning("failed to get latest version, $l");
throw l;
},
(remote) {
if (remote.version.compareTo(currentVersion) > 0) {
loggy.info("new version available: $remote");
return remote;
}
loggy.info("already using latest version[$currentVersion], remote: $remote");
return null;
},
).run();
}
}

View File

@@ -1,5 +1,5 @@
export 'app_update_notifier.dart';
export 'confirmation_dialogs.dart';
export 'custom_app_bar.dart';
export 'profile_tile.dart';
export 'qr_code_scanner_screen.dart';
export 'runtime_details.dart';

View File

@@ -16,7 +16,7 @@ class NewVersionDialog extends HookConsumerWidget {
this.canIgnore = true,
});
final InstalledVersionInfo currentVersion;
final String currentVersion;
final RemoteVersionInfo newVersion;
final bool canIgnore;
@@ -48,7 +48,7 @@ class NewVersionDialog extends HookConsumerWidget {
style: theme.textTheme.bodySmall,
),
TextSpan(
text: currentVersion.fullVersion,
text: currentVersion,
style: theme.textTheme.labelMedium,
),
],

View File

@@ -1,79 +0,0 @@
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:riverpod_annotation/riverpod_annotation.dart';
part 'runtime_details.freezed.dart';
part 'runtime_details.g.dart';
// TODO add clash version
@Riverpod(keepAlive: true)
class RuntimeDetailsNotifier extends _$RuntimeDetailsNotifier with AppLogger {
@override
Future<RuntimeDetails> build() async {
loggy.debug("initializing");
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 InstalledVersionInfo appVersion,
@Default(AsyncData(null)) AsyncValue<RemoteVersionInfo?> latestVersion,
}) = _RuntimeDetails;
bool get newVersionAvailable => latestVersion.maybeWhen(
data: (data) =>
data != null &&
data.fullVersion.compareTo(this.appVersion.fullVersion) > 0,
orElse: () => false,
);
}

View File

@@ -1,7 +1,9 @@
import 'package:dartx/dartx.dart';
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:hiddify/core/core_providers.dart';
import 'package:hiddify/core/router/router.dart';
import 'package:hiddify/domain/environment.dart';
import 'package:hiddify/domain/failures.dart';
import 'package:hiddify/features/common/active_profile/active_profile_notifier.dart';
import 'package:hiddify/features/common/active_profile/has_any_profile_notifier.dart';
@@ -86,16 +88,12 @@ class AppVersionLabel extends HookConsumerWidget {
final t = ref.watch(translationsProvider);
final theme = Theme.of(context);
final version = ref.watch(
appVersionProvider.select(
(value) => switch (value) {
AsyncData(:final value) => value.version,
_ => "",
},
),
);
if (version.isEmpty) return const SizedBox();
final appInfo = ref.watch(appInfoProvider);
final version = appInfo.version +
(appInfo.environment == Environment.prod
? ""
: " ${appInfo.environment.name}");
if (version.isBlank) return const SizedBox();
return Semantics(
label: t.about.version,
@@ -111,6 +109,7 @@ class AppVersionLabel extends HookConsumerWidget {
),
child: Text(
version,
textDirection: TextDirection.ltr,
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSecondaryContainer,
),