Compare commits
4 Commits
ab9b3e0bf9
...
8e6b574023
Author | SHA1 | Date | |
---|---|---|---|
8e6b574023 | |||
fcf4fca33b | |||
0aa8fdf271 | |||
3bf3dd3190 |
37
lib/db.dart
37
lib/db.dart
@ -24,21 +24,45 @@ class DatabaseHelper {
|
|||||||
String path = join(documentsDirectory.path, "com_hyperling_expense.db");
|
String path = join(documentsDirectory.path, "com_hyperling_expense.db");
|
||||||
return await openDatabase(
|
return await openDatabase(
|
||||||
path,
|
path,
|
||||||
version: 1,
|
version: 2,
|
||||||
onCreate: _onCreate,
|
onCreate: _onCreate,
|
||||||
|
onUpgrade: _onUpgrade,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future _onCreate(Database db, int version) async {
|
Future _onCreate(Database db, int version) async {
|
||||||
await db.execute("""
|
await db.execute("""
|
||||||
CREATE TABLE expense
|
CREATE TABLE expense
|
||||||
|
( id INTEGER PRIMARY KEY
|
||||||
|
, name TEXT NOT NULL UNIQUE
|
||||||
|
, cost DOUBLE NOT NULL
|
||||||
|
, frequency TEXT NOT NULL
|
||||||
|
, description TEXT
|
||||||
|
)
|
||||||
|
""");
|
||||||
|
}
|
||||||
|
|
||||||
|
Future _onUpgrade(Database db, int previousVersion, int newVersion) async {
|
||||||
|
// Added in DB version 2.
|
||||||
|
if (previousVersion < 2 && newVersion >= 2) {
|
||||||
|
await db.execute("""
|
||||||
|
CREATE TABLE income
|
||||||
( id INTEGER PRIMARY KEY
|
( id INTEGER PRIMARY KEY
|
||||||
, name TEXT NOT NULL UNIQUE
|
, name TEXT NOT NULL UNIQUE
|
||||||
, cost DOUBLE NOT NULL
|
, revenue DOUBLE NOT NULL
|
||||||
, frequency TEXT NOT NULL
|
, frequency TEXT NOT NULL
|
||||||
, description TEXT
|
, description TEXT
|
||||||
)
|
)
|
||||||
""");
|
""");
|
||||||
|
await db.execute("""
|
||||||
|
CREATE TABLE asset
|
||||||
|
( id INTEGER PRIMARY KEY
|
||||||
|
, name TEXT NOT NULL UNIQUE
|
||||||
|
, amount DOUBLE NOT NULL
|
||||||
|
, description TEXT
|
||||||
|
)
|
||||||
|
""");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Expense Section
|
/// Expense Section
|
||||||
@ -81,8 +105,11 @@ class DatabaseHelper {
|
|||||||
|
|
||||||
Future<bool> checkExpenseNameExists(String name) async {
|
Future<bool> checkExpenseNameExists(String name) async {
|
||||||
Database db = await instance.db;
|
Database db = await instance.db;
|
||||||
var expenses = await db.query("expense",
|
var expenses = await db.query(
|
||||||
where: "name = ?", whereArgs: [name],);
|
"expense",
|
||||||
|
where: "name = ?",
|
||||||
|
whereArgs: [name],
|
||||||
|
);
|
||||||
return expenses.isNotEmpty;
|
return expenses.isNotEmpty;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
19
lib/models/asset.dart
Normal file
19
lib/models/asset.dart
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import '/models/tracked_type.dart';
|
||||||
|
|
||||||
|
class Asset extends TrackedType {
|
||||||
|
static String amountText = "Amount";
|
||||||
|
|
||||||
|
Asset({
|
||||||
|
super.id,
|
||||||
|
required super.name,
|
||||||
|
required super.amount,
|
||||||
|
required super.description,
|
||||||
|
});
|
||||||
|
|
||||||
|
factory Asset.fromMap(Map<String, dynamic> json) => Asset(
|
||||||
|
id: json['id'],
|
||||||
|
name: json['name'],
|
||||||
|
amount: json['amount'],
|
||||||
|
description: json['description'],
|
||||||
|
);
|
||||||
|
}
|
@ -21,4 +21,15 @@ class Expense extends RecurringTrackedType {
|
|||||||
.first,
|
.first,
|
||||||
description: json['description'],
|
description: json['description'],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Map<String, dynamic> toMap() {
|
||||||
|
return {
|
||||||
|
'id': id,
|
||||||
|
'name': name,
|
||||||
|
'cost': amount,
|
||||||
|
'frequency': frequency.title,
|
||||||
|
'description': description,
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
35
lib/models/income.dart
Normal file
35
lib/models/income.dart
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import '/models/recurring_tracked_type.dart';
|
||||||
|
import '/models/frequency.dart';
|
||||||
|
|
||||||
|
class Income extends RecurringTrackedType {
|
||||||
|
static String amountText = "Revenue";
|
||||||
|
|
||||||
|
Income({
|
||||||
|
super.id,
|
||||||
|
required super.name,
|
||||||
|
required super.amount,
|
||||||
|
required super.frequency,
|
||||||
|
required super.description,
|
||||||
|
});
|
||||||
|
|
||||||
|
factory Income.fromMap(Map<String, dynamic> json) => Income(
|
||||||
|
id: json['id'],
|
||||||
|
name: json['name'],
|
||||||
|
amount: json['revenue'],
|
||||||
|
frequency: Frequency.values
|
||||||
|
.where((freq) => freq.title == json['frequency'])
|
||||||
|
.first,
|
||||||
|
description: json['description'],
|
||||||
|
);
|
||||||
|
|
||||||
|
@override
|
||||||
|
Map<String, dynamic> toMap() {
|
||||||
|
return {
|
||||||
|
'id': id,
|
||||||
|
'name': name,
|
||||||
|
'revenue': amount,
|
||||||
|
'frequency': frequency.title,
|
||||||
|
'description': description,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -289,15 +289,15 @@ class _ExpenseInputDialogState extends State<ExpenseInputDialog> {
|
|||||||
if (value == null || value.isEmpty) {
|
if (value == null || value.isEmpty) {
|
||||||
return "${Expense.amountText} must be provided.";
|
return "${Expense.amountText} must be provided.";
|
||||||
}
|
}
|
||||||
|
if (double.tryParse(value) == null) {
|
||||||
|
return "${Expense.amountText} must be a valid number.";
|
||||||
|
}
|
||||||
if (double.parse(value) < 0) {
|
if (double.parse(value) < 0) {
|
||||||
return "Please use the Income page rather than having negative expenses.";
|
return "Please use the Income page rather than having negative expenses.";
|
||||||
}
|
}
|
||||||
if (double.parse(value) < 0.01) {
|
if (double.parse(value) < 0.01) {
|
||||||
return "${Expense.amountText} must be one hundreth (0.01) or higher.";
|
return "${Expense.amountText} must be one hundreth (0.01) or higher.";
|
||||||
}
|
}
|
||||||
if (double.tryParse(value) == null) {
|
|
||||||
return "${Expense.amountText} must be a valid number.";
|
|
||||||
}
|
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
onSaved: (value) {
|
onSaved: (value) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user