Skip to content

Commit

Permalink
supporting import/export of grid
Browse files Browse the repository at this point in the history
  • Loading branch information
nkitsaini committed Apr 3, 2024
1 parent 4679fea commit 029fc8a
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 19 deletions.
43 changes: 30 additions & 13 deletions lib/app_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ import 'package:flutter/material.dart';
// import 'package:json_serializable/json_serializable.dart';

class CacheData {
final List<GridApp> grid;
List<App> apps;
List<GridApp> grid;
Map<String, App> apps;

CacheData(this.grid, this.apps);

factory CacheData.fromJson(Map<String, dynamic> json) => CacheData(
(json['grid'] as List).map((x) => gridAppFromJson(x)).toList(),
(json['apps'] as List).map((x) => App.fromJson(x)).toList());
factory CacheData.fromJson(Map<String, dynamic> json) {
var apps = (json['apps'] as Map<String, dynamic>)
.map((x, y) => MapEntry(x, App.fromJson(y)));

List<GridApp> grid =
(json['grid'] as List).map((x) => gridAppFromJson(x)).toList();

return CacheData(grid, apps);
}

static gridAppToJson(GridApp gridApp) {
return gridApp.toJson();
Expand All @@ -27,23 +33,23 @@ class CacheData {
Map<String, dynamic> toJson() {
return {
'grid': grid.map((x) => gridAppToJson(x)).toList(),
'apps': apps.map((x) => x.toJson()).toList()
'apps': apps.map((x, y) => MapEntry(x, y.toJson()))
};
}
}

class GridApp {
final App app;
final String packageName;
String? iconSlug;

GridApp(this.app, this.iconSlug);
GridApp(this.packageName, this.iconSlug);

factory GridApp.fromJson(Map<String, dynamic> json) {
return GridApp(App.fromJson(json['app']), json['iconSlug']);
return GridApp(json['packageName'], json['iconSlug']);
}

Map<String, dynamic> toJson() {
return {'app': app.toJson(), 'iconSlug': iconSlug};
return {'packageName': packageName, 'iconSlug': iconSlug};
}
}

Expand Down Expand Up @@ -78,7 +84,7 @@ class AppListCacher extends ChangeNotifier {
}
}

Future<List<App>> getAppList() async {
Future<Map<String, App>> getAppList() async {
if (data == null) {
await readCache();
}
Expand Down Expand Up @@ -107,12 +113,23 @@ class AppListCacher extends ChangeNotifier {
notifyListeners();
}

setGridCache(List<GridApp> apps) async {
if (data == null) {
await updateCache();
}
data!.grid = apps;
flushChanges();
}

Future updateCache() async {
print("updating cache");
List<App> apps = (await _readApps())
List<App> appList = (await _readApps())
.where((x) => x.enabled)
.map((x) => App.fromApplication(x))
.toList();
Map<String, App> apps = {};
for (final app in appList) {
apps[app.packageName] = app;
}
if (data == null) {
data = CacheData([], apps);
} else {
Expand Down
48 changes: 42 additions & 6 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import 'dart:convert';
import 'dart:io';

import 'package:android_intent_plus/android_intent.dart';
import 'package:android_intent_plus/flag.dart';
import 'package:flutter/material.dart';
import 'package:device_apps/device_apps.dart';
import 'package:flutter_file_dialog/flutter_file_dialog.dart';
import 'package:nkit_launcher/app_icon.dart';
import 'package:provider/provider.dart';
import 'package:flutter_iconpicker/flutter_iconpicker.dart';
Expand Down Expand Up @@ -143,6 +147,28 @@ class _MyHomePageState extends State<MyHomePage> {
switch (action) {
case GlobalAction.refreshApps:
await appList.updateCache();
case GlobalAction.exportGridJson:
if (appList.data == null) {
return;
}
var grid = appList.data!.grid.map((x) => x.toJson()).toList();
final body = json.encode(grid);
final params = SaveFileDialogParams(
data: const Utf8Encoder().convert(body),
fileName: "nkit_launcher_grid.json");
await FlutterFileDialog.saveFile(params: params);
case GlobalAction.importGridJson:
var file = await FlutterFileDialog.pickFile(
params: const OpenFileDialogParams());
if (file != null) {
var fileContent = File(file).readAsStringSync();
var content = json.decode(fileContent);

appList.setGridCache((content as List)
.map((x) => GridApp.fromJson(x))
.toList());
}
await appList.updateCache();
case null:
}
},
Expand Down Expand Up @@ -195,8 +221,10 @@ class IconAppGridWidget extends StatelessWidget {
}

longPressActionHandler(int index) async {
GridIconAction? action = await askGridIconAction(
context, appList.data!.grid[index].app.appName);
var item = appList.data!.grid[index];
var app = appList.data!.apps[item.packageName];
GridIconAction? action =
await askGridIconAction(context, app?.appName ?? "Unknown");
switch (action) {
case GridIconAction.selectIcon:
IconData? icon =
Expand Down Expand Up @@ -239,7 +267,7 @@ class IconAppGridWidget extends StatelessWidget {
button = IconButton(
icon: icon,
onPressed: () {
DeviceApps.openApp(app.app.packageName);
DeviceApps.openApp(app.packageName);
},
);

Expand Down Expand Up @@ -352,7 +380,7 @@ Future<GridIconAction?> askGridIconAction(
});
}

enum GlobalAction { refreshApps }
enum GlobalAction { refreshApps, exportGridJson, importGridJson }

Future<GlobalAction?> askGlobalAction(BuildContext context) async {
return await showDialog<GlobalAction>(
Expand All @@ -362,6 +390,14 @@ Future<GlobalAction?> askGlobalAction(BuildContext context) async {
SimpleDialogOption(
onPressed: () => Navigator.pop(context, GlobalAction.refreshApps),
child: const Text('Refresh Apps')),
SimpleDialogOption(
onPressed: () =>
Navigator.pop(context, GlobalAction.exportGridJson),
child: const Text('Export Grid Action')),
SimpleDialogOption(
onPressed: () =>
Navigator.pop(context, GlobalAction.importGridJson),
child: const Text('Import Grid Action')),
];
return SimpleDialog(children: children);
});
Expand Down Expand Up @@ -399,7 +435,7 @@ class AppListState extends State<AppListWidget> {
);
}

List<App> apps = appList.data!.apps.toList();
List<App> apps = appList.data!.apps.values.toList();
if (widget.filterKey == null) {
return const Row();
}
Expand Down Expand Up @@ -439,7 +475,7 @@ class AppListState extends State<AppListWidget> {
);
intent.launch();
case AppAction.addToGrid:
appList.data!.grid.add(GridApp(app, null));
appList.data!.grid.add(GridApp(app.packageName, null));
await appList.flushChanges();
case null:
}
Expand Down
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_file_dialog:
dependency: "direct main"
description:
name: flutter_file_dialog
sha256: "9344b8f07be6a1b6f9854b723fb0cf84a8094ba94761af1d213589d3cb087488"
url: "https://pub.dev"
source: hosted
version: "3.0.2"
flutter_iconpicker:
dependency: "direct main"
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ dependencies:
android_intent_plus: ^5.0.1
provider: ^6.1.2
flutter_iconpicker: ^3.4.4
flutter_file_dialog: ^3.0.2

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 029fc8a

Please sign in to comment.