81 lines
2.7 KiB
Dart
81 lines
2.7 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:csv/csv.dart';
|
|
import 'package:equatable/equatable.dart';
|
|
import 'package:file_picker/file_picker.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:tunas/repositories/account/account_repository.dart';
|
|
import 'package:tunas/repositories/account/models/transaction.dart';
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
part 'account_event.dart';
|
|
part 'account_state.dart';
|
|
|
|
class AccountBloc extends Bloc<AccountEvent, AccountState> {
|
|
final AccountRepository _accountRepository;
|
|
|
|
AccountBloc({required AccountRepository accountRepository})
|
|
: _accountRepository = accountRepository,
|
|
super(const AccountState()) {
|
|
on<AccountImportJSON>(_onAccountImportJSON);
|
|
on<AccountImportCSV>(_onAccountImportCSV);
|
|
// on<AccountExportJSON>(_onAccountImportJSON);
|
|
// on<AccountExportCSV>(_onAccountImportJSON);
|
|
}
|
|
|
|
double _universalConvertToDouble(dynamic value) {
|
|
if (value is String) {
|
|
return double.parse(value);
|
|
} else if (value is int) {
|
|
return value.toDouble();
|
|
} else if (value is double) {
|
|
return value;
|
|
} else {
|
|
throw Error();
|
|
}
|
|
}
|
|
|
|
_onAccountImportCSV(
|
|
AccountImportCSV event, Emitter<AccountState> emit) async {
|
|
FilePickerResult? result = await FilePicker.platform.pickFiles();
|
|
|
|
final csvPath = result?.files.first.path;
|
|
if (csvPath != null) {
|
|
final File csv = File(csvPath);
|
|
final String csvFileContent = await csv.readAsString();
|
|
final List<List<dynamic>> csvList = const CsvToListConverter(fieldDelimiter: '|', eol: '\n').convert(csvFileContent);
|
|
|
|
final transactions = csvList
|
|
.map((line) => Transaction(
|
|
uuid: const Uuid().v8(),
|
|
date: DateTime.parse(line[0]),
|
|
category: line[1],
|
|
description: line[2],
|
|
account: line[3],
|
|
value: _universalConvertToDouble(line[4]))
|
|
)
|
|
.toList();
|
|
|
|
await _accountRepository.deleteAccount();
|
|
await _accountRepository.saveTransactions(transactions);
|
|
}
|
|
}
|
|
|
|
_onAccountImportJSON(
|
|
AccountImportJSON event, Emitter<AccountState> emit) async {
|
|
FilePickerResult? result = await FilePicker.platform.pickFiles();
|
|
|
|
final jsonPath = result?.files.first.path;
|
|
if (jsonPath != null) {
|
|
final File json = File(jsonPath);
|
|
final String jsonString = await json.readAsString();
|
|
final List<dynamic> jsonList = jsonDecode(jsonString);
|
|
final List<Transaction> transactions = jsonList.map((transaction) => Transaction.fromJson(transaction)).toList();
|
|
|
|
await _accountRepository.deleteAccount();
|
|
await _accountRepository.saveTransactions(transactions);
|
|
}
|
|
}
|
|
}
|