feat: working app selector.

This commit is contained in:
Alberto Ponces 2022-08-09 00:01:06 +01:00
parent 960646ba77
commit 51801b5748
13 changed files with 129 additions and 105 deletions

View File

@ -12,6 +12,7 @@ import 'package:stacked_core/stacked_core.dart';
import 'package:stacked_services/src/navigation/navigation_service.dart'; import 'package:stacked_services/src/navigation/navigation_service.dart';
import '../services/patcher_api.dart'; import '../services/patcher_api.dart';
import '../ui/views/patcher/patcher_viewmodel.dart';
final locator = StackedLocator.instance; final locator = StackedLocator.instance;
@ -24,4 +25,5 @@ Future<void> setupLocator(
// Register dependencies // Register dependencies
locator.registerLazySingleton(() => NavigationService()); locator.registerLazySingleton(() => NavigationService());
locator.registerLazySingleton(() => PatcherService()); locator.registerLazySingleton(() => PatcherService());
locator.registerLazySingleton(() => PatcherViewModel());
} }

View File

@ -51,7 +51,7 @@ class Navigation extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return ViewModelBuilder<MainViewModel>.reactive( return ViewModelBuilder<MainViewModel>.reactive(
viewModelBuilder: () => MainViewModel(), viewModelBuilder: () => MainViewModel(),
builder: (context, MainViewModel model, child) => Scaffold( builder: (context, model, child) => Scaffold(
body: getViewForIndex(model.currentIndex), body: getViewForIndex(model.currentIndex),
bottomNavigationBar: NavigationBar( bottomNavigationBar: NavigationBar(
onDestinationSelected: model.setIndex, onDestinationSelected: model.setIndex,

View File

@ -3,7 +3,7 @@ import 'package:dio/dio.dart';
import 'package:injectable/injectable.dart'; import 'package:injectable/injectable.dart';
import 'package:path_provider/path_provider.dart' as p; import 'package:path_provider/path_provider.dart' as p;
import 'package:revanced_manager/constants.dart'; import 'package:revanced_manager/constants.dart';
import 'github_api.dart'; import 'package:revanced_manager/services/github_api.dart';
// use path_provider to get the path of the storage directory // use path_provider to get the path of the storage directory
@lazySingleton @lazySingleton

View File

@ -1,21 +1,30 @@
import 'dart:io'; import 'dart:io';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart'; import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:injectable/injectable.dart';
import 'package:installed_apps/app_info.dart'; import 'package:installed_apps/app_info.dart';
import 'package:installed_apps/installed_apps.dart'; import 'package:installed_apps/installed_apps.dart';
import 'package:revanced_manager/models/patch.dart'; import 'package:revanced_manager/models/patch.dart';
import 'package:revanced_manager/services/github_api.dart'; import 'package:revanced_manager/services/github_api.dart';
import 'package:revanced_manager/utils/string.dart'; import 'package:revanced_manager/utils/string.dart';
@lazySingleton
class PatcherService { class PatcherService {
File? _patchBundleFile; final GithubAPI githubAPI = GithubAPI();
final List<AppInfo> _filteredPackages = []; final List<AppInfo> _filteredPackages = [];
final Map<String, List<Patch>> _filteredPatches = <String, List<Patch>>{}; final Map<String, List<Patch>> _filteredPatches = <String, List<Patch>>{};
final GithubAPI githubAPI = GithubAPI(); File? _patchBundleFile;
String _selectedApp = '';
List<Patch> _selectedPatches = [];
static const platform = MethodChannel('app.revanced/patcher'); static const platform = MethodChannel('app.revanced/patcher');
static final PatcherService _instance = PatcherService.internal();
factory PatcherService() => _instance; String getSelectedApp() => _selectedApp;
PatcherService.internal();
void setSelectedApp(String app) => _selectedApp = app;
List<Patch> getSelectedPatches() => _selectedPatches;
void setSelectedPatches(List<Patch> patches) => _selectedPatches = patches;
Future<void> loadPatches() async { Future<void> loadPatches() async {
if (_patchBundleFile == null) { if (_patchBundleFile == null) {

View File

@ -1,7 +1,8 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_i18n/flutter_i18n.dart'; import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:installed_apps/app_info.dart'; import 'package:revanced_manager/app/app.locator.dart';
import 'package:installed_apps/installed_apps.dart'; import 'package:revanced_manager/services/patcher_api.dart';
import 'package:revanced_manager/ui/views/patcher/patcher_viewmodel.dart';
import 'package:revanced_manager/ui/widgets/installed_app_item.dart'; import 'package:revanced_manager/ui/widgets/installed_app_item.dart';
import 'package:revanced_manager/ui/widgets/search_bar.dart'; import 'package:revanced_manager/ui/widgets/search_bar.dart';
import 'package:stacked/stacked.dart'; import 'package:stacked/stacked.dart';
@ -15,23 +16,13 @@ class AppSelectorView extends StatefulWidget {
} }
class _AppSelectorViewState extends State<AppSelectorView> { class _AppSelectorViewState extends State<AppSelectorView> {
List<AppInfo> apps = []; final PatcherService patcherService = locator<PatcherService>();
String query = ''; String query = '';
void getApps() async {
apps = await InstalledApps.getInstalledApps(false, true);
setState(() {});
}
@override
void initState() {
getApps();
super.initState();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return ViewModelBuilder.reactive( return ViewModelBuilder<AppSelectorViewModel>.reactive(
onModelReady: (model) => model.initialise(),
builder: (context, model, child) => Scaffold( builder: (context, model, child) => Scaffold(
body: SafeArea( body: SafeArea(
child: Padding( child: Padding(
@ -51,41 +42,53 @@ class _AppSelectorViewState extends State<AppSelectorView> {
}, },
), ),
if (query.isEmpty || query.length < 2) if (query.isEmpty || query.length < 2)
apps.isEmpty model.apps.isEmpty
? const Center( ? const Center(
child: CircularProgressIndicator(), child: CircularProgressIndicator(),
) )
: Expanded( : Expanded(
child: ListView.builder( child: ListView.builder(
itemCount: apps.length, itemCount: model.apps.length,
itemBuilder: (context, index) { itemBuilder: (context, index) {
//sort alphabetically //sort alphabetically
apps.sort((a, b) => a.name!.compareTo(b.name!)); model.apps
return InstalledAppItem( .sort((a, b) => a.name!.compareTo(b.name!));
name: apps[index].name!, return InkWell(
pkgName: apps[index].packageName!, onTap: () {
icon: apps[index].icon!, patcherService.setSelectedApp(
model.apps[index].packageName!);
Navigator.of(context).pop();
locator<PatcherViewModel>().notifyListeners();
},
child: InstalledAppItem(
name: model.apps[index].name!,
pkgName: model.apps[index].packageName!,
icon: model.apps[index].icon!,
),
); );
}, },
), ),
), ),
if (query.isNotEmpty) if (query.isNotEmpty)
apps.isEmpty model.apps.isEmpty
? Center( ? Center(
child: I18nText('appSelectorCard.noAppsLabel'), child: I18nText('appSelectorCard.noAppsLabel'),
) )
: Expanded( : Expanded(
child: ListView.builder( child: ListView.builder(
itemCount: apps.length, itemCount: model.apps.length,
itemBuilder: (context, index) { itemBuilder: (context, index) {
apps.sort((a, b) => a.name!.compareTo(b.name!)); model.apps
if (apps[index].name!.toLowerCase().contains( .sort((a, b) => a.name!.compareTo(b.name!));
if (model.apps[index].name!
.toLowerCase()
.contains(
query.toLowerCase(), query.toLowerCase(),
)) { )) {
return InstalledAppItem( return InstalledAppItem(
name: apps[index].name!, name: model.apps[index].name!,
pkgName: apps[index].packageName!, pkgName: model.apps[index].packageName!,
icon: apps[index].icon!, icon: model.apps[index].icon!,
); );
} else { } else {
return const SizedBox(); return const SizedBox();

View File

@ -1,16 +1,20 @@
import 'package:installed_apps/app_info.dart'; import 'package:installed_apps/app_info.dart';
import 'package:installed_apps/installed_apps.dart'; import 'package:revanced_manager/app/app.locator.dart';
import 'package:revanced_manager/services/patcher_api.dart';
import 'package:stacked/stacked.dart'; import 'package:stacked/stacked.dart';
class AppSelectorViewModel extends BaseViewModel { class AppSelectorViewModel extends BaseViewModel {
final PatcherService patcherService = locator<PatcherService>();
List<AppInfo> apps = []; List<AppInfo> apps = [];
String query = ''; String query = '';
void initialization() { Future<void> initialise() async {
getApps(); await getApps();
notifyListeners();
} }
void getApps() async { Future<void> getApps() async {
apps = await InstalledApps.getInstalledApps(false, true); await patcherService.loadPatches();
apps = await patcherService.getFilteredInstalledApps();
} }
} }

View File

@ -1,11 +1,11 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_i18n/flutter_i18n.dart'; import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:google_fonts/google_fonts.dart'; import 'package:google_fonts/google_fonts.dart';
import 'package:revanced_manager/ui/views/home/home_viewmodel.dart';
import 'package:revanced_manager/ui/widgets/available_updates_card.dart'; import 'package:revanced_manager/ui/widgets/available_updates_card.dart';
import 'package:revanced_manager/ui/widgets/installed_apps_card.dart'; import 'package:revanced_manager/ui/widgets/installed_apps_card.dart';
import 'package:revanced_manager/ui/widgets/latest_commit_card.dart'; import 'package:revanced_manager/ui/widgets/latest_commit_card.dart';
import 'package:stacked/stacked.dart'; import 'package:stacked/stacked.dart';
import 'home_viewmodel.dart';
class HomeView extends StatelessWidget { class HomeView extends StatelessWidget {
const HomeView({Key? key}) : super(key: key); const HomeView({Key? key}) : super(key: key);

View File

@ -1,6 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_i18n/flutter_i18n.dart'; import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:google_fonts/google_fonts.dart'; import 'package:google_fonts/google_fonts.dart';
import 'package:revanced_manager/app/app.locator.dart';
import 'package:revanced_manager/ui/widgets/app_selector_card.dart'; import 'package:revanced_manager/ui/widgets/app_selector_card.dart';
import 'package:revanced_manager/ui/widgets/patch_selector_card.dart'; import 'package:revanced_manager/ui/widgets/patch_selector_card.dart';
import 'package:stacked/stacked.dart'; import 'package:stacked/stacked.dart';
@ -12,8 +13,8 @@ class PatcherView extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return ViewModelBuilder.reactive( return ViewModelBuilder<PatcherViewModel>.reactive(
builder: (context, PatcherViewModel model, child) => Scaffold( builder: (context, model, child) => Scaffold(
floatingActionButton: FloatingActionButton( floatingActionButton: FloatingActionButton(
onPressed: () {}, onPressed: () {},
child: const Icon( child: const Icon(
@ -51,7 +52,7 @@ class PatcherView extends StatelessWidget {
), ),
), ),
), ),
viewModelBuilder: () => PatcherViewModel(), viewModelBuilder: () => locator<PatcherViewModel>(),
); );
} }
} }

View File

@ -18,9 +18,9 @@ class _PatchesSelectorViewState extends State<PatchesSelectorView> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return ViewModelBuilder.reactive( return ViewModelBuilder<PatchesSelectorViewModel>.reactive(
viewModelBuilder: () => PatchesSelectorViewModel(), viewModelBuilder: () => PatchesSelectorViewModel(),
builder: (context, PatchesSelectorViewModel model, child) => Scaffold( builder: (context, model, child) => Scaffold(
body: Container( body: Container(
margin: const EdgeInsets.fromLTRB(6.0, 26.0, 6.0, 0), margin: const EdgeInsets.fromLTRB(6.0, 26.0, 6.0, 0),
child: Column( child: Column(

View File

@ -1,23 +1,21 @@
import 'package:installed_apps/app_info.dart'; import 'package:installed_apps/app_info.dart';
import 'package:installed_apps/installed_apps.dart'; import 'package:installed_apps/installed_apps.dart';
import 'package:revanced_manager/app/app.locator.dart';
import 'package:revanced_manager/models/patch.dart'; import 'package:revanced_manager/models/patch.dart';
import 'package:revanced_manager/services/patcher_api.dart'; import 'package:revanced_manager/services/patcher_api.dart';
import 'package:stacked/stacked.dart'; import 'package:stacked/stacked.dart';
class PatchesSelectorViewModel extends BaseViewModel { class PatchesSelectorViewModel extends BaseViewModel {
PatcherService patcherService = PatcherService(); final PatcherService patcherService = locator<PatcherService>();
List<Patch>? patches = [];
AppInfo? appInfo; AppInfo? appInfo;
Future<void> getApp() async { Future<void> getApp() async {
AppInfo app = await InstalledApps.getAppInfo("com.google.android.youtube"); AppInfo app = await InstalledApps.getAppInfo("com.google.android.youtube");
appInfo = app; appInfo = app;
} }
Future<List<Patch>?>? getPatches() async {
Future<List<Patch>?> getPatches() async {
getApp(); getApp();
PatcherService patcherService = PatcherService();
patcherService.loadPatches();
return patcherService.getFilteredPatches(appInfo); return patcherService.getFilteredPatches(appInfo);
} }
}
}

View File

@ -1,15 +1,19 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_i18n/flutter_i18n.dart'; import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:google_fonts/google_fonts.dart'; import 'package:google_fonts/google_fonts.dart';
import 'package:revanced_manager/app/app.locator.dart';
import 'package:revanced_manager/constants.dart'; import 'package:revanced_manager/constants.dart';
import 'package:revanced_manager/services/patcher_api.dart';
class AppSelectorCard extends StatelessWidget { class AppSelectorCard extends StatelessWidget {
final Function()? onPressed; final Function()? onPressed;
const AppSelectorCard({ AppSelectorCard({
Key? key, Key? key,
this.onPressed, this.onPressed,
}) : super(key: key); }) : super(key: key);
final PatcherService patcherService = locator<PatcherService>();
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return GestureDetector( return GestureDetector(
@ -35,13 +39,18 @@ class AppSelectorCard extends StatelessWidget {
), ),
), ),
const SizedBox(height: 10), const SizedBox(height: 10),
I18nText( patcherService.getSelectedApp().isNotEmpty
'appSelectorCard.widgetSubtitle', ? Text(
child: Text( patcherService.getSelectedApp(),
'', style: robotoTextStyle,
style: robotoTextStyle, )
), : I18nText(
), 'appSelectorCard.widgetSubtitle',
child: Text(
'',
style: robotoTextStyle,
),
),
], ],
), ),
), ),

View File

@ -23,52 +23,49 @@ class InstalledAppItem extends StatefulWidget {
class _InstalledAppItemState extends State<InstalledAppItem> { class _InstalledAppItemState extends State<InstalledAppItem> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return InkWell( return Padding(
onTap: () => Navigator.pop(context), padding: const EdgeInsets.symmetric(vertical: 4.0),
child: Padding( child: Container(
padding: const EdgeInsets.symmetric(vertical: 4.0), padding: const EdgeInsets.all(12.0),
child: Container( decoration: BoxDecoration(
padding: const EdgeInsets.all(12.0), borderRadius: BorderRadius.circular(12),
decoration: BoxDecoration( color: const Color(0xff1B222B),
borderRadius: BorderRadius.circular(12), ),
color: const Color(0xff1B222B), child: Row(
), mainAxisAlignment: MainAxisAlignment.spaceBetween,
child: Row( children: [
mainAxisAlignment: MainAxisAlignment.spaceBetween, Container(
children: [ width: 48,
Container( height: 48,
width: 48, padding: const EdgeInsets.symmetric(vertical: 4.0),
height: 48, alignment: Alignment.center,
padding: const EdgeInsets.symmetric(vertical: 4.0), child: CircleAvatar(
alignment: Alignment.center, child: Image.memory(widget.icon),
child: CircleAvatar(
child: Image.memory(widget.icon),
),
), ),
const SizedBox(width: 12), ),
Expanded( const SizedBox(width: 12),
child: Column( Expanded(
crossAxisAlignment: CrossAxisAlignment.start, child: Column(
children: [ crossAxisAlignment: CrossAxisAlignment.start,
Text( children: [
widget.name, Text(
maxLines: 2, widget.name,
overflow: TextOverflow.visible, maxLines: 2,
style: GoogleFonts.inter( overflow: TextOverflow.visible,
fontSize: 16, style: GoogleFonts.inter(
fontWeight: FontWeight.w500, fontSize: 16,
), fontWeight: FontWeight.w500,
), ),
const SizedBox(height: 4), ),
Text( const SizedBox(height: 4),
widget.pkgName, Text(
style: robotoTextStyle, widget.pkgName,
), style: robotoTextStyle,
], ),
), ],
), ),
], ),
), ],
), ),
), ),
); );

View File

@ -1,6 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart'; import 'package:google_fonts/google_fonts.dart';
// ignore: must_be_immutable
class PatchItem extends StatefulWidget { class PatchItem extends StatefulWidget {
final String name; final String name;
final String description; final String description;