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

Categories V2 #39

Merged
merged 2 commits into from
Sep 5, 2024
Merged
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
44 changes: 34 additions & 10 deletions lib/extensions.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';

import 'screens/user/user_controller.dart';

extension DateExtension on DateTime {
DateTime copyTime(TimeOfDay from) {
return DateTime(year, month, day, from.hour, from.minute);
}

bool isSameDate(DateTime other) {
return isSameMonth(other) &&
day == other.day;
return isSameMonth(other) && day == other.day;
}

bool isSameMonth(DateTime other) {
return year == other.year &&
month == other.month;
return year == other.year && month == other.month;
}

String formatDate() {
Expand All @@ -38,8 +39,8 @@ extension VsString on String {
String get toPascalCase {
return replaceAllMapped(
RegExp(r'(\w+)'),
(match) =>
"${match.group(0)![0].toUpperCase()}${match.group(0)!.substring(1)}");
(match) =>
"${match.group(0)![0].toUpperCase()}${match.group(0)!.substring(1)}");
}

String get toKebabCase {
Expand All @@ -50,9 +51,32 @@ extension VsString on String {
String get toTitleCase {
return replaceAllMapped(
RegExp(r'(\w+)'),
(match) =>
"${match.group(0)![0].toUpperCase()}${match.group(0)!
.substring(1)
.toLowerCase()} ");
(match) =>
"${match.group(0)![0].toUpperCase()}${match.group(0)!.substring(1).toLowerCase()} ");
}
}

extension VsDouble on double {
String get n {
final controller = Get.find<UserController>();
final comma = controller.thousandSep.value;
final decimal = controller.decimalSep.value;
var f = NumberFormat.simpleCurrency(locale: 'en-us');
String amt = f.format(this);
amt = amt.replaceAll('\$', '');
if (decimal == 1 && comma == 0) {
amt = amt.replaceAll('.', '@');
amt = amt.replaceAll(',', '.');
amt = amt.replaceAll('@', ',');
} else if (comma == 0 && decimal == 0) {
amt = amt.replaceAll(',', '.');
} else if (comma == 1 && decimal == 1) {
amt = amt.replaceAll('.', ',');
}
return controller.currency.value + amt;
}

String get s {
return n.replaceFirst('-', '');
}
}
16 changes: 13 additions & 3 deletions lib/screens/dashboard/dashboard_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class DashboardController extends GetxController {
DashboardController(this.currentDate);
RxList<Sector> sectors = RxList.empty();
DbController dbController = Get.find();
double total = 0;

@override
void onInit() {
Expand All @@ -18,20 +19,29 @@ class DashboardController extends GetxController {

Future<void> fetchSectors() async {
var transList = await dbController.db.rawQuery(
'''SELECT SUM(${Const.trans}.amount) AS total , count(${Const.categories}.category_name) AS share , ${Const.categories}.category_name , ${Const.categories}.color from ${Const.trans} LEFT JOIN ${Const.categories}
'''SELECT SUM(${Const.trans}.amount) AS total , count(${Const.categories}.category_name) AS share , ${Const.categories}.category_name ,
${Const.categories}.color, ${Const.categories}.icon from ${Const.trans}
LEFT JOIN ${Const.categories}
on ${Const.trans}.category_id = ${Const.categories}.id
WHERE ${Const.trans}.created_at BETWEEN ${Utils.getFirstDate(currentDate)} AND ${Utils.getLastDate(currentDate)}
GROUP BY ${Const.categories}.category_name
ORDER BY share DESC
''',
);
total = 0;
for (int i = 0; i < transList.length; i++) {
if (double.parse(transList[i]['total'].toString()) < 0 &&
transList[i]['category_name'] != null) {
final transaction = transList[i];
if (double.parse(transaction['total'].toString()) < 0 &&
transaction['category_name'] != null) {
Sector s = Sector.fromJson(transList[i]);
total += s.amount;
sectors.add(s);
}
}
update();
}

List<Sector> getFilteredSectors() {
return sectors.where((sector) => sector.include).toList();
}
}
36 changes: 24 additions & 12 deletions lib/screens/dashboard/dashboard_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,34 @@ import 'package:flutter/material.dart';
class Sector {
Sector(
{required this.color,
required this.total,
required this.amount,
required this.share,
required this.title});
required this.title,
required this.icon,
this.include = true});

Color color;
double total;
double amount;
int share;
String title;
String icon;
bool include;

factory Sector.fromJson(Map<String, dynamic> json) => Sector(
color: json['color'] != null
? Color(json['color'])
: Colors.primaries[Random().nextInt(Colors.primaries.length)],
title: json['category_name'].length > 10
? json['category_name'].substring(0, 9)
: json['category_name'],
share: json['share'],
total: json['total'],
);
color: json['color'] != null
? Color(json['color'])
: Colors.primaries[Random().nextInt(Colors.primaries.length)],
title: json['category_name'],
share: json['share'],
amount: json['total'],
icon: json['icon']);

void switchInclusion() {
include = !include;
}

String totalPercent(double total) {
if (!include) return "0";
return ((amount / total) * 100).toStringAsFixed(2);
}
}
73 changes: 32 additions & 41 deletions lib/screens/dashboard/dashboard_screen.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:vase/colors.dart';
import 'package:vase/extensions.dart';
import 'package:vase/screens/dashboard/dashboard_controller.dart';
import 'package:vase/screens/dashboard/dashboard_model.dart';
import 'package:vase/screens/dashboard/pie_chart.dart';
import 'package:vase/widgets/category_icon.dart';
import 'package:vase/widgets/focused_layout.dart';
import 'package:vase/widgets/wrapper.dart';

Expand Down Expand Up @@ -32,48 +36,35 @@ class DashboardScreen extends StatelessWidget {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
PieChartWidget(controller.sectors),
const Padding(
padding: EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Title',
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 16),
),
Text(
'Expense',
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 16),
)
],
),
),
ListView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemBuilder: (context, index) {
return ListTile(
leading: Container(
width: 10,
height: 10,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: controller.sectors[index].color,
PieChartWidget(controller.getFilteredSectors()),
Card(
child: ListView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemBuilder: (context, index) {
Sector sector = controller.sectors[index];
return ListTile(
onTap: () {
sector.switchInclusion();
controller.update();
},
leading: CategoryIcon(
icon: sector.icon,
bgColor: sector.include
? sector.color
: AppColors.darkGreyColor,
),
title: Text(sector.title),
subtitle: Text(
"${sector.share} Items • ${sector.totalPercent(controller.total)}%"),
trailing: Text(
sector.amount.s,
style: const TextStyle(fontSize: 14),
),
),
title: Text(
controller.sectors[index].title
),
trailing: Text(
controller.sectors[index].total.toString(),
style: const TextStyle(fontSize: 14),
),
);
},
itemCount: controller.sectors.length,
);
},
itemCount: controller.sectors.length,
),
)
],
);
Expand Down
12 changes: 6 additions & 6 deletions lib/screens/dashboard/pie_chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ class PieChartWidget extends StatelessWidget {
return AspectRatio(
aspectRatio: 1.0,
child: PieChart(PieChartData(
sections: _chartSections(sectors),
centerSpaceRadius: 80.0,
)));
sections: _chartSections(sectors),
centerSpaceRadius: 40.0,
sectionsSpace: 1)));
}

List<PieChartSectionData> _chartSections(List<Sector> sectors) {
final List<PieChartSectionData> list = [];
for (var sector in sectors) {
const double radius = 20.0;
const double radius = 40.0;
final data = PieChartSectionData(
titlePositionPercentageOffset: 2.6,
titlePositionPercentageOffset: 2,
color: sector.color,
value: sector.total,
value: sector.amount,
radius: radius,
title: sector.title,
);
Expand Down
16 changes: 2 additions & 14 deletions lib/screens/transactions/date_list_item.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'package:flutter/material.dart';
import 'package:flutter_iconpicker/flutter_iconpicker.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:vase/screens/transactions/date_chip.dart';
import 'package:vase/screens/transactions/new_transaction.dart';
import 'package:vase/screens/transactions/trans_controller.dart';
import 'package:vase/text_styles.dart';
import 'package:vase/widgets/category_icon.dart';

import '../../controllers/db_controller.dart';
import '../categories/category_model.dart';
Expand Down Expand Up @@ -66,19 +66,7 @@ class DateListItem extends StatelessWidget {
});
}
},
leading: CircleAvatar(
child: SizedBox(
width: 40,
height: 20,
child: Icon(
deserializeIcon({
'pack': cat != null ? 'fontAwesomeIcons' : 'material',
'key': cat != null ? cat.icon : 'sync_alt_rounded'
}),
size: 20,
),
),
),
leading: CategoryIcon(icon: cat?.icon ?? 'ban'),
title: Text(
transaction.desc,
style: const TextStyle(fontWeight: FontWeight.bold),
Expand Down
50 changes: 15 additions & 35 deletions lib/screens/widgets/txn_text.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:vase/colors.dart';
import 'package:vase/screens/user/user_controller.dart';
import 'package:vase/extensions.dart';

class TxnText extends StatelessWidget {
const TxnText(
Expand All @@ -22,38 +21,19 @@ class TxnText extends StatelessWidget {

@override
Widget build(BuildContext context) {
return GetBuilder<UserController>(builder: (controller) {
final comma = controller.thousandSep.value;
final decimal = controller.decimalSep.value;
var f = NumberFormat.simpleCurrency(locale: 'en-us');
String amt = f.format(amount);
amt = amt.replaceAll('\$', '');
if (!showSign) {
amt = amt.replaceFirst("-", "");
}
if (decimal == 1 && comma == 0) {
amt = amt.replaceAll('.', '@');
amt = amt.replaceAll(',', '.');
amt = amt.replaceAll('@', ',');
} else if (comma == 0 && decimal == 0) {
amt = amt.replaceAll(',', '.');
} else if (comma == 1 && decimal == 1) {
amt = amt.replaceAll('.', ',');
}
return Text(
'${controller.currency.value}$amt',
maxLines: 1,
textAlign: textAlign,
style: TextStyle(
color: customColor ??
(showDynamicColor
? (amount.isNegative
? AppColors.errorColor
: AppColors.accentColor)
: null),
fontSize: 14,
fontWeight: FontWeight.w700),
);
});
return Obx(() => Text(
amount.s,
maxLines: 1,
textAlign: textAlign,
style: TextStyle(
color: customColor ??
(showDynamicColor
? (amount.isNegative
? AppColors.errorColor
: AppColors.accentColor)
: null),
fontSize: 14,
fontWeight: FontWeight.w700),
));
}
}
2 changes: 1 addition & 1 deletion lib/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class Utils {
}

static int getLastDate(DateTime currentDate) {
DateTime dateTime = DateTime(currentDate.year, currentDate.month + 1, 0);
DateTime dateTime = DateTime(currentDate.year, currentDate.month + 1, 1).subtract(const Duration(seconds: 1));
return dateTime.millisecondsSinceEpoch;
}

Expand Down
Loading