Browser-Tab-Manager/lib/utils/helpers.dart

96 lines
2.8 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import '../models/tab_data.dart';
class Helpers {
static String getItemIcon(TabData item) {
if (item.favicon.isNotEmpty && !item.favicon.contains('data:')) {
return '🌐';
}
switch (item.type) {
case 'tab':
return '🔖';
case 'bookmark':
return '';
case 'history':
return '🕒';
default:
return '🌐';
}
}
static Color getTypeColor(BuildContext context, String type) {
switch (type) {
case 'tab':
return Colors.blue;
case 'bookmark':
return Colors.orange;
case 'history':
return Colors.purple;
default:
return Theme.of(context).colorScheme.primary;
}
}
static Map<String, int> getItemStats(List<TabData> items) {
return {
'tabs': items.where((i) => i.type == 'tab').length,
'bookmarks': items.where((i) => i.type == 'bookmark').length,
'history': items.where((i) => i.type == 'history').length,
};
}
static List<TabData> filterItems(
List<TabData> items,
String query,
String filterType,
) {
return items.where((item) {
if (filterType != 'all' && item.type != filterType.replaceAll('s', '')) {
return false;
}
if (query.isNotEmpty) {
return item.title.toLowerCase().contains(query.toLowerCase()) ||
item.url.toLowerCase().contains(query.toLowerCase());
}
return true;
}).toList();
}
static void sortItems(List<TabData> items, String sortBy) {
switch (sortBy) {
case 'recent':
items.sort((a, b) => b.lastAccessed.compareTo(a.lastAccessed));
break;
case 'title':
items.sort((a, b) => a.title.compareTo(b.title));
break;
case 'url':
items.sort((a, b) => a.url.compareTo(b.url));
break;
case 'visits':
items.sort((a, b) => (b.visitCount ?? 0).compareTo(a.visitCount ?? 0));
break;
}
// Keep pinned items at top
items.sort((a, b) => b.isPinned ? 1 : (a.isPinned ? -1 : 0));
}
}
// This is a utility class containing reusable helper functions used throughout the app.
//
// The getItemIcon method returns the appropriate emoji icon for each item type.
//
// The getTypeColor method assigns consistent colors to tabs, bookmarks, and history items.
//
// The getItemStats method calculates how many items of each type exist in a list.
//
// The filterItems method filters a list based on search query and filter type selection.
//
// The sortItems method sorts items by different criteria like recent, title, url, or visit count.
//
// All methods are static so they can be called without creating an instance of the class.
//
// This keeps common logic in one place rather than duplicating it across multiple files.
//
// Usin