Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Angelo Cordero committed Feb 10, 2023
1 parent 03254cc commit 47fba52
Show file tree
Hide file tree
Showing 28 changed files with 289 additions and 499 deletions.
49 changes: 36 additions & 13 deletions lib/core/constants.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dart:ui';

import 'package:path/path.dart' as p;
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:tuple/tuple.dart';

Expand Down Expand Up @@ -28,6 +28,25 @@ enum SlideType {
song,
}

TextStyle songSlideTextStyle = const TextStyle(
fontFamily: 'LemonMilk',
fontSize: 80,
color: Colors.white,
);

TextStyle verseSlideTextStyle = const TextStyle(
fontFamily: 'SegoeUI',
fontSize: 80,
color: Colors.white,
);

TextStyle refTextStyle = GoogleFonts.raleway(
textStyle: const TextStyle(
fontSize: 50,
color: Colors.white,
),
);

List<String> videoFileExtensions = ['mp4', 'mov'];
List<String> photoFileExtensions = ['jpg', 'jpeg', 'png'];
List<String> bibleFileExtenstion = ['cpsb'];
Expand All @@ -39,43 +58,47 @@ Future<String> appDirectory() async {
}

Future<String> biblesDirectory() async {
return p.join(await appDirectory(), 'bibles');
return join(await appDirectory(), 'bibles');
}

Future<String> songsDirectory() async {
return p.join(await mediaDirectory(), 'songs');
return join(await mediaDirectory(), 'songs');
}

Future<String> mediaDirectory() async {
return p.join(await appDirectory(), 'media');
return join(await appDirectory(), 'media');
}

Future<String> settingsFile() async {
return p.join(await settingsDir(),'settings.json');
return join(await settingsDir(), 'settings.json');
}

Future<String> settingsDir() async {
return p.join(await appDirectory(), 'settings');
return join(await appDirectory(), 'settings');
}

Future<String> photosDirectory() async {
return p.join(await mediaDirectory(), 'photos');
return join(await mediaDirectory(), 'photos');
}

Future<String> photoThumbnailsDirectory() async {
return p.join(await photosDirectory(), 'thumbnails');
return join(await photosDirectory(), 'thumbnails');
}

Future<String> videoThumbnailsDirectory() async {
return join(await videosDirectory(), 'thumbnails');
}

Future<String> videosDirectory() async {
return p.join(await mediaDirectory(), 'videos');
return join(await mediaDirectory(), 'videos');
}

Future<String> playlistsDirectory() async {
return p.join(await mediaDirectory(), 'playlists');
return join(await mediaDirectory(), 'playlists');
}

Future<String> getPlaylistPath(String fileName) async {
return p.join(await playlistsDirectory(), fileName);
return join(await playlistsDirectory(), fileName);
}

List<String> superscriptMap = [
Expand Down
46 changes: 25 additions & 21 deletions lib/core/file_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@ import 'dart:convert';
import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:freecps/core/bible_parser.dart';
import 'package:image_compression/image_compression.dart';
import 'package:nanoid/nanoid.dart';
import 'package:path/path.dart' as p;
import 'package:path/path.dart';

import '../models/playlist_model.dart';
import '../models/song_model.dart';
import 'constants.dart' as constants;
import 'bible_parser.dart';
import 'constants.dart';
import 'helper_functions.dart';

class FileUtils {
static initializeDirectories() async {
Directory('${await photosDirectory()}/thumbnails').create(recursive: true);
Directory('${await videosDirectory()}/thumbnails').create(recursive: true);
Directory(await photoThumbnailsDirectory()).create(recursive: true);
Directory(await videoThumbnailsDirectory()).create(recursive: true);
Directory(await songsDirectory()).create(recursive: false);
Directory(await playlistsDirectory()).create(recursive: false);
Directory(await biblesDirectory()).create(recursive: false);
Expand All @@ -25,14 +23,14 @@ class FileUtils {
}

static Future<void> importPhotos(List<File> files) async {
String dir = await constants.photosDirectory();
String dir = await photosDirectory();

for (File file in files) {
String filePath = p.join(dir, basename(file.path));
String filePath = join(dir, basename(file.path));

await file.copy(filePath);

String thumbnailPath = p.join(dir, 'thumbnails', basename(file.path));
String thumbnailPath = join(dir, 'thumbnails', basename(file.path));

final output = compress(
ImageFileConfiguration(
Expand All @@ -53,30 +51,30 @@ class FileUtils {

//! TODO: make thumbnails
static void importVideos(List<File> files) async {
String dir = await constants.videosDirectory();
String dir = await videosDirectory();

for (File file in files) {
String filePath = p.join(dir, basename(file.path));
String filePath = join(dir, basename(file.path));

await file.copy(filePath);
}
}

static void importSongs(List<File> files) async {
String dir = await constants.songsDirectory();
String dir = await songsDirectory();

for (File file in files) {
String filePath = p.join(dir, basename(file.path));
String filePath = join(dir, basename(file.path));

await file.copy(filePath);
}
}

static void importPlaylist(List<File> files) async {
String dir = await constants.playlistsDirectory();
String dir = await playlistsDirectory();

for (File file in files) {
String filePath = p.join(dir, basename(file.path));
String filePath = join(dir, basename(file.path));

await file.copy(filePath);
}
Expand All @@ -93,13 +91,19 @@ class FileUtils {
}

static getVideoFilePath(String fileName) async {
String dir = await constants.videosDirectory();
String dir = await videosDirectory();

return p.join(dir, fileName);
return join(dir, fileName);
}

static getPhotoFilePath(String fileName) async {
String dir = await photosDirectory();

return join(dir, fileName);
}

static Future<String> getPlaylistPath(String fileName) async {
return p.join(await playlistsDirectory(), fileName);
return join(await playlistsDirectory(), fileName);
}

static void savePlaylist(Playlist playlist) async {
Expand All @@ -112,7 +116,7 @@ class FileUtils {
}
}

static void addMediaToPlaylist(Set<String> media, Playlist playlist) async {
static void addMediaToPlaylist(Set<String> media, Playlist playlist) {
Set<String> list = {...playlist.media, ...media};

savePlaylist(playlist.copyWith(media: list.toList()));
Expand All @@ -123,9 +127,9 @@ class FileUtils {
}

static void addNewPlaylist() async {
String fileName = '${customAlphabet(customIdAlphabet, 30)}.cpss';
String fileName = '$generateRandomID.cpss';

File(p.join(await playlistsDirectory(), fileName)).writeAsStringSync(Playlist.addNew(fileName).toJson());
File(join(await playlistsDirectory(), fileName)).writeAsStringSync(Playlist.addNew(fileName).toJson());
}

static void saveSettings(Map<String, String> settings) async {
Expand Down
21 changes: 10 additions & 11 deletions lib/core/helper_functions.dart
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
import 'package:flutter/painting.dart';
import 'package:nanoid/nanoid.dart';

import '../models/scripture_reference_model.dart';
import 'constants.dart';

String scriptureRefToRefString(ScriptureReference ref) {
String scriptureRefToString(ScriptureReference ref) {
return '${ref.translation} ${ref.book} ${ref.chapter}:${ref.verse!.verseString}';
}

String generateRandomID() {
return customAlphabet(customIdAlphabet, 30);
}

//TODO refactor
int maxCharacters(
String text,
TextStyle style,
) {
final TextPainter heightPainter = TextPainter(
text: TextSpan(text: text, style: style),
maxLines: 1,
textDirection: TextDirection.ltr,
)..layout(minWidth: 0, maxWidth: double.infinity);

final TextPainter widthPainter = TextPainter(
final TextPainter sizePainter = TextPainter(
// M as text because it probably is the widest
text: TextSpan(text: 'M', style: style),
maxLines: 1,
textDirection: TextDirection.ltr,
)..layout(minWidth: 0, maxWidth: double.infinity);

double height = heightPainter.height;
double paddingSize = 30;
int maxLines = (1080 - (2 * paddingSize) - 100) ~/ height;
int maxLines = (1080 - (2 * paddingSize) - 100) ~/ sizePainter.height;

final TextPainter textPainter = TextPainter(
text: TextSpan(
Expand All @@ -37,5 +36,5 @@ int maxCharacters(
textDirection: TextDirection.ltr,
)..layout(minWidth: 0, maxWidth: 1920 - 30 - 30);

return ((textPainter.width ~/ widthPainter.width) * maxLines) - 5;
return ((textPainter.width ~/ sizePainter.width) * maxLines) - 5;
}
19 changes: 12 additions & 7 deletions lib/core/providers_declaration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final ctrlKeyNotifier = StateProvider<bool>((ref) {
});

final scriptureProvider = StateNotifierProvider<ScriptureNotifier, Scripture>((ref) {
return ScriptureNotifier(ref);
return ScriptureNotifier(ref, ref.read(directoriesProvider)['biblesDir']!);
});

final biblesDirectoryProvider = StateProvider<String?>((ref) {
Expand All @@ -45,14 +45,19 @@ final verseListControllerProvider = StateNotifierProvider<VerseListControllerNot
});

final activePlaylistProvider = StateNotifierProvider<PlaylistNotifier, Playlist>((ref) {
return PlaylistNotifier(ref);
// return ref.watch(initProvider).maybeMap(
// data: (data) {
return PlaylistNotifier(ref);
// },
// orElse: () {
// return PlaylistNotifier(null);
// },
// );
});

final slidePanelTitleProvider = StateProvider<String>(
(ref) {
return '';
},
);
final slidePanelTitleProvider = StateProvider<String>((ref) {
return '';
});

final settingsProvider = StateNotifierProvider<SettingsNotifier, Map<String, String>>((ref) {
String file = ref.watch(directoriesProvider)['settingsFile']!;
Expand Down
23 changes: 12 additions & 11 deletions lib/media_center/media_center_providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,38 @@ import 'dart:io';
import 'package:flutter/widgets.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../core/constants.dart';
import '../core/providers_declaration.dart';
import '../models/playlist_model.dart';
import '../models/song_model.dart';
import 'notifiers/bibles_notifier.dart';
import 'notifiers/media_center_bibles_notifier.dart';
import 'notifiers/media_center_photos_notifier.dart';
import 'notifiers/media_center_playlist_preview_notifier.dart';
import 'notifiers/media_center_playlists_notifier.dart';
import 'notifiers/media_center_videos_notifier.dart';
import 'notifiers/songs_notifier.dart';
import 'notifiers/media_center_songs_notifier.dart';

//TODO make these async

final photosProvider = StateNotifierProvider.autoDispose<MediaCenterPhotosNotifier, List<File>>((ref) {
return MediaCenterPhotosNotifier(photoThumbnailsDirectory());
return MediaCenterPhotosNotifier(ref.watch(directoriesProvider)['photoThumbnailsDir']!);
});

final videosProvider = StateNotifierProvider.autoDispose<MediaCenterVideosNotifier, List<File>>((ref) {
return MediaCenterVideosNotifier(videosDirectory());
return MediaCenterVideosNotifier(ref.watch(directoriesProvider)['videosDir']!);
});

final playlistsProvider = StateNotifierProvider.autoDispose<MediaCenterPlaylistsNotifier, List<Playlist>>((ref) {
return MediaCenterPlaylistsNotifier(playlistsDirectory(), songsDirectory());
Map<String, String> dirs = ref.watch(directoriesProvider);

return MediaCenterPlaylistsNotifier(dirs['playlistDir']!, dirs['songsDir']!);
});

final songsProvider = StateNotifierProvider.autoDispose<SongsNotifier, List<Song>>((ref) {
return SongsNotifier(songsDirectory());
final songsProvider = StateNotifierProvider.autoDispose<MediaCenterSongsNotifier, List<Song>>((ref) {
return MediaCenterSongsNotifier(ref.watch(directoriesProvider)['songsDir']!);
});

final biblesProvider = StateNotifierProvider.autoDispose<BiblesNotifier, List<BibleData>>((ref) {
return BiblesNotifier(biblesDirectory());
final biblesProvider = StateNotifierProvider.autoDispose<MediaCenterBiblesNotifier, List<BibleData>>((ref) {
return MediaCenterBiblesNotifier(ref.watch(directoriesProvider)['biblesDir']!);
});

final mediaCenterCtrlKeyNotifier = StateProvider.autoDispose<bool>((ref) {
Expand Down Expand Up @@ -76,7 +77,7 @@ final previewedPlaylistProvider = StateProvider.autoDispose<Playlist>((ref) {
final playlistPreviewProvider = StateNotifierProvider.autoDispose<MediaCenterPlaylistPreviewNotifier, Widget>((ref) {
dynamic selected = ref.watch(playlistPreviewSelectedObjectProvider);

return MediaCenterPlaylistPreviewNotifier(selected);
return MediaCenterPlaylistPreviewNotifier(args: selected, photosDir: ref.watch(directoriesProvider)['photosDir']!);
});

final playlistPreviewSelectedObjectProvider = StateProvider.autoDispose<dynamic>((ref) {
Expand Down
Loading

0 comments on commit 47fba52

Please sign in to comment.