Complete refactor to clean up project
This commit is contained in:
235
lib/features/home/home_page.dart
Normal file
235
lib/features/home/home_page.dart
Normal file
@@ -0,0 +1,235 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_fullscreen/flutter_fullscreen.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:package_info_plus/package_info_plus.dart';
|
||||
import 'package:sheetless/core/models/config.dart';
|
||||
import 'package:sheetless/core/models/sheet.dart';
|
||||
import 'package:sheetless/core/services/api_client.dart';
|
||||
import 'package:sheetless/core/services/storage_service.dart';
|
||||
|
||||
import '../../app.dart';
|
||||
import '../auth/login_page.dart';
|
||||
import '../sheet_viewer/sheet_viewer_page.dart';
|
||||
import 'widgets/app_drawer.dart';
|
||||
import 'widgets/sheets_list.dart';
|
||||
|
||||
/// Main home page displaying the list of sheet music.
|
||||
///
|
||||
/// Features:
|
||||
/// - Pull-to-refresh sheet list
|
||||
/// - Shuffle mode for random practice
|
||||
/// - Navigation to sheet viewer
|
||||
/// - Logout functionality
|
||||
class HomePage extends StatefulWidget {
|
||||
final Config config;
|
||||
|
||||
const HomePage({super.key, required this.config});
|
||||
|
||||
@override
|
||||
State<HomePage> createState() => _HomePageState();
|
||||
}
|
||||
|
||||
class _HomePageState extends State<HomePage> with RouteAware {
|
||||
final _log = Logger('HomePage');
|
||||
final _storageService = StorageService();
|
||||
|
||||
ApiClient? _apiClient;
|
||||
late Future<List<Sheet>> _sheetsFuture;
|
||||
bool _isShuffling = false;
|
||||
String? _appName;
|
||||
String? _appVersion;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
// Exit fullscreen when entering home page
|
||||
FullScreen.setFullScreen(false);
|
||||
|
||||
// Subscribe to route changes
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
routeObserver.subscribe(this, ModalRoute.of(context)!);
|
||||
});
|
||||
|
||||
_loadAppInfo();
|
||||
_sheetsFuture = _loadSheets();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
routeObserver.unsubscribe(this);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Route Aware (Fullscreen Management)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@override
|
||||
void didPush() {
|
||||
FullScreen.setFullScreen(false);
|
||||
super.didPush();
|
||||
}
|
||||
|
||||
@override
|
||||
void didPopNext() {
|
||||
// Exit fullscreen when returning to home page
|
||||
FullScreen.setFullScreen(false);
|
||||
super.didPopNext();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Data Loading
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
Future<void> _loadAppInfo() async {
|
||||
final info = await PackageInfo.fromPlatform();
|
||||
setState(() {
|
||||
_appName = info.appName;
|
||||
_appVersion = info.version;
|
||||
});
|
||||
}
|
||||
|
||||
Future<List<Sheet>> _loadSheets() async {
|
||||
final url = await _storageService.readSecure(SecureStorageKey.url);
|
||||
final jwt = await _storageService.readSecure(SecureStorageKey.jwt);
|
||||
|
||||
_apiClient = ApiClient(baseUrl: url!, token: jwt);
|
||||
|
||||
final sheets = await _apiClient!.fetchSheets();
|
||||
_log.info('${sheets.length} sheets fetched');
|
||||
|
||||
final sortedSheets = await _sortSheetsByRecency(sheets);
|
||||
_log.info('${sortedSheets.length} sheets sorted');
|
||||
|
||||
return sortedSheets;
|
||||
}
|
||||
|
||||
Future<List<Sheet>> _sortSheetsByRecency(List<Sheet> sheets) async {
|
||||
final accessTimes = await _storageService.readSheetAccessTimes();
|
||||
|
||||
sheets.sort((a, b) {
|
||||
// Use local access time if available and more recent than server update
|
||||
var dateA = accessTimes[a.uuid];
|
||||
var dateB = accessTimes[b.uuid];
|
||||
|
||||
if (dateA == null || a.updatedAt.isAfter(dateA)) {
|
||||
dateA = a.updatedAt;
|
||||
}
|
||||
if (dateB == null || b.updatedAt.isAfter(dateB)) {
|
||||
dateB = b.updatedAt;
|
||||
}
|
||||
|
||||
return dateB.compareTo(dateA); // Most recent first
|
||||
});
|
||||
|
||||
return sheets;
|
||||
}
|
||||
|
||||
Future<void> _refreshSheets() async {
|
||||
setState(() {
|
||||
_sheetsFuture = _loadSheets();
|
||||
});
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Actions
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
void _handleShuffleChanged(bool enabled) async {
|
||||
final sheets = await _sheetsFuture;
|
||||
|
||||
if (enabled) {
|
||||
sheets.shuffle();
|
||||
} else {
|
||||
await _sortSheetsByRecency(sheets);
|
||||
}
|
||||
|
||||
setState(() => _isShuffling = enabled);
|
||||
}
|
||||
|
||||
Future<void> _handleLogout() async {
|
||||
await _storageService.clearToken();
|
||||
|
||||
if (!mounted) return;
|
||||
|
||||
Navigator.of(
|
||||
context,
|
||||
).pushReplacement(MaterialPageRoute(builder: (_) => const LoginPage()));
|
||||
}
|
||||
|
||||
void _openSheet(Sheet sheet) {
|
||||
// Record access time for recency sorting
|
||||
_storageService.writeSheetAccessTime(sheet.uuid, DateTime.now());
|
||||
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) => SheetViewerPage(
|
||||
sheet: sheet,
|
||||
apiClient: _apiClient!,
|
||||
config: widget.config,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// UI
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Sheetless')),
|
||||
endDrawer: AppDrawer(
|
||||
isShuffling: _isShuffling,
|
||||
onShuffleChanged: _handleShuffleChanged,
|
||||
onLogout: _handleLogout,
|
||||
appName: _appName,
|
||||
appVersion: _appVersion,
|
||||
),
|
||||
body: RefreshIndicator(onRefresh: _refreshSheets, child: _buildBody()),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildBody() {
|
||||
return FutureBuilder<List<Sheet>>(
|
||||
future: _sheetsFuture,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState != ConnectionState.done) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
if (snapshot.hasError) {
|
||||
_log.warning('Error loading sheets', snapshot.error);
|
||||
return _buildError(snapshot.error.toString());
|
||||
}
|
||||
|
||||
if (snapshot.hasData) {
|
||||
return SheetsList(
|
||||
sheets: snapshot.data!,
|
||||
onSheetSelected: _openSheet,
|
||||
);
|
||||
}
|
||||
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildError(String message) {
|
||||
return Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Text(
|
||||
message,
|
||||
style: Theme.of(
|
||||
context,
|
||||
).textTheme.titleMedium?.copyWith(color: Colors.red),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
76
lib/features/home/widgets/app_drawer.dart
Normal file
76
lib/features/home/widgets/app_drawer.dart
Normal file
@@ -0,0 +1,76 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// Callback for shuffle state changes.
|
||||
typedef ShuffleCallback = void Function(bool enabled);
|
||||
|
||||
/// Navigation drawer for the home page.
|
||||
///
|
||||
/// Provides access to app-level actions like shuffle mode and logout.
|
||||
class AppDrawer extends StatelessWidget {
|
||||
final bool isShuffling;
|
||||
final ShuffleCallback onShuffleChanged;
|
||||
final VoidCallback onLogout;
|
||||
final String? appName;
|
||||
final String? appVersion;
|
||||
|
||||
const AppDrawer({
|
||||
super.key,
|
||||
required this.isShuffling,
|
||||
required this.onShuffleChanged,
|
||||
required this.onLogout,
|
||||
this.appName,
|
||||
this.appVersion,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Drawer(
|
||||
child: SafeArea(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 30),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
_buildActions(),
|
||||
_buildAppInfo(),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildActions() {
|
||||
return Column(
|
||||
children: [
|
||||
ListTile(
|
||||
leading: Icon(
|
||||
Icons.shuffle,
|
||||
color: isShuffling ? Colors.blue : null,
|
||||
),
|
||||
title: const Text('Shuffle'),
|
||||
onTap: () => onShuffleChanged(!isShuffling),
|
||||
),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.logout),
|
||||
title: const Text('Logout'),
|
||||
onTap: onLogout,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildAppInfo() {
|
||||
final versionText = appName != null && appVersion != null
|
||||
? '$appName v$appVersion'
|
||||
: 'Loading...';
|
||||
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Text(
|
||||
versionText,
|
||||
style: const TextStyle(color: Colors.grey),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
29
lib/features/home/widgets/sheet_list_item.dart
Normal file
29
lib/features/home/widgets/sheet_list_item.dart
Normal file
@@ -0,0 +1,29 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../core/models/sheet.dart';
|
||||
|
||||
/// A list tile displaying a single sheet's information.
|
||||
///
|
||||
/// Shows the sheet name and composer, with tap and long-press handlers.
|
||||
class SheetListItem extends StatelessWidget {
|
||||
final Sheet sheet;
|
||||
final VoidCallback onTap;
|
||||
final VoidCallback onLongPress;
|
||||
|
||||
const SheetListItem({
|
||||
super.key,
|
||||
required this.sheet,
|
||||
required this.onTap,
|
||||
required this.onLongPress,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ListTile(
|
||||
title: Text(sheet.name),
|
||||
subtitle: Text(sheet.composerName),
|
||||
onTap: onTap,
|
||||
onLongPress: onLongPress,
|
||||
);
|
||||
}
|
||||
}
|
||||
38
lib/features/home/widgets/sheet_search_bar.dart
Normal file
38
lib/features/home/widgets/sheet_search_bar.dart
Normal file
@@ -0,0 +1,38 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// Search bar for filtering sheets.
|
||||
///
|
||||
/// Provides a text input with search icon and clear button.
|
||||
class SheetSearchBar extends StatelessWidget {
|
||||
final TextEditingController controller;
|
||||
final VoidCallback onClear;
|
||||
|
||||
const SheetSearchBar({
|
||||
super.key,
|
||||
required this.controller,
|
||||
required this.onClear,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: TextField(
|
||||
controller: controller,
|
||||
decoration: InputDecoration(
|
||||
hintText: 'Search sheets...',
|
||||
prefixIcon: const Icon(Icons.search),
|
||||
suffixIcon: controller.text.isNotEmpty
|
||||
? IconButton(
|
||||
icon: const Icon(Icons.clear),
|
||||
onPressed: onClear,
|
||||
)
|
||||
: null,
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(8.0),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
187
lib/features/home/widgets/sheets_list.dart
Normal file
187
lib/features/home/widgets/sheets_list.dart
Normal file
@@ -0,0 +1,187 @@
|
||||
import 'dart:async';
|
||||
import 'dart:ui';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:sheetless/core/models/change.dart';
|
||||
import 'package:sheetless/core/models/sheet.dart';
|
||||
import 'package:sheetless/core/services/storage_service.dart';
|
||||
|
||||
import '../../../shared/widgets/edit_sheet_bottom_sheet.dart';
|
||||
import 'sheet_list_item.dart';
|
||||
import 'sheet_search_bar.dart';
|
||||
|
||||
/// Widget displaying a searchable list of sheets.
|
||||
///
|
||||
/// Features:
|
||||
/// - Debounced search filtering by name and composer
|
||||
/// - Long-press to edit sheet metadata
|
||||
/// - Cross-platform scroll support (mouse and touch)
|
||||
class SheetsList extends StatefulWidget {
|
||||
final List<Sheet> sheets;
|
||||
final ValueSetter<Sheet> onSheetSelected;
|
||||
|
||||
const SheetsList({
|
||||
super.key,
|
||||
required this.sheets,
|
||||
required this.onSheetSelected,
|
||||
});
|
||||
|
||||
@override
|
||||
State<SheetsList> createState() => _SheetsListState();
|
||||
}
|
||||
|
||||
class _SheetsListState extends State<SheetsList> {
|
||||
static const _searchDebounceMs = 500;
|
||||
|
||||
final _storageService = StorageService();
|
||||
final _searchController = TextEditingController();
|
||||
Timer? _debounceTimer;
|
||||
late List<Sheet> _filteredSheets;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_filteredSheets = widget.sheets;
|
||||
_searchController.addListener(_onSearchChanged);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_searchController.removeListener(_onSearchChanged);
|
||||
_searchController.dispose();
|
||||
_debounceTimer?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Search
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
void _onSearchChanged() {
|
||||
_debounceTimer?.cancel();
|
||||
_debounceTimer = Timer(
|
||||
const Duration(milliseconds: _searchDebounceMs),
|
||||
_filterSheets,
|
||||
);
|
||||
}
|
||||
|
||||
void _filterSheets() {
|
||||
final query = _searchController.text.toLowerCase().trim();
|
||||
|
||||
if (query.isEmpty) {
|
||||
setState(() => _filteredSheets = widget.sheets);
|
||||
return;
|
||||
}
|
||||
|
||||
// Split query into terms for multi-word search
|
||||
final terms = query.split(RegExp(r'\s+'));
|
||||
|
||||
setState(() {
|
||||
_filteredSheets = widget.sheets.where((sheet) {
|
||||
final name = sheet.name.toLowerCase();
|
||||
final composer = sheet.composerName.toLowerCase();
|
||||
|
||||
// Each term must appear in either name or composer
|
||||
return terms.every(
|
||||
(term) => name.contains(term) || composer.contains(term),
|
||||
);
|
||||
}).toList();
|
||||
});
|
||||
}
|
||||
|
||||
void _clearSearch() {
|
||||
_searchController.clear();
|
||||
setState(() => _filteredSheets = widget.sheets);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Edit Sheet
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
void _openEditSheet(BuildContext context, Sheet sheet) {
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
isScrollControlled: true,
|
||||
builder: (_) => EditSheetBottomSheet(
|
||||
sheet: sheet,
|
||||
onSave: (newName, newComposer) =>
|
||||
_handleSheetEdit(sheet, newName, newComposer),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _handleSheetEdit(Sheet sheet, String newName, String newComposer) {
|
||||
// Queue changes for server sync
|
||||
if (newName != sheet.name) {
|
||||
_storageService.writeChange(
|
||||
Change(
|
||||
type: ChangeType.sheetNameChange,
|
||||
sheetUuid: sheet.uuid,
|
||||
value: newName,
|
||||
),
|
||||
);
|
||||
}
|
||||
if (newComposer != sheet.composerName) {
|
||||
_storageService.writeChange(
|
||||
Change(
|
||||
type: ChangeType.composerNameChange,
|
||||
sheetUuid: sheet.uuid,
|
||||
value: newComposer,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// Update local state
|
||||
setState(() {
|
||||
sheet.name = newName;
|
||||
sheet.composerName = newComposer;
|
||||
});
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Sheet Selection
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
void _handleSheetTap(Sheet sheet) {
|
||||
// Move selected sheet to top of list (most recently accessed)
|
||||
widget.sheets.remove(sheet);
|
||||
widget.sheets.insert(0, sheet);
|
||||
|
||||
widget.onSheetSelected(sheet);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// UI
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
children: [
|
||||
SheetSearchBar(controller: _searchController, onClear: _clearSearch),
|
||||
Expanded(child: _buildList()),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildList() {
|
||||
// Enable both mouse and touch scrolling for web compatibility
|
||||
return ScrollConfiguration(
|
||||
behavior: ScrollConfiguration.of(context).copyWith(
|
||||
dragDevices: {PointerDeviceKind.touch, PointerDeviceKind.mouse},
|
||||
),
|
||||
child: ListView.builder(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
itemCount: _filteredSheets.length,
|
||||
itemBuilder: (context, index) {
|
||||
final sheet = _filteredSheets[index];
|
||||
return SheetListItem(
|
||||
sheet: sheet,
|
||||
onTap: () => _handleSheetTap(sheet),
|
||||
onLongPress: () => _openEditSheet(context, sheet),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user