Skip to content

Commit 50b8fa4

Browse files
committed
Persisting rate related user choices
1 parent 0615e93 commit 50b8fa4

File tree

3 files changed

+55
-36
lines changed

3 files changed

+55
-36
lines changed

lib/shared_state/rate_state.dart

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
import 'package:flutter/widgets.dart';
2+
import 'package:intl/intl.dart';
23
import 'package:sentinelx/channels/api_channel.dart';
34
import 'package:sentinelx/models/db/prefs_store.dart';
45
import 'package:sentinelx/models/exchange/LocalBitcoinRateProvider.dart';
56
import 'package:sentinelx/models/exchange/exchange_provider.dart';
67
import 'package:sentinelx/utils/format_util.dart';
78

8-
class RateModel extends ChangeNotifier {
9+
class RateState extends ChangeNotifier {
910
ExchangeProvider provider;
1011

11-
int index = 0;
12+
int index = 1;
1213

13-
RateModel._privateConstructor() {
14+
RateState._privateConstructor() {
1415
init();
1516
}
1617

17-
static final RateModel _instance = RateModel._privateConstructor();
18+
static final RateState _instance = RateState._privateConstructor();
1819

19-
factory RateModel() {
20+
factory RateState() {
2021
return _instance;
2122
}
2223

2324
String currency = "USD";
2425
num rate = 1;
2526

2627
update(ExchangeProvider provider) {
27-
print("rateProvider ${provider.getRate()}");
2828
this.provider = provider;
2929
this.provider.setCurrency(currency);
3030
this.rate = this.provider.getRate().rate;
@@ -34,30 +34,46 @@ class RateModel extends ChangeNotifier {
3434

3535
Future getExchangeRates() async {
3636
String ratePayload = await ApiChannel().getExchangeRates(LocalBitcoinRateProvider.API);
37-
LocalBitcoinRateProvider rateProvider = new LocalBitcoinRateProvider(ratePayload);
38-
rateProvider.setCurrency(currency);
39-
this.update(rateProvider);
37+
String period = await PrefsStore().getString(PrefsStore.CURRENCY_RATE_PERIOD);
38+
LocalBitcoinRateProvider rateProvider = new LocalBitcoinRateProvider(ratePayload, period);
39+
update(rateProvider);
4040
}
4141

4242
formatToBTCRate(int result) {
43-
// double satRate = (rate / 100000000).toDouble();
44-
if (result != null) {
45-
if (currency == "BTC") {
46-
47-
}
43+
var f = NumberFormat.currency(symbol: "");
44+
if ((satToBtcAsDouble(result)) > 0.1) {
45+
print("------");
46+
print("LIO 2 RATE ${this.rate}");
47+
print("LIO ${(satToBtcAsDouble(result) * this.rate)}");
48+
print("------");
4849
}
49-
return " ${ (satToBtcAsDouble(result) * this.rate ).toStringAsFixed(2)} ${this.currency}";
50+
return " ${f.format((satToBtcAsDouble(result) * this.rate))} ${this.currency}";
51+
}
52+
53+
formatRate(int result) {
54+
var f = NumberFormat.currency(symbol: "", decimalDigits: 8);
55+
return "${satToBtc(result)} BTC";
56+
}
57+
58+
String formatSatRate(num result) {
59+
var f = NumberFormat.currency(symbol: "", decimalDigits: 0);
60+
return "${f.format(result)} sat";
5061
}
51-
52-
formatRate(int result) {
5362

54-
// double satRate = (rate / 100000000).toDouble();
55-
return "${satToBtc(result)} BTC";
63+
Future setCurrency(String curr) {
64+
this.currency = curr;
65+
this.index = 0;
66+
this.notifyListeners();
67+
return this.getExchangeRates();
5668
}
5769

5870
void init() async {
59-
String currency = await PrefsStore().getString(PrefsStore.CURRENCY);
60-
print("currency ${currency}");
71+
currency = await PrefsStore().getString(PrefsStore.CURRENCY);
72+
index = await PrefsStore().getNum(PrefsStore.AMOUNT_VIEW_TYPE);
73+
print("index ${index}");
74+
if (index == null) {
75+
index = 1;
76+
}
6177
if (currency == null) {
6278
currency = "BTC";
6379
this.rate = 1;
@@ -67,28 +83,17 @@ class RateModel extends ChangeNotifier {
6783
this.rate = 1;
6884
}
6985
}
86+
this.notifyListeners();
7087
}
7188

72-
void setIn(int index){
89+
void setViewIndex(int index) {
7390
this.index = index;
7491
this.notifyListeners();
92+
this.save();
7593
}
7694

7795
void save() async {
7896
await PrefsStore().put(PrefsStore.CURRENCY_RATE, this.rate);
97+
await PrefsStore().put(PrefsStore.AMOUNT_VIEW_TYPE, this.index);
7998
}
8099
}
81-
// Map<String, dynamic> toJson() {
82-
// final Map<String, dynamic> data = new Map<String, dynamic>();
83-
// if (this.balance != null) {
84-
// data['balance'] = this.balance;
85-
// }
86-
// return data;
87-
// }
88-
//
89-
// void fromJSON(Map<String, dynamic> json) {
90-
// if (json['balance'] != null) {
91-
// this.balance = json['balance'];
92-
// }
93-
// }
94-
//}

lib/utils/format_util.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
import 'dart:ffi';
2+
13
import 'package:intl/intl.dart';
24

35
String satToBtc(num coin) {
46
return (coin / 100000000).toDouble().toStringAsFixed(8);
57
}
68

9+
double satToBtcAsDouble(num coin) {
10+
return (coin / 100000000).toDouble();
11+
}
12+
713
String formatTime(num timestamp) {
814
var formatter = new DateFormat("h:mm");
915
var time = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);

lib/utils/utils.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import 'dart:convert';
33
import 'package:flutter/material.dart';
44
import 'package:sentinelx/channels/network_channel.dart';
55
import 'package:sentinelx/models/db/prefs_store.dart';
6+
import 'package:sentinelx/models/exchange/rate.dart';
7+
import 'package:sentinelx/shared_state/app_state.dart';
68

79
Future<Map<String, dynamic>> parseJsonResponse(String response) async {
810
Map<String, dynamic> json = jsonDecode(response);
@@ -43,6 +45,12 @@ String getTorStatusInText(TorStatus torStatus) {
4345
return "";
4446
}
4547

48+
String formattedRate(int rate) {
49+
Rate selectedRate = AppState().selectedRate;
50+
double satRate = (rate / 100000000).toDouble();
51+
return " ${satRate * selectedRate.rate} ${selectedRate.currency}";
52+
}
53+
4654
Future<bool> checkNetworkStatusBeforeApiCall(
4755
Function(SnackBar) callback) async {
4856
bool isConnected = await NetworkChannel().getConnectivityStatus() ==

0 commit comments

Comments
 (0)