import 'dart:async'; import 'package:flutter/material.dart'; import 'package:grouped_list/grouped_list.dart'; import 'package:ocarina/api/audio/audioplayer_service.dart'; import 'package:ocarina/api/login_manager.dart'; import 'package:ocarina/api/subsonic/artistindex.dart'; import 'package:ocarina/api/subsonic/subsonic.dart'; import 'package:ocarina/views/artist_view.dart'; import 'package:ocarina/views/login_view.dart'; import 'package:ocarina/widgets/image_cover.dart'; import 'package:responsive_sizer/responsive_sizer.dart'; class HomeView extends StatefulWidget { const HomeView({super.key}); @override State createState() => _HomeViewState(); } class _HomeViewState extends State { @override void initState() { super.initState(); AudioPlayerService().setup(); authenticate(); } var _loading = true; final _artists = []; Future authenticate() async { if (!(await LoginManager.hasSavedLogin)) { if (!mounted) return; unawaited( Navigator.of(context).pushReplacement( MaterialPageRoute(builder: (c) => const LoginView()), ), ); return; } if (SubsonicApiService().user == "") { final details = await LoginManager.getDetails(); if (details == null) { if (!mounted) return; unawaited( Navigator.of(context).pushReplacement( MaterialPageRoute(builder: (c) => const LoginView()), ), ); return; } SubsonicApiService().user = details["user"]!; SubsonicApiService().baseUrl = details["url"]!; SubsonicApiService().password = details["pass"]!; final r = await SubsonicApiService().verifyCredentials(); if (!r) { if (!mounted) return; ScaffoldMessenger.of(context).clearSnackBars(); ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text("Could not log in :("), duration: Duration(seconds: 3), ), ); return; } } _artists ..clear() ..addAll(await SubsonicApiService().getArtistIndex() ?? []); _loading = false; setState(() {}); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("Home"), ), body: Center( child: SingleChildScrollView( child: SizedBox( height: 100.h, width: 95.w, child: _loading ? const Column( mainAxisAlignment: MainAxisAlignment.center, children: [ CircularProgressIndicator.adaptive(), ], ) : GroupedListView( elements: _artists, groupBy: (a) => a.index, groupHeaderBuilder: (index) => Text( index.index, style: TextStyle( color: Theme.of(context).colorScheme.primary, ), ), itemBuilder: (c, index) => Wrap( alignment: (index.artists.length > 1) ? WrapAlignment.spaceBetween : WrapAlignment.start, children: List.generate( index.artists.length, (n) => InkWell( onTap: () async { await Navigator.of(context).push( MaterialPageRoute( builder: (c) => ArtistView( artist: index.artists[n], ), ), ); }, child: ImageCover( imageUrl: index.artists[n].coverArt, title: index.artists[n].name, heroTag: index.artists[n].name, ), ), ), ), ), ), ), ), ); } }