Skip to content

Commit

Permalink
feat: ability to search query for suggested version (ReVanced#1151)
Browse files Browse the repository at this point in the history
Co-authored-by: oSumAtrIX <johan.melkonyan1@web.de>
Co-authored-by: Ushie <ushiekane@gmail.com>
  • Loading branch information
3 people committed Nov 21, 2023
1 parent c0516c3 commit 9bd48c1
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 25 deletions.
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 @@ -41,6 +43,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 @@ -176,6 +189,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)
}
}

@OptIn(InternalCoroutinesApi::class)
private fun runPatcher(
result: MethodChannel.Result,
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 @@ -112,6 +112,10 @@ class _AppSelectorViewState extends State<AppSelectorView> {
context,
app.packageName,
),
onLinkTap: () =>
model.searchSuggestedVersionOnWeb(
packageName: app.packageName,
),
),
)
,
Expand All @@ -126,6 +130,10 @@ class _AppSelectorViewState extends State<AppSelectorView> {
onTap: () {
model.showDownloadToast();
},
onLinkTap: () =>
model.searchSuggestedVersionOnWeb(
packageName: app,
),
),
)
,
Expand Down
28 changes: 28 additions & 0 deletions lib/ui/views/app_selector/app_selector_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:io';
import 'package:device_apps/device_apps.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_file_dialog/flutter_file_dialog.dart';
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:revanced_manager/app/app.locator.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(
BuildContext context,
ApplicationWithIcon application, [
Expand Down
45 changes: 42 additions & 3 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 @@ -157,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);

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 @@ -169,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 Down
58 changes: 48 additions & 10 deletions lib/ui/widgets/appSelectorView/installed_app_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ class InstalledAppItem extends StatefulWidget {
required this.suggestedVersion,
required this.installedVersion,
this.onTap,
this.onLinkTap,
});

final String name;
final String pkgName;
final Uint8List icon;
final int patchesCount;
final String suggestedVersion;
final String installedVersion;
final Function()? onTap;
final Function()? onLinkTap;

@override
State<InstalledAppItem> createState() => _InstalledAppItemState();
Expand Down Expand Up @@ -71,17 +74,52 @@ class _InstalledAppItemState extends State<InstalledAppItem> {
),
),
Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
children: [
I18nText(
'suggested',
translationParams: {
'version': widget.suggestedVersion.isEmpty
? FlutterI18n.translate(
context,
'appSelectorCard.allVersions',
)
: 'v${widget.suggestedVersion}',
},
Material(
color: Theme.of(context).colorScheme.secondaryContainer,
borderRadius:
const BorderRadius.all(Radius.circular(8)),
child: InkWell(
onTap: widget.onLinkTap,
borderRadius:
const BorderRadius.all(Radius.circular(8)),
child: Container(
padding: const EdgeInsets.all(4),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
I18nText(
'suggested',
translationParams: {
'version': widget.suggestedVersion.isEmpty
? FlutterI18n.translate(
context,
'appSelectorCard.allVersions',
)
: 'v${widget.suggestedVersion}',
},
child: Text(
'',
style: TextStyle(
color: Theme.of(context)
.colorScheme
.onSecondaryContainer,
),
),
),
const SizedBox(width: 4),
Icon(
Icons.search,
size: 16,
color: Theme.of(context)
.colorScheme
.onSecondaryContainer,
),
],
),
),
),
),
const SizedBox(width: 4),
Text(
Expand Down
58 changes: 48 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,14 @@ class NotInstalledAppItem extends StatefulWidget {
required this.patchesCount,
required this.suggestedVersion,
this.onTap,
this.onLinkTap,
});

final String name;
final int patchesCount;
final String suggestedVersion;
final Function()? onTap;
final Function()? onLinkTap;

@override
State<NotInstalledAppItem> createState() => _NotInstalledAppItem();
Expand Down Expand Up @@ -65,17 +68,52 @@ class _NotInstalledAppItem extends State<NotInstalledAppItem> {
),
),
Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
children: [
I18nText(
'suggested',
translationParams: {
'version': widget.suggestedVersion.isEmpty
? FlutterI18n.translate(
context,
'appSelectorCard.allVersions',
)
: 'v${widget.suggestedVersion}',
},
Material(
color: Theme.of(context).colorScheme.secondaryContainer,
borderRadius:
const BorderRadius.all(Radius.circular(8)),
child: InkWell(
onTap: widget.onLinkTap,
borderRadius:
const BorderRadius.all(Radius.circular(8)),
child: Container(
padding: const EdgeInsets.all(4),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
I18nText(
'suggested',
translationParams: {
'version': widget.suggestedVersion.isEmpty
? FlutterI18n.translate(
context,
'appSelectorCard.allVersions',
)
: 'v${widget.suggestedVersion}',
},
child: Text(
'',
style: TextStyle(
color: Theme.of(context)
.colorScheme
.onSecondaryContainer,
),
),
),
const SizedBox(width: 4),
Icon(
Icons.search,
size: 16,
color: Theme.of(context)
.colorScheme
.onSecondaryContainer,
),
],
),
),
),
),
const SizedBox(width: 4),
Text(
Expand Down
46 changes: 44 additions & 2 deletions lib/ui/widgets/patcherView/app_selector_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class AppSelectorCard extends StatelessWidget {
super.key,
required this.onPressed,
});

final Function() onPressed;

@override
Expand Down Expand Up @@ -61,11 +62,52 @@ 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: [
Material(
color: Theme.of(context).colorScheme.secondaryContainer,
borderRadius: const BorderRadius.all(Radius.circular(8)),
child: InkWell(
onTap: () {
locator<PatcherViewModel>()
.searchSuggestedVersionOnWeb();
},
borderRadius:
const BorderRadius.all(Radius.circular(8)),
child: Container(
padding: const EdgeInsets.all(4),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Text(
locator<PatcherViewModel>()
.getSuggestedVersionString(context),
style: TextStyle(
color: Theme.of(context)
.colorScheme
.onSecondaryContainer,
),
),
const SizedBox(width: 4),
Icon(
Icons.search,
size: 16,
color: Theme.of(context)
.colorScheme
.onSecondaryContainer,
),
],
),
),
),
),
],
),
],
),
Expand Down

0 comments on commit 9bd48c1

Please sign in to comment.