Skip to content

Commit

Permalink
add stitch verse functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Angelo Cordero committed Feb 10, 2023
1 parent fb15081 commit 03254cc
Show file tree
Hide file tree
Showing 12 changed files with 263 additions and 116 deletions.
8 changes: 8 additions & 0 deletions lib/core/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ Future<String> mediaDirectory() async {
return p.join(await appDirectory(), 'media');
}

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

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

Future<String> photosDirectory() async {
return p.join(await mediaDirectory(), 'photos');
}
Expand Down
7 changes: 7 additions & 0 deletions lib/core/file_utils.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:convert';
import 'dart:io';

import 'package:file_picker/file_picker.dart';
Expand All @@ -19,6 +20,8 @@ class FileUtils {
Directory(await songsDirectory()).create(recursive: false);
Directory(await playlistsDirectory()).create(recursive: false);
Directory(await biblesDirectory()).create(recursive: false);

File(await settingsFile()).createSync(recursive: true);
}

static Future<void> importPhotos(List<File> files) async {
Expand Down Expand Up @@ -124,4 +127,8 @@ class FileUtils {

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

static void saveSettings(Map<String, String> settings) async {
File(await settingsFile()).writeAsStringSync(const JsonEncoder.withIndent(' ').convert(settings));
}
}
44 changes: 43 additions & 1 deletion lib/core/providers_declaration.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:freecps/core/constants.dart';
import 'package:freecps/core/file_utils.dart';
import 'package:freecps/notifiers/settings_notifier.dart';
import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';

import '../models/playlist_model.dart';
Expand Down Expand Up @@ -42,11 +45,50 @@ final verseListControllerProvider = StateNotifierProvider<VerseListControllerNot
});

final activePlaylistProvider = StateNotifierProvider<PlaylistNotifier, Playlist>((ref) {
return PlaylistNotifier();
return PlaylistNotifier(ref);
});

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

final settingsProvider = StateNotifierProvider<SettingsNotifier, Map<String, String>>((ref) {
String file = ref.watch(directoriesProvider)['settingsFile']!;

return SettingsNotifier(file);
});

final directoriesProvider = StateProvider<Map<String, String>>((ref) {
return {};
});

final initProvider = FutureProvider<bool?>((ref) async {
bool? initialized;

try {
await FileUtils.initializeDirectories();

ref.read(directoriesProvider.notifier).state = {
'photosDir': await photosDirectory(),
'videosDir': await videosDirectory(),
'appDir': await appDirectory(),
'biblesDir': await biblesDirectory(),
'songsDir': await songsDirectory(),
'mediaDir': await mediaDirectory(),
'settingsDir': await settingsDir(),
'settingsFile': await settingsFile(),
'photoThumbnailsDir': await photoThumbnailsDirectory(),
'playlistDir': await playlistsDirectory(),
};

await Future.delayed(const Duration(seconds: 1));

initialized = true;
} catch (e) {
//
}

return initialized;
});
7 changes: 1 addition & 6 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import 'package:dart_vlc/dart_vlc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:hive/hive.dart';
import 'package:path_provider/path_provider.dart';

import 'core/file_utils.dart';
import 'windows/main_window.dart';
import 'windows/projection_window.dart';
import 'package:window_manager/window_manager.dart';
Expand All @@ -15,9 +12,7 @@ void main(List<String> args) async {
if (args.isEmpty) {
// TODO: put minimum size in main window
// TODO: put in initialize window before main window?
FileUtils.initializeDirectories();
Hive.init(await getApplicationSupportDirectory().then((value) => value.path));
await Hive.openBox('settings');

runApp(
ProviderScope(
child: MaterialApp(
Expand Down
2 changes: 1 addition & 1 deletion lib/models/playlist_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class Playlist {
};
}

String toJson() => json.encode(toMap());
String toJson() => const JsonEncoder.withIndent(' ').convert(toMap());

Playlist copyWith({
String? title,
Expand Down
16 changes: 10 additions & 6 deletions lib/notifiers/playlist_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,32 @@ import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:hive/hive.dart';
import 'package:freecps/core/providers_declaration.dart';
import 'package:path/path.dart';

import '../core/constants.dart';
import '../core/file_utils.dart';
import '../models/playlist_model.dart';

class PlaylistNotifier extends StateNotifier<Playlist> {
PlaylistNotifier() : super(Playlist.empty()) {
PlaylistNotifier(this.ref) : super(Playlist.empty()) {
_init();
}

late String playlistDir;
late String songsDir;
StateNotifierProviderRef<PlaylistNotifier, Playlist> ref;

select(String fileName) async {
String fileDir = await FileUtils.getPlaylistPath(fileName);


try {
state = Playlist.fromJson(File(fileDir).readAsStringSync(), songsDir);

// save selected playlist filename to hive database
Hive.box('settings').put('playlist', fileName);
// Hive.box('settings').put('playlist', fileName);

ref.read(settingsProvider.notifier).update('playlist', fileName);
} catch (e) {
debugPrint(e.toString());
state = Playlist.error();
Expand All @@ -37,10 +39,12 @@ class PlaylistNotifier extends StateNotifier<Playlist> {
songsDir = await songsDirectory();

// get already selected playlist from hive database
String? playlistFileName = Hive.box('settings').get('playlist');
//String? playlistFileName = Hive.box('settings').get('playlist');

String? playlistFileName = ref.read(settingsProvider)['playlist'];

if (playlistFileName != null) {
await select( playlistFileName);
await select(playlistFileName);
}

await _listen();
Expand Down
34 changes: 34 additions & 0 deletions lib/notifiers/settings_notifier.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'dart:convert';
import 'dart:io';

import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../core/file_utils.dart';

class SettingsNotifier extends StateNotifier<Map<String, String>> {
SettingsNotifier(this.fileName) : super({}) {
_init();
}

String fileName;

void _init() {
try {
state = Map<String, String>.from(jsonDecode(File(fileName).readAsStringSync()));
} catch (e) {
//
}
}

void update(String key, String value) {
state.update(
key,
(_) => value,
ifAbsent: () => value,
);

state = Map<String, String>.from(state);

FileUtils.saveSettings(state);
}
}
44 changes: 44 additions & 0 deletions lib/notifiers/slides_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ class SlidesNotifier extends StateNotifier<List<Slide>> {
_scriptureReference = scripture.scriptureRef;
_setSlidesPanelTitle(scriptureRefToRefString(scripture.scriptureRef));

bool breakOnNewVerse = _ref.read(settingsProvider.select((value) => value['break_on_new_verse'])) == 'true' ? true : false;

if (endVerse != null) {
if (breakOnNewVerse == false) {
stitchVerses(scripture, startVerse, endVerse);
return;
}
_setMultipleScriptureSlides(scripture, startVerse, endVerse);
return;
}
Expand Down Expand Up @@ -196,4 +202,42 @@ class SlidesNotifier extends StateNotifier<List<Slide>> {
void _setSlidesPanelTitle(String title) {
_ref.read(slidePanelTitleProvider.notifier).state = title;
}

void stitchVerses(Scripture scripture, int startVerse, int endVerse) {
String passage = '';

scripture.verses!
.getRange(
startVerse - 1,
endVerse,
)
.forEach(
(verse) {
String text = _getTextWithSuperscript(verse.num, verse.text);

passage = '$passage $text';
},
);

int maxChars = maxCharacters(
passage,
const TextStyle(
fontFamily: 'LemonMilk',
fontSize: 80,
color: Colors.white,
),
);

if (passage.length > maxChars) {
state = splitSlides(passage, maxChars, scripture);
} else {
state = [
Slide(
text: passage,
reference: _scriptureRefToString(scripture.scriptureRef),
slideType: SlideType.scripture,
),
];
}
}
}
10 changes: 10 additions & 0 deletions lib/panels/scripture_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ class ScriptureSettings extends ConsumerWidget {
},
child: const Text('Save Verses To Playlist'),
),
const SizedBox(
height: 50,
),
SwitchListTile(
title: const Text('Break on new verse'),
value: ref.watch(settingsProvider.select((value) => value['break_on_new_verse'])) == 'true' ? true : false,
onChanged: (bool newValue) {
ref.read(settingsProvider.notifier).update('break_on_new_verse', newValue.toString());
},
),
],
),
);
Expand Down
Loading

0 comments on commit 03254cc

Please sign in to comment.