import 'dart:typed_data'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:device_apps/device_apps.dart'; import 'package:flutter/services.dart'; import 'package:flutter_svg/flutter_svg.dart'; import 'package:google_fonts/google_fonts.dart'; import 'package:plasmoid/preferences.dart'; import 'package:plasmoid/read_only.dart'; import 'package:plasmoid/settings.dart'; import 'package:provider/provider.dart'; import 'day.dart'; import 'night.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); await SharedPreferencesService().initialize(); runApp( ChangeNotifierProvider( create: (_) => AppSettings(), child: MyApp(), ), ); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return Consumer( builder: (context, settings, child) { return MaterialApp( title: 'Plasmoid Launcher', theme: ThemeData( brightness: Brightness.light, scaffoldBackgroundColor: Color(0xffF6F4F1), appBarTheme: AppBarTheme(backgroundColor: Color(0xffF6F4F1)), textTheme: GoogleFonts.ibmPlexMonoTextTheme(dayText), ), darkTheme: ThemeData( brightness: Brightness.dark, scaffoldBackgroundColor: Color(0xFF121212), appBarTheme: AppBarTheme(backgroundColor: Color(0xFF121212)), textTheme: GoogleFonts.ibmPlexMonoTextTheme(nightText), ), themeMode: settings.darkTheme ? ThemeMode.dark : ThemeMode.light, home: AppLauncherScreen(), ); }, ); } } class AppInfo { final Uint8List? icon; final String name; final String packageName; final List tags; DateTime? lastUsed; // New field to track last used time AppInfo({ this.icon, required this.name, required this.packageName, required this.tags, this.lastUsed, }); } class AppLauncherScreen extends StatefulWidget { @override _AppLauncherScreenState createState() => _AppLauncherScreenState(); } class _AppLauncherScreenState extends State with WidgetsBindingObserver { final TextEditingController searchController = TextEditingController(); final FocusNode searchFocusNode = FocusNode(); List apps = []; List filteredApps = []; bool isLoading = true; @override void initState() { super.initState(); WidgetsBinding.instance.addObserver(this); _loadInstalledApps(); searchController.addListener(_filterApps); WidgetsBinding.instance.addPostFrameCallback((_) { if (context.read().autoFocus) { FocusScope.of(context).requestFocus(searchFocusNode); } }); } @override void dispose() { WidgetsBinding.instance.removeObserver(this); super.dispose(); } @override void didChangeAppLifecycleState(AppLifecycleState state) { if (state == AppLifecycleState.resumed) { _loadInstalledApps(); } } Future _loadInstalledApps() async { List installedApps = await DeviceApps.getInstalledApplications( includeAppIcons: true, onlyAppsWithLaunchIntent: true, includeSystemApps: true, ); List loadedApps = installedApps.map((app) { final lastUsedMillis = SharedPreferencesService().getInt("lastUsed_${app.packageName}"); final storedTags = SharedPreferencesService().getString("tags_${app.packageName}"); List tags = []; if (storedTags != null && storedTags.trim().isNotEmpty) { tags = storedTags.split(" "); } else if (app.category != null) { String categoryStr = app.category.toString().split('.').last; if (categoryStr.toLowerCase() != 'undefined') { tags.add('@${categoryStr.toLowerCase()}'); } } return AppInfo( icon: app is ApplicationWithIcon ? app.icon : null, name: app.appName, packageName: app.packageName, tags: tags, lastUsed: lastUsedMillis != null ? DateTime.fromMillisecondsSinceEpoch(lastUsedMillis) : null, ); }).toList(); setState(() { apps = loadedApps; filteredApps = apps; isLoading = false; }); _filterApps(); } void _filterApps() { String query = searchController.text.toLowerCase(); setState(() { filteredApps = apps.where((app) { bool matchesName = app.name.toLowerCase().contains(query); bool matchesTag = app.tags.any((tag) => tag.toLowerCase().contains(query)); return matchesName || matchesTag; }).toList(); if (context.read().sortLastUsed) { filteredApps.sort((a, b) { DateTime aTime = a.lastUsed ?? DateTime.fromMillisecondsSinceEpoch(0); DateTime bTime = b.lastUsed ?? DateTime.fromMillisecondsSinceEpoch(0); return bTime.compareTo(aTime); }); } else { filteredApps.sort((a, b) => a.name.compareTo(b.name)); } }); } Future _launchApp(AppInfo app) async { bool launched = await DeviceApps.openApp(app.packageName); if (launched) { DateTime now = DateTime.now(); await SharedPreferencesService().setInt("lastUsed_${app.packageName}", now.millisecondsSinceEpoch); setState(() { app.lastUsed = now; _filterApps(); }); } else { ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('Could not launch ${app.name}')), ); } } Future _handleDismiss(DismissDirection direction, AppInfo app) async { if (direction == DismissDirection.endToStart) { _openAppInfo(app); } else if (direction == DismissDirection.startToEnd) { _editAppTags(app); } return false; } Future _openAppInfo(AppInfo app) async { bool launched = await DeviceApps.openAppSettings(app.packageName); if (!launched) { ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('Could not launch ${app.name} Settings')), ); } } void _editAppTags(AppInfo app) { TextEditingController tagController = TextEditingController(text: app.tags.join(" ")); showDialog( context: context, builder: (_) => AlertDialog( title: Text('Edit Tags for ${app.name}'), content: TextField( controller: tagController, decoration: InputDecoration( hintText: 'Enter tags (e.g., #general, @context, +project)', ), ), actions: [ TextButton( onPressed: () async { setState(() { app.tags ..clear() ..addAll(tagController.text.split(" ").where((t) => t.isNotEmpty)); }); await SharedPreferencesService().setString("tags_${app.packageName}", app.tags.join(" ")); Navigator.of(context).pop(); _filterApps(); }, child: Text('Save'), ), TextButton( onPressed: () => Navigator.of(context).pop(), child: Text('Cancel'), ), ], ), ); } List _formatTags(String content) { final tagPattern = RegExp(r'([#@+][a-zA-Z0-9_]+)'); final Set tagSet = {}; for (final match in tagPattern.allMatches(content)) { tagSet.add(match.group(0)!); } final sortedTags = tagSet.toList() ..sort((a, b) { if (a.startsWith('+') && !b.startsWith('+')) return -1; if (b.startsWith('+') && !a.startsWith('+')) return 1; if (a.startsWith('@') && !b.startsWith('@')) return -1; if (b.startsWith('@') && !a.startsWith('@')) return 1; return a.compareTo(b); }); return sortedTags.map((tag) { Color tagColor = Colors.white; if (tag.startsWith('#')) tagColor = Colors.pinkAccent; if (tag.startsWith('+')) tagColor = Colors.cyan; if (tag.startsWith('@')) tagColor = Colors.orange; return TextSpan( text: "$tag ", style: TextStyle(color: tagColor, fontWeight: FontWeight.bold), ); }).toList(); } Widget _buildAppIcon(AppInfo app) { Widget iconWidget; if (app.icon != null) { iconWidget = Image.memory( app.icon!, width: 40, height: 40, fit: BoxFit.contain, ); } else { iconWidget = Icon(Icons.apps, size: 40); } if (!context.read().colorIcons) { iconWidget = ColorFiltered( colorFilter: ColorFilter.matrix([ 0.2126, 0.7152, 0.0722, 0, 0, 0.2126, 0.7152, 0.0722, 0, 0, 0.2126, 0.7152, 0.0722, 0, 0, 0, 0, 0, 1, 0, ]), child: iconWidget, ); } return iconWidget; } void _showInfoPage(BuildContext context, String title, String content) { Navigator.push( context, MaterialPageRoute( builder: (context) => ReadOnlyPage( title: title, content: content, isDarkMode: context.read().darkTheme, ), ), ); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: TextField( focusNode: context.read().autoFocus ? searchFocusNode : null, controller: searchController, decoration: InputDecoration( hintText: 'Search apps...', border: InputBorder.none, ), style: TextStyle(color: Colors.white), ), actions: [ Padding( padding: EdgeInsets.symmetric(horizontal: 15), child: GestureDetector( onTap: () { if (searchController.text.isNotEmpty) { searchController.clear(); } }, child: PopupMenuButton( enabled: searchController.text.isEmpty, icon: SvgPicture.asset( "assets/plasmoid.svg", width: 30, color: Color(0xff38818a), ), onSelected: (String value) async { final settings = context.read(); switch (value) { case 'Toggle Tags': settings.updateSetting('showTags', !settings.showTags); break; case 'Toggle Icons': settings.updateSetting('showIcons', !settings.showIcons); break; case 'Toggle Color Icons': settings.updateSetting('colorIcons', !settings.colorIcons); break; case 'Toggle Theme': settings.updateSetting('darkTheme', !settings.darkTheme); break; case 'Toggle Sorting': settings.updateSetting('sortLastUsed', !settings.sortLastUsed); _filterApps(); break; case 'Toggle AutoFocus': settings.updateSetting('autoFocus', !settings.autoFocus); if (!settings.autoFocus) FocusScope.of(context).unfocus(); break; case 'About': _showInfoPage(context, "About", aboutContent); break; } }, itemBuilder: (BuildContext context) { final settings = context.read(); return [ PopupMenuItem( value: 'Toggle Tags', child: ListTile( leading: Icon(settings.showTags ? Icons.short_text : Icons.tag), title: Text(settings.showTags ? 'Hide Tags' : 'Show Tags'), ), ), PopupMenuItem( value: 'Toggle Icons', child: ListTile( leading: Icon(settings.showIcons ? Icons.apps : Icons.apps_outlined), title: Text(settings.showIcons ? 'Hide Icons' : 'Show Icons'), ), ), PopupMenuItem( value: 'Toggle Color Icons', child: ListTile( leading: Icon(Icons.palette), title: Text(settings.colorIcons ? 'Use Monochrome Icons' : 'Use Color Icons'), ), ), PopupMenuItem( value: 'Toggle Theme', child: ListTile( leading: Icon(settings.darkTheme ? Icons.light_mode : Icons.dark_mode), title: Text(settings.darkTheme ? 'Switch to Light Mode' : 'Switch to Dark Mode'), ), ), PopupMenuItem( value: 'Toggle Sorting', child: ListTile( leading: Icon(settings.sortLastUsed ? Icons.access_time : Icons.sort_by_alpha), title: Text(settings.sortLastUsed ? 'Sort Alphabetically' : 'Sort by Last Used'), ), ), PopupMenuItem( value: 'Toggle AutoFocus', child: ListTile( leading: Icon(Icons.center_focus_strong), title: Text(settings.autoFocus ? 'Disable Auto Focus' : 'Enable Auto Focus'), ), ), PopupMenuItem( value: 'About', child: ListTile( leading: Icon(Icons.info_outline), title: Text('About'), ), ), ]; }, ), ), ), ], ), body: isLoading ? Center(child: CircularProgressIndicator()) : ListView.builder( itemCount: filteredApps.length, itemBuilder: (context, index) { final app = filteredApps[index]; return Dismissible( key: Key(app.packageName), confirmDismiss: (direction) => _handleDismiss(direction, app), background: Container( color: Colors.orange, alignment: Alignment.centerLeft, padding: EdgeInsets.only(left: 20.0), child: Icon(Icons.edit, color: Colors.white), ), secondaryBackground: Container( color: Colors.green, alignment: Alignment.centerRight, padding: EdgeInsets.only(right: 20.0), child: Icon(Icons.info, color: Colors.white), ), child: ListTile( leading: context.read().showIcons ? _buildAppIcon(app) : null, title: Text(app.name), subtitle: context.read().showTags ? RichText( text: TextSpan( style: DefaultTextStyle.of(context).style, children: _formatTags(app.tags.join(' ')), ), overflow: TextOverflow.ellipsis, ) : null, onTap: () => _launchApp(app), ), ); }, ), ); } } String aboutContent = """ **Plasmoid** is a minimalist and opinionated app launcher for Android. ## Search & Launch: Open Plasmoid and start typing in the search bar to filter your apps and shared files by name or tag. Tap an app to launch it immediately. ## Tagging: Swipe right on any item to edit its tags. For shared files, a tag dialog appears immediately upon sharing. Tags help you organize items and are searchable using prefixes like @context, #general, or +project. ## Gestures & Settings: Swipe left on an app to view its settings. Use the menu button (plasmoid) to toggle settings such as icon visibility, color mode, theme, sorting (alphabetically or by last used), and auto-focus. --- Made with 💚 by randogoth Icon by [Game Icons.net](https://game-icons.net/). """;