Skip to content

Commit

Permalink
favorite hive db
Browse files Browse the repository at this point in the history
  • Loading branch information
husen-hn committed Apr 20, 2021
1 parent 5f3fe07 commit c4015c7
Show file tree
Hide file tree
Showing 12 changed files with 594 additions and 395 deletions.
53 changes: 44 additions & 9 deletions lib/bloc/player_bloc/player_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class PlayerBloc extends Bloc<PlayerEvent, PlayerState> {
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";
Expand Down Expand Up @@ -59,7 +60,8 @@ class PlayerBloc extends Bloc<PlayerEvent, PlayerState> {
completeTime: _completetime,
currentSecond: _currentSecond,
completeSecond: _completeSecond,
isComplete: true));
isComplete: true,
isFavorite: _isFavorite));
});

_audioplayer.onPlayerError.listen((event) {
Expand All @@ -74,6 +76,7 @@ class PlayerBloc extends Bloc<PlayerEvent, PlayerState> {
if (_isPlaying) {
// pause
if (_played) {
_isPlaying = false;
try {
int status = await _audioplayer.pause();
if (status == 1) {
Expand All @@ -82,14 +85,16 @@ class PlayerBloc extends Bloc<PlayerEvent, PlayerState> {
completeTime: _completetime,
currentTime: _currenttime,
completeSecond: _completeSecond,
currentSecond: _currentSecond);
currentSecond: _currentSecond,
isFavorite: _isFavorite);
}
} catch (e) {
PlayerError(message: e.toString());
}
}
// resume
else {
_isPlaying = true;
try {
int status = await _audioplayer.resume();
if (status == 1) {
Expand All @@ -98,7 +103,8 @@ class PlayerBloc extends Bloc<PlayerEvent, PlayerState> {
completeTime: _completetime,
currentTime: _currenttime,
completeSecond: _completeSecond,
currentSecond: _currentSecond);
currentSecond: _currentSecond,
isFavorite: _isFavorite);
}
} catch (e) {
PlayerError(message: e.toString());
Expand All @@ -112,7 +118,8 @@ class PlayerBloc extends Bloc<PlayerEvent, PlayerState> {
currentTime: _currenttime,
completeTime: _completetime,
currentSecond: _currentSecond,
completeSecond: _completeSecond));
completeSecond: _completeSecond,
isFavorite: _isFavorite));

int status = await _audioplayer.play(event.url, stayAwake: true);

Expand All @@ -126,7 +133,8 @@ class PlayerBloc extends Bloc<PlayerEvent, PlayerState> {
currentSecond: _currentSecond,
completeSecond: _completeSecond,
isComplete: false,
isPlaying: true));
isPlaying: true,
isFavorite: _isFavorite));
}
} catch (e) {
PlayerError(message: e.toString());
Expand All @@ -142,7 +150,8 @@ class PlayerBloc extends Bloc<PlayerEvent, PlayerState> {
currentSecond: event.currentSecond,
completeSecond: event.completeSecond,
isComplete: false,
isPlaying: true));
isPlaying: true,
isFavorite: _isFavorite));
} catch (e) {
PlayerError(message: e.toString());
}
Expand All @@ -152,7 +161,8 @@ class PlayerBloc extends Bloc<PlayerEvent, PlayerState> {
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();
Expand All @@ -173,7 +183,24 @@ class PlayerBloc extends Bloc<PlayerEvent, PlayerState> {
.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) {
Expand All @@ -189,7 +216,15 @@ class PlayerBloc extends Bloc<PlayerEvent, PlayerState> {
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);
}
}

Expand Down
17 changes: 9 additions & 8 deletions lib/bloc/player_bloc/player_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand Down
73 changes: 46 additions & 27 deletions lib/bloc/player_bloc/player_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,29 @@ 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 {
final String currentTime;
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 {
Expand All @@ -38,42 +40,46 @@ class PlayerRunning extends PlayerState {
final int completeSecond;
final bool isComplete;
final bool isPlaying;
final bool isFavorite;

PlayerRunning(
{@required this.currentTime,
@required this.completeTime,
@required this.currentSecond,
@required this.completeSecond,
@required this.isComplete,
this.isPlaying = false});
this.isPlaying = false,
this.isFavorite = false});
}

class PlayerPause extends PlayerState {
final String currentTime;
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 {
final String currentTime;
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 {}
Expand All @@ -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});
}
5 changes: 3 additions & 2 deletions lib/model/player_model.dart
Original file line number Diff line number Diff line change
@@ -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});
}
28 changes: 28 additions & 0 deletions lib/provider/favorite.dart
Original file line number Diff line number Diff line change
@@ -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<Audio> _box = args.hiveBox;
// return _box.values.where((item) => item.audioName == title).toList();
// }

// deleteFavorite(String title) {
// Box<Audio> _box = args.hiveBox;
// List _audio = _box.values.where((item) => item.audioName == title).toList();
// _audio[0].delete();
// notifyListeners();
// }

// addFavorite(String title, String url) {
// Box<Audio> _box = args.hiveBox;
// Audio _audio = Audio(audioName: title, audioUrl: url);
// _box.add(_audio);
// notifyListeners();
// }
// }
2 changes: 2 additions & 0 deletions lib/route_generator.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:SilentMoon/bloc/player_bloc/player_bloc.dart';
import 'package:SilentMoon/main.dart';
import 'package:SilentMoon/provider/favorite.dart';
import 'package:SilentMoon/screen/main_screen.dart';
import 'package:SilentMoon/screen/nav_bar_screens/profile/about_us.dart';
import 'package:SilentMoon/screen/onBoarding/onboarding_page_view.dart';
Expand All @@ -9,6 +10,7 @@ import 'package:SilentMoon/screen/player.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:provider/provider.dart';

class RouteGenerator {
static Route<dynamic> generateRoute(RouteSettings settings) {
Expand Down
Loading

0 comments on commit c4015c7

Please sign in to comment.