Skip to content

Commit

Permalink
Auto-shield
Browse files Browse the repository at this point in the history
  • Loading branch information
hhanh00 committed Aug 23, 2021
1 parent e6ca7ed commit c3cfd70
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 22 deletions.
1 change: 1 addition & 0 deletions lib/generated/intl/messages_en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class MessageLookup extends MessageLookupByLibrary {
"settings" : MessageLookupByLibrary.simpleMessage("Settings"),
"shieldTranspBalance" : MessageLookupByLibrary.simpleMessage("Shield Transp. Balance"),
"shieldTransparentBalance" : MessageLookupByLibrary.simpleMessage("Shield Transparent Balance"),
"shieldTransparentBalanceWithSending" : MessageLookupByLibrary.simpleMessage("Shield Transparent Balance When Sending"),
"shieldingInProgress" : MessageLookupByLibrary.simpleMessage("Shielding in progress..."),
"spendable" : MessageLookupByLibrary.simpleMessage("Spendable:"),
"synching" : MessageLookupByLibrary.simpleMessage("Synching"),
Expand Down
10 changes: 10 additions & 0 deletions lib/generated/l10n.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,6 @@
"M1": "1 M",
"M3": "3 M",
"M6": "6 M",
"Y1": "1 Y"
"Y1": "1 Y",
"shieldTransparentBalanceWithSending": "Shield Transparent Balance When Sending"
}
61 changes: 42 additions & 19 deletions lib/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ final _settingsFormKey = GlobalKey<FormBuilderState>();
class SettingsState extends State<SettingsPage> {
var _anchorController =
MaskedTextController(mask: "00", text: "${settings.anchorOffset}");
var _thresholdController = MoneyMaskedTextController(
decimalSeparator: '.', thousandSeparator: ',', precision: 3);

@override
void initState() {
super.initState();
_thresholdController.updateValue(settings.autoShieldThreshold);
}

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -125,21 +133,38 @@ class SettingsState extends State<SettingsPage> {
FormBuilderFieldOption(
child: Text(S.of(context).Y1), value: '1Y'),
]),
FormBuilderCheckbox(
name: 'get_tx',
title: Text(
S.of(context).retrieveTransactionDetails),
initialValue: settings.getTx,
onSaved: _onGetTx),
FormBuilderCheckbox(
name: 'auto_shield',
title: Text(S.of(context).shieldTransparentBalance),
initialValue: settings.shieldBalance,
onSaved: _shieldBalance),
ButtonBar(children: confirmButtons(context, _onSave))
FormBuilderCheckbox(
name: 'get_tx',
title: Text(
S.of(context).retrieveTransactionDetails),
initialValue: settings.getTx,
onSaved: _onGetTx),
TextFormField(
decoration: InputDecoration(
labelText: 'Auto Shield Threshold'),
keyboardType: TextInputType.number,
controller: _thresholdController,
onSaved: _onAutoShieldThreshold,
validator: _checkAmount),
FormBuilderCheckbox(
name: 'shield_send',
title: Text(S
.of(context)
.shieldTransparentBalanceWithSending),
initialValue: settings.shieldBalance,
onSaved: _shieldBalance),
ButtonBar(children: confirmButtons(context, _onSave))
]))))));
}

String _checkAmount(String vs) {
final vss = vs.replaceAll(',', '');
final v = double.tryParse(vss);
if (v == null) return S.of(context).amountMustBeANumber;
if (v <= 0.0) return S.of(context).amountMustBePositive;
return null;
}

_onChoice(v) {
settings.setURLChoice(v);
}
Expand All @@ -164,6 +189,11 @@ class SettingsState extends State<SettingsPage> {
settings.setShieldBalance(v);
}

_onAutoShieldThreshold(_) {
final v = _thresholdController.numberValue;
settings.setAutoShieldThreshold(v);
}

_onSave() {
final form = _settingsFormKey.currentState;
if (form.validate()) {
Expand All @@ -179,11 +209,4 @@ class SettingsState extends State<SettingsPage> {
_onGetTx(v) {
settings.updateGetTx(v);
}

String _checkFx(String vs) {
final v = double.tryParse(vs);
if (v == null) return 'FX rate must be a number';
if (v <= 0.0) return 'FX rate must be positive';
return null;
}
}
15 changes: 15 additions & 0 deletions lib/store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ abstract class _Settings with Store {
@observable
bool shieldBalance = false;

@observable
double autoShieldThreshold = 0.0;

var palette = charts.MaterialPalette.blue;

@action
Expand All @@ -80,6 +83,7 @@ abstract class _Settings with Store {
currency = prefs.getString('currency') ?? "USD";
chartRange = prefs.getString('chart_range') ?? "1Y";
shieldBalance = prefs.getBool('shield_balance') ?? false;
autoShieldThreshold = prefs.getDouble('autoshield_threshold') ?? 0.0;
_updateThemeData();
Future.microtask(_loadCurrencies); // lazily
return true;
Expand Down Expand Up @@ -227,6 +231,13 @@ abstract class _Settings with Store {
shieldBalance = v;
prefs.setBool('shield_balance', shieldBalance);
}

@action
Future<void> setAutoShieldThreshold(double v) async {
final prefs = await SharedPreferences.getInstance();
autoShieldThreshold = v;
prefs.setDouble('autoshield_threshold', autoShieldThreshold);
}
}

class AccountManager = _AccountManager with _$AccountManager;
Expand Down Expand Up @@ -427,6 +438,7 @@ abstract class _AccountManager with Store {

Future<void> _fetchData(int accountId, bool force) async {
await _updateBalance(accountId);

final hasNewTx = await _fetchNotesAndHistory(accountId, force);
int countNewPrices = await WarpApi.syncHistoricalPrices(settings.currency);
if (hasNewTx) {
Expand Down Expand Up @@ -682,6 +694,9 @@ abstract class _AccountManager with Store {
if (active == null) return;
int balance = WarpApi.getTBalance(active.id);
if (balance != tbalance) tbalance = balance;
if (tbalance / ZECUNIT >= settings.autoShieldThreshold) {
WarpApi.shieldTAddr(active.id);
}
}

Future<void> _fetchContacts(int accountId) async {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.9+90
version: 1.0.9+91

environment:
sdk: ">=2.9.0 <3.0.0"
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.9+90
version: 1.0.9+91

environment:
sdk: ">=2.9.0 <3.0.0"
Expand Down

0 comments on commit c3cfd70

Please sign in to comment.