Skip to content

Commit

Permalink
add actions
Browse files Browse the repository at this point in the history
  • Loading branch information
pyrossh committed Aug 18, 2023
1 parent b4cc016 commit 8419794
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 56 deletions.
File renamed without changes.
6 changes: 6 additions & 0 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ PODS:
- PromisesObjC (2.3.1)
- PromisesSwift (2.3.1):
- PromisesObjC (= 2.3.1)
- share_plus (0.0.1):
- Flutter
- shared_preferences_foundation (0.0.1):
- Flutter
- FlutterMacOS
Expand All @@ -130,6 +132,7 @@ DEPENDENCIES:
- integration_test (from `.symlinks/plugins/integration_test/ios`)
- just_audio (from `.symlinks/plugins/just_audio/ios`)
- path_provider_foundation (from `.symlinks/plugins/path_provider_foundation/darwin`)
- share_plus (from `.symlinks/plugins/share_plus/ios`)
- shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/darwin`)

SPEC REPOS:
Expand Down Expand Up @@ -175,6 +178,8 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/just_audio/ios"
path_provider_foundation:
:path: ".symlinks/plugins/path_provider_foundation/darwin"
share_plus:
:path: ".symlinks/plugins/share_plus/ios"
shared_preferences_foundation:
:path: ".symlinks/plugins/shared_preferences_foundation/darwin"

Expand Down Expand Up @@ -208,6 +213,7 @@ SPEC CHECKSUMS:
path_provider_foundation: 29f094ae23ebbca9d3d0cec13889cd9060c0e943
PromisesObjC: c50d2056b5253dadbd6c2bea79b0674bd5a52fa4
PromisesSwift: 28dca69a9c40779916ac2d6985a0192a5cb4a265
share_plus: 599aa54e4ea31d4b4c0e9c911bcc26c55e791028
shared_preferences_foundation: 5b919d13b803cadd15ed2dc053125c68730e5126

PODFILE CHECKSUM: 70d9d25280d0dd177a5f637cdb0f0b0b12c6a189
Expand Down
27 changes: 9 additions & 18 deletions lib/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,12 @@ class Chapter {
}

class Verse {
final int index;
final int book;
final int chapter;
final String text;
final TimeRange audioRange;

const Verse({required this.text, required this.audioRange});
}

class TimeRange {
final double start;
final double end;

const TimeRange({required this.start, required this.end});
const Verse({required this.index, required this.text, required this.chapter, required this.book});
}

const bookNames = <String, List<String>>{
Expand Down Expand Up @@ -281,7 +276,7 @@ const bookNames = <String, List<String>>{
};

final bibles = [
Bible(id: 1, name: "KJV", hasAudio: false),
Bible(id: 1, name: "English", hasAudio: false),
Bible(id: 2, name: "Kannada", hasAudio: true),
Bible(id: 3, name: "Nepali", hasAudio: false),
Bible(id: 4, name: "Hindi", hasAudio: false),
Expand All @@ -304,14 +299,8 @@ List<Book> getBibleFromText(String languageCode, String text) {
}
var book = int.parse(line.substring(0, 2));
var chapter = int.parse(line.substring(3, 6));
// var verseNo = line.substring(7, 10);
var verseNo = int.parse(line.substring(7, 10));
var verseText = line.substring(11);
double start = 0;
double end = 0;
// if (item.length > 4) {
// start = double.parse(item[4]);
// end = double.parse(item[5]);
// }
if (books.length < book) {
books.add(
Book(
Expand All @@ -327,8 +316,10 @@ List<Book> getBibleFromText(String languageCode, String text) {
}
books[book - 1].chapters[chapter - 1].verses.add(
Verse(
index: verseNo - 1,
text: verseText,
audioRange: TimeRange(start: start, end: end),
chapter: chapter - 1,
book: book - 1,
),
);
}
Expand Down
1 change: 1 addition & 0 deletions lib/screens/chapter_select_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ChapterSelectScreen extends StatelessWidget {

const ChapterSelectScreen({super.key, required this.selectedBookIndex, required this.book});

// TODO: move this to app and allow to pause
onChapterSelected(BuildContext context, int index) {
Navigator.of(context).pushReplacement(
createNoTransitionPageRoute(
Expand Down
77 changes: 55 additions & 22 deletions lib/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import "package:only_bible_app/widgets/actions_sheet.dart";
import "package:only_bible_app/widgets/scaffold_menu.dart";
import "package:only_bible_app/widgets/settings_sheet.dart";
import "package:provider/provider.dart";
import "package:share_plus/share_plus.dart";
import "package:shared_preferences/shared_preferences.dart";
import "package:get_storage/get_storage.dart";

class HistoryFrame {
final int book;
Expand All @@ -35,6 +37,7 @@ class AppModel extends ChangeNotifier {
double textScaleFactor = 0;
bool actionsShown = false;
List<HistoryFrame> history = [];
final box = GetStorage("only-bible-app-backup");

static AppModel of(BuildContext context) {
return Provider.of(context, listen: true);
Expand Down Expand Up @@ -202,7 +205,7 @@ class AppModel extends ChangeNotifier {
class ChapterViewModel extends ChangeNotifier {
final int book;
final int chapter;
final List<int> selectedVerses;
final List<Verse> selectedVerses;
final player = AudioPlayer();
bool isPlaying = false;

Expand Down Expand Up @@ -235,6 +238,9 @@ class ChapterViewModel extends ChangeNotifier {
}

navigateBookChapter(BuildContext context, int book, int chapter, TextDirection? dir) {
if (isPlaying) {
pause();
}
AppModel.ofEvent(context).hideActions(context);
Navigator.of(context).push(
createSlideRoute(
Expand Down Expand Up @@ -279,45 +285,74 @@ class ChapterViewModel extends ChangeNotifier {
return selectedVerses.isNotEmpty;
}

bool isVerseSelected(int i) {
return selectedVerses.contains(i);
void clearSelections(BuildContext context) {
selectedVerses.clear();
AppModel.ofEvent(context).hideActions(context);
notifyListeners();
}

bool isVerseSelected(Verse v) {
return selectedVerses.any((el) => el.index == v.index);
}

void onVerseSelected(BuildContext context, int i) {
bool isVerseHighlighted(BuildContext context) {
// box.read("${book}:${chapter}:${verse}", "color");
return false;
}

void onVerseSelected(BuildContext context, Verse v) {
if (selectedVerses.isEmpty) {
AppModel.ofEvent(context).showActions(context);
}
if (selectedVerses.contains(i)) {
selectedVerses.remove(i);
if (isVerseSelected(v)) {
selectedVerses.removeWhere((it) => it.index == v.index);
} else {
selectedVerses.add(i);
selectedVerses.add(v);
}
if (selectedVerses.isEmpty) {
AppModel.ofEvent(context).hideActions(context);
}
notifyListeners();
}

void copyVerses() {
final text = selectedVerses.map((e) => e.text).join("\n");
Clipboard.setData(ClipboardData(text: text));
// maybe close the action menu or show a snackbar
// ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("Email address copied to clipboard")));
}

void shareVerses() {
final text = selectedVerses.map((e) => e.text).join("\n");
Share.share(text);
}

pause() async {
await player.pause();
isPlaying = false;
notifyListeners();
}

onPlay(BuildContext context) async {
final bible = AppModel.ofEvent(context).bible;
final model = ChapterViewModel.ofEvent(context);
// if (!bible.hasAudio) {
// showError(context, "This bible version doesn't support ");
// return;
// }
if (!bible.hasAudio) {
showError(
context,
"This Bible doesn't support audio. Currently audio is only available for the Kannada Bible.",
);
return;
}
if (isPlaying) {
await player.pause();
isPlaying = false;
notifyListeners();
pause();
} else {
isPlaying = true;
notifyListeners();
for (final v in selectedVerses) {
final bibleName = bible.name;
final book = (model.book + 1).toString().padLeft(2, "0");
final chapter = (model.chapter + 1).toString().padLeft(3, "0");
final verse = (v + 1).toString().padLeft(3, "0");
final pathname = "$bibleName/$book-$chapter-$verse.mp3";
final book = (v.book + 1).toString().padLeft(2, "0");
final chapter = (v.chapter + 1).toString().padLeft(3, "0");
final verseNo = (v.index + 1).toString().padLeft(3, "0");
final pathname = "$bibleName/$book-$chapter-$verseNo.mp3";
try {
final url = await FirebaseStorage.instance.ref(pathname).getDownloadURL();
await player.setUrl(url);
Expand All @@ -329,9 +364,7 @@ class ChapterViewModel extends ChangeNotifier {
showError(context, "Could not play audio");
return;
} finally {
await player.pause();
isPlaying = false;
notifyListeners();
pause();
}
}
}
Expand Down
19 changes: 12 additions & 7 deletions lib/widgets/actions_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,17 @@ class ActionsSheet extends StatelessWidget {
IconButtonText(
leading: IconButton(
padding: EdgeInsets.zero,
onPressed: () {},
onPressed: () {
model.clearSelections(context);
},
icon: Icon(Icons.cancel_outlined, size: 24 + iconSize, color: iconColor),
),
trailing: Text("Clear", style: bodySmall),
),
IconButtonText(
leading: IconButton(
padding: EdgeInsets.zero,
onPressed: () {},
onPressed: model.copyVerses,
icon: Icon(Icons.copy, size: 24 + iconSize, color: iconColor),
),
trailing: Text("Copy", style: bodySmall),
Expand All @@ -78,13 +80,16 @@ class ActionsSheet extends StatelessWidget {
leading: IconButton(
padding: EdgeInsets.zero,
onPressed: () {
if (app.bible.hasAudio) {
model.onPlay(context);
}
model.onPlay(context);
},
icon: Icon(audioIcon, size: 34 + iconSize, color: app.bible.hasAudio ? iconColor : Colors.grey),
),
trailing: Text(audioText, style: bodySmall),
trailing: Text(
audioText,
style: bodySmall!.copyWith(
color: app.bible.hasAudio ? bodySmall!.color : Colors.grey,
),
),
),
IconButtonText(
leading: IconButton(
Expand All @@ -97,7 +102,7 @@ class ActionsSheet extends StatelessWidget {
IconButtonText(
leading: IconButton(
padding: EdgeInsets.zero,
onPressed: () {},
onPressed: model.shareVerses,
icon: Icon(Icons.share_outlined, size: 28 + iconSize, color: iconColor),
),
trailing: Text("Share", style: bodySmall),
Expand Down
2 changes: 2 additions & 0 deletions lib/widgets/chapter_app_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class ChapterAppBar extends StatelessWidget implements PreferredSizeWidget {
@override
Size get preferredSize => const Size.fromHeight(40);

// TODO: add next/prev buttons for desktop mode

@override
Widget build(BuildContext context) {
final app = AppModel.of(context);
Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/settings_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class SettingsSheet extends StatelessWidget {
tiles: [
SettingsTile.navigation(
leading: const Icon(Icons.language, color: Colors.green),
title: const Text("Language"),
title: const Text("App Language"),
value: const Text("English"),
),
SettingsTile.navigation(
Expand Down
12 changes: 5 additions & 7 deletions lib/widgets/verses_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,22 @@ class VersesView extends StatelessWidget {
: textStyle,
// recognizer: TapAndPanGestureRecognizer()..onDragEnd = (e) => print("Hello"),
children: chapter.verses
.asMap()
.entries
.map(
(e) => [
(v) => [
WidgetSpan(
child: Transform.translate(
offset: const Offset(0, -2),
child: Text("${e.key + 1} ", style: Theme.of(context).textTheme.labelMedium),
child: Text("${v.index + 1} ", style: Theme.of(context).textTheme.labelMedium),
),
),
TextSpan(
text: "${e.value.text}\n",
style: model.isVerseSelected(e.key)
text: "${v.text}\n",
style: model.isVerseSelected(v)
? TextStyle(
backgroundColor: Theme.of(context).highlightColor,
)
: null,
recognizer: TapGestureRecognizer()..onTap = () => model.onVerseSelected(context, e.key),
recognizer: TapGestureRecognizer()..onTap = () => model.onVerseSelected(context, v),
),
const WidgetSpan(
child: Padding(
Expand Down
6 changes: 6 additions & 0 deletions macos/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ PODS:
- PromisesObjC (2.3.1)
- PromisesSwift (2.3.1):
- PromisesObjC (= 2.3.1)
- share_plus (0.0.1):
- FlutterMacOS
- shared_preferences_foundation (0.0.1):
- Flutter
- FlutterMacOS
Expand All @@ -97,6 +99,7 @@ DEPENDENCIES:
- FlutterMacOS (from `Flutter/ephemeral`)
- just_audio (from `Flutter/ephemeral/.symlinks/plugins/just_audio/macos`)
- path_provider_foundation (from `Flutter/ephemeral/.symlinks/plugins/path_provider_foundation/darwin`)
- share_plus (from `Flutter/ephemeral/.symlinks/plugins/share_plus/macos`)
- shared_preferences_foundation (from `Flutter/ephemeral/.symlinks/plugins/shared_preferences_foundation/darwin`)

SPEC REPOS:
Expand Down Expand Up @@ -133,6 +136,8 @@ EXTERNAL SOURCES:
:path: Flutter/ephemeral/.symlinks/plugins/just_audio/macos
path_provider_foundation:
:path: Flutter/ephemeral/.symlinks/plugins/path_provider_foundation/darwin
share_plus:
:path: Flutter/ephemeral/.symlinks/plugins/share_plus/macos
shared_preferences_foundation:
:path: Flutter/ephemeral/.symlinks/plugins/shared_preferences_foundation/darwin

Expand Down Expand Up @@ -160,6 +165,7 @@ SPEC CHECKSUMS:
path_provider_foundation: 29f094ae23ebbca9d3d0cec13889cd9060c0e943
PromisesObjC: c50d2056b5253dadbd6c2bea79b0674bd5a52fa4
PromisesSwift: 28dca69a9c40779916ac2d6985a0192a5cb4a265
share_plus: 76dd39142738f7a68dd57b05093b5e8193f220f7
shared_preferences_foundation: 5b919d13b803cadd15ed2dc053125c68730e5126

PODFILE CHECKSUM: 236401fc2c932af29a9fcf0e97baeeb2d750d367
Expand Down
Loading

0 comments on commit 8419794

Please sign in to comment.