feat: working patches selector and improve app selector.

This commit is contained in:
Alberto Ponces 2022-08-09 01:16:33 +01:00
parent 51801b5748
commit 33fb2a81b5
14 changed files with 228 additions and 188 deletions

View File

@ -27,7 +27,8 @@
"updateButton": "Update Manager"
},
"patcherView": {
"widgetTitle": "Patcher"
"widgetTitle": "Patcher",
"fabButton": "Patch"
},
"appSelectorCard": {
"widgetTitle": "Select application",
@ -42,6 +43,7 @@
"searchBarHint": "Search applications"
},
"patchesSelectorView": {
"searchBarHint": "Search patches"
"searchBarHint": "Search patches",
"fabButton": "Done"
}
}

View File

@ -12,7 +12,9 @@ import 'package:stacked_core/stacked_core.dart';
import 'package:stacked_services/src/navigation/navigation_service.dart';
import '../services/patcher_api.dart';
import '../ui/views/app_selector/app_selector_viewmodel.dart';
import '../ui/views/patcher/patcher_viewmodel.dart';
import '../ui/views/patches_selector/patches_selector_viewmodel.dart';
final locator = StackedLocator.instance;
@ -26,4 +28,6 @@ Future<void> setupLocator(
locator.registerLazySingleton(() => NavigationService());
locator.registerLazySingleton(() => PatcherService());
locator.registerLazySingleton(() => PatcherViewModel());
locator.registerLazySingleton(() => AppSelectorViewModel());
locator.registerLazySingleton(() => PatchesSelectorViewModel());
}

View File

@ -7,23 +7,34 @@ class GithubAPI {
var github = GitHub();
Future<String?> latestRelease(String org, repoName) async {
String? dlurl = '';
try {
var latestRelease = await github.repositories.getLatestRelease(
RepositorySlug(org, repoName),
);
var dlurl = latestRelease.assets
dlurl = latestRelease.assets
?.firstWhere((asset) =>
asset.name != null &&
(asset.name!.endsWith('.dex') || asset.name!.endsWith('.apk')) &&
!asset.name!.contains('-sources') &&
!asset.name!.contains('-javadoc'))
.browserDownloadUrl;
} on Exception {
dlurl = '';
}
return dlurl;
}
Future<String> latestCommitTime(String org, repoName) async {
String pushedAt = '';
try {
var repo = await github.repositories.getRepository(
RepositorySlug(org, repoName),
);
return format(repo.pushedAt!);
pushedAt = repo.pushedAt != null ? format(repo.pushedAt!) : '';
} on Exception {
pushedAt = '';
}
return pushedAt;
}
}

View File

@ -14,23 +14,13 @@ class PatcherService {
final List<AppInfo> _filteredPackages = [];
final Map<String, List<Patch>> _filteredPatches = <String, List<Patch>>{};
File? _patchBundleFile;
String _selectedApp = '';
List<Patch> _selectedPatches = [];
static const platform = MethodChannel('app.revanced/patcher');
String getSelectedApp() => _selectedApp;
void setSelectedApp(String app) => _selectedApp = app;
List<Patch> getSelectedPatches() => _selectedPatches;
void setSelectedPatches(List<Patch> patches) => _selectedPatches = patches;
Future<void> loadPatches() async {
if (_patchBundleFile == null) {
String? dexFileUrl =
await githubAPI.latestRelease('revanced', 'revanced-patches');
if (dexFileUrl != null) {
if (dexFileUrl != null && dexFileUrl.isNotEmpty) {
_patchBundleFile =
await DefaultCacheManager().getSingleFile(dexFileUrl);
try {

View File

@ -1,8 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:revanced_manager/app/app.locator.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/search_bar.dart';
import 'package:stacked/stacked.dart';
@ -16,19 +14,21 @@ class AppSelectorView extends StatefulWidget {
}
class _AppSelectorViewState extends State<AppSelectorView> {
final PatcherService patcherService = locator<PatcherService>();
String query = '';
@override
Widget build(BuildContext context) {
return ViewModelBuilder<AppSelectorViewModel>.reactive(
disposeViewModel: false,
onModelReady: (model) => model.initialise(),
viewModelBuilder: () => locator<AppSelectorViewModel>(),
builder: (context, model, child) => Scaffold(
body: SafeArea(
child: Padding(
padding:
const EdgeInsets.symmetric(vertical: 4.0, horizontal: 12.0),
child: Column(
child: model.apps.isNotEmpty
? Column(
children: [
SearchBar(
hintText: FlutterI18n.translate(
@ -41,24 +41,37 @@ class _AppSelectorViewState extends State<AppSelectorView> {
});
},
),
if (query.isEmpty || query.length < 2)
model.apps.isEmpty
? const Center(
child: CircularProgressIndicator(),
const SizedBox(height: 12),
query.isEmpty || query.length < 2
? _getAllResults(model)
: _getFilteredResults(model)
],
)
: Expanded(
: query.isEmpty || query.length < 2
? const Center(
child: CircularProgressIndicator(
color: Color(0xff7792BA),
),
)
: Center(
child: I18nText('appSelectorCard.noAppsLabel'),
),
),
),
),
);
}
Widget _getAllResults(AppSelectorViewModel model) {
return Expanded(
child: ListView.builder(
itemCount: model.apps.length,
itemBuilder: (context, index) {
//sort alphabetically
model.apps
.sort((a, b) => a.name!.compareTo(b.name!));
model.apps.sort((a, b) => a.name!.compareTo(b.name!));
return InkWell(
onTap: () {
patcherService.setSelectedApp(
model.apps[index].packageName!);
model.selectApp(model.apps[index]);
Navigator.of(context).pop();
locator<PatcherViewModel>().notifyListeners();
},
child: InstalledAppItem(
name: model.apps[index].name!,
@ -68,40 +81,34 @@ class _AppSelectorViewState extends State<AppSelectorView> {
);
},
),
),
if (query.isNotEmpty)
model.apps.isEmpty
? Center(
child: I18nText('appSelectorCard.noAppsLabel'),
)
: Expanded(
);
}
Widget _getFilteredResults(AppSelectorViewModel model) {
return Expanded(
child: ListView.builder(
itemCount: model.apps.length,
itemBuilder: (context, index) {
model.apps
.sort((a, b) => a.name!.compareTo(b.name!));
if (model.apps[index].name!
.toLowerCase()
.contains(
model.apps.sort((a, b) => a.name!.compareTo(b.name!));
if (model.apps[index].name!.toLowerCase().contains(
query.toLowerCase(),
)) {
return InstalledAppItem(
return InkWell(
onTap: () {
model.selectApp(model.apps[index]);
Navigator.of(context).pop();
},
child: InstalledAppItem(
name: model.apps[index].name!,
pkgName: model.apps[index].packageName!,
icon: model.apps[index].icon!,
),
);
} else {
return const SizedBox();
}
},
),
),
],
),
),
),
),
viewModelBuilder: () => AppSelectorViewModel(),
);
}
}

View File

@ -1,12 +1,13 @@
import 'package:installed_apps/app_info.dart';
import 'package:revanced_manager/app/app.locator.dart';
import 'package:revanced_manager/services/patcher_api.dart';
import 'package:revanced_manager/ui/views/patcher/patcher_viewmodel.dart';
import 'package:stacked/stacked.dart';
class AppSelectorViewModel extends BaseViewModel {
final PatcherService patcherService = locator<PatcherService>();
List<AppInfo> apps = [];
String query = '';
AppInfo? selectedApp;
Future<void> initialise() async {
await getApps();
@ -17,4 +18,9 @@ class AppSelectorViewModel extends BaseViewModel {
await patcherService.loadPatches();
apps = await patcherService.getFilteredInstalledApps();
}
void selectApp(AppInfo appInfo) {
locator<AppSelectorViewModel>().selectedApp = appInfo;
locator<PatcherViewModel>().notifyListeners();
}
}

View File

@ -13,6 +13,7 @@ class HomeView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ViewModelBuilder.reactive(
viewModelBuilder: () => HomeViewModel(),
builder: (context, model, child) => Scaffold(
body: SafeArea(
child: SingleChildScrollView(
@ -24,7 +25,7 @@ class HomeView extends StatelessWidget {
Align(
alignment: Alignment.topRight,
child: IconButton(
onPressed: () {},
onPressed: () => {},
icon: const Icon(
Icons.more_vert,
),
@ -72,7 +73,6 @@ class HomeView extends StatelessWidget {
),
),
),
viewModelBuilder: () => HomeViewModel(),
);
}
}

View File

@ -14,13 +14,15 @@ class PatcherView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ViewModelBuilder<PatcherViewModel>.reactive(
disposeViewModel: false,
viewModelBuilder: () => locator<PatcherViewModel>(),
builder: (context, model, child) => Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: const Icon(
Icons.build,
color: Colors.white,
),
floatingActionButton: FloatingActionButton.extended(
onPressed: () => {},
label: I18nText('patcherView.fabButton'),
icon: const Icon(Icons.build),
backgroundColor: const Color(0xff7792BA),
foregroundColor: Colors.white,
),
body: SafeArea(
child: Padding(
@ -52,7 +54,6 @@ class PatcherView extends StatelessWidget {
),
),
),
viewModelBuilder: () => locator<PatcherViewModel>(),
);
}
}

View File

@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:revanced_manager/models/patch.dart';
import 'package:revanced_manager/app/app.locator.dart';
import 'package:revanced_manager/ui/views/patches_selector/patches_selector_viewmodel.dart';
import 'package:revanced_manager/ui/widgets/patch_item.dart';
import 'package:revanced_manager/ui/widgets/search_bar.dart';
@ -19,74 +19,89 @@ class _PatchesSelectorViewState extends State<PatchesSelectorView> {
@override
Widget build(BuildContext context) {
return ViewModelBuilder<PatchesSelectorViewModel>.reactive(
viewModelBuilder: () => PatchesSelectorViewModel(),
disposeViewModel: false,
onModelReady: (model) => model.initialise(),
viewModelBuilder: () => locator<PatchesSelectorViewModel>(),
builder: (context, model, child) => Scaffold(
body: Container(
margin: const EdgeInsets.fromLTRB(6.0, 26.0, 6.0, 0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
floatingActionButton: FloatingActionButton.extended(
onPressed: () => {},
label: I18nText('patchesSelectorView.fabButton'),
icon: const Icon(Icons.check),
backgroundColor: const Color(0xff7792BA),
foregroundColor: Colors.white,
),
body: SafeArea(
child: Padding(
padding:
const EdgeInsets.symmetric(vertical: 4.0, horizontal: 12.0),
child: model.patches != null && model.patches!.isNotEmpty
? Column(
children: [
Padding(
padding: const EdgeInsets.only(top: 8.0, left: 8.0, right: 8.0),
child: SearchBar(
SearchBar(
hintText: FlutterI18n.translate(
context,
'patchesSelectorView.searchBarHint',
),
onQueryChanged: (searchQuery) {
setState(
() {
setState(() {
query = searchQuery;
},
);
});
},
),
),
Expanded(
child: FutureBuilder<List<Patch>?>(
future: model.getPatches(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data!.length,
itemBuilder: (context, index) {
if (query.isEmpty || query.length < 2) {
return PatchItem(
name: snapshot.data![index].simpleName,
version: snapshot.data![index].version,
description: snapshot.data![index].description,
isSelected: false,
);
} else if (query.isNotEmpty &&
query.length >= 2 &&
snapshot.data![index].simpleName
.toLowerCase()
.contains(query.toLowerCase())) {
return PatchItem(
name: snapshot.data![index].simpleName,
version: snapshot.data![index].version,
description: snapshot.data![index].description,
isSelected: false,
);
} else {
return Container();
}
},
);
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
} else {
return const Center(
child: CircularProgressIndicator(),
);
}
},
),
),
const SizedBox(height: 12),
query.isEmpty || query.length < 2
? _getAllResults(model)
: _getFilteredResults(model)
],
)
: const Center(
child: CircularProgressIndicator(
color: Color(0xff7792BA),
),
),
),
),
),
);
}
Widget _getAllResults(PatchesSelectorViewModel model) {
return Expanded(
child: ListView.builder(
itemCount: model.patches!.length,
itemBuilder: (context, index) {
model.patches!.sort((a, b) => a.simpleName.compareTo(b.simpleName));
return PatchItem(
name: model.patches![index].simpleName,
version: model.patches![index].version,
description: model.patches![index].description,
isSelected: false,
);
},
),
);
}
Widget _getFilteredResults(PatchesSelectorViewModel model) {
return Expanded(
child: ListView.builder(
itemCount: model.patches!.length,
itemBuilder: (context, index) {
model.patches!.sort((a, b) => a.simpleName.compareTo(b.simpleName));
if (model.patches![index].simpleName.toLowerCase().contains(
query.toLowerCase(),
)) {
return PatchItem(
name: model.patches![index].simpleName,
version: model.patches![index].version,
description: model.patches![index].description,
isSelected: false,
);
} else {
return const SizedBox();
}
},
),
);
}
}

View File

@ -1,21 +1,24 @@
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/models/patch.dart';
import 'package:revanced_manager/services/patcher_api.dart';
import 'package:revanced_manager/ui/views/app_selector/app_selector_viewmodel.dart';
import 'package:stacked/stacked.dart';
class PatchesSelectorViewModel extends BaseViewModel {
final PatcherService patcherService = locator<PatcherService>();
AppInfo? appInfo;
List<Patch>? patches = [];
List<Patch> selectedPatches = [];
Future<void> getApp() async {
AppInfo app = await InstalledApps.getAppInfo("com.google.android.youtube");
appInfo = app;
Future<void> initialise() async {
await getPatches();
notifyListeners();
}
Future<List<Patch>?> getPatches() async {
getApp();
return patcherService.getFilteredPatches(appInfo);
Future<void> getPatches() async {
AppInfo? appInfo = locator<AppSelectorViewModel>().selectedApp;
patches = await patcherService.getFilteredPatches(appInfo);
}
void selectPatches(List<Patch> patches) {}
}

View File

@ -4,6 +4,7 @@ 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/services/patcher_api.dart';
import 'package:revanced_manager/ui/views/app_selector/app_selector_viewmodel.dart';
class AppSelectorCard extends StatelessWidget {
final Function()? onPressed;
@ -39,9 +40,9 @@ class AppSelectorCard extends StatelessWidget {
),
),
const SizedBox(height: 10),
patcherService.getSelectedApp().isNotEmpty
locator<AppSelectorViewModel>().selectedApp != null
? Text(
patcherService.getSelectedApp(),
locator<AppSelectorViewModel>().selectedApp!.packageName!,
style: robotoTextStyle,
)
: I18nText(

View File

@ -38,7 +38,7 @@ class AvailableUpdatesCard extends StatelessWidget {
context,
'availableUpdatesCard.patchButton',
),
onPressed: () {},
onPressed: () => {},
backgroundColor: const Color(0xff7792BA),
),
],
@ -47,13 +47,13 @@ class AvailableUpdatesCard extends StatelessWidget {
asset: 'assets/images/revanced.svg',
name: 'ReVanced',
releaseDate: '2 days ago',
onPressed: () {},
onPressed: () => {},
),
ApplicationItem(
asset: 'assets/images/reddit.png',
name: 'ReReddit',
releaseDate: 'Released 1 month ago',
onPressed: () {},
onPressed: () => {},
),
const SizedBox(height: 4),
I18nText(

View File

@ -33,7 +33,7 @@ class InstalledAppsCard extends StatelessWidget {
asset: 'assets/images/revanced.svg',
name: 'ReVanced',
releaseDate: '2 days ago',
onPressed: () {},
onPressed: () => {},
),
I18nText(
'installedAppsCard.changelogLabel',

View File

@ -90,7 +90,7 @@ class _LatestCommitCardState extends State<LatestCommitCard> {
context,
'latestCommitCard.updateButton',
),
onPressed: () {},
onPressed: () => {},
backgroundColor: const Color(0xff7792BA),
),
],