Skip to content

Commit

Permalink
Move state
Browse files Browse the repository at this point in the history
  • Loading branch information
pyrossh committed Aug 20, 2023
1 parent 36a8273 commit cfa4825
Show file tree
Hide file tree
Showing 19 changed files with 514 additions and 553 deletions.
14 changes: 12 additions & 2 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ android {
// You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
minSdkVersion 30
targetSdkVersion 34
targetSdkVersion 32
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
Expand All @@ -73,5 +73,15 @@ flutter {
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
}

configurations.all {
resolutionStrategy {
eachDependency {
if ((requested.group == "org.jetbrains.kotlin") && (requested.name.startsWith("kotlin-stdlib"))) {
useVersion("1.8.0")
}
}
}
}
2 changes: 1 addition & 1 deletion lib/app.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "package:flutter/material.dart";
import "package:flutter_gen/gen_l10n/app_localizations.dart";
import "package:only_bible_app/providers/app_model.dart";
import "package:only_bible_app/screens/chapter_view_screen.dart";
import "package:only_bible_app/state.dart";
import "package:only_bible_app/theme.dart";

class App extends StatelessWidget {
Expand Down
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import "package:firebase_core/firebase_core.dart";
import "package:firebase_crashlytics/firebase_crashlytics.dart";
import "package:only_bible_app/firebase_options.dart";
import "package:flutter_native_splash/flutter_native_splash.dart";
import "package:only_bible_app/state.dart";
import "package:only_bible_app/app.dart";
import "package:only_bible_app/providers/app_model.dart";
import "package:provider/provider.dart";

void main() async {
Expand Down
235 changes: 235 additions & 0 deletions lib/providers/app_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
// import "package:firebase_performance/firebase_performance.dart";
import "package:flutter/services.dart";
import "package:flutter/material.dart";
import "package:only_bible_app/screens/bible_select_screen.dart";
import "package:only_bible_app/screens/book_select_screen.dart";
import "package:only_bible_app/models.dart";
import "package:only_bible_app/widgets/actions_sheet.dart";
import "package:only_bible_app/widgets/note_sheet.dart";
import "package:only_bible_app/widgets/settings_sheet.dart";
import "package:provider/provider.dart";
import "package:shared_preferences/shared_preferences.dart";
import "package:get_storage/get_storage.dart";
import "package:only_bible_app/utils.dart";

class HistoryFrame {
final int book;
final int chapter;
final int? verse;

const HistoryFrame({required this.book, required this.chapter, this.verse});
}

class AppModel extends ChangeNotifier {
String languageCode = "en";
Bible bible = bibles.first;
bool darkMode = false;
bool fontBold = false;
double textScaleFactor = 0;
bool actionsShown = false;
final TextEditingController noteTextController = TextEditingController();
List<HistoryFrame> history = [];
final box = GetStorage("only-bible-app-backup");

static AppModel of(BuildContext context) {
return Provider.of(context, listen: true);
}

static AppModel ofEvent(BuildContext context) {
return Provider.of(context, listen: false);
}

save() async {
final prefs = await SharedPreferences.getInstance();
await prefs.setInt("bibleId", bible.id);
await prefs.setBool("darkMode", darkMode);
await prefs.setBool("fontBold", fontBold);
await prefs.setDouble("textScaleFactor", textScaleFactor);
}

Future<(int, int)> loadData() async {
final prefs = await SharedPreferences.getInstance();
final bibleId = prefs.getInt("bibleId") ?? 1;
darkMode = prefs.getBool("darkMode") ?? false;
fontBold = prefs.getBool("fontBold") ?? false;
textScaleFactor = prefs.getDouble("textScaleFactor") ?? 1;
bible = await loadBible(bibleId);
// await Future.delayed(Duration(seconds: 3));
final book = prefs.getInt("book") ?? 0;
final chapter = prefs.getInt("chapter") ?? 0;
updateStatusBar();
return (book, chapter);
}

Future<Bible> loadBible(int id) async {
final selectedBible = bibles.firstWhere((it) => it.id == id);
// Trace customTrace;
// if (!isDesktop()) {
// customTrace = FirebasePerformance.instance.newTrace("loadBible");
// await customTrace.start();
// }
final books = await getBibleFromAsset(languageCode, selectedBible.name);
// if (!isDesktop()) {
// await customTrace.stop();
// }
return Bible.withBooks(
id: selectedBible.id,
name: selectedBible.name,
hasAudio: selectedBible.hasAudio,
books: books,
);
}

changeBible(BuildContext context) {
Navigator.of(context).pushReplacement(
createNoTransitionPageRoute(
const BibleSelectScreen(),
),
);
}

changeBibleFromHeader(BuildContext context) {
Navigator.of(context).push(
createNoTransitionPageRoute(
const BibleSelectScreen(),
),
);
}

updateCurrentBible(BuildContext context, int id) async {
// TODO: maybe use a future as the bible needs to load
bible = await loadBible(id);
notifyListeners();
save();
}

changeBook(BuildContext context) {
Navigator.of(context).push(
createNoTransitionPageRoute(
BookSelectScreen(bible: bible),
),
);
}

toggleMode() async {
darkMode = !darkMode;
updateStatusBar();
notifyListeners();
save();
}

updateStatusBar() {
if (darkMode) {
SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
systemNavigationBarColor: Color(0xFF1F1F22),
statusBarColor: Color(0xFF1F1F22),
systemNavigationBarIconBrightness: Brightness.light,
statusBarIconBrightness: Brightness.light,
));
} else {
SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
systemNavigationBarColor: Colors.white,
statusBarColor: Colors.white,
systemNavigationBarIconBrightness: Brightness.dark,
statusBarIconBrightness: Brightness.dark,
));
}
}

toggleBold() async {
fontBold = !fontBold;
notifyListeners();
save();
}

increaseFont() async {
textScaleFactor += 0.1;
notifyListeners();
save();
}

decreaseFont() async {
textScaleFactor -= 0.1;
notifyListeners();
save();
}

showSettings(BuildContext context) {
// if (isWide(context)) {
// Navigator.of(context).push(
// createNoTransitionPageRoute(
// const ScaffoldMenu(
// backgroundColor: Color(0xFFF2F2F7),
// child: SettingsSheet(),
// ),
// ),
// );
// } else {
showModalBottomSheet(
context: context,
isDismissible: true,
enableDrag: true,
showDragHandle: true,
useSafeArea: true,
builder: (context) => const SettingsSheet(),
);
// }
}

showActions(BuildContext context) {
actionsShown = true;
Scaffold.of(context).showBottomSheet(
enableDrag: false,
(context) => const ActionsSheet(),
);
notifyListeners();
}

hideActions(BuildContext context) {
if (actionsShown) {
actionsShown = false;
Navigator.of(context).pop();
notifyListeners();
}
}

bool hasNote(Verse v) {
return box.hasData("${v.book}:${v.chapter}:${v.index}:note");
}

showNoteField(BuildContext context, Verse v) {
final noteText = box.read("${v.book}:${v.chapter}:${v.index}:note") ?? "";
noteTextController.text = noteText;
showModalBottomSheet(
context: context,
isDismissible: true,
enableDrag: true,
showDragHandle: true,
useSafeArea: true,
isScrollControlled: true,
builder: (context) => NoteSheet(verse: v),
);
}

saveNote(BuildContext context, Verse v) {
final note = noteTextController.text;
box.write("${v.book}:${v.chapter}:${v.index}:note", note);
box.save();
// Close the bottom sheet
// if (!mounted) return;
// Navigator.of(context).pop();
notifyListeners();
hideNoteField(context);
}

deleteNote(BuildContext context, Verse v) {
box.remove("${v.book}:${v.chapter}:${v.index}:note");
box.save();
notifyListeners();
hideNoteField(context);
}

hideNoteField(BuildContext context) {
Navigator.of(context).pop();
}
}
Loading

0 comments on commit cfa4825

Please sign in to comment.