Skip to content

Commit

Permalink
feat: Migrate to sound null safety 💪
Browse files Browse the repository at this point in the history
  • Loading branch information
ashwinkey04 committed Jan 25, 2022
1 parent 253758a commit a7c7584
Show file tree
Hide file tree
Showing 25 changed files with 254 additions and 207 deletions.
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class _MyAppState extends State<MyApp> {
ChangeNotifierProvider<DBProvider>(create: (_) => DBProvider()),
],
child: Consumer<SettingsProvider>(
builder: (BuildContext context, value, Widget child) {
builder: (BuildContext context, value, Widget? child) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: Styles.themeData(settingsProvider.darkTheme, context),
Expand Down
4 changes: 2 additions & 2 deletions lib/model/SharedPreferences.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:shared_preferences/shared_preferences.dart';

SharedPreferences prefs;
late SharedPreferences prefs;

class Preferences {
static const THEME_STATUS = 'THEME_STATUS';
Expand All @@ -22,7 +22,7 @@ class Preferences {
prefs.setString(key, value);
}

Future<String> getString(String key) async {
Future<String?> getString(String key) async {
prefs = await SharedPreferences.getInstance();
return prefs.getString(key);
}
Expand Down
18 changes: 9 additions & 9 deletions lib/provider/audio_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import 'package:path_provider/path_provider.dart';
import 'package:raag/provider/audio_query.dart';

//TODO: Change global variables to singletons
AnimationController playPauseController;
Directory appDirectory;
File defaultArt;
late AnimationController playPauseController;
late Directory appDirectory;
File? defaultArt;

String parseToMinutesSeconds(int ms) {
String data;
Expand All @@ -29,7 +29,7 @@ String parseToMinutesSeconds(int ms) {
return data;
}

String formatDuration(Duration d) {
String formatDuration(Duration? d) {
if (d == null) return "--:--";
int minute = d.inMinutes;
int second = (d.inSeconds >= 60) ? (d.inSeconds % 60) : d.inSeconds;
Expand Down Expand Up @@ -71,9 +71,9 @@ Widget getAlbumArt(SongModel song, Color iconColor) {
// image: FileImage(File(song.albumArtwork)),
// );
return FutureBuilder(
future: audioQuery.getAlbumArt(id: song?.id),
future: audioQuery.getAlbumArt(id: song.id),
builder: (context, snapshot) {
Uint8List _imageBytes = snapshot.data;
Uint8List? _imageBytes = snapshot.data as Uint8List?;
if (_imageBytes == null || _imageBytes.isEmpty)
return defaultIcon;
else
Expand All @@ -92,10 +92,10 @@ Widget getMediaAlbumArt(MediaItem song, Color iconColor) {
// image: FileImage(File(song.albumArtwork)),
// );
return FutureBuilder(
future: audioQuery.getAlbumArt(
id: int.parse(song?.id), size: 300, quality: 200),
future:
audioQuery.getAlbumArt(id: int.parse(song.id), size: 300, quality: 200),
builder: (context, snapshot) {
Uint8List _imageBytes = snapshot.data;
Uint8List? _imageBytes = snapshot.data as Uint8List?;
if (_imageBytes == null || _imageBytes.isEmpty)
return defaultIcon;
else
Expand Down
48 changes: 25 additions & 23 deletions lib/provider/audio_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ class OfflineAudioQuery {
}

Future<List<SongModel>> getSongs({
SongSortType sortType,
OrderType orderType,
String path,
SongSortType? sortType,
OrderType? orderType,
String? path,
}) async {
return audioQuery.querySongs(
sortType: sortType ?? SongSortType.DATE_ADDED,
Expand All @@ -46,49 +46,50 @@ class OfflineAudioQuery {
);
}

Future<Uint8List> getAlbumArt({int id, int quality = 100, int size = 150}) =>
Future<Uint8List?> getAlbumArt(
{required int id, int quality = 100, int size = 150}) =>
audioQuery.queryArtwork(id, ArtworkType.AUDIO,
quality: quality, size: size);

Future<List<PlaylistModel>> getPlaylists() async {
return audioQuery.queryPlaylists();
}

Future<bool> createPlaylist({String name}) async {
Future<bool> createPlaylist({required String name}) async {
name.replaceAll(avoid, '').replaceAll(' ', ' ');
return audioQuery.createPlaylist(name);
}

Future<bool> removePlaylist({int playlistId}) async {
Future<bool> removePlaylist({required int playlistId}) async {
return audioQuery.removePlaylist(playlistId);
}

Future<bool> addToPlaylist({
int playlistId,
int audioId,
required int playlistId,
required int audioId,
}) async {
return audioQuery.addToPlaylist(playlistId, audioId);
}

Future<bool> removeFromPlaylist({
int playlistId,
int audioId,
required int playlistId,
required int audioId,
}) async {
return audioQuery.removeFromPlaylist(playlistId, audioId);
}

Future<bool> renamePlaylist({
int playlistId,
String newName,
required int playlistId,
required String newName,
}) async {
return audioQuery.renamePlaylist(playlistId, newName);
}

Future<List<SongModel>> getPlaylistSongs(
int playlistId, {
SongSortType sortType,
OrderType orderType,
String path,
SongSortType? sortType,
OrderType? orderType,
String? path,
}) async {
return audioQuery.queryAudiosFrom(
AudiosFromType.PLAYLIST,
Expand All @@ -99,8 +100,8 @@ class OfflineAudioQuery {
}

Future<List<AlbumModel>> getAlbums({
AlbumSortType sortType,
OrderType orderType,
AlbumSortType? sortType,
OrderType? orderType,
}) async {
return audioQuery.queryAlbums(
sortType: sortType,
Expand All @@ -110,8 +111,8 @@ class OfflineAudioQuery {
}

Future<List<ArtistModel>> getArtists({
ArtistSortType sortType,
OrderType orderType,
ArtistSortType? sortType,
OrderType? orderType,
}) async {
return audioQuery.queryArtists(
sortType: sortType,
Expand All @@ -121,8 +122,8 @@ class OfflineAudioQuery {
}

Future<List<GenreModel>> getGenres({
GenreSortType sortType,
OrderType orderType,
GenreSortType? sortType,
OrderType? orderType,
}) async {
return audioQuery.queryGenres(
sortType: sortType,
Expand All @@ -132,7 +133,7 @@ class OfflineAudioQuery {
}

static Future<String> imageQuery(SongModel song) async {
final Uint8List _imageBytes = await audioQuery.queryArtwork(
final Uint8List? _imageBytes = await audioQuery.queryArtwork(
song.id,
ArtworkType.AUDIO,
format: ArtworkFormat.JPEG,
Expand All @@ -142,7 +143,8 @@ class OfflineAudioQuery {
if (_imageBytes == null || _imageBytes.isEmpty)
return (await getDefaultArt()).path;

final File file = File('${appDirectory.path}/${song.id}_${song.displayNameWOExt}.jpg');
final File file =
File('${appDirectory.path}/${song.id}_${song.displayNameWOExt}.jpg');
if (!await file.exists()) {
await file.create();
file.writeAsBytesSync(_imageBytes);
Expand Down
37 changes: 18 additions & 19 deletions lib/provider/db_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'dart:io';
import 'package:flutter/material.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:on_audio_query/on_audio_query.dart';
import 'package:path_provider/path_provider.dart';
import 'package:raag/model/song_model_adapter.dart';
import 'audio_query.dart';

Expand Down Expand Up @@ -44,22 +43,22 @@ Future<void> initHive() async {
}

Future<void> openHiveBox(String boxName, {bool limit = false}) async {
final box = await Hive.openBox(boxName).onError((error, stackTrace) async {
final Directory dir = await getApplicationDocumentsDirectory();
final String dirPath = dir.path;
File dbFile = File('$dirPath/$boxName.hive');
File lockFile = File('$dirPath/$boxName.lock');
if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
dbFile = File('$dirPath/BlackHole/$boxName.hive');
lockFile = File('$dirPath/BlackHole/$boxName.lock');
}
await dbFile.delete();
await lockFile.delete();
await Hive.openBox(boxName);
throw 'Failed to open $boxName Box\nError: $error';
});
// clear box if it grows large
if (limit && box.length > 800) {
box.clear();
}
// final box = await Hive.openBox(boxName).onError((dynamic error, stackTrace) async {
// final Directory dir = await getApplicationDocumentsDirectory();
// final String dirPath = dir.path;
// File dbFile = File('$dirPath/$boxName.hive');
// File lockFile = File('$dirPath/$boxName.lock');
// if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
// dbFile = File('$dirPath/BlackHole/$boxName.hive');
// lockFile = File('$dirPath/BlackHole/$boxName.lock');
// }
// await dbFile.delete();
// await lockFile.delete();
// await Hive.openBox(boxName);
// throw 'Failed to open $boxName Box\nError: $error';
// } as FutureOr<Box<dynamic>> Function(_, StackTrace));
// // clear box if it grows large
// if (limit && box.length > 800) {
// box.clear();
// }
}
Loading

0 comments on commit a7c7584

Please sign in to comment.