Skip to content

Commit

Permalink
Refactors to source_provider - less redundancy
Browse files Browse the repository at this point in the history
  • Loading branch information
Imran Remtulla committed Aug 27, 2022
1 parent 7e5affe commit f9044e2
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions lib/providers/source_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,19 @@ List<String> getLinksFromParsedHTML(
.toList();

abstract class AppSource {
late String sourceId;
late String host;
String standardizeURL(String url);
Future<APKDetails> getLatestAPKDetails(String standardUrl);
AppNames getAppNames(String standardUrl);
}

class GitHub implements AppSource {
@override
String sourceId = 'github';
late String host = 'github.com';

@override
String standardizeURL(String url) {
RegExp standardUrlRegEx = RegExp(r'^https?://github.com/[^/]*/[^/]*');
RegExp standardUrlRegEx = RegExp('^https?://$host/[^/]*/[^/]*');
RegExpMatch? match = standardUrlRegEx.firstMatch(url.toLowerCase());
if (match == null) {
throw 'Not a valid URL';
Expand Down Expand Up @@ -144,11 +144,11 @@ class GitHub implements AppSource {

class GitLab implements AppSource {
@override
String sourceId = 'gitlab';
late String host = 'gitlab.com';

@override
String standardizeURL(String url) {
RegExp standardUrlRegEx = RegExp(r'^https?://gitlab.com/[^/]*/[^/]*');
RegExp standardUrlRegEx = RegExp('^https?://$host/[^/]*/[^/]*');
RegExpMatch? match = standardUrlRegEx.firstMatch(url.toLowerCase());
if (match == null) {
throw 'Not a valid URL';
Expand Down Expand Up @@ -197,17 +197,17 @@ class GitLab implements AppSource {

class Signal implements AppSource {
@override
String sourceId = 'signal';
late String host = 'signal.org';

@override
String standardizeURL(String url) {
return 'https://signal.org';
return 'https://$host';
}

@override
Future<APKDetails> getLatestAPKDetails(String standardUrl) async {
Response res =
await get(Uri.parse('https://updates.signal.org/android/latest.json'));
await get(Uri.parse('https://updates.$host/android/latest.json'));
if (res.statusCode == 200) {
var json = jsonDecode(res.body);
String? apkUrl = json['url'];
Expand All @@ -229,16 +229,21 @@ class Signal implements AppSource {
}

class SourceProvider {
List<AppSource> sources = [GitHub(), GitLab(), Signal()];

// Add more source classes here so they are available via the service
AppSource getSource(String url) {
if (url.toLowerCase().contains('://github.com')) {
return GitHub();
} else if (url.toLowerCase().contains('://gitlab.com')) {
return GitLab();
} else if (url.toLowerCase().contains('://signal.org')) {
return Signal();
AppSource? source;
for (var s in sources) {
if (url.toLowerCase().contains('://${s.host}')) {
source = s;
break;
}
}
if (source == null) {
throw 'URL does not match a known source';
}
throw 'URL does not match a known source';
return source;
}

Future<App> getApp(String url) async {
Expand All @@ -254,7 +259,7 @@ class SourceProvider {
AppNames names = source.getAppNames(standardUrl);
APKDetails apk = await source.getLatestAPKDetails(standardUrl);
return App(
'${names.author.toLowerCase()}_${names.name.toLowerCase()}_${source.sourceId}',
'${names.author.toLowerCase()}_${names.name.toLowerCase()}_${source.host}',
standardUrl,
names.author[0].toUpperCase() + names.author.substring(1),
names.name[0].toUpperCase() + names.name.substring(1),
Expand All @@ -263,5 +268,5 @@ class SourceProvider {
apk.apkUrls);
}

List<String> getSourceHosts() => ['github.com', 'gitlab.com', 'signal.org'];
List<String> getSourceHosts() => sources.map((e) => e.host).toList();
}

0 comments on commit f9044e2

Please sign in to comment.