Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hijri date offset #67

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"languageToolLinter.languageTool.ignoredWordsInWorkspace": [
"hijri"
]
}
10 changes: 10 additions & 0 deletions MIGRATIONS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Migrations

This document contains a list of migrations that were undertaken by Ramadan Taskminder
to update the data structure. The migration index prior to version 1.2.0 was `-1`.
The following list is in descending order.

- `0`: Add the hijri date to the date field of Qur'an entries to support hijri date
offsets

`[ "date", [ ... ] ]` => `[ [ "gregorian", "hijri" ], [ ... ] ]`
2 changes: 1 addition & 1 deletion ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: c4c93c5f6502fe2754f48404d3594bf779584011

COCOAPODS: 1.14.3
COCOAPODS: 1.15.2
2 changes: 1 addition & 1 deletion ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
97C146E61CF9000F007C117D /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1430;
LastUpgradeCheck = 1510;
ORGANIZATIONNAME = "";
TargetAttributes = {
97C146ED1CF9000F007C117D = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1430"
LastUpgradeVersion = "1510"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
3 changes: 3 additions & 0 deletions lib/constants.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const appName = "Ramadan Taskminder";

/// The current index of migrations to be compared to `SharedPreferences.migrationIndex`.
const int currentMigrationIndex = 0;

final List<String> boxes = ["quran", "tasks", "prayers"];

Uri feedbackUrl = Uri.parse(
Expand Down
17 changes: 17 additions & 0 deletions lib/date.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:jhijri/jHijri.dart';

JHijri jHijriFromIso8601Style(String iso8601) {
final components = iso8601.split("-");
if (components.length != 3) {
print("Invalid ISO-8601 style string — \"$iso8601\"");
throw Error();
}

final numericalComponents = components.map((e) => int.parse(e));

return JHijri(
fYear: numericalComponents.elementAt(0),
fMonth: numericalComponents.elementAt(1),
fDay: numericalComponents.elementAt(2),
);
}
45 changes: 45 additions & 0 deletions lib/extensions/date.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,48 @@
import 'package:jhijri/jHijri.dart';

extension Date on DateTime {
String getYMD() => toIso8601String().split("T")[0];

DateTime offset(int offset) {
if (offset == 1) {
return add(const Duration(days: 1));
} else if (offset == -1) {
return subtract(const Duration(days: 1));
}

return this;
}
}

extension JHijriDateTime on JHijri {
String hijriMonth() {
switch (month) {
case 1:
return "Muharram";
case 2:
return "Safar";
case 3:
return "Rabi' Al-Awwal";
case 4:
return "Rabi' Al-Thani";
case 5:
return "Jumada Al-Awwal";
case 6:
return "Jumada Al-Thani";
case 7:
return "Rajab";
case 8:
return "Sha'aban";
case 9:
return "Ramadan";
case 10:
return "Shawwal";
case 11:
return "Dhu Al-Qi'dah";
case 12:
return "Dhu Al-Hijjah";
default:
return "";
}
}
}
16 changes: 12 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Future<void> main() async {
await Hive.openBox("tasks");
await Hive.openBox("quran");
await Hive.openBox("prayers");
await Hive.openBox("settings");

runApp(const RamadanTaskminder());
}
Expand Down Expand Up @@ -66,19 +67,21 @@ class RamadanTaskminder extends StatelessWidget {
return MaterialApp.router(
title: appName,
theme: ThemeData(
useMaterial3: true,
primaryColor: getPrimaryColor(context),
scaffoldBackgroundColor: backgroundColor,
useMaterial3: true,
textTheme: GoogleFonts.mPlus1pTextTheme(),
),
darkTheme: ThemeData(
useMaterial3: true,
brightness: Brightness.dark,
scaffoldBackgroundColor: Colors.black,
colorScheme: const ColorScheme.dark(
primary: Colors.black,
onPrimary: Colors.black,
secondary: primaryDarkColor,
primary: primaryDarkColor,
onPrimary: Colors.white,
surface: Colors.black,
onSurface: Colors.white,
secondary: primaryLightColor,
),
cardTheme: const CardTheme(
color: Colors.black87,
Expand All @@ -98,6 +101,11 @@ class RamadanTaskminder extends StatelessWidget {
),
),
),
textButtonTheme: TextButtonThemeData(
style: TextButton.styleFrom(
foregroundColor: buttonTextDarkColor,
),
),
textSelectionTheme: const TextSelectionThemeData(
cursorColor: primaryDarkColor,
selectionColor: primaryDarkColor,
Expand Down
64 changes: 57 additions & 7 deletions lib/screens/home.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:hijri/hijri_calendar.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:intl/intl.dart';
import 'package:jhijri/jHijri.dart';
import 'package:ramadan_taskminder/constants.dart';
import 'package:ramadan_taskminder/extensions/date.dart';
import 'package:ramadan_taskminder/extensions/int.dart';
import 'package:ramadan_taskminder/prayers.dart';
import 'package:ramadan_taskminder/quran.dart';
import 'package:ramadan_taskminder/theme.dart';
Expand All @@ -27,33 +28,82 @@ class HomeScreen extends StatefulWidget {
}

class _HomeScreenState extends State<HomeScreen> {
DateTime current = DateTime.now();
HijriCalendar hijriCurrent = HijriCalendar.now();

Box tasksBox = Hive.box("tasks");
Box prayersBox = Hive.box("prayers");
Box quran = Hive.box("quran");
Box settingsBox = Hive.box("settings");

late List<String> allTasks;
late Map<String, bool> tasks;
late Map<String, bool> prayers;

DateTime current = DateTime.now();
late JHijri hijriCurrent;

@override
void initState() {
super.initState();
firstRun();

startup();
initializeSettings();
initializeTasks();
initializeHistory();
initializePrayers();
}

void firstRun() async {
void startup() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
bool firstRun = preferences.getBool("firstRun") ?? true;
if (firstRun) {
tasksBox.put("allTasks", initialTasks);
preferences.setBool("firstRun", false);
}

int migrationIndex = preferences.getInt("migrationIndex") ?? -1;
// print("Migration Index (setting): $migrationIndex, migration index (app): $currentMigrationIndex");
if (migrationIndex < currentMigrationIndex) {
// print("Running migrations...");
final migrations = migrationIndex.upTo(currentMigrationIndex).skip(1);
for (final migration in migrations) {
// print("Running migration #$migration...");
migrate(migration);
}
}
}

void migrate(int migrationIndex) {
if (migrationIndex == 0) {
List? quranHistory = quran.get("history");
if (quranHistory != null) {
List newHistory = quranHistory.map((entry) {
if (entry[0].runtimeType == List<String>) {
return entry;
}

final gregorianDate = DateTime.parse(entry[0].toString());
final hijriDate =
JHijri(fDate: gregorianDate, fDisplay: DisplayFormat.YYYYMMDD);

return [
[entry[0], hijriDate.fullDate],
entry[1]
];
}).toList();

// print("Old: $quranHistory");
// print("New: $newHistory");
quran.put("history", newHistory);
}
}
}

void initializeSettings() {
hijriCurrent = JHijri(
fDate: DateTime.now().offset(
settingsBox.get("dateOffset", defaultValue: 0),
),
fDisplay: DisplayFormat.YYYYMMDD,
);
}

void initializeTasks() {
Expand Down Expand Up @@ -172,7 +222,7 @@ class _HomeScreenState extends State<HomeScreen> {
PageHeader(
header: appName,
title:
"${hijriCurrent.hDay} ${hijriCurrent.longMonthName} ${hijriCurrent.hYear}",
"${hijriCurrent.day} ${hijriCurrent.hijriMonth()} ${hijriCurrent.year}",
rightAlign: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
Expand Down
39 changes: 26 additions & 13 deletions lib/screens/quran.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'package:flutter/material.dart';
import 'package:hijri/hijri_calendar.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:intl/intl.dart';
import 'package:jhijri/jHijri.dart';
import 'package:ramadan_taskminder/date.dart';
import 'package:ramadan_taskminder/extensions/date.dart';
import 'package:ramadan_taskminder/theme.dart';
import 'package:ramadan_taskminder/quran.dart';
import 'package:ramadan_taskminder/widgets/page_footer.dart';
Expand Down Expand Up @@ -53,8 +55,8 @@ class _QuranScreenState extends State<QuranScreen> {
} else {
history = quranHistory;
history.sort(
(a, b) => DateTime.parse(a[0].toString())
.compareTo(DateTime.parse(b[0].toString())),
(a, b) => DateTime.parse(a[0][0].toString())
.compareTo(DateTime.parse(b[0][0].toString())),
);
}
}
Expand Down Expand Up @@ -90,8 +92,8 @@ class _QuranScreenState extends State<QuranScreen> {
context: context,
builder: (BuildContext context) {
final entry = history[deletingHistoryEntry];
final date = DateTime.parse(entry[0].toString());
final hijriDate = HijriCalendar.fromDate(date);
final date = DateTime.parse(entry[0][0].toString());
final hijriDate = jHijriFromIso8601Style(entry[0][1]);

final starting = (entry[1] as List<String>)[0].split("-");
final ending = (entry[1] as List<String>)[1].split("-");
Expand All @@ -106,7 +108,7 @@ class _QuranScreenState extends State<QuranScreen> {
"Are you sure you want to delete the following entry?"),
StackedCard(
header:
"${DateFormat.MMMMd().format(date)} / ${hijriDate.longMonthName} ${hijriDate.hDay}",
"${DateFormat.MMMMd().format(date)} / ${hijriDate.hijriMonth()} ${hijriDate.day}",
title:
"${surahs[int.parse(starting[0]) - 1]["name"].toString()} ${starting[1]} - ${surahs[int.parse(ending[0]) - 1]["name"].toString()} ${ending[1]}",
fullWidth: true,
Expand Down Expand Up @@ -264,9 +266,19 @@ class _QuranScreenState extends State<QuranScreen> {
onTap: () => setState(
() {
final entry = [
DateTime.now()
.toIso8601String()
.split("T")[0],
[
DateTime.now()
.toIso8601String()
.split("T")[0],
JHijri(
fDate:
DateTime.now().subtract(
const Duration(days: 1),
),
fDisplay:
DisplayFormat.YYYYMMDD,
).fullDate,
],
[
"${startingSurah + 1}-$startingAyah",
"${endingSurah + 1}-$endingAyah"
Expand Down Expand Up @@ -305,10 +317,11 @@ class _QuranScreenState extends State<QuranScreen> {
runSpacing: 10,
children: history.reversed.map(
(entry) {
final date =
DateTime.parse(entry[0].toString());
final date = DateTime.parse(
entry[0][0].toString(),
);
final hijriDate =
HijriCalendar.fromDate(date);
jHijriFromIso8601Style(entry[0][1]);

final starting =
(entry[1] as List<String>)[0]
Expand All @@ -319,7 +332,7 @@ class _QuranScreenState extends State<QuranScreen> {

return StackedCard(
header:
"${DateFormat.MMMMd().format(date)} / ${hijriDate.longMonthName} ${hijriDate.hDay}",
"${DateFormat.MMMMd().format(date)} / ${hijriDate.hijriMonth()} ${hijriDate.day}",
title:
"${surahs[int.parse(starting[0]) - 1]["name"].toString()} ${starting[1]} - ${surahs[int.parse(ending[0]) - 1]["name"].toString()} ${ending[1]}",
fullWidth: true,
Expand Down
Loading