refactor for main.dart and provider usage
This commit is contained in:
parent
306c9ec6f3
commit
f388e2f843
9 changed files with 70 additions and 71 deletions
|
@ -1,21 +1,17 @@
|
|||
import 'package:audio_service/audio_service.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_audio_query/flutter_audio_query.dart';
|
||||
import 'package:mosh/presentation/widgets/injection_widget.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import 'presentation/pages/home_page.dart';
|
||||
import 'presentation/pages/library_page.dart';
|
||||
import 'presentation/pages/settings_page.dart';
|
||||
import 'presentation/state/music_store.dart';
|
||||
import 'presentation/state/music_data_store.dart';
|
||||
import 'presentation/theming.dart';
|
||||
import 'presentation/widgets/audio_service_widget.dart';
|
||||
import 'presentation/widgets/navbar.dart';
|
||||
import 'system/datasources/audio_manager.dart';
|
||||
import 'system/datasources/local_music_fetcher.dart';
|
||||
import 'system/datasources/moor_music_data_source.dart';
|
||||
import 'system/repositories/audio_repository_impl.dart';
|
||||
import 'system/repositories/music_data_repository_impl.dart';
|
||||
|
||||
void main() => runApp(MyApp());
|
||||
|
||||
|
@ -28,18 +24,11 @@ class MyApp extends StatelessWidget {
|
|||
]);
|
||||
|
||||
return MaterialApp(
|
||||
title: 'mosh',
|
||||
title: 'mucke',
|
||||
theme: theme(),
|
||||
home: AudioServiceWidget(
|
||||
child: Provider<MusicStore>(
|
||||
child: const RootPage(),
|
||||
create: (BuildContext context) => MusicStore(
|
||||
musicDataRepository: MusicDataRepositoryImpl(
|
||||
localMusicFetcher: LocalMusicFetcherImpl(FlutterAudioQuery()),
|
||||
musicDataSource: MoorMusicDataSource(),
|
||||
),
|
||||
audioRepository: AudioRepositoryImpl(AudioManagerImpl()),
|
||||
),
|
||||
home: const AudioServiceWidget(
|
||||
child: InjectionWidget(
|
||||
child: RootPage(),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
@ -56,37 +45,28 @@ class RootPage extends StatefulWidget {
|
|||
class _RootPageState extends State<RootPage> {
|
||||
var navIndex = 0;
|
||||
|
||||
List<Widget> _pages;
|
||||
MusicStore _musicStore;
|
||||
final List<Widget> _pages = <Widget>[
|
||||
HomePage(),
|
||||
const LibraryPage(
|
||||
key: PageStorageKey('LibraryPage'),
|
||||
),
|
||||
const SettingsPage(
|
||||
key: PageStorageKey('SettingsPage'),
|
||||
),
|
||||
];
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
_musicStore = Provider.of<MusicStore>(context);
|
||||
final MusicDataStore _musicStore = Provider.of<MusicDataStore>(context);
|
||||
_musicStore.fetchAlbums();
|
||||
_musicStore.fetchSongs();
|
||||
|
||||
// TODO: don't do this here...
|
||||
AudioService.start(backgroundTaskEntrypoint: _backgroundTaskEntrypoint);
|
||||
|
||||
_pages = <Widget>[
|
||||
HomePage(),
|
||||
LibraryPage(
|
||||
key: const PageStorageKey('LibraryPage'),
|
||||
store: _musicStore,
|
||||
),
|
||||
SettingsPage(
|
||||
key: const PageStorageKey('SettingsPage'),
|
||||
store: _musicStore,
|
||||
),
|
||||
];
|
||||
super.didChangeDependencies();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
AudioService.stop();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
|
|
|
@ -1,20 +1,20 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_mobx/flutter_mobx.dart';
|
||||
import 'package:mobx/mobx.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../../domain/entities/album.dart';
|
||||
import '../state/music_store.dart';
|
||||
import '../state/music_data_store.dart';
|
||||
import '../widgets/album_art_list_tile.dart';
|
||||
import 'album_details_page.dart';
|
||||
|
||||
class AlbumsPage extends StatelessWidget {
|
||||
const AlbumsPage({Key key, @required this.store}) : super(key: key);
|
||||
|
||||
final MusicStore store;
|
||||
const AlbumsPage({Key key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => Observer(builder: (_) {
|
||||
print('AlbumsPage.build');
|
||||
final MusicDataStore store = Provider.of<MusicDataStore>(context);
|
||||
final ObservableFuture<List<Album>> future = store.albumsFuture;
|
||||
|
||||
switch (future.status) {
|
||||
|
|
|
@ -1,13 +1,10 @@
|
|||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../state/music_store.dart';
|
||||
import 'albums_page.dart';
|
||||
import 'songs_page.dart';
|
||||
|
||||
class LibraryPage extends StatefulWidget {
|
||||
const LibraryPage({Key key, @required this.store}) : super(key: key);
|
||||
|
||||
final MusicStore store;
|
||||
const LibraryPage({Key key}) : super(key: key);
|
||||
|
||||
@override
|
||||
_LibraryPageState createState() => _LibraryPageState();
|
||||
|
@ -43,11 +40,9 @@ class _LibraryPageState extends State<LibraryPage> {
|
|||
),
|
||||
AlbumsPage(
|
||||
key: const PageStorageKey('AlbumsPage'),
|
||||
store: widget.store,
|
||||
),
|
||||
SongsPage(
|
||||
key: const PageStorageKey('SongsPage'),
|
||||
store: widget.store,
|
||||
),
|
||||
],
|
||||
),
|
||||
|
|
|
@ -1,17 +1,18 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_mobx/flutter_mobx.dart';
|
||||
import 'package:mobx/mobx.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../../domain/entities/album.dart';
|
||||
import '../state/music_store.dart';
|
||||
import '../state/music_data_store.dart';
|
||||
|
||||
class SettingsPage extends StatelessWidget {
|
||||
const SettingsPage({Key key, @required this.store}) : super(key: key);
|
||||
|
||||
final MusicStore store;
|
||||
const SettingsPage({Key key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final MusicDataStore store = Provider.of<MusicDataStore>(context);
|
||||
|
||||
return ListView(
|
||||
children: [
|
||||
Container(
|
||||
|
@ -61,7 +62,7 @@ class SettingsPage extends StatelessWidget {
|
|||
height: 4.0,
|
||||
),
|
||||
ListTile(
|
||||
title: Text('Select library folders'),
|
||||
title: const Text('Select library folders'),
|
||||
trailing: Icon(Icons.chevron_right),
|
||||
onTap: () {},
|
||||
),
|
||||
|
|
|
@ -1,16 +1,13 @@
|
|||
import 'package:audio_service/audio_service.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_mobx/flutter_mobx.dart';
|
||||
import 'package:mobx/mobx.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../../domain/entities/song.dart';
|
||||
import '../state/music_store.dart';
|
||||
import '../state/music_data_store.dart';
|
||||
import '../widgets/album_art_list_tile.dart';
|
||||
|
||||
class SongsPage extends StatefulWidget {
|
||||
SongsPage({Key key, @required this.store}) : super(key: key);
|
||||
|
||||
final MusicStore store;
|
||||
SongsPage({Key key}) : super(key: key);
|
||||
|
||||
@override
|
||||
_SongsPageState createState() => _SongsPageState();
|
||||
|
@ -22,11 +19,12 @@ class _SongsPageState extends State<SongsPage>
|
|||
@override
|
||||
Widget build(BuildContext context) {
|
||||
print('SongsPage.build');
|
||||
final MusicDataStore store = Provider.of<MusicDataStore>(context);
|
||||
|
||||
super.build(context);
|
||||
return Observer(builder: (_) {
|
||||
print('SongsPage.build -> Observer.builder');
|
||||
final bool isFetching = widget.store.isFetchingSongs;
|
||||
final bool isFetching = store.isFetchingSongs;
|
||||
|
||||
if (isFetching) {
|
||||
return Column(
|
||||
|
@ -37,7 +35,7 @@ class _SongsPageState extends State<SongsPage>
|
|||
],
|
||||
);
|
||||
} else {
|
||||
final List<Song> songs = widget.store.songs;
|
||||
final List<Song> songs = store.songs;
|
||||
return ListView.separated(
|
||||
itemCount: songs.length,
|
||||
itemBuilder: (_, int index) {
|
||||
|
@ -46,7 +44,7 @@ class _SongsPageState extends State<SongsPage>
|
|||
title: song.title,
|
||||
subtitle: '${song.artist} • ${song.album}',
|
||||
albumArtPath: song.albumArtPath,
|
||||
onTap: () => _playSong(index, songs),
|
||||
onTap: () => store.playSong(index, songs),
|
||||
);
|
||||
},
|
||||
separatorBuilder: (BuildContext context, int index) => const Divider(
|
||||
|
@ -59,9 +57,4 @@ class _SongsPageState extends State<SongsPage>
|
|||
|
||||
@override
|
||||
bool get wantKeepAlive => true;
|
||||
|
||||
void _playSong(int index, List<Song> songList) {
|
||||
widget.store.playSong(index, songList);
|
||||
// AudioService.playFromMediaId(songList[index].path);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,10 +12,10 @@ import '../../domain/usecases/get_albums.dart';
|
|||
import '../../domain/usecases/get_songs.dart';
|
||||
import '../../domain/usecases/update_database.dart';
|
||||
|
||||
part 'music_store.g.dart';
|
||||
part 'music_data_store.g.dart';
|
||||
|
||||
class MusicStore extends _MusicStore with _$MusicStore {
|
||||
MusicStore({@required MusicDataRepository musicDataRepository, @required AudioRepository audioRepository})
|
||||
class MusicDataStore extends _MusicStore with _$MusicDataStore {
|
||||
MusicDataStore({@required MusicDataRepository musicDataRepository, @required AudioRepository audioRepository})
|
||||
: super(musicDataRepository, audioRepository);
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'music_store.dart';
|
||||
part of 'music_data_store.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// StoreGenerator
|
||||
|
@ -8,7 +8,7 @@ part of 'music_store.dart';
|
|||
|
||||
// ignore_for_file: non_constant_identifier_names, unnecessary_lambdas, prefer_expression_function_bodies, lines_longer_than_80_chars, avoid_as, avoid_annotating_with_dynamic
|
||||
|
||||
mixin _$MusicStore on _MusicStore, Store {
|
||||
mixin _$MusicDataStore on _MusicStore, Store {
|
||||
final _$albumsFutureAtom = Atom(name: '_MusicStore.albumsFuture');
|
||||
|
||||
@override
|
|
@ -21,6 +21,7 @@ class _AudioServiceWidgetState extends State<AudioServiceWidget>
|
|||
|
||||
@override
|
||||
void dispose() {
|
||||
AudioService.stop();
|
||||
AudioService.disconnect();
|
||||
WidgetsBinding.instance.removeObserver(this);
|
||||
super.dispose();
|
||||
|
|
29
lib/presentation/widgets/injection_widget.dart
Normal file
29
lib/presentation/widgets/injection_widget.dart
Normal file
|
@ -0,0 +1,29 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_audio_query/flutter_audio_query.dart';
|
||||
import 'package:mosh/presentation/state/music_data_store.dart';
|
||||
import 'package:mosh/system/datasources/audio_manager.dart';
|
||||
import 'package:mosh/system/datasources/local_music_fetcher.dart';
|
||||
import 'package:mosh/system/datasources/moor_music_data_source.dart';
|
||||
import 'package:mosh/system/repositories/audio_repository_impl.dart';
|
||||
import 'package:mosh/system/repositories/music_data_repository_impl.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
class InjectionWidget extends StatelessWidget {
|
||||
const InjectionWidget({Key key, this.child}) : super(key: key);
|
||||
|
||||
final Widget child;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Provider<MusicDataStore>(
|
||||
child: child,
|
||||
create: (BuildContext context) => MusicDataStore(
|
||||
musicDataRepository: MusicDataRepositoryImpl(
|
||||
localMusicFetcher: LocalMusicFetcherImpl(FlutterAudioQuery()),
|
||||
musicDataSource: MoorMusicDataSource(),
|
||||
),
|
||||
audioRepository: AudioRepositoryImpl(AudioManagerImpl()),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue