diff --git a/lib/bloc/player_bloc/player_bloc.dart b/lib/bloc/player_bloc/player_bloc.dart index 2e82240..4c46a33 100644 --- a/lib/bloc/player_bloc/player_bloc.dart +++ b/lib/bloc/player_bloc/player_bloc.dart @@ -16,6 +16,7 @@ class PlayerBloc extends Bloc { bool _isPlaying = false; bool _played = false; bool _isComplete = false; + bool _isFavorite = false; String _currenttime = "0:00:00"; String _beforeCurrentTime = "0:00:00"; String _completetime = "0:00:00"; @@ -59,7 +60,8 @@ class PlayerBloc extends Bloc { completeTime: _completetime, currentSecond: _currentSecond, completeSecond: _completeSecond, - isComplete: true)); + isComplete: true, + isFavorite: _isFavorite)); }); _audioplayer.onPlayerError.listen((event) { @@ -74,6 +76,7 @@ class PlayerBloc extends Bloc { if (_isPlaying) { // pause if (_played) { + _isPlaying = false; try { int status = await _audioplayer.pause(); if (status == 1) { @@ -82,7 +85,8 @@ class PlayerBloc extends Bloc { completeTime: _completetime, currentTime: _currenttime, completeSecond: _completeSecond, - currentSecond: _currentSecond); + currentSecond: _currentSecond, + isFavorite: _isFavorite); } } catch (e) { PlayerError(message: e.toString()); @@ -90,6 +94,7 @@ class PlayerBloc extends Bloc { } // resume else { + _isPlaying = true; try { int status = await _audioplayer.resume(); if (status == 1) { @@ -98,7 +103,8 @@ class PlayerBloc extends Bloc { completeTime: _completetime, currentTime: _currenttime, completeSecond: _completeSecond, - currentSecond: _currentSecond); + currentSecond: _currentSecond, + isFavorite: _isFavorite); } } catch (e) { PlayerError(message: e.toString()); @@ -112,7 +118,8 @@ class PlayerBloc extends Bloc { currentTime: _currenttime, completeTime: _completetime, currentSecond: _currentSecond, - completeSecond: _completeSecond)); + completeSecond: _completeSecond, + isFavorite: _isFavorite)); int status = await _audioplayer.play(event.url, stayAwake: true); @@ -126,7 +133,8 @@ class PlayerBloc extends Bloc { currentSecond: _currentSecond, completeSecond: _completeSecond, isComplete: false, - isPlaying: true)); + isPlaying: true, + isFavorite: _isFavorite)); } } catch (e) { PlayerError(message: e.toString()); @@ -142,7 +150,8 @@ class PlayerBloc extends Bloc { currentSecond: event.currentSecond, completeSecond: event.completeSecond, isComplete: false, - isPlaying: true)); + isPlaying: true, + isFavorite: _isFavorite)); } catch (e) { PlayerError(message: e.toString()); } @@ -152,7 +161,8 @@ class PlayerBloc extends Bloc { completeTime: event.completeTime, currentSecond: event.currentSecond, completeSecond: event.completeSecond, - isComplete: event.isComplete); + isComplete: event.isComplete, + isFavorite: _isFavorite); } else if (event is Stop) { try { int status = await _audioplayer.stop(); @@ -173,7 +183,24 @@ class PlayerBloc extends Bloc { .where((item) => item.audioName == event.audioInfo.title) .toList(); - yield _audio.isEmpty ? FavoriteChecking(false) : FavoriteChecking(true); + _isFavorite = _audio.isEmpty ? false : true; + yield _audio.isEmpty + ? FavoriteChecking( + completeTime: _completetime, + currentTime: _currenttime, + completeSecond: _completeSecond, + currentSecond: _currentSecond, + isComplete: _isComplete, + isPlaying: _isPlaying, + isFavorite: false) + : FavoriteChecking( + completeTime: _completetime, + currentTime: _currenttime, + completeSecond: _completeSecond, + currentSecond: _currentSecond, + isComplete: _isComplete, + isPlaying: _isPlaying, + isFavorite: true); } // set audio to favorite list and if right now exist on this list so delete it from list. else if (event is SetFavorite) { @@ -189,7 +216,15 @@ class PlayerBloc extends Bloc { audioName: event.audioInfo.title, audioUrl: event.audioInfo.url); box.add(_audio); } - yield FavoriteChecking(!event.isFav); + _isFavorite = !event.isFav; + yield FavoriteChecking( + completeTime: _completetime, + currentTime: _currenttime, + completeSecond: _completeSecond, + currentSecond: _currentSecond, + isComplete: _isComplete, + isPlaying: _isPlaying, + isFavorite: !event.isFav); } } diff --git a/lib/bloc/player_bloc/player_event.dart b/lib/bloc/player_bloc/player_event.dart index 55b9f54..a68e26e 100644 --- a/lib/bloc/player_bloc/player_event.dart +++ b/lib/bloc/player_bloc/player_event.dart @@ -28,14 +28,15 @@ class Completion extends PlayerEvent { final int currentSecond; final int completeSecond; final bool isComplete; - - Completion({ - @required this.currentTime, - @required this.completeTime, - @required this.currentSecond, - @required this.completeSecond, - @required this.isComplete, - }); + final bool isFavorite; + + Completion( + {@required this.currentTime, + @required this.completeTime, + @required this.currentSecond, + @required this.completeSecond, + @required this.isComplete, + this.isFavorite = false}); } class Stop extends PlayerEvent {} diff --git a/lib/bloc/player_bloc/player_state.dart b/lib/bloc/player_bloc/player_state.dart index 44a867d..2279533 100644 --- a/lib/bloc/player_bloc/player_state.dart +++ b/lib/bloc/player_bloc/player_state.dart @@ -8,13 +8,14 @@ class PlayerInitial extends PlayerState { final String completeTime; final int currentSecond; final int completeSecond; + final bool isFavorite; - PlayerInitial({ - this.currentTime = "0:00:00", - this.completeTime = "0:00:00", - this.currentSecond = 0, - this.completeSecond = 120, - }); + PlayerInitial( + {this.currentTime = "0:00:00", + this.completeTime = "0:00:00", + this.currentSecond = 0, + this.completeSecond = 120, + this.isFavorite = false}); } class PlayerLoading extends PlayerState { @@ -22,13 +23,14 @@ class PlayerLoading extends PlayerState { final String completeTime; final int currentSecond; final int completeSecond; + final bool isFavorite; - PlayerLoading({ - @required this.currentTime, - @required this.completeTime, - @required this.currentSecond, - @required this.completeSecond, - }); + PlayerLoading( + {@required this.currentTime, + @required this.completeTime, + @required this.currentSecond, + @required this.completeSecond, + this.isFavorite = false}); } class PlayerRunning extends PlayerState { @@ -38,6 +40,7 @@ class PlayerRunning extends PlayerState { final int completeSecond; final bool isComplete; final bool isPlaying; + final bool isFavorite; PlayerRunning( {@required this.currentTime, @@ -45,7 +48,8 @@ class PlayerRunning extends PlayerState { @required this.currentSecond, @required this.completeSecond, @required this.isComplete, - this.isPlaying = false}); + this.isPlaying = false, + this.isFavorite = false}); } class PlayerPause extends PlayerState { @@ -53,13 +57,14 @@ class PlayerPause extends PlayerState { final String completeTime; final int currentSecond; final int completeSecond; + final bool isFavorite; - PlayerPause({ - @required this.currentTime, - @required this.completeTime, - @required this.currentSecond, - @required this.completeSecond, - }); + PlayerPause( + {@required this.currentTime, + @required this.completeTime, + @required this.currentSecond, + @required this.completeSecond, + this.isFavorite = false}); } class PlayerResume extends PlayerState { @@ -67,13 +72,14 @@ class PlayerResume extends PlayerState { final String completeTime; final int currentSecond; final int completeSecond; + final bool isFavorite; - PlayerResume({ - @required this.currentTime, - @required this.completeTime, - @required this.currentSecond, - @required this.completeSecond, - }); + PlayerResume( + {@required this.currentTime, + @required this.completeTime, + @required this.currentSecond, + @required this.completeSecond, + this.isFavorite = false}); } class PlayerStop extends PlayerState {} @@ -87,6 +93,19 @@ class PlayerError extends PlayerState { } class FavoriteChecking extends PlayerState { - final bool isFav; - FavoriteChecking(this.isFav); + final String currentTime; + final String completeTime; + final int currentSecond; + final int completeSecond; + final bool isComplete; + final bool isPlaying; + final bool isFavorite; + FavoriteChecking( + {@required this.currentTime, + @required this.completeTime, + @required this.currentSecond, + @required this.completeSecond, + @required this.isComplete, + this.isPlaying = false, + this.isFavorite = false}); } diff --git a/lib/model/player_model.dart b/lib/model/player_model.dart index 9969fde..9799ba0 100644 --- a/lib/model/player_model.dart +++ b/lib/model/player_model.dart @@ -1,10 +1,11 @@ +import 'package:SilentMoon/model/audio.dart'; import 'package:flutter/cupertino.dart'; +import 'package:hive/hive.dart'; class PlayerModel { final String title; final String boxTitle; final String url; - PlayerModel( - {@required this.title, @required this.boxTitle, @required this.url}); + PlayerModel({this.title, this.boxTitle, this.url}); } diff --git a/lib/provider/favorite.dart b/lib/provider/favorite.dart new file mode 100644 index 0000000..d818110 --- /dev/null +++ b/lib/provider/favorite.dart @@ -0,0 +1,28 @@ +// import 'package:SilentMoon/model/audio.dart'; +// import 'package:SilentMoon/model/player_model.dart'; +// import 'package:flutter/material.dart'; +// import 'package:hive/hive.dart'; + +// class Favorite extends ChangeNotifier { +// PlayerModel args; +// Favorite(this.args); + +// List checkFavorite(String title) { +// Box