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

dashboard features... #2

Open
wants to merge 9 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
1 change: 1 addition & 0 deletions lib/data/models/archive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ class Archive {
class ArchiveMenu {
String name = '';
int price = 0;
int quantity = 0;
}
64 changes: 64 additions & 0 deletions lib/data/models/archive.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 3 additions & 14 deletions lib/data/services/archive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,13 @@ class ArchiveService {
}
}

Future<List<Archive>> getAllArchiveOnDate(DateTime date) async {
Future<IsarCollection<Archive>?> getArchiveCollection() async {
try {
final Isar isar = await db;
final IsarCollection<Archive> archives = isar.archives;

final DateTime dateLowerBound = DateTime(
date.year,
date.month,
date.day,
); // it automatically set to 00:00:00 o'clock
final DateTime dateUpperBound =
DateTime(date.year, date.month, date.day, 23, 59, 59);
return archives
.filter()
.paidAtBetween(dateLowerBound, dateUpperBound)
.findAll();
return archives;
} catch (e) {
return <Archive>[];
return null;
}
}

Expand Down
52 changes: 52 additions & 0 deletions lib/logic/analytic/analytic.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import 'package:chrysant/data/models/archive.dart';
import 'package:chrysant/data/services/archive.dart';
import 'package:chrysant/logic/analytic/archiveFilterer.dart';
import 'package:isar/isar.dart';

Future<int> countArchivesOnDate(DateTime date) async {
final List<Archive> archiveList = await getAllArchiveOnDate(date);
return archiveList.length;
}

Future<List<(String, int)>> getMenuPopularity(
DateTime rangeMin,
DateTime rangeMax,
) async {
try {
final IsarCollection<Archive>? archives =
await ArchiveService().getArchiveCollection();

if (archives == null) {
throw Exception('Archive collection is not found');
}

final List<Archive> archiveOnDateBetween =
await getAllArchiveBetweenDate(rangeMin, rangeMax);
List<(String, int)> counted = <(String, int)>[];

// TODO(bootloopmaster636): need to refactor this sometime because it's inefficient
for (final Archive archive in archiveOnDateBetween) {
for (final ArchiveMenu menu in archive.items) {
// if this counter list doesn't have this menu, add it to the list
// else just increment its counter
if (counted
.where(((String, int) element) => element.$1 == menu.name)
.isEmpty) {
counted.add((menu.name, menu.quantity));
} else {
counted = counted.map(((String, int) element) {
if (element.$1 == menu.name) {
return (element.$1, element.$2 + menu.quantity);
} else {
return element;
}
}).toList();
}
}
}

return counted;
} catch (e) {
throw Exception('Something went wrong in counting menu popularity');
}
}
66 changes: 66 additions & 0 deletions lib/logic/analytic/archiveFilterer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import 'package:chrysant/data/models/archive.dart';
import 'package:chrysant/data/services/archive.dart';
import 'package:isar/isar.dart';

Future<List<Archive>> getAllArchiveOnDate(DateTime date) async {
try {
final IsarCollection<Archive>? archives =
await ArchiveService().getArchiveCollection();

if (archives == null) {
throw Exception('Archive collection is not found');
}

final DateTime dateLowerBound = DateTime(
date.year,
date.month,
date.day,
); // it automatically set to 00:00:00 o'clock
final DateTime dateUpperBound =
DateTime(date.year, date.month, date.day, 23, 59, 59);
return archives
.filter()
.paidAtBetween(dateLowerBound, dateUpperBound)
.findAll();
} catch (e) {
return <Archive>[];
}
}

Future<List<Archive>> getAllArchiveBetweenDate(
DateTime rangeMin, DateTime rangeMax) async {
try {
final IsarCollection<Archive>? archives =
await ArchiveService().getArchiveCollection();

if (archives == null) {
throw Exception('Archive collection is not found');
}

return archives.filter().paidAtBetween(rangeMin, rangeMax).findAll();
} catch (e) {
return <Archive>[];
}
}

Future<List<Archive>> getAllArchiveContainingMenu(String menuName) async {
try {
final IsarCollection<Archive>? archives =
await ArchiveService().getArchiveCollection();

if (archives == null) {
throw Exception('Archive collection is not found');
}

final List<Archive> archiveThatContainsThisMenu = await archives
.filter()
.itemsElement(
(QueryBuilder<ArchiveMenu, ArchiveMenu, QFilterCondition> query) =>
query.nameContains(menuName),
)
.findAll();
return archiveThatContainsThisMenu;
} catch (e) {
return <Archive>[];
}
}
8 changes: 0 additions & 8 deletions lib/logic/manage/archive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,3 @@ class ArchiveManager extends _$ArchiveManager {
});
}
}

class ArchiveUtils {
static Future<int> countArchivesOnDate(DateTime date) async {
final ArchiveService service = ArchiveService();
final List<Archive> archiveList = await service.getAllArchiveOnDate(date);
return archiveList.length;
}
}
14 changes: 0 additions & 14 deletions lib/pages/analytic/analytics.dart

This file was deleted.

76 changes: 76 additions & 0 deletions lib/pages/dashboard/analyticWidgets/menuPopularity.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import 'package:chrysant/logic/analytic/analytic.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

class MenuPopularityWidget extends HookConsumerWidget {
const MenuPopularityWidget({super.key});

@override
Widget build(BuildContext context, WidgetRef ref) {
final Future<List<(String, int)>> count = getMenuPopularity(
DateTime.now().subtract(const Duration(days: 90)),
DateTime.now(),
);

return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
const Text(
'Menu popularity',
style: TextStyle(fontSize: 16),
),
const Gap(16),
FutureBuilder(
future: count,
builder: (
BuildContext context,
AsyncSnapshot<List<(String, int)>> snapshot,
) {
if (snapshot.hasData) {
return Expanded(
child: BarChart(
BarChartData(
alignment: BarChartAlignment.spaceAround,
barGroups: snapshot.data?.map(((String, int) element) {
return BarChartGroupData(
x: snapshot.data!.indexOf((element.$1, element.$2)),
barRods: <BarChartRodData>[
BarChartRodData(
toY: element.$2.toDouble(),
width: 20,
borderRadius: BorderRadius.circular(2),
),
],
);
}).toList(),
minY: 0,
titlesData: FlTitlesData(
bottomTitles: AxisTitles(
sideTitles: SideTitles(
showTitles: true,
reservedSize: 30,
getTitlesWidget: (value, meta) {
return Text(
snapshot.data?[value.toInt()].$1 ?? '',
);
},
),
),
topTitles: const AxisTitles(
sideTitles: SideTitles(showTitles: false),
),
),
),
),
);
} else {
return const CircularProgressIndicator();
}
},
),
],
);
}
}
5 changes: 2 additions & 3 deletions lib/pages/dashboard/analyticWidgets/todaysOrder.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:chrysant/logic/manage/archive.dart';
import 'package:chrysant/logic/analytic/analytic.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:gap/gap.dart';
Expand All @@ -8,8 +8,7 @@ class TodaysOrderCounter extends ConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) {
final Future<int> noOfOrders =
ArchiveUtils.countArchivesOnDate(DateTime.now());
final Future<int> noOfOrders = countArchivesOnDate(DateTime.now());
return Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
Expand Down
Loading