Skip to content

Commit

Permalink
Add pages to see, import and delete scrips
Browse files Browse the repository at this point in the history
  • Loading branch information
sudo-panda committed Sep 6, 2023
1 parent 711a852 commit 86c89d8
Show file tree
Hide file tree
Showing 17 changed files with 2,031 additions and 141 deletions.
485 changes: 409 additions & 76 deletions lib/helpers/database_actions.dart

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions lib/helpers/stock_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ class StockRepository {

static Future<String?> getName(String code, String exchange) async {
String? name = await QueryAPI.getName(exchange: exchange, code: code);

DatabaseActions.setScripName(exchange, name, code);

if (name != null)
DatabaseActions.setScripName(exchange, name, code);

return name?.toUpperCase();
}

Expand Down
82 changes: 82 additions & 0 deletions lib/models/database/scrip.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import 'package:folio/helpers/database_actions.dart';
import 'package:folio/services/database/database.dart';

class Scrip {
int stockID = -1;
late String name;
String? bseCode;
String? nseCode;
List<String> oldBSECodes = [];
List<String> oldNSECodes = [];

Scrip(this.name);

Scrip.fromDbTuple(Map<String, dynamic> tuple) {
stockID = tuple[Db.colRowID];
name = tuple[Db.colName];
bseCode = tuple[Db.colBSECode];
nseCode = tuple[Db.colNSECode];
oldBSECodes =
tuple[Db.colOldBSECodes]?.split(DatabaseActions.delimiter) ?? [];
oldNSECodes =
tuple[Db.colOldNSECodes]?.split(DatabaseActions.delimiter) ?? [];
oldBSECodes.remove("");
oldNSECodes.remove("");
}

Map<String, dynamic> toDbTuple() {
return {
Db.colName: name,
Db.colBSECode: bseCode,
Db.colNSECode: nseCode,
Db.colOldBSECodes: oldBSECodes.join(DatabaseActions.delimiter),
Db.colOldNSECodes: oldNSECodes.join(DatabaseActions.delimiter),
};
}

String? code(String exchange) {
switch (exchange) {
case "BSE":
return bseCode;
case "NSE":
return nseCode;
}
throw Exception("Wrong exchange passed to code() of ${this.toString()}");
}

List<String> oldCodes(String exchange) {
switch (exchange) {
case "BSE":
return oldBSECodes;
case "NSE":
return oldNSECodes;
}
throw Exception(
"Wrong exchange passed to oldCodes() of ${this.toString()}");
}

void setCode(String exchange, String? code) {
switch (exchange) {
case "BSE":
bseCode = code;
return;
case "NSE":
nseCode = code;
return;
}
throw Exception("Wrong exchange passed to setCode() of ${this.toString()}");
}

void setOldCodes(String exchange, List<String> oldCodes) {
switch (exchange) {
case "BSE":
oldBSECodes = oldCodes;
return;
case "NSE":
oldNSECodes = oldCodes;
return;
}
throw Exception(
"Wrong exchange passed to setOldCodes() of ${this.toString()}");
}
}
1 change: 1 addition & 0 deletions lib/models/trade/parsed_file_logs.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class FileLog {
DateTime? date;
int? stockID;
String? name;
String? bseCode;
String? nseCode;
Expand Down
8 changes: 6 additions & 2 deletions lib/services/database/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Db {
static String colCode = 'code';
static String colBSECode = 'bse_code';
static String colNSECode = 'nse_code';
static String colOldBSECodes = 'old_bse_codes';
static String colOldNSECodes = 'old_nse_codes';
static String colExch = 'exchange';
static String colName = 'name';
static String colKey = 'key';
Expand Down Expand Up @@ -80,7 +82,9 @@ class Db {
'$colRowID INTEGER PRIMARY KEY, '
'$colBSECode TEXT UNIQUE, '
'$colNSECode TEXT UNIQUE, '
'$colName TEXT'
'$colOldBSECodes TEXT, '
'$colOldNSECodes TEXT, '
'$colName TEXT UNIQUE'
')');
}

Expand All @@ -103,7 +107,7 @@ class Db {
return count;
}

Future<List<Map>> getAllTuples(String table) async {
Future<List<Map<String, dynamic>>> getAllTuples(String table) async {
var dbClient = await db;
List<Map<String, dynamic>> result = await dbClient.query(table);

Expand Down
141 changes: 85 additions & 56 deletions lib/views/settings/data/data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:folio/views/settings/data/scrips_list.dart';
import 'package:intl/intl.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:device_info_plus/device_info_plus.dart';
Expand Down Expand Up @@ -93,11 +94,14 @@ class _ImportAreaState extends State<ImportArea> {
Icons.folder_open_outlined,
color: Theme.of(context).colorScheme.onPrimary,
),
onTap: !_isButtonEnabled ? null : () {
Navigator.push(context, MaterialPageRoute(builder: (context) {
return ImportFileRoute();
}));
},
onTap: !_isButtonEnabled
? null
: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) {
return ImportFileRoute();
}));
},
),
ListTile(
title: Text("Track a stock"),
Expand Down Expand Up @@ -147,6 +151,29 @@ class _ImportAreaState extends State<ImportArea> {
: null,
),
Divider(),
Text(
"Securities",
style: Theme.of(context)
.textTheme
.bodyLarge
?.copyWith(fontWeight: FontWeight.bold),
),
ListTile(
title: Text("Show Securities"),
trailing: Icon(
Icons.view_list_outlined,
color: Theme.of(context).colorScheme.onPrimary,
),
onTap: !_isButtonEnabled
? null
: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) {
return ShowSecuritiesRoute();
}));
},
),
Divider(),
Text(
"Export",
style: Theme.of(context)
Expand Down Expand Up @@ -185,56 +212,7 @@ class _ImportAreaState extends State<ImportArea> {
),
title: Text("Delete Database"),
onTap: () async {
String? result = await showDialog(
context: context,
builder: (_) => AlertDialog(
title: Center(child: Text("WARNING")),
content: Text(
"This will delete the database. Proceed only if you know what you are doing.",
style: Theme.of(context).textTheme.bodyLarge,
textAlign: TextAlign.justify,
),
actions: [
TextButton(
style: TextButton.styleFrom(
minimumSize: Size(88, 36),
padding: EdgeInsets.symmetric(horizontal: 16),
shape: const RoundedRectangleBorder(
borderRadius:
BorderRadius.all(Radius.circular(5)),
)),
onPressed: () {
Navigator.pop(context, "Delete");
},
child: Text("Delete"),
),
SizedBox(
width: 5,
),
ElevatedButton(
style: TextButton.styleFrom(
foregroundColor:
Theme.of(context).colorScheme.background,
minimumSize: Size(88, 36),
padding: EdgeInsets.symmetric(horizontal: 16),
shape: const RoundedRectangleBorder(
borderRadius:
BorderRadius.all(Radius.circular(5)),
),
),
onPressed: () {
Navigator.pop(context, "Cancel");
},
child: Text("Cancel"),
),
],
actionsPadding:
EdgeInsets.symmetric(horizontal: 10, vertical: 10),
),
);
if (result == "Delete") {
DatabaseActions.deleteDbThenInit();
}
await deleteDatabase(context);
},
)
],
Expand Down Expand Up @@ -292,7 +270,58 @@ class _ImportAreaState extends State<ImportArea> {
);
}


Future<void> deleteDatabase(BuildContext context) async {
String? result = await showDialog(
context: context,
builder: (_) => AlertDialog(
title: Center(child: Text("WARNING")),
content: Text(
"This will delete the database. Proceed only if you know what you are doing.",
style: Theme.of(context).textTheme.bodyLarge,
textAlign: TextAlign.justify,
),
actions: [
TextButton(
style: TextButton.styleFrom(
minimumSize: Size(88, 36),
padding: EdgeInsets.symmetric(horizontal: 16),
shape: const RoundedRectangleBorder(
borderRadius:
BorderRadius.all(Radius.circular(5)),
)),
onPressed: () {
Navigator.pop(context, "Delete");
},
child: Text("Delete"),
),
SizedBox(
width: 5,
),
ElevatedButton(
style: TextButton.styleFrom(
foregroundColor:
Theme.of(context).colorScheme.background,
minimumSize: Size(88, 36),
padding: EdgeInsets.symmetric(horizontal: 16),
shape: const RoundedRectangleBorder(
borderRadius:
BorderRadius.all(Radius.circular(5)),
),
),
onPressed: () {
Navigator.pop(context, "Cancel");
},
child: Text("Cancel"),
),
],
actionsPadding:
EdgeInsets.symmetric(horizontal: 10, vertical: 10),
),
);
if (result == "Delete") {
DatabaseActions.deleteDbThenInit();
}
}

void exportLogs() async {
setState(() {
Expand Down
4 changes: 2 additions & 2 deletions lib/views/settings/data/import_file/file_log_tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ class _InvalidFileLogTile extends State<FileLogTile>
),
)),
enabled:
widget._log.date == null && !widget.isLogValid,
widget._log.qty == null && !widget.isLogValid,
cursorColor: Theme.of(context).colorScheme.secondary,
style: Theme.of(context).textTheme.bodyLarge,
keyboardType: TextInputType.number,
Expand Down Expand Up @@ -452,7 +452,7 @@ class _InvalidFileLogTile extends State<FileLogTile>
),
)),
enabled:
widget._log.date == null && !widget.isLogValid,
widget._log.rate == null && !widget.isLogValid,
cursorColor: Theme.of(context).colorScheme.secondary,
style: Theme.of(context).textTheme.bodyLarge,
keyboardType:
Expand Down
Loading

0 comments on commit 86c89d8

Please sign in to comment.