ocarina2/lib/views/home_view.dart

133 lines
4.1 KiB
Dart
Raw Normal View History

2024-05-23 19:14:08 +02:00
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:grouped_list/grouped_list.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<HomeView> createState() => _HomeViewState();
}
class _HomeViewState extends State<HomeView> {
@override
void initState() {
super.initState();
authenticate();
}
var _loading = true;
final _artists = <ArtistIndex>[];
Future<void> authenticate() async {
if (!(await LoginManager.hasSavedLogin)) {
if (!mounted) return;
unawaited(
Navigator.of(context).pushReplacement(
MaterialPageRoute<void>(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<void>(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<ArtistIndex, String>(
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<Widget>.generate(
index.artists.length,
(n) => InkWell(
onTap: () async {
await Navigator.of(context).push(
MaterialPageRoute<void>(
builder: (c) => ArtistView(
artist: index.artists[n],
),
),
);
},
child: ImageCover(
imageUrl: index.artists[n].coverArt,
2024-05-23 19:14:08 +02:00
title: index.artists[n].name,
heroTag: index.artists[n].name,
),
),
),
),
),
),
),
),
);
}
}