feat: Custom api endpoints. (#216)

Co-authored-by: Alberto Ponces <ponces26@gmail.com>
This commit is contained in:
Aunali321 2022-09-18 23:12:30 +05:30 committed by GitHub
parent a9f64e449b
commit c5ad337daa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 162 additions and 14 deletions

View File

@ -97,6 +97,7 @@
"patcherSectionTitle": "Patcher",
"teamSectionTitle": "Team",
"infoSectionTitle": "Info",
"advancedSectionTitle": "Advanced",
"darkThemeLabel": "Dark Mode",
"darkThemeHint": "Welcome to the Dark Side",
"dynamicThemeLabel": "Material You",
@ -112,10 +113,14 @@
"sourcesIntegrationsLabel": "Integrations Source",
"sourcesResetDialogTitle": "Reset",
"sourcesResetDialogText": "Are you sure you want to reset custom sources to their default values?",
"apiURLResetDialogText": "Are you sure you want to reset API URL to their default values?",
"contributorsLabel": "Contributors",
"contributorsHint": "A list of contributors of ReVanced",
"logsLabel": "Logs",
"logsHint": "Share device debug logs",
"apiURLLabel": "API URL",
"apiURLHint": "Configure your custom API URL",
"selectApiURL": "Select URL",
"aboutLabel": "About",
"snackbarMessage": "Copied to clipboard"
},

View File

@ -19,6 +19,7 @@ class ManagerAPI {
final String patcherRepo = 'revanced-patcher';
final String cliRepo = 'revanced-cli';
late SharedPreferences _prefs;
String defaultApiUrl = 'https://revanced-releases-api.afterst0rm.xyz';
String defaultPatcherRepo = 'revanced/revanced-patcher';
String defaultPatchesRepo = 'revanced/revanced-patches';
String defaultIntegrationsRepo = 'revanced/revanced-integrations';
@ -29,6 +30,17 @@ class ManagerAPI {
_prefs = await SharedPreferences.getInstance();
}
String getApiUrl() {
return _prefs.getString('apiUrl') ?? defaultApiUrl;
}
Future<void> setApiUrl(String url) async {
if (url.isEmpty || url == ' ') {
url = defaultApiUrl;
}
await _prefs.setString('apiUrl', url);
}
String getPatchesRepo() {
return _prefs.getString('patchesRepo') ?? defaultPatchesRepo;
}
@ -106,21 +118,26 @@ class ManagerAPI {
}
Future<Map<String, List<dynamic>>> getContributors() async {
return await _revancedAPI.getContributors();
String apiUrl = getApiUrl();
return await _revancedAPI.getContributors(apiUrl);
}
Future<List<Patch>> getPatches() async {
if (getPatchesRepo() == defaultPatchesRepo) {
return await _revancedAPI.getPatches();
String repoName = getPatchesRepo();
if (repoName == defaultPatchesRepo) {
String apiUrl = getApiUrl();
return await _revancedAPI.getPatches(apiUrl);
} else {
return await _githubAPI.getPatches(getPatchesRepo());
return await _githubAPI.getPatches(repoName);
}
}
Future<File?> downloadPatches() async {
String repoName = getPatchesRepo();
if (repoName == defaultPatchesRepo) {
String apiUrl = getApiUrl();
return await _revancedAPI.getLatestReleaseFile(
apiUrl,
'.jar',
defaultPatchesRepo,
);
@ -132,7 +149,9 @@ class ManagerAPI {
Future<File?> downloadIntegrations() async {
String repoName = getIntegrationsRepo();
if (repoName == defaultIntegrationsRepo) {
String apiUrl = getApiUrl();
return await _revancedAPI.getLatestReleaseFile(
apiUrl,
'.apk',
defaultIntegrationsRepo,
);
@ -142,19 +161,36 @@ class ManagerAPI {
}
Future<File?> downloadManager() async {
return await _revancedAPI.getLatestReleaseFile('.apk', defaultManagerRepo);
String apiUrl = getApiUrl();
return await _revancedAPI.getLatestReleaseFile(
apiUrl,
'.apk',
defaultManagerRepo,
);
}
Future<String?> getLatestPatcherReleaseTime() async {
return await _revancedAPI.getLatestReleaseTime('.gz', defaultPatcherRepo);
String apiUrl = getApiUrl();
return await _revancedAPI.getLatestReleaseTime(
apiUrl,
'.gz',
defaultPatcherRepo,
);
}
Future<String?> getLatestManagerReleaseTime() async {
return await _revancedAPI.getLatestReleaseTime('.apk', defaultManagerRepo);
String apiUrl = getApiUrl();
return await _revancedAPI.getLatestReleaseTime(
apiUrl,
'.apk',
defaultManagerRepo,
);
}
Future<String?> getLatestManagerVersion() async {
String apiUrl = getApiUrl();
return await _revancedAPI.getLatestReleaseVersion(
apiUrl,
'.apk',
defaultManagerRepo,
);

View File

@ -9,7 +9,6 @@ import 'package:timeago/timeago.dart';
@lazySingleton
class RevancedAPI {
final String apiUrl = 'https://revanced-releases-api.afterst0rm.xyz';
final Dio _dio = Dio();
final DioCacheManager _dioCacheManager = DioCacheManager(CacheConfig());
final Options _cacheOptions = buildCacheOptions(
@ -17,7 +16,7 @@ class RevancedAPI {
maxStale: const Duration(days: 7),
);
void initialize() {
Future<void> initialize() async {
_dio.interceptors.add(_dioCacheManager.interceptor);
}
@ -25,7 +24,7 @@ class RevancedAPI {
await _dioCacheManager.clearAll();
}
Future<Map<String, List<dynamic>>> getContributors() async {
Future<Map<String, List<dynamic>>> getContributors(String apiUrl) async {
Map<String, List<dynamic>> contributors = {};
try {
var response = await _dio.get(
@ -43,7 +42,7 @@ class RevancedAPI {
return contributors;
}
Future<List<Patch>> getPatches() async {
Future<List<Patch>> getPatches(String apiUrl) async {
try {
var response = await _dio.get('$apiUrl/patches', options: _cacheOptions);
List<dynamic> patches = response.data;
@ -54,6 +53,7 @@ class RevancedAPI {
}
Future<Map<String, dynamic>?> _getLatestRelease(
String apiUrl,
String extension,
String repoName,
) async {
@ -71,10 +71,13 @@ class RevancedAPI {
}
Future<String?> getLatestReleaseVersion(
String extension, String repoName) async {
String apiUrl,
String extension,
String repoName,
) async {
try {
Map<String, dynamic>? release =
await _getLatestRelease(extension, repoName);
await _getLatestRelease(apiUrl, extension, repoName);
if (release != null) {
return release['version'];
}
@ -84,9 +87,14 @@ class RevancedAPI {
return null;
}
Future<File?> getLatestReleaseFile(String extension, String repoName) async {
Future<File?> getLatestReleaseFile(
String apiUrl,
String extension,
String repoName,
) async {
try {
Map<String, dynamic>? release = await _getLatestRelease(
apiUrl,
extension,
repoName,
);
@ -101,11 +109,13 @@ class RevancedAPI {
}
Future<String?> getLatestReleaseTime(
String apiUrl,
String extension,
String repoName,
) async {
try {
Map<String, dynamic>? release = await _getLatestRelease(
apiUrl,
extension,
repoName,
);

View File

@ -145,6 +145,17 @@ class SettingsView extends StatelessWidget {
const AboutWidget(),
],
),
const Divider(thickness: 1.0),
SettingsSection(
title: 'settingsView.advancedSectionTitle',
children: <Widget>[
SettingsTileDialog(
title: 'settingsView.apiURLLabel',
subtitle: 'settingsView.apiURLHint',
onTap: () => model.showApiUrlDialog(context),
),
],
),
],
),
),

View File

@ -27,6 +27,7 @@ class SettingsViewModel extends BaseViewModel {
final TextEditingController _patSourceController = TextEditingController();
final TextEditingController _orgIntSourceController = TextEditingController();
final TextEditingController _intSourceController = TextEditingController();
final TextEditingController _apiUrlController = TextEditingController();
void setLanguage(String language) {
notifyListeners();
@ -208,6 +209,65 @@ class SettingsViewModel extends BaseViewModel {
);
}
Future<void> showApiUrlDialog(BuildContext context) async {
String apiUrl = _managerAPI.getApiUrl();
_apiUrlController.text = apiUrl.replaceAll('https://', '');
return showDialog(
context: context,
builder: (context) => AlertDialog(
title: Row(
children: <Widget>[
I18nText('settingsView.apiURLLabel'),
const Spacer(),
IconButton(
icon: const Icon(Icons.manage_history_outlined),
onPressed: () => showApiUrlResetDialog(context),
color: Theme.of(context).colorScheme.secondary,
)
],
),
backgroundColor: Theme.of(context).colorScheme.secondaryContainer,
content: SingleChildScrollView(
child: Column(
children: <Widget>[
CustomTextField(
leadingIcon: Icon(
Icons.api_outlined,
color: Theme.of(context).colorScheme.secondary,
),
inputController: _apiUrlController,
label: I18nText('settingsView.selectApiURL'),
hint: apiUrl.split('/')[0],
onChanged: (value) => notifyListeners(),
),
],
),
),
actions: <Widget>[
CustomMaterialButton(
isFilled: false,
label: I18nText('cancelButton'),
onPressed: () {
_apiUrlController.clear();
Navigator.of(context).pop();
},
),
CustomMaterialButton(
label: I18nText('okButton'),
onPressed: () {
String apiUrl = _apiUrlController.text;
if (!apiUrl.startsWith('https')) {
apiUrl = 'https://$apiUrl';
}
_managerAPI.setApiUrl(apiUrl);
Navigator.of(context).pop();
},
)
],
),
);
}
Future<void> showResetConfirmationDialog(BuildContext context) async {
return showDialog(
context: context,
@ -235,6 +295,32 @@ class SettingsViewModel extends BaseViewModel {
);
}
Future<void> showApiUrlResetDialog(BuildContext context) async {
return showDialog(
context: context,
builder: (context) => AlertDialog(
title: I18nText('settingsView.sourcesResetDialogTitle'),
backgroundColor: Theme.of(context).colorScheme.secondaryContainer,
content: I18nText('settingsView.apiURLResetDialogText'),
actions: <Widget>[
CustomMaterialButton(
isFilled: false,
label: I18nText('cancelButton'),
onPressed: () => Navigator.of(context).pop(),
),
CustomMaterialButton(
label: I18nText('okButton'),
onPressed: () {
_managerAPI.setApiUrl('');
Navigator.of(context).pop();
Navigator.of(context).pop();
},
)
],
),
);
}
Future<int> getSdkVersion() async {
AndroidDeviceInfo info = await DeviceInfoPlugin().androidInfo;
return info.version.sdkInt ?? -1;