Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ability to search query for suggested version #1151

Merged
merged 23 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package app.revanced.manager.flutter

import android.app.SearchManager
import android.content.Intent
import android.os.Handler
import android.os.Looper
import app.revanced.manager.flutter.utils.Aapt
Expand Down Expand Up @@ -38,6 +40,17 @@ class MainActivity : FlutterActivity() {

val patcherChannel = "app.revanced.manager.flutter/patcher"
val installerChannel = "app.revanced.manager.flutter/installer"
val openBrowserChannel = "app.revanced.manager.flutter/browser"

MethodChannel(flutterEngine.dartExecutor.binaryMessenger, openBrowserChannel).setMethodCallHandler { call, result ->
if (call.method == "openBrowser") {
val searchQuery = call.argument<String>("query")
openBrowser(searchQuery)
result.success(null)
} else {
result.notImplemented()
}
}

val mainChannel =
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, patcherChannel)
Expand Down Expand Up @@ -161,6 +174,15 @@ class MainActivity : FlutterActivity() {
}
}

fun openBrowser(query: String?) {
val intent = Intent(Intent.ACTION_WEB_SEARCH).apply {
putExtra(SearchManager.QUERY, query)
}
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}
}

private fun runPatcher(
result: MethodChannel.Result,
originalFilePath: String,
Expand Down
8 changes: 8 additions & 0 deletions lib/ui/views/app_selector/app_selector_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ class _AppSelectorViewState extends State<AppSelectorView> {
context,
app.packageName,
),
onLinkTap: () =>
model.searchSuggestedVersionOnWeb(
packageName: app.packageName,
),
),
)
.toList(),
Expand All @@ -129,6 +133,10 @@ class _AppSelectorViewState extends State<AppSelectorView> {
onTap: () {
model.showDownloadToast();
},
onLinkTap: () =>
model.searchSuggestedVersionOnWeb(
packageName: app,
),
),
)
.toList(),
Expand Down
35 changes: 33 additions & 2 deletions lib/ui/views/app_selector/app_selector_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:device_apps/device_apps.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:revanced_manager/app/app.locator.dart';
import 'package:revanced_manager/models/patch.dart';
Expand Down Expand Up @@ -70,6 +71,33 @@ class AppSelectorViewModel extends BaseViewModel {
return true;
}

Future<void> searchSuggestedVersionOnWeb({
required String packageName,
}) async {
final String suggestedVersion = getSuggestedVersion(packageName);

if (suggestedVersion.isNotEmpty) {
await openDefaultBrowser('$packageName apk version v$suggestedVersion');
} else {
await openDefaultBrowser('$packageName apk');
}
}

Future<void> openDefaultBrowser(String query) async {
if (Platform.isAndroid) {
try {
const platform = MethodChannel('app.revanced.manager.flutter/browser');
await platform.invokeMethod('openBrowser', {'query': query});
} catch (e) {
if (kDebugMode) {
print(e);
}
}
} else {
throw 'Platform not supported';
}
}

Future<void> selectApp(ApplicationWithIcon application) async {
locator<PatcherViewModel>().selectedApp = PatchedApplication(
name: application.appName,
Expand Down Expand Up @@ -98,8 +126,11 @@ class AppSelectorViewModel extends BaseViewModel {
if (context.mounted) {
Navigator.pop(context);
}
final List<Option> requiredNullOptions = getNullRequiredOptions(locator<PatcherViewModel>().selectedPatches, packageName);
if(requiredNullOptions.isNotEmpty){
final List<Option> requiredNullOptions = getNullRequiredOptions(
locator<PatcherViewModel>().selectedPatches,
packageName,
);
if (requiredNullOptions.isNotEmpty) {
locator<PatcherViewModel>().showRequiredOptionDialog();
}
}
Expand Down
55 changes: 48 additions & 7 deletions lib/ui/views/patcher/patcher_viewmodel.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// ignore_for_file: use_build_context_synchronously

import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:injectable/injectable.dart';
import 'package:revanced_manager/app/app.locator.dart';
Expand Down Expand Up @@ -80,7 +84,8 @@ class PatcherViewModel extends BaseViewModel {
}

bool checkRequiredPatchOption(BuildContext context) {
if (getNullRequiredOptions(selectedPatches, selectedApp!.packageName).isNotEmpty) {
if (getNullRequiredOptions(selectedPatches, selectedApp!.packageName)
.isNotEmpty) {
showRequiredOptionDialog(context);
return false;
}
Expand Down Expand Up @@ -156,6 +161,29 @@ class PatcherViewModel extends BaseViewModel {
return text;
}

String getCurrentVersionString(BuildContext context) {
return '${FlutterI18n.translate(
context,
'appSelectorCard.currentVersion',
)}: v${selectedApp!.version}';
}

Future<void> searchSuggestedVersionOnWeb() async {
final String suggestedVersion =
_patcherAPI.getSuggestedVersion(selectedApp!.packageName);
Ushie marked this conversation as resolved.
Show resolved Hide resolved

if (suggestedVersion.isNotEmpty) {
await openDefaultBrowser(
'${selectedApp!.packageName} apk version v$suggestedVersion');
} else {
await openDefaultBrowser('${selectedApp!.packageName} apk');
}
}

String getSuggestedVersion() {
return _patcherAPI.getSuggestedVersion(selectedApp!.packageName);
}

String getSuggestedVersionString(BuildContext context) {
String suggestedVersion =
_patcherAPI.getSuggestedVersion(selectedApp!.packageName);
Expand All @@ -168,14 +196,26 @@ class PatcherViewModel extends BaseViewModel {
suggestedVersion = 'v$suggestedVersion';
}
return '${FlutterI18n.translate(
context,
'appSelectorCard.currentVersion',
)}: v${selectedApp!.version}\n${FlutterI18n.translate(
context,
'appSelectorCard.suggestedVersion',
)}: $suggestedVersion';
}

Future<void> openDefaultBrowser(String query) async {
if (Platform.isAndroid) {
try {
const platform = MethodChannel('app.revanced.manager.flutter/browser');
await platform.invokeMethod('openBrowser', {'query': query});
} catch (e) {
if (kDebugMode) {
print(e);
}
}
} else {
throw 'Platform not supported';
}
}

Future<void> loadLastSelectedPatches() async {
this.selectedPatches.clear();
removedPatches.clear();
Expand All @@ -199,11 +239,12 @@ class PatcherViewModel extends BaseViewModel {
.removeWhere((patch) => patch.compatiblePackages.isEmpty);
}
final usedPatches = _managerAPI.getUsedPatches(selectedApp!.packageName);
for (final patch in usedPatches){
if (!patches.any((p) => p.name == patch.name)){
for (final patch in usedPatches) {
if (!patches.any((p) => p.name == patch.name)) {
removedPatches.add('• ${patch.name}');
for (final option in patch.options) {
_managerAPI.clearPatchOption(selectedApp!.packageName, patch.name, option.key);
_managerAPI.clearPatchOption(
selectedApp!.packageName, patch.name, option.key);
}
}
}
Expand Down
46 changes: 36 additions & 10 deletions lib/ui/widgets/appSelectorView/installed_app_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class InstalledAppItem extends StatefulWidget {
required this.suggestedVersion,
required this.installedVersion,
this.onTap,
this.onLinkTap,
}) : super(key: key);
final String name;
final String pkgName;
Expand All @@ -21,6 +22,7 @@ class InstalledAppItem extends StatefulWidget {
final String suggestedVersion;
final String installedVersion;
final Function()? onTap;
final Function()? onLinkTap;

@override
State<InstalledAppItem> createState() => _InstalledAppItemState();
Expand Down Expand Up @@ -72,16 +74,40 @@ class _InstalledAppItemState extends State<InstalledAppItem> {
),
Wrap(
children: [
I18nText(
'suggested',
translationParams: {
'version': widget.suggestedVersion.isEmpty
? FlutterI18n.translate(
context,
'appSelectorCard.allVersions',
)
: 'v${widget.suggestedVersion}',
},
GestureDetector(
onTap: widget.onLinkTap,
child: Container(
decoration: BoxDecoration(
color: Theme.of(context)
.colorScheme
.onSecondaryContainer
.withOpacity(0.4),
borderRadius:
const BorderRadius.all(Radius.circular(7)),
),
padding: const EdgeInsets.symmetric(horizontal: 3),
child: Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
children: [
I18nText(
'suggested',
translationParams: {
'version': widget.suggestedVersion.isEmpty
? FlutterI18n.translate(
context,
'appSelectorCard.allVersions',
)
: 'v${widget.suggestedVersion}',
},
),
const SizedBox(width: 4),
const Icon(
Icons.launch,
size: 16,
),
],
),
),
),
const SizedBox(width: 4),
Text(
Expand Down
46 changes: 36 additions & 10 deletions lib/ui/widgets/appSelectorView/not_installed_app_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ class NotInstalledAppItem extends StatefulWidget {
required this.patchesCount,
required this.suggestedVersion,
this.onTap,
this.onLinkTap,
}) : super(key: key);
final String name;
final int patchesCount;
final String suggestedVersion;
final Function()? onTap;
final Function()? onLinkTap;

@override
State<NotInstalledAppItem> createState() => _NotInstalledAppItem();
Expand Down Expand Up @@ -66,16 +68,40 @@ class _NotInstalledAppItem extends State<NotInstalledAppItem> {
),
Wrap(
children: [
I18nText(
'suggested',
translationParams: {
'version': widget.suggestedVersion.isEmpty
? FlutterI18n.translate(
context,
'appSelectorCard.allVersions',
)
: 'v${widget.suggestedVersion}',
},
GestureDetector(
onTap: widget.onLinkTap,
child: Container(
decoration: BoxDecoration(
color: Theme.of(context)
.colorScheme
.onSecondaryContainer
.withOpacity(0.4),
borderRadius:
const BorderRadius.all(Radius.circular(7)),
),
padding: const EdgeInsets.symmetric(horizontal: 3),
child: Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
children: [
I18nText(
'suggested',
translationParams: {
'version': widget.suggestedVersion.isEmpty
? FlutterI18n.translate(
context,
'appSelectorCard.allVersions',
)
: 'v${widget.suggestedVersion}',
},
),
const SizedBox(width: 4),
const Icon(
Icons.link,
size: 17,
),
],
),
),
),
const SizedBox(width: 4),
Text(
Expand Down
25 changes: 23 additions & 2 deletions lib/ui/widgets/patcherView/app_selector_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,32 @@ class AppSelectorCard extends StatelessWidget {
Container()
else
Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const SizedBox(height: 4),
Text(
locator<PatcherViewModel>()
.getSuggestedVersionString(context),
locator<PatcherViewModel>().getCurrentVersionString(context),
),
Row(
children: [
Text(
locator<PatcherViewModel>()
.getSuggestedVersionString(context),
),
const SizedBox(
width: 20,
),
if (locator<PatcherViewModel>()
.getSuggestedVersion()
.isNotEmpty)
InkWell(
onTap: () {
locator<PatcherViewModel>()
.searchSuggestedVersionOnWeb();
},
child: const Icon(Icons.link),
),
],
),
],
),
Expand Down