mirror of
https://github.com/ImranR98/Obtainium.git
synced 2025-10-26 19:23:45 +01:00
More progress (untested) and refactoring
This commit is contained in:
@@ -14,12 +14,12 @@ void main() async {
|
|||||||
// Extract a GitHub project name and author account name from a GitHub URL (can be any sub-URL of the project)
|
// Extract a GitHub project name and author account name from a GitHub URL (can be any sub-URL of the project)
|
||||||
Map<String, String>? getAppNamesFromGitHubURL(String url) {
|
Map<String, String>? getAppNamesFromGitHubURL(String url) {
|
||||||
RegExp regex = RegExp(r'://github.com/[^/]*/[^/]*');
|
RegExp regex = RegExp(r'://github.com/[^/]*/[^/]*');
|
||||||
var match = regex.firstMatch(url.toLowerCase());
|
RegExpMatch? match = regex.firstMatch(url.toLowerCase());
|
||||||
if (match != null) {
|
if (match != null) {
|
||||||
var uri = url.substring(match.start + 14, match.end);
|
String uri = url.substring(match.start + 14, match.end);
|
||||||
var slashIndex = uri.indexOf('/');
|
int slashIndex = uri.indexOf('/');
|
||||||
var author = uri.substring(0, slashIndex);
|
String author = uri.substring(0, slashIndex);
|
||||||
var appName = uri.substring(slashIndex + 1);
|
String appName = uri.substring(slashIndex + 1);
|
||||||
return {'author': author, 'appName': appName};
|
return {'author': author, 'appName': appName};
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@@ -51,7 +51,7 @@ class MyHomePage extends StatefulWidget {
|
|||||||
|
|
||||||
class _MyHomePageState extends State<MyHomePage> {
|
class _MyHomePageState extends State<MyHomePage> {
|
||||||
int ind = 0;
|
int ind = 0;
|
||||||
var urls = [
|
List<String> urls = [
|
||||||
'https://github.com/Ashinch/ReadYou/releases/download', // Should work
|
'https://github.com/Ashinch/ReadYou/releases/download', // Should work
|
||||||
'http://github.com/syncthing/syncthing-android/releases/tag/1.20.4', // Should work
|
'http://github.com/syncthing/syncthing-android/releases/tag/1.20.4', // Should work
|
||||||
'https://github.com/videolan/vlc' // Should not
|
'https://github.com/videolan/vlc' // Should not
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
// Provider that manages App-related state and provides functions to retrieve App info download/install Apps
|
// Provider that manages App-related state and provides functions to retrieve App info download/install Apps
|
||||||
|
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
import 'dart:isolate';
|
import 'dart:isolate';
|
||||||
import 'dart:ui';
|
import 'dart:ui';
|
||||||
@@ -15,6 +16,7 @@ import 'package:obtainium/services/source_service.dart';
|
|||||||
class AppsProvider with ChangeNotifier {
|
class AppsProvider with ChangeNotifier {
|
||||||
// In memory App state (should always be kept in sync with local storage versions)
|
// In memory App state (should always be kept in sync with local storage versions)
|
||||||
Map<String, App> apps = {};
|
Map<String, App> apps = {};
|
||||||
|
bool loadingApps = false;
|
||||||
|
|
||||||
AppsProvider() {
|
AppsProvider() {
|
||||||
initializeDownloader();
|
initializeDownloader();
|
||||||
@@ -88,73 +90,110 @@ class AppsProvider with ChangeNotifier {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Change App status to no longer downloading
|
||||||
|
App? foundApp;
|
||||||
|
apps.forEach((id, app) {
|
||||||
|
if (app.currentDownloadId == id) {
|
||||||
|
foundApp = apps[app.id];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
foundApp!.currentDownloadId = null;
|
||||||
|
saveApp(foundApp!);
|
||||||
|
// Install the App (and remove warning notification if any)
|
||||||
FlutterDownloader.open(taskId: id);
|
FlutterDownloader.open(taskId: id);
|
||||||
downloaderNotifications.cancel(1);
|
downloaderNotifications.cancel(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Given a URL (assumed valid), initiate an APK download (will trigger install callback when complete)
|
// Given a URL (assumed valid), initiate an APK download (will trigger install callback when complete)
|
||||||
Future<void> backgroundDownloadAndInstallAPK(String url, String appId) async {
|
Future<void> backgroundDownloadAndInstallApp(App app) async {
|
||||||
var apkDir = Directory(
|
Directory apkDir = Directory(
|
||||||
'${(await getExternalStorageDirectory())?.path as String}/$appId');
|
'${(await getExternalStorageDirectory())?.path as String}/apks/${app.id}');
|
||||||
if (apkDir.existsSync()) apkDir.deleteSync(recursive: true);
|
if (apkDir.existsSync()) apkDir.deleteSync(recursive: true);
|
||||||
apkDir.createSync();
|
apkDir.createSync(recursive: true);
|
||||||
await FlutterDownloader.enqueue(
|
String? downloadId = await FlutterDownloader.enqueue(
|
||||||
url: url,
|
url: app.url,
|
||||||
savedDir: apkDir.path,
|
savedDir: apkDir.path,
|
||||||
showNotification: true,
|
showNotification: true,
|
||||||
openFileFromNotification: false,
|
openFileFromNotification: false,
|
||||||
);
|
);
|
||||||
|
if (downloadId != null) {
|
||||||
|
app.currentDownloadId = downloadId;
|
||||||
|
saveApp(app);
|
||||||
|
} else {
|
||||||
|
throw "Could not start download";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void loadApps() {
|
Future<Directory> getAppsDir() async {
|
||||||
// TODO: Load Apps JSON and fill the array
|
Directory appsDir = Directory(
|
||||||
|
'${(await getExternalStorageDirectory())?.path as String}/apps');
|
||||||
|
if (!appsDir.existsSync()) {
|
||||||
|
appsDir.createSync();
|
||||||
|
}
|
||||||
|
return appsDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> loadApps() async {
|
||||||
|
loadingApps = true;
|
||||||
|
notifyListeners();
|
||||||
|
List<FileSystemEntity> appFiles = (await getAppsDir())
|
||||||
|
.listSync()
|
||||||
|
.where((item) => item.path.toLowerCase().endsWith('.json'))
|
||||||
|
.toList();
|
||||||
|
apps.clear();
|
||||||
|
for (int i = 0; i < appFiles.length; i++) {
|
||||||
|
App app =
|
||||||
|
App.fromJson(jsonDecode(File(appFiles[i].path).readAsStringSync()));
|
||||||
|
apps.putIfAbsent(app.id, () => app);
|
||||||
|
}
|
||||||
|
loadingApps = false;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
void saveApp(App app) {
|
Future<void> saveApp(App app) async {
|
||||||
// TODO: Save/update an App JSON and update the array
|
File('${(await getAppsDir()).path}/${app.id}.json')
|
||||||
|
.writeAsStringSync(jsonEncode(app));
|
||||||
|
apps.update(app.id, (value) => app, ifAbsent: () => app);
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool checkUpdate(App app) {
|
bool checkUpdate(App app) {
|
||||||
// TODO: Check the given App against the existing version in the array (if it does not exist, throw an error)
|
if (!apps.containsKey(app.id)) {
|
||||||
return false;
|
throw 'App not found';
|
||||||
|
}
|
||||||
|
return app.latestVersion != apps[app.id]?.installedVersion;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> installApp(String url) async {
|
Future<void> installApp(String url) async {
|
||||||
var app = await SourceService().getApp(url);
|
App app = await SourceService().getApp(url);
|
||||||
await backgroundDownloadAndInstallAPK(app.apkUrl, app.id);
|
await backgroundDownloadAndInstallApp(app);
|
||||||
// TODO: Apps array should notify consumers about download progress (will need to change FlutterDownloader callbacks)
|
|
||||||
saveApp(app);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<App>> checkUpdates() async {
|
Future<List<App>> checkUpdates() async {
|
||||||
List<App> updates = [];
|
List<App> updates = [];
|
||||||
var appIds = apps.keys.toList();
|
List<String> appIds = apps.keys.toList();
|
||||||
for (var i = 0; i < appIds.length; i++) {
|
for (int i = 0; i < appIds.length; i++) {
|
||||||
var currentApp = apps[appIds[i]];
|
App? currentApp = apps[appIds[i]];
|
||||||
var newApp = await SourceService().getApp(currentApp!.url);
|
App newApp = await SourceService().getApp(currentApp!.url);
|
||||||
if (newApp.latestVersion != currentApp.latestVersion) {
|
if (newApp.latestVersion != currentApp.latestVersion) {
|
||||||
newApp.installedVersion = currentApp.installedVersion;
|
newApp.installedVersion = currentApp.installedVersion;
|
||||||
updates.add(newApp);
|
updates.add(newApp);
|
||||||
saveApp(newApp);
|
await saveApp(newApp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return updates;
|
return updates;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> installUpdates() async {
|
Future<void> installUpdates() async {
|
||||||
var appIds = apps.keys.toList();
|
List<String> appIds = apps.keys.toList();
|
||||||
for (var i = 0; i < appIds.length; i++) {
|
for (int i = 0; i < appIds.length; i++) {
|
||||||
var app = apps[appIds[i]];
|
App? app = apps[appIds[i]];
|
||||||
if (app != null) {
|
if (app!.installedVersion != app.latestVersion) {
|
||||||
if (app.installedVersion != app.latestVersion) {
|
|
||||||
await installApp(app.apkUrl);
|
await installApp(app.apkUrl);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
|
|||||||
@@ -36,13 +36,30 @@ class App {
|
|||||||
String? installedVersion;
|
String? installedVersion;
|
||||||
late String latestVersion;
|
late String latestVersion;
|
||||||
late String apkUrl;
|
late String apkUrl;
|
||||||
App(this.id, this.url, this.installedVersion, this.latestVersion,
|
String? currentDownloadId;
|
||||||
this.apkUrl);
|
App(this.id, this.url, this.installedVersion, this.latestVersion, this.apkUrl,
|
||||||
|
this.currentDownloadId);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String toString() {
|
String toString() {
|
||||||
return 'ID: $id URL: $url INSTALLED: $installedVersion LATEST: $latestVersion APK: $apkUrl';
|
return 'ID: $id URL: $url INSTALLED: $installedVersion LATEST: $latestVersion APK: $apkUrl';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
factory App.fromJson(Map<String, dynamic> json) => _appFromJson(json);
|
||||||
|
}
|
||||||
|
|
||||||
|
App _appFromJson(Map<String, dynamic> json) {
|
||||||
|
return App(
|
||||||
|
json['id'] as String,
|
||||||
|
json['url'] as String,
|
||||||
|
json['installedVersion'] == null
|
||||||
|
? null
|
||||||
|
: json['installedVersion'] as String,
|
||||||
|
json['latestVersion'] as String,
|
||||||
|
json['apkUrl'] as String,
|
||||||
|
json['currentDownloadId'] == null
|
||||||
|
? null
|
||||||
|
: json['currentDownloadId'] as String);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Specific App Source classes
|
// Specific App Source classes
|
||||||
@@ -51,7 +68,7 @@ class GitHub implements AppSource {
|
|||||||
@override
|
@override
|
||||||
String standardizeURL(String url) {
|
String standardizeURL(String url) {
|
||||||
RegExp standardUrlRegEx = RegExp(r'^https?://github.com/[^/]*/[^/]*');
|
RegExp standardUrlRegEx = RegExp(r'^https?://github.com/[^/]*/[^/]*');
|
||||||
var match = standardUrlRegEx.firstMatch(url.toLowerCase());
|
RegExpMatch? match = standardUrlRegEx.firstMatch(url.toLowerCase());
|
||||||
if (match == null) {
|
if (match == null) {
|
||||||
throw 'Not a valid URL';
|
throw 'Not a valid URL';
|
||||||
}
|
}
|
||||||
@@ -69,8 +86,8 @@ class GitHub implements AppSource {
|
|||||||
Response res = await get(Uri.parse(
|
Response res = await get(Uri.parse(
|
||||||
'${convertURL(standardUrl, 'api.github.com/repos')}/releases/latest'));
|
'${convertURL(standardUrl, 'api.github.com/repos')}/releases/latest'));
|
||||||
if (res.statusCode == 200) {
|
if (res.statusCode == 200) {
|
||||||
var release = jsonDecode(res.body);
|
dynamic release = jsonDecode(res.body);
|
||||||
for (var i = 0; i < release['assets'].length; i++) {
|
for (int i = 0; i < release['assets'].length; i++) {
|
||||||
if (release['assets'][i]['name']
|
if (release['assets'][i]['name']
|
||||||
.toString()
|
.toString()
|
||||||
.toLowerCase()
|
.toLowerCase()
|
||||||
@@ -95,7 +112,7 @@ class GitHub implements AppSource {
|
|||||||
|
|
||||||
class SourceService {
|
class SourceService {
|
||||||
// Add more source classes here so they are available via the service
|
// Add more source classes here so they are available via the service
|
||||||
var github = GitHub();
|
AppSource github = GitHub();
|
||||||
AppSource getSource(String url) {
|
AppSource getSource(String url) {
|
||||||
if (url.toLowerCase().contains('://github.com')) {
|
if (url.toLowerCase().contains('://github.com')) {
|
||||||
return github;
|
return github;
|
||||||
@@ -109,6 +126,6 @@ class SourceService {
|
|||||||
AppNames names = source.getAppNames(standardUrl);
|
AppNames names = source.getAppNames(standardUrl);
|
||||||
APKDetails apk = await source.getLatestAPKUrl(standardUrl);
|
APKDetails apk = await source.getLatestAPKUrl(standardUrl);
|
||||||
return App('${names.author}_${names.name}', standardUrl, null, apk.version,
|
return App('${names.author}_${names.name}', standardUrl, null, apk.version,
|
||||||
apk.downloadUrl);
|
apk.downloadUrl, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user