2023-09-12 15:22:58 +03:30
|
|
|
import 'dart:io';
|
|
|
|
|
|
2023-07-27 18:03:41 +03:30
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
|
import 'package:fpdart/fpdart.dart';
|
|
|
|
|
import 'package:hiddify/data/repository/exception_handlers.dart';
|
|
|
|
|
import 'package:hiddify/domain/app/app.dart';
|
|
|
|
|
import 'package:hiddify/domain/constants.dart';
|
2023-09-12 15:22:58 +03:30
|
|
|
import 'package:hiddify/domain/environment.dart';
|
2023-07-27 18:03:41 +03:30
|
|
|
import 'package:hiddify/utils/custom_loggers.dart';
|
|
|
|
|
import 'package:package_info_plus/package_info_plus.dart';
|
|
|
|
|
|
2023-09-12 15:22:58 +03:30
|
|
|
class AppRepositoryImpl
|
2023-07-27 18:03:41 +03:30
|
|
|
with ExceptionHandler, InfraLogger
|
2023-09-12 15:22:58 +03:30
|
|
|
implements AppRepository {
|
|
|
|
|
AppRepositoryImpl(this.dio);
|
2023-07-27 18:03:41 +03:30
|
|
|
|
|
|
|
|
final Dio dio;
|
|
|
|
|
|
2023-09-12 15:22:58 +03:30
|
|
|
static Future<AppInfo> getAppInfo(Environment environment) async {
|
|
|
|
|
final packageInfo = await PackageInfo.fromPlatform();
|
|
|
|
|
return AppInfo(
|
|
|
|
|
name: packageInfo.appName,
|
|
|
|
|
version: packageInfo.version,
|
|
|
|
|
buildNumber: packageInfo.buildNumber,
|
2023-09-15 23:09:58 +03:30
|
|
|
release: Release.read(),
|
2023-09-12 15:22:58 +03:30
|
|
|
installerMedia: packageInfo.installerStore,
|
|
|
|
|
operatingSystem: Platform.operatingSystem,
|
|
|
|
|
environment: environment,
|
2023-07-27 18:03:41 +03:30
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-15 23:09:58 +03:30
|
|
|
// TODO add market-specific update checking
|
2023-07-27 18:03:41 +03:30
|
|
|
@override
|
2023-09-12 15:22:58 +03:30
|
|
|
TaskEither<AppFailure, RemoteVersionInfo> getLatestVersion({
|
2023-07-27 18:03:41 +03:30
|
|
|
bool includePreReleases = false,
|
2023-09-15 23:09:58 +03:30
|
|
|
Release release = Release.general,
|
2023-07-27 18:03:41 +03:30
|
|
|
}) {
|
|
|
|
|
return exceptionHandler(
|
|
|
|
|
() async {
|
2023-09-15 23:09:58 +03:30
|
|
|
if (!release.allowCustomUpdateChecker) {
|
|
|
|
|
throw Exception("custom update checkers are not supported");
|
|
|
|
|
}
|
2023-07-27 18:03:41 +03:30
|
|
|
final response = await dio.get<List>(Constants.githubReleasesApiUrl);
|
|
|
|
|
if (response.statusCode != 200 || response.data == null) {
|
|
|
|
|
loggy.warning("failed to fetch latest version info");
|
2023-09-12 15:22:58 +03:30
|
|
|
return left(const AppFailure.unexpected());
|
2023-07-27 18:03:41 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final releases = response.data!
|
|
|
|
|
.map((e) => RemoteVersionInfo.fromJson(e as Map<String, dynamic>));
|
|
|
|
|
late RemoteVersionInfo latest;
|
|
|
|
|
if (includePreReleases) {
|
|
|
|
|
latest = releases.first;
|
|
|
|
|
} else {
|
|
|
|
|
latest = releases.firstWhere((e) => e.preRelease == false);
|
|
|
|
|
}
|
|
|
|
|
return right(latest);
|
|
|
|
|
},
|
2023-09-12 15:22:58 +03:30
|
|
|
AppFailure.unexpected,
|
2023-07-27 18:03:41 +03:30
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|