Skip to content

Commit

Permalink
Merge pull request #16 from hkamran80/feature/quran-statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
hkamran80 authored Mar 26, 2023
2 parents f2ba86c + ec895f4 commit 3a506d0
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 67 deletions.
5 changes: 5 additions & 0 deletions lib/extensions/int.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Source: https://stackoverflow.com/a/65314708/7313822
extension RangeExtension on int {
List<int> upTo(int maxInclusive) =>
[for (int i = this; i <= maxInclusive; i++) i];
}
152 changes: 92 additions & 60 deletions lib/quran.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import "package:ramadan_taskminder/extensions/int.dart";

const totalAyahCount = 6236;
const totalJuzCount = 30;

const surahs = [
{"name": "Al-Fatihah", "ayahs": 7},
{"name": "Al-Baqarah", "ayahs": 286},
Expand Down Expand Up @@ -117,123 +122,150 @@ const surahs = [

const juz = [
{
"start": {"surah": 1, "verse": 1},
"end": {"surah": 2, "verse": 141}
"start": {"surah": 1, "ayah": 1},
"end": {"surah": 2, "ayah": 141}
},
{
"start": {"surah": 2, "verse": 142},
"end": {"surah": 2, "verse": 252}
"start": {"surah": 2, "ayah": 142},
"end": {"surah": 2, "ayah": 252}
},
{
"start": {"surah": 2, "verse": 253},
"end": {"surah": 3, "verse": 92}
"start": {"surah": 2, "ayah": 253},
"end": {"surah": 3, "ayah": 92}
},
{
"start": {"surah": 3, "verse": 93},
"end": {"surah": 4, "verse": 23}
"start": {"surah": 3, "ayah": 93},
"end": {"surah": 4, "ayah": 23}
},
{
"start": {"surah": 4, "verse": 24},
"end": {"surah": 4, "verse": 147}
"start": {"surah": 4, "ayah": 24},
"end": {"surah": 4, "ayah": 147}
},
{
"start": {"surah": 4, "verse": 148},
"end": {"surah": 5, "verse": 81}
"start": {"surah": 4, "ayah": 148},
"end": {"surah": 5, "ayah": 81}
},
{
"start": {"surah": 5, "verse": 82},
"end": {"surah": 6, "verse": 110}
"start": {"surah": 5, "ayah": 82},
"end": {"surah": 6, "ayah": 110}
},
{
"start": {"surah": 6, "verse": 111},
"end": {"surah": 7, "verse": 87}
"start": {"surah": 6, "ayah": 111},
"end": {"surah": 7, "ayah": 87}
},
{
"start": {"surah": 7, "verse": 88},
"end": {"surah": 8, "verse": 40}
"start": {"surah": 7, "ayah": 88},
"end": {"surah": 8, "ayah": 40}
},
{
"start": {"surah": 8, "verse": 41},
"end": {"surah": 9, "verse": 92}
"start": {"surah": 8, "ayah": 41},
"end": {"surah": 9, "ayah": 92}
},
{
"start": {"surah": 9, "verse": 93},
"end": {"surah": 11, "verse": 5}
"start": {"surah": 9, "ayah": 93},
"end": {"surah": 11, "ayah": 5}
},
{
"start": {"surah": 11, "verse": 6},
"end": {"surah": 12, "verse": 52}
"start": {"surah": 11, "ayah": 6},
"end": {"surah": 12, "ayah": 52}
},
{
"start": {"surah": 12, "verse": 53},
"end": {"surah": 14, "verse": 52}
"start": {"surah": 12, "ayah": 53},
"end": {"surah": 14, "ayah": 52}
},
{
"start": {"surah": 15, "verse": 1},
"end": {"surah": 16, "verse": 128}
"start": {"surah": 15, "ayah": 1},
"end": {"surah": 16, "ayah": 128}
},
{
"start": {"surah": 17, "verse": 1},
"end": {"surah": 18, "verse": 74}
"start": {"surah": 17, "ayah": 1},
"end": {"surah": 18, "ayah": 74}
},
{
"start": {"surah": 18, "verse": 75},
"end": {"surah": 20, "verse": 135}
"start": {"surah": 18, "ayah": 75},
"end": {"surah": 20, "ayah": 135}
},
{
"start": {"surah": 21, "verse": 1},
"end": {"surah": 22, "verse": 78}
"start": {"surah": 21, "ayah": 1},
"end": {"surah": 22, "ayah": 78}
},
{
"start": {"surah": 23, "verse": 1},
"end": {"surah": 25, "verse": 20}
"start": {"surah": 23, "ayah": 1},
"end": {"surah": 25, "ayah": 20}
},
{
"start": {"surah": 25, "verse": 21},
"end": {"surah": 27, "verse": 55}
"start": {"surah": 25, "ayah": 21},
"end": {"surah": 27, "ayah": 55}
},
{
"start": {"surah": 27, "verse": 56},
"end": {"surah": 29, "verse": 45}
"start": {"surah": 27, "ayah": 56},
"end": {"surah": 29, "ayah": 45}
},
{
"start": {"surah": 29, "verse": 46},
"end": {"surah": 33, "verse": 30}
"start": {"surah": 29, "ayah": 46},
"end": {"surah": 33, "ayah": 30}
},
{
"start": {"surah": 33, "verse": 31},
"end": {"surah": 36, "verse": 27}
"start": {"surah": 33, "ayah": 31},
"end": {"surah": 36, "ayah": 27}
},
{
"start": {"surah": 36, "verse": 28},
"end": {"surah": 39, "verse": 31}
"start": {"surah": 36, "ayah": 28},
"end": {"surah": 39, "ayah": 31}
},
{
"start": {"surah": 39, "verse": 32},
"end": {"surah": 41, "verse": 46}
"start": {"surah": 39, "ayah": 32},
"end": {"surah": 41, "ayah": 46}
},
{
"start": {"surah": 41, "verse": 47},
"end": {"surah": 45, "verse": 37}
"start": {"surah": 41, "ayah": 47},
"end": {"surah": 45, "ayah": 37}
},
{
"start": {"surah": 46, "verse": 1},
"end": {"surah": 51, "verse": 30}
"start": {"surah": 46, "ayah": 1},
"end": {"surah": 51, "ayah": 30}
},
{
"start": {"surah": 51, "verse": 31},
"end": {"surah": 57, "verse": 29}
"start": {"surah": 51, "ayah": 31},
"end": {"surah": 57, "ayah": 29}
},
{
"start": {"surah": 58, "verse": 1},
"end": {"surah": 66, "verse": 12}
"start": {"surah": 58, "ayah": 1},
"end": {"surah": 66, "ayah": 12}
},
{
"start": {"surah": 67, "verse": 1},
"end": {"surah": 77, "verse": 50}
"start": {"surah": 67, "ayah": 1},
"end": {"surah": 77, "ayah": 50}
},
{
"start": {"surah": 78, "verse": 1},
"end": {"surah": 114, "verse": 6}
"start": {"surah": 78, "ayah": 1},
"end": {"surah": 114, "ayah": 6}
}
];

final List<List<int>> juzSurahs = juz
.map((juzEntry) =>
juzEntry["start"]!["surah"]!.upTo(juzEntry["end"]!["surah"]!))
.toList();

int calculateAyahsRead(List history) {
int ayahs = 0;

Iterable entries = history.map((entry) => entry[1]);
for (var entry in entries) {
List<int> start = entry[0].toString().split("-").map(int.parse).toList();
List<int> end = entry[1].toString().split("-").map(int.parse).toList();

if (start[0] - 1 == end[0] - 1) {
ayahs += end[1] - start[1];
} else {
ayahs += surahs
.sublist(start[0] - 1, end[0] - 1)
.map((surah) => int.parse(surah["ayahs"].toString()))
.reduce((p, c) => p + c) +
end[1];
}
}

return ayahs;
}
30 changes: 28 additions & 2 deletions lib/screens/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:intl/intl.dart';
import 'package:ramadan_taskminder/constants.dart';
import 'package:ramadan_taskminder/extensions/date.dart';
import 'package:ramadan_taskminder/prayers.dart';
import 'package:ramadan_taskminder/quran.dart';
import 'package:ramadan_taskminder/theme.dart';
import 'package:ramadan_taskminder/tasks.dart';
import 'package:ramadan_taskminder/widgets/page_footer.dart';
Expand Down Expand Up @@ -111,6 +112,13 @@ class _HomeScreenState extends State<HomeScreen> {
),
);

String currentDate = DateTime.now().toIso8601String().split("T")[0];
List history = quran.get("history");
List todaysEntries =
history.where((entry) => entry[0] == currentDate).toList();
int ayahsRead = calculateAyahsRead(history);
String percentageRead = (ayahsRead / totalAyahCount).toStringAsFixed(1);

Iterable completedPrayers = prayers.entries
.where(
(prayer) => prayer.value == true,
Expand Down Expand Up @@ -215,12 +223,30 @@ class _HomeScreenState extends State<HomeScreen> {
children: [
SectionHeader(
title: "Qur'an",
subtitle:
"${quran.get('history').length} entries",
subtitle: todaysEntries.isEmpty
? "Nothing read today"
: "${calculateAyahsRead(todaysEntries)} ayahs read today",
buttonText: "Track",
onClick: (() =>
GoRouter.of(context).go("/quran")),
),
const SizedBox(height: 15),
history.isNotEmpty
? Wrap(
spacing: 10,
runSpacing: 10,
children: [
Statistic(
statistic: "$percentageRead% read",
),
Statistic(
statistic: "$ayahsRead ayahs",
),
],
)
: const Text(
"You haven't logged any entries yet.",
),
],
),
const SizedBox(
Expand Down
49 changes: 44 additions & 5 deletions lib/screens/quran.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,29 @@ class QuranScreen extends StatefulWidget {

class _QuranScreenState extends State<QuranScreen> {
List history = [];

Box quran = Hive.box("quran");

// Logging
int startingSurah = -1;
int startingAyah = 0;

int endingSurah = -1;
int endingAyah = 0;

bool showHelp = false;
// Statistics
int ayahsRead = 0;
String percentageRead = "0";

// Helpers
bool showHelp = false;
int deletingHistoryEntry = -1;

@override
void initState() {
super.initState();
initializeHistory();
setStartingEntry();
calculateStatistics();
}

void initializeHistory() {
Expand Down Expand Up @@ -121,7 +126,7 @@ class _QuranScreenState extends State<QuranScreen> {
history.removeAt(deletingHistoryEntry);
setStartingEntry();
});

quran.put("history", history);
deletingHistoryEntry = -1;

Expand All @@ -135,8 +140,17 @@ class _QuranScreenState extends State<QuranScreen> {
}
}

void calculateStatistics() {
ayahsRead = calculateAyahsRead(history);
percentageRead = (ayahsRead / totalAyahCount).toStringAsFixed(1);
}

@override
Widget build(BuildContext context) {
String currentDate = DateTime.now().toIso8601String().split("T")[0];
List todaysEntries =
history.where((entry) => entry[0] == currentDate).toList();

return Material(
color: getBackgroundColor(context),
child: Stack(
Expand All @@ -155,10 +169,34 @@ class _QuranScreenState extends State<QuranScreen> {
[
PageHeader(
header: "Qur'an",
title: "${history.length} entries",
title: todaysEntries.isEmpty
? "Nothing read today"
: "${calculateAyahsRead(todaysEntries)} ayahs read today",
),
const SizedBox(height: 15),
Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SectionHeader(
title: "Overview",
),
const SizedBox(height: 15),
Wrap(
runSpacing: 10,
children: [
WideCard(
content: "$percentageRead% read of Qur'an",
),
WideCard(
content: "$ayahsRead ayahs read",
),
],
),
],
),
const SizedBox(
height: 15,
height: 25,
),
Column(
mainAxisSize: MainAxisSize.min,
Expand Down Expand Up @@ -236,6 +274,7 @@ class _QuranScreenState extends State<QuranScreen> {
endingAyah = 0;

setStartingEntry();
calculateStatistics();
},
),
)
Expand Down

0 comments on commit 3a506d0

Please sign in to comment.