sorting fix
This commit is contained in:
parent
dc7af87493
commit
f87e554df0
1 changed files with 72 additions and 19 deletions
|
|
@ -43,6 +43,8 @@ class _MyAppState extends State<MyApp> {
|
||||||
|
|
||||||
Future<void> _loadSettings() async {
|
Future<void> _loadSettings() async {
|
||||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||||
|
bool? savedSorting = prefs.getBool('sortAlphabetically');
|
||||||
|
print('Saved sorting: $savedSorting'); // Debug print
|
||||||
setState(() {
|
setState(() {
|
||||||
settings.showTags = prefs.getBool('showTags') ?? true;
|
settings.showTags = prefs.getBool('showTags') ?? true;
|
||||||
settings.showIcons = prefs.getBool('showIcons') ?? true;
|
settings.showIcons = prefs.getBool('showIcons') ?? true;
|
||||||
|
|
@ -98,12 +100,14 @@ class AppInfo {
|
||||||
final String name;
|
final String name;
|
||||||
final String packageName;
|
final String packageName;
|
||||||
final List<String> tags;
|
final List<String> tags;
|
||||||
|
DateTime? lastUsed; // New field to track last used time
|
||||||
|
|
||||||
AppInfo({
|
AppInfo({
|
||||||
this.icon,
|
this.icon,
|
||||||
required this.name,
|
required this.name,
|
||||||
required this.packageName,
|
required this.packageName,
|
||||||
required this.tags,
|
required this.tags,
|
||||||
|
this.lastUsed,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -131,25 +135,31 @@ class _AppLauncherScreenState extends State<AppLauncherScreen> {
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _loadInstalledApps() async {
|
Future<void> _loadInstalledApps() async {
|
||||||
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||||
List<Application> installedApps = await DeviceApps.getInstalledApplications(
|
List<Application> installedApps = await DeviceApps.getInstalledApplications(
|
||||||
includeAppIcons: true,
|
includeAppIcons: true,
|
||||||
onlyAppsWithLaunchIntent: true,
|
onlyAppsWithLaunchIntent: true,
|
||||||
);
|
);
|
||||||
|
|
||||||
List<AppInfo> loadedApps = installedApps.map((app) {
|
List<AppInfo> loadedApps = installedApps.map((app) {
|
||||||
|
final lastUsedMillis = prefs.getInt("lastUsed_${app.packageName}");
|
||||||
return AppInfo(
|
return AppInfo(
|
||||||
icon: app is ApplicationWithIcon ? app.icon : null,
|
icon: app is ApplicationWithIcon ? app.icon : null,
|
||||||
name: app.appName,
|
name: app.appName,
|
||||||
packageName: app.packageName,
|
packageName: app.packageName,
|
||||||
tags: [],
|
tags: [],
|
||||||
|
lastUsed: lastUsedMillis != null
|
||||||
|
? DateTime.fromMillisecondsSinceEpoch(lastUsedMillis)
|
||||||
|
: null,
|
||||||
);
|
);
|
||||||
}).toList();
|
}).toList();
|
||||||
|
|
||||||
setState(() {
|
setState(() {
|
||||||
apps = loadedApps;
|
apps = loadedApps;
|
||||||
filteredApps = apps;
|
|
||||||
isLoading = false;
|
isLoading = false;
|
||||||
});
|
});
|
||||||
|
// Force initial sorting (by last used if sortAlphabetically is false)
|
||||||
|
_filterApps();
|
||||||
}
|
}
|
||||||
|
|
||||||
void _filterApps() {
|
void _filterApps() {
|
||||||
|
|
@ -163,17 +173,38 @@ class _AppLauncherScreenState extends State<AppLauncherScreen> {
|
||||||
|
|
||||||
if (widget.settings.sortAlphabetically) {
|
if (widget.settings.sortAlphabetically) {
|
||||||
filteredApps.sort((a, b) => a.name.compareTo(b.name));
|
filteredApps.sort((a, b) => a.name.compareTo(b.name));
|
||||||
|
print('Sorting alphabetically');
|
||||||
|
} else {
|
||||||
|
filteredApps.sort((a, b) {
|
||||||
|
// If both have a lastUsed timestamp, sort descending.
|
||||||
|
if (a.lastUsed != null && b.lastUsed != null) {
|
||||||
|
int cmp = b.lastUsed!.compareTo(a.lastUsed!);
|
||||||
|
if (cmp != 0) return cmp;
|
||||||
|
return a.name.compareTo(b.name);
|
||||||
}
|
}
|
||||||
|
// If one has a timestamp, that one comes first.
|
||||||
if (filteredApps.length == 1 && query.isNotEmpty) {
|
if (a.lastUsed != null) return -1;
|
||||||
_launchApp(filteredApps.first);
|
if (b.lastUsed != null) return 1;
|
||||||
|
// Both null: fallback alphabetical.
|
||||||
|
return a.name.compareTo(b.name);
|
||||||
|
});
|
||||||
|
print('Sorting by last used, with fallback alphabetical');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _launchApp(AppInfo app) async {
|
Future<void> _launchApp(AppInfo app) async {
|
||||||
bool launched = await DeviceApps.openApp(app.packageName);
|
bool launched = await DeviceApps.openApp(app.packageName);
|
||||||
if (!launched) {
|
if (launched) {
|
||||||
|
DateTime now = DateTime.now();
|
||||||
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||||
|
await prefs.setInt("lastUsed_${app.packageName}", now.millisecondsSinceEpoch);
|
||||||
|
// Update the lastUsed timestamp and re-sort immediately.
|
||||||
|
setState(() {
|
||||||
|
app.lastUsed = now;
|
||||||
|
_filterApps();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
SnackBar(content: Text('Could not launch ${app.name}')),
|
SnackBar(content: Text('Could not launch ${app.name}')),
|
||||||
);
|
);
|
||||||
|
|
@ -199,7 +230,8 @@ class _AppLauncherScreenState extends State<AppLauncherScreen> {
|
||||||
}
|
}
|
||||||
|
|
||||||
void _editAppTags(AppInfo app) {
|
void _editAppTags(AppInfo app) {
|
||||||
TextEditingController tagController = TextEditingController(text: app.tags.join(" "));
|
TextEditingController tagController =
|
||||||
|
TextEditingController(text: app.tags.join(" "));
|
||||||
showDialog(
|
showDialog(
|
||||||
context: context,
|
context: context,
|
||||||
builder: (_) => AlertDialog(
|
builder: (_) => AlertDialog(
|
||||||
|
|
@ -216,7 +248,8 @@ class _AppLauncherScreenState extends State<AppLauncherScreen> {
|
||||||
setState(() {
|
setState(() {
|
||||||
app.tags
|
app.tags
|
||||||
..clear()
|
..clear()
|
||||||
..addAll(tagController.text.split(" ").where((t) => t.isNotEmpty));
|
..addAll(
|
||||||
|
tagController.text.split(" ").where((t) => t.isNotEmpty));
|
||||||
});
|
});
|
||||||
Navigator.of(context).pop();
|
Navigator.of(context).pop();
|
||||||
_filterApps();
|
_filterApps();
|
||||||
|
|
@ -336,36 +369,54 @@ class _AppLauncherScreenState extends State<AppLauncherScreen> {
|
||||||
PopupMenuItem<String>(
|
PopupMenuItem<String>(
|
||||||
value: 'Toggle Tags',
|
value: 'Toggle Tags',
|
||||||
child: ListTile(
|
child: ListTile(
|
||||||
leading: Icon(widget.settings.showTags ? Icons.short_text : Icons.tag),
|
leading: Icon(widget.settings.showTags
|
||||||
title: Text(widget.settings.showTags ? 'Hide Tags' : 'Show Tags'),
|
? Icons.short_text
|
||||||
|
: Icons.tag),
|
||||||
|
title: Text(widget.settings.showTags
|
||||||
|
? 'Hide Tags'
|
||||||
|
: 'Show Tags'),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
PopupMenuItem<String>(
|
PopupMenuItem<String>(
|
||||||
value: 'Toggle Icons',
|
value: 'Toggle Icons',
|
||||||
child: ListTile(
|
child: ListTile(
|
||||||
leading: Icon(widget.settings.showIcons ? Icons.apps : Icons.apps_outlined),
|
leading: Icon(widget.settings.showIcons
|
||||||
title: Text(widget.settings.showIcons ? 'Hide Icons' : 'Show Icons'),
|
? Icons.apps
|
||||||
|
: Icons.apps_outlined),
|
||||||
|
title: Text(widget.settings.showIcons
|
||||||
|
? 'Hide Icons'
|
||||||
|
: 'Show Icons'),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
PopupMenuItem<String>(
|
PopupMenuItem<String>(
|
||||||
value: 'Toggle Color Icons',
|
value: 'Toggle Color Icons',
|
||||||
child: ListTile(
|
child: ListTile(
|
||||||
leading: Icon(Icons.palette),
|
leading: Icon(Icons.palette),
|
||||||
title: Text(widget.settings.colorIcons ? 'Use Monochrome Icons' : 'Use Color Icons'),
|
title: Text(widget.settings.colorIcons
|
||||||
|
? 'Use Monochrome Icons'
|
||||||
|
: 'Use Color Icons'),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
PopupMenuItem<String>(
|
PopupMenuItem<String>(
|
||||||
value: 'Toggle Theme',
|
value: 'Toggle Theme',
|
||||||
child: ListTile(
|
child: ListTile(
|
||||||
leading: Icon(widget.settings.darkTheme ? Icons.light_mode : Icons.dark_mode),
|
leading: Icon(widget.settings.darkTheme
|
||||||
title: Text(widget.settings.darkTheme ? 'Switch to Light Mode' : 'Switch to Dark Mode'),
|
? Icons.light_mode
|
||||||
|
: Icons.dark_mode),
|
||||||
|
title: Text(widget.settings.darkTheme
|
||||||
|
? 'Switch to Light Mode'
|
||||||
|
: 'Switch to Dark Mode'),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
PopupMenuItem<String>(
|
PopupMenuItem<String>(
|
||||||
value: 'Toggle Sorting',
|
value: 'Toggle Sorting',
|
||||||
child: ListTile(
|
child: ListTile(
|
||||||
leading: Icon(widget.settings.sortAlphabetically ? Icons.access_time : Icons.sort_by_alpha),
|
leading: Icon(widget.settings.sortAlphabetically
|
||||||
title: Text(widget.settings.sortAlphabetically ? 'List by Last Used' : 'List Alphabetically'),
|
? Icons.access_time
|
||||||
|
: Icons.sort_by_alpha),
|
||||||
|
title: Text(widget.settings.sortAlphabetically
|
||||||
|
? 'Sort Alphabetically'
|
||||||
|
: 'Sort by Last Used'),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|
@ -395,9 +446,11 @@ class _AppLauncherScreenState extends State<AppLauncherScreen> {
|
||||||
child: Icon(Icons.info, color: Colors.white),
|
child: Icon(Icons.info, color: Colors.white),
|
||||||
),
|
),
|
||||||
child: ListTile(
|
child: ListTile(
|
||||||
leading: widget.settings.showIcons ? _buildAppIcon(app) : null,
|
leading:
|
||||||
|
widget.settings.showIcons ? _buildAppIcon(app) : null,
|
||||||
title: Text(app.name),
|
title: Text(app.name),
|
||||||
subtitle: widget.settings.showTags ? Text(app.tags.join(" ")) : null,
|
subtitle:
|
||||||
|
widget.settings.showTags ? Text(app.tags.join(" ")) : null,
|
||||||
onTap: () => _launchApp(app),
|
onTap: () => _launchApp(app),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue