11import 'package:flutter/widgets.dart' ;
2+ import 'package:intl/intl.dart' ;
23import 'package:sentinelx/channels/api_channel.dart' ;
34import 'package:sentinelx/models/db/prefs_store.dart' ;
45import 'package:sentinelx/models/exchange/LocalBitcoinRateProvider.dart' ;
56import 'package:sentinelx/models/exchange/exchange_provider.dart' ;
67import '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- //}
0 commit comments