Auto-shield

This commit is contained in:
Hanh 2021-08-23 23:44:41 +08:00
parent e6ca7edef4
commit c3cfd703a6
7 changed files with 72 additions and 22 deletions

View File

@ -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"),

View File

@ -1154,6 +1154,16 @@ class S {
args: [],
);
}
/// `Shield Transparent Balance When Sending`
String get shieldTransparentBalanceWithSending {
return Intl.message(
'Shield Transparent Balance When Sending',
name: 'shieldTransparentBalanceWithSending',
desc: '',
args: [],
);
}
}
class AppLocalizationDelegate extends LocalizationsDelegate<S> {

View File

@ -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"
}

View File

@ -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) {
@ -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);
}
@ -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()) {
@ -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;
}
}

View File

@ -62,6 +62,9 @@ abstract class _Settings with Store {
@observable
bool shieldBalance = false;
@observable
double autoShieldThreshold = 0.0;
var palette = charts.MaterialPalette.blue;
@action
@ -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;
@ -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;
@ -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) {
@ -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 {

View File

@ -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"

View File

@ -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"