Progress. Bit less confusion, 0% tested.

This commit is contained in:
Imran Remtulla
2022-08-13 19:25:19 -04:00
parent 77f9f2557c
commit 6b6fa11dba
6 changed files with 67 additions and 123 deletions

View File

@@ -1,10 +1,10 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:obtainium/services/apk_service.dart'; import 'package:obtainium/services/apk_service.dart';
import 'package:obtainium/services/app_service.dart';
import 'package:obtainium/services/source_service.dart'; import 'package:obtainium/services/source_service.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
void main() async { void main() async {
;
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
runApp(MultiProvider( runApp(MultiProvider(
providers: [ providers: [
@@ -12,7 +12,9 @@ void main() async {
create: (context) => APKService(), create: (context) => APKService(),
dispose: (context, apkInstallService) => apkInstallService.dispose(), dispose: (context, apkInstallService) => apkInstallService.dispose(),
), ),
Provider(create: (context) => SourceService()) Provider(create: (context) => SourceService()),
Provider(
create: (context) => AppService(Provider.of<SourceService>(context)))
], ],
child: const MyApp(), child: const MyApp(),
)); ));
@@ -59,9 +61,9 @@ class MyHomePage extends StatefulWidget {
class _MyHomePageState extends State<MyHomePage> { class _MyHomePageState extends State<MyHomePage> {
int ind = 0; int ind = 0;
var urls = [ var urls = [
"https://github.com/Ashinch/ReadYou/releases/download/0.8.0/ReadYou-0.8.0-eec397e.apk", "https://github.com/Ashinch/ReadYou/releases/download", // Should work
"https://github.com/Ashinch/ReadYou/releases/download/0.8.1/ReadYou-0.8.1-c741f19.apk", "http://github.com/syncthing/syncthing-android/releases/tag/1.20.4", // Should work
"https://github.com/Ashinch/ReadYou/releases/download/0.8.3/ReadYou-0.8.3-7a47329.apk" "https://github.com/videolan/vlc" // Should not
]; ];
@override @override
@@ -83,16 +85,14 @@ class _MyHomePageState extends State<MyHomePage> {
), ),
floatingActionButton: FloatingActionButton( floatingActionButton: FloatingActionButton(
onPressed: () { onPressed: () {
var source = Provider.of<SourceService>(context).getSource(urls[ind]); Provider.of<AppService>(context).getApp(urls[ind]).then((app) {
Provider.of<APKService>(context, listen: false)
var standardURL = Provider.of<SourceService>(context) .downloadAndInstallAPK(app.apkUrl, app.id);
.standardizeURL(urls[ind], source.standardURLRegEx); setState(() {
var names = ind = ind == (urls.length - 1) ? 0 : ind + 1;
Provider.of<SourceService>(context).getAppNames(standardURL); });
Provider.of<APKService>(context, listen: false).downloadAndInstallAPK( }).catchError((err) {
urls[ind], "${names.author}_${names.name}"); print(err);
setState(() {
ind = ind == (urls.length - 1) ? 0 : ind + 1;
}); });
}, },
tooltip: 'Increment', tooltip: 'Increment',

View File

@@ -1,10 +0,0 @@
class App {
late String id;
late String url;
String? installedVersion;
late String latestVersion;
String? readmeHTML;
String? base64Icon;
App(this.id, this.url, this.installedVersion, this.latestVersion,
this.readmeHTML, this.base64Icon);
}

View File

@@ -0,0 +1,27 @@
import 'package:obtainium/services/source_service.dart';
class App {
late String id;
late String url;
String? installedVersion;
late String latestVersion;
late String apkUrl;
App(this.id, this.url, this.installedVersion, this.latestVersion,
this.apkUrl);
}
class AppService {
late SourceService sourceService;
AppService(this.sourceService);
Future<App> getApp(String url) async {
AppSource source = sourceService.getSource(url);
String standardUrl = source.standardizeURL(url);
AppNames names = source.getAppNames(standardUrl);
APKDetails apk = await source.getLatestAPKUrl(standardUrl);
return App("${names.author}_${names.name}", standardUrl, null, apk.version,
apk.downloadUrl);
}
// Load Apps, Save App
}

View File

@@ -1,9 +1,7 @@
import 'dart:convert'; import 'dart:convert';
import 'package:http/http.dart'; import 'package:http/http.dart';
import 'package:markdown/markdown.dart';
import 'package:html/parser.dart';
// Sub-classes of App Source // Sub-classes used in App Source
class AppNames { class AppNames {
late String author; late String author;
@@ -19,33 +17,38 @@ class APKDetails {
APKDetails(this.version, this.downloadUrl); APKDetails(this.version, this.downloadUrl);
} }
// App Source abstract class (GitHub, GitLab, etc.) // App Source abstract class (diff. implementations for GitHub, GitLab, etc.)
abstract class AppSource { abstract class AppSource {
late RegExp standardURLRegEx; String standardizeURL(String url);
Future<APKDetails?> getLatestAPKUrl(String url); Future<APKDetails> getLatestAPKUrl(String standardUrl);
Future<String?> getReadMeHTML(String url); AppNames getAppNames(String standardUrl);
Future<String?> getBase64IconURLFromHTML(String url, String html);
AppSource(this.standardURLRegEx);
} }
// Specific App Source definitions // Specific App Source classes
class GitHub extends AppSource { class GitHub implements AppSource {
GitHub() : super(RegExp(r"^https?://github.com/[^/]*/[^/]*")); @override
String standardizeURL(String url) {
RegExp standardUrlRegEx = RegExp(r"^https?://github.com/[^/]*/[^/]*");
var match = standardUrlRegEx.firstMatch(url.toLowerCase());
if (match == null) {
throw "Not a valid URL";
}
return url.substring(0, match.end);
}
String getRawContentURL(String url) { String convertURLToRawContentURL(String url) {
int tempInd1 = url.indexOf('://') + 3; int tempInd1 = url.indexOf('://') + 3;
int tempInd2 = url.indexOf('://') + 13; int tempInd2 = url.substring(tempInd1).indexOf('/') + tempInd1;
return "${url.substring(0, tempInd1)}raw.githubusercontent.com${url.substring(tempInd2)}"; return "${url.substring(0, tempInd1)}raw.githubusercontent.com${url.substring(tempInd2)}";
} }
@override @override
Future<APKDetails?> getLatestAPKUrl(String url) async { Future<APKDetails> getLatestAPKUrl(String standardUrl) async {
int tempInd = url.indexOf('://') + 3; int tempInd = standardUrl.indexOf('://') + 3;
Response res = await get(Uri.parse( Response res = await get(Uri.parse(
"${url.substring(0, tempInd)}api.${url.substring(tempInd)}/releases/latest")); "${standardUrl.substring(0, tempInd)}api.${standardUrl.substring(tempInd)}/releases/latest"));
if (res.statusCode == 200) { if (res.statusCode == 200) {
var release = jsonDecode(res.body); var release = jsonDecode(res.body);
for (var i = 0; i < release['assets'].length; i++) { for (var i = 0; i < release['assets'].length; i++) {
@@ -65,51 +68,14 @@ class GitHub extends AppSource {
} }
@override @override
Future<String?> getReadMeHTML(String url) async { AppNames getAppNames(String standardUrl) {
String uri = getRawContentURL(url); String temp = standardUrl.substring(standardUrl.indexOf('://') + 3);
List<String> possibleSuffixes = ["main/README.md", "master/README.md"]; List<String> names = temp.substring(temp.indexOf('/')).split('/');
for (var i = 0; i < possibleSuffixes.length; i++) { return AppNames(names[0], names[1]);
Response res = await get(Uri.parse("$uri/${possibleSuffixes[i]}"));
if (res.statusCode == 200) {
return markdownToHtml(res.body);
}
}
return null;
}
@override
Future<String?> getBase64IconURLFromHTML(String url, String html) async {
var icon = parse(html).getElementsByClassName("img")?[0];
if (icon != null) {
String uri = getRawContentURL(url);
List<String> possibleBranches = ["main", "master"];
for (var i = 0; i < possibleBranches.length; i++) {
var imgUrl = "$uri/${possibleBranches[i]}/${icon.attributes['src']}";
Response res = await get(Uri.parse(imgUrl));
if (res.statusCode == 200) {
return imgUrl;
}
}
}
return null;
} }
} }
class SourceService { class SourceService {
String standardizeURL(String url, RegExp standardURLRegEx) {
var match = standardURLRegEx.firstMatch(url.toLowerCase());
if (match == null) {
throw "Not a valid URL";
}
return url.substring(0, match.end);
}
AppNames getAppNames(String standardURL) {
String temp = standardURL.substring(standardURL.indexOf('://') + 3);
List<String> names = temp.substring(temp.indexOf('/')).split('/');
return AppNames(names[0], names[1]);
}
// 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(); var github = GitHub();
AppSource getSource(String url) { AppSource getSource(String url) {
@@ -119,19 +85,3 @@ class SourceService {
throw "URL does not match a known source"; throw "URL does not match a known source";
} }
} }
/*
- Make a function that validates and standardizes github URLs, do the same for gitlab (fail = error)
- Make a function that gets the App title and Author name from a github URL, do the same for gitlab (can't fail)
- Make a function that takes a github URL and finds the latest APK release if any (with version), do the same for gitlab (fail = error)
- Make a function that takes a github URL and returns a README HTML if any, do the same for gitlab (fail = "no description")
- Make a function that looks for the first image in a README HTML and returns its url (fail = no icon)
- Make a function that integrates all above and returns an App object for a given github URL, do the same for gitlab
- Make a function that detects the URL (Github or Gitlab) and runs the right function above
- Make a function that can save/load an App object to/from persistent storage (JSON file with unique App ID as file name)
- Make a function (using the above fn) that loads an array of all Apps
*/

View File

@@ -43,13 +43,6 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.16.0" version: "1.16.0"
csslib:
dependency: transitive
description:
name: csslib
url: "https://pub.dartlang.org"
source: hosted
version: "0.17.2"
cupertino_icons: cupertino_icons:
dependency: "direct main" dependency: "direct main"
description: description:
@@ -137,13 +130,6 @@ packages:
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" version: "0.0.0"
html:
dependency: "direct main"
description:
name: html
url: "https://pub.dartlang.org"
source: hosted
version: "0.15.0"
http: http:
dependency: "direct main" dependency: "direct main"
description: description:
@@ -165,13 +151,6 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.0.0" version: "2.0.0"
markdown:
dependency: "direct main"
description:
name: markdown
url: "https://pub.dartlang.org"
source: hosted
version: "6.0.0"
matcher: matcher:
dependency: transitive dependency: transitive
description: description:

View File

@@ -42,8 +42,6 @@ dependencies:
flutter_local_notifications: ^9.7.0 flutter_local_notifications: ^9.7.0
provider: ^6.0.3 provider: ^6.0.3
http: ^0.13.5 http: ^0.13.5
markdown: ^6.0.0
html: ^0.15.0
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: