Skip to content

Commit

Permalink
audio favorite
Browse files Browse the repository at this point in the history
  • Loading branch information
husen-hn committed Apr 18, 2021
1 parent fa27a25 commit 5f3fe07
Show file tree
Hide file tree
Showing 16 changed files with 472 additions and 95 deletions.
2 changes: 1 addition & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ android {
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.example.feedTest"
minSdkVersion 16
minSdkVersion 21
targetSdkVersion 29
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
Expand Down
1 change: 1 addition & 0 deletions assets/meditating_lady.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion assets/meditation.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import 'dart:async';

import 'package:SilentMoon/model/audio.dart';
import 'package:SilentMoon/model/player_model.dart';
import 'package:audioplayers/audioplayers.dart';
import 'package:bloc/bloc.dart';
import 'package:hive/hive.dart';
import 'package:meta/meta.dart';

part 'player_event.dart';
part 'player_state.dart';

class PlayerBloc extends Bloc<PlayerEvent, PlayerState> {
PlayerBloc() : super(PlayerInitial());

AudioPlayer _audioplayer = AudioPlayer();

bool _isPlaying = false;
Expand All @@ -20,6 +21,8 @@ class PlayerBloc extends Bloc<PlayerEvent, PlayerState> {
String _completetime = "0:00:00";
int _currentSecond = 0;
int _completeSecond = 120;

PlayerBloc() : super(PlayerInitial());
@override
Stream<PlayerState> mapEventToState(
PlayerEvent event,
Expand Down Expand Up @@ -162,6 +165,32 @@ class PlayerBloc extends Bloc<PlayerEvent, PlayerState> {
} else if (event is Error) {
yield PlayerError(message: event.message);
}
// check the audio is favorite or not
else if (event is CheckFavorite) {
var box = await Hive.openBox<Audio>('${event.audioInfo.boxTitle}Fav');

List _audio = box.values
.where((item) => item.audioName == event.audioInfo.title)
.toList();

yield _audio.isEmpty ? FavoriteChecking(false) : FavoriteChecking(true);
}
// set audio to favorite list and if right now exist on this list so delete it from list.
else if (event is SetFavorite) {
var box = await Hive.openBox<Audio>('${event.audioInfo.boxTitle}Fav');

if (event.isFav) {
List _audio = box.values
.where((item) => item.audioName == event.audioInfo.title)
.toList();
_audio[0].delete();
} else {
Audio _audio = Audio(
audioName: event.audioInfo.title, audioUrl: event.audioInfo.url);
box.add(_audio);
}
yield FavoriteChecking(!event.isFav);
}
}

int _timeToSecondFormatter(String duration) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,14 @@ class Error extends PlayerEvent {
final String message;
Error({@required this.message});
}

class CheckFavorite extends PlayerEvent {
final PlayerModel audioInfo;
CheckFavorite(this.audioInfo);
}

class SetFavorite extends PlayerEvent {
final PlayerModel audioInfo;
final bool isFav;
SetFavorite(this.audioInfo, this.isFav);
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,8 @@ class PlayerError extends PlayerState {
@required this.message,
});
}

class FavoriteChecking extends PlayerState {
final bool isFav;
FavoriteChecking(this.isFav);
}
14 changes: 12 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import 'dart:io';

import 'package:SilentMoon/generated/l10n.dart';
import 'package:SilentMoon/model/audio.dart';
import 'package:SilentMoon/provider/theme_changer.dart';
import 'package:SilentMoon/theme/style.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:SilentMoon/route_generator.dart';
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:path_provider/path_provider.dart' as pathProvider;
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() => runApp(SilentMoonApp());
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
Directory directory = await pathProvider.getApplicationDocumentsDirectory();
Hive.init(directory.path);
Hive.registerAdapter(AudioAdapter());
runApp(SilentMoonApp());
}

class SilentMoonApp extends StatelessWidget {
@override
Expand Down
13 changes: 13 additions & 0 deletions lib/model/audio.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:hive/hive.dart';
part 'audio.g.dart';

@HiveType(typeId: 0)
class Audio extends HiveObject {
@HiveField(0)
String audioName;

@HiveField(1)
String audioUrl;

Audio({this.audioName, this.audioUrl});
}
44 changes: 44 additions & 0 deletions lib/model/audio.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion lib/model/player_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import 'package:flutter/cupertino.dart';
class PlayerModel {
final String title;
final String boxTitle;
final String url;

PlayerModel({@required this.title, @required this.boxTitle});
PlayerModel(
{@required this.title, @required this.boxTitle, @required this.url});
}
5 changes: 3 additions & 2 deletions lib/route_generator.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:SilentMoon/bloc/player_bloc.dart';
import 'package:SilentMoon/bloc/player_bloc/player_bloc.dart';
import 'package:SilentMoon/main.dart';
import 'package:SilentMoon/screen/main_screen.dart';
import 'package:SilentMoon/screen/nav_bar_screens/profile/about_us.dart';
Expand Down Expand Up @@ -33,7 +33,8 @@ class RouteGenerator {
case '/sound_player':
return MaterialPageRoute(
builder: (_) => BlocProvider<PlayerBloc>(
create: (BuildContext context) => PlayerBloc(),
create: (BuildContext context) =>
PlayerBloc()..add(CheckFavorite(arguament)),
child: Player(
soundPlayArgs: arguament,
)));
Expand Down
4 changes: 2 additions & 2 deletions lib/screen/nav_bar_screens/profile/profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ class _ProfileState extends State<Profile> {
Center(
child: MediaQuery.of(context).size.height > 800
? Lottie.asset(
'assets/meditation.json',
height: MediaQuery.of(context).size.height * .3,
'assets/meditating_lady.json',
height: 350.0,
)
: Container(),
)
Expand Down
Loading

0 comments on commit 5f3fe07

Please sign in to comment.