Skip to content

Commit

Permalink
refactoring recurring transactions: models and main function
Browse files Browse the repository at this point in the history
  • Loading branch information
mikev-cw committed Apr 23, 2024
1 parent ff87484 commit 08d06e1
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 25 deletions.
18 changes: 15 additions & 3 deletions lib/database/sossoldi_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,12 @@ class SossoldiDatabase {
await database.execute('''
CREATE TABLE `$recurringTransactionTable`(
`${RecurringTransactionFields.id}` $integerPrimaryKeyAutoincrement,
`${RecurringTransactionFields.from}` $textNotNull,
`${RecurringTransactionFields.to}` $text,
`${RecurringTransactionFields.fromDate}` $textNotNull,
`${RecurringTransactionFields.toDate}` $text,
`${RecurringTransactionFields.amount}` $realNotNull,
`${RecurringTransactionFields.note}` $textNotNull,
`${RecurringTransactionFields.recurrency}` $textNotNull,
`${RecurringTransactionFields.idCategory}` $integerNotNull,
`${RecurringTransactionFields.lastInsertion}` $text,
`${RecurringTransactionFields.createdAt}` $textNotNull,
`${RecurringTransactionFields.updatedAt}` $textNotNull
Expand Down Expand Up @@ -151,7 +154,8 @@ class SossoldiDatabase {
(12, "Furniture", "home", 2, '', 11, '${DateTime.now()}', '${DateTime.now()}'),
(13, "Shopping", "shopping_cart", 3, '', null, '${DateTime.now()}', '${DateTime.now()}'),
(14, "Leisure", "subscriptions", 4, '', null, '${DateTime.now()}', '${DateTime.now()}'),
(15, "Salary", "work", 5, '', null, '${DateTime.now()}', '${DateTime.now()}');
(15, "Salary", "work", 5, '', null, '${DateTime.now()}', '${DateTime.now()}'),
(16, "Transports", "directions_car", 6, '', null, '${DateTime.now()}', '${DateTime.now()}');
''');

// Add currencies
Expand All @@ -170,6 +174,14 @@ class SossoldiDatabase {
(11, "Home", 123.45, 0, '${DateTime.now()}', '${DateTime.now()}');
''');

// Add fake recurring transactions
await _database?.execute('''
INSERT INTO recurringTransaction(fromDate, toDate, amount, note, recurrency, idCategory, createdAt, updatedAt) VALUES
("2024-02-23", null, 10.99, "Spotify", "MONTHLY", 14, '${DateTime.now()}', '${DateTime.now()}'),
("2023-12-13", null, 4.97, "ETF Consultant Parcel", "DAILY", 14, '${DateTime.now()}', '${DateTime.now()}'),
("2023-02-11", "2028-02-11", 1193.40, "Car Loan", "QUARTERLY", 16, '${DateTime.now()}', '${DateTime.now()}');
''');

// Add fake transactions
// First initialize some config stuff
final rnd = Random();
Expand Down
25 changes: 24 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:intl/date_symbol_data_local.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:workmanager/workmanager.dart';
import 'package:sossoldi/utils/worker_manager.dart';

import 'model/recurring_transaction.dart';
import 'utils/worker_manager.dart';
import 'pages/notifications/notifications_service.dart';
import 'providers/theme_provider.dart';
import 'routes.dart';
Expand All @@ -16,20 +17,42 @@ bool? _isFirstLogin = true;

void main() async {
WidgetsFlutterBinding.ensureInitialized();

if(Platform.isAndroid){
requestNotificationPermissions();
initializeNotifications();
Workmanager().initialize(callbackDispatcher);
}

SharedPreferences preferences = await SharedPreferences.getInstance();

bool? getPref = preferences.getBool('is_first_login');
getPref == null ? await preferences.setBool('is_first_login', false) : null;
_isFirstLogin = getPref;

// perform recurring transactions checks
DateTime? lastCheckGetPref = preferences.getString('last_recurring_transactions_check') != null ? DateTime.parse(preferences.getString('last_recurring_transactions_check')!) : null;
DateTime? lastRecurringTransactionsCheck = lastCheckGetPref;

if(lastRecurringTransactionsCheck == null || DateTime.now().difference(lastRecurringTransactionsCheck).inDays >= 1){
checkRecurringTransactions();
// update last recurring transactions runtime
await preferences.setString('last_recurring_transactions_check', DateTime.now().toIso8601String());
}

initializeDateFormatting('it_IT', null).then((_) => runApp(const ProviderScope(child: Launcher())));
}

// put here the function to check recurring transactions
void checkRecurringTransactions() async {
// get all recurring transactions
final accounts = await RecurringTransactionMethods().selectAll();
// check if the recurring transaction is due
// if it is due, insert a new transaction
// update the last insertion date
// update the recurring transaction
}

class Launcher extends ConsumerWidget {
const Launcher({super.key});

Expand Down
67 changes: 46 additions & 21 deletions lib/model/recurring_transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,77 +5,102 @@ const String recurringTransactionTable = 'recurringTransaction';

class RecurringTransactionFields extends BaseEntityFields {
static String id = BaseEntityFields.getId;
static String from = 'from';
static String to = 'to';
static String fromDate = 'fromDate';
static String toDate = 'toDate';
static String amount = 'amount';
static String note = 'note';
static String recurrency = 'recurrency';
static String idCategory = 'idCategory';
static String lastInsertion = 'lastInsertion';
static String createdAt = BaseEntityFields.getCreatedAt;
static String updatedAt = BaseEntityFields.getUpdatedAt;

static final List<String> allFields = [
BaseEntityFields.id,
from,
to,
fromDate,
toDate,
amount,
note,
recurrency,
idCategory,
lastInsertion,
BaseEntityFields.createdAt,
BaseEntityFields.updatedAt
];
}

class RecurringTransaction extends BaseEntity {
final DateTime from;
final DateTime to;
final DateTime fromDate;
final DateTime? toDate;
final num amount;
final String note;
final String recurrency;
final num idCategory;
final DateTime? lastInsertion;

const RecurringTransaction(
{int? id,
required this.from,
required this.to,
required this.fromDate,
this.toDate,
required this.amount,
required this.note,
required this.recurrency,
required this.idCategory,
this.lastInsertion,
DateTime? createdAt,
DateTime? updatedAt})
: super(id: id, createdAt: createdAt, updatedAt: updatedAt);

RecurringTransaction copy(
{int? id,
DateTime? from,
DateTime? to,
DateTime? fromDate,
DateTime? toDate,
num? amount,
String? note,
String? recurrency,
num? idCategory,
DateTime? lastInsertion,
DateTime? createdAt,
DateTime? updatedAt}) =>
RecurringTransaction(
id: id ?? this.id,
from: from ?? this.from,
to: to ?? this.to,
fromDate: fromDate ?? this.fromDate,
toDate: toDate ?? this.toDate,
amount: amount ?? this.amount,
note: note ?? this.note,
recurrency: recurrency ?? this.recurrency,
idCategory: idCategory ?? this.idCategory,
lastInsertion: lastInsertion ?? this.lastInsertion,
createdAt: createdAt ?? this.createdAt,
updatedAt: updatedAt ?? this.updatedAt);

static RecurringTransaction fromJson(Map<String, Object?> json) =>
RecurringTransaction(
id: json[BaseEntityFields.id] as int?,
from: DateTime.parse(
json[RecurringTransactionFields.from] as String),
to: DateTime.parse(
json[RecurringTransactionFields.to] as String),
fromDate: DateTime.parse(
json[RecurringTransactionFields.fromDate] as String),
toDate: json[RecurringTransactionFields.toDate] != null
? DateTime.parse(json[RecurringTransactionFields.toDate] as String)
: null,
amount: json[RecurringTransactionFields.amount] as num,
lastInsertion:
json[RecurringTransactionFields.lastInsertion]
as DateTime,
note: json[RecurringTransactionFields.note] as String,
recurrency: json[RecurringTransactionFields.recurrency] as String,
idCategory: json[RecurringTransactionFields.idCategory] as num,
lastInsertion: json[RecurringTransactionFields.lastInsertion] != null
? DateTime.parse(json[RecurringTransactionFields.lastInsertion] as String)
: null,
createdAt: DateTime.parse(json[BaseEntityFields.createdAt] as String),
updatedAt:
DateTime.parse(json[BaseEntityFields.updatedAt] as String));

Map<String, Object?> toJson() => {
BaseEntityFields.id: id,
RecurringTransactionFields.from: from.toIso8601String(),
RecurringTransactionFields.to: to.toIso8601String(),
RecurringTransactionFields.fromDate: fromDate.toIso8601String(),
RecurringTransactionFields.toDate: toDate?.toIso8601String(),
RecurringTransactionFields.amount: amount,
RecurringTransactionFields.note: note,
RecurringTransactionFields.recurrency: recurrency,
RecurringTransactionFields.idCategory: idCategory,
RecurringTransactionFields.lastInsertion: lastInsertion?.toIso8601String(),
BaseEntityFields.createdAt: createdAt?.toIso8601String(),
BaseEntityFields.updatedAt: updatedAt?.toIso8601String(),
Expand Down

0 comments on commit 08d06e1

Please sign in to comment.