Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manual fee override #378

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 96 additions & 41 deletions app/src/main/java/com/samourai/wallet/send/SendActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
Expand Down Expand Up @@ -133,6 +133,8 @@ public class SendActivity extends AppCompatActivity {
private ViewGroup totalMinerFeeLayout;
private Switch cahootsSwitch;
private SeekBar feeSeekBar;
private EditText feeManualEditText;
private Switch feeOverrideSwitch;
private Group ricochetStaggeredOptionGroup;
private boolean shownWalletLoadingMessage = false;
private long balance = 0L;
Expand Down Expand Up @@ -175,6 +177,8 @@ public class SendActivity extends AppCompatActivity {
private CompositeDisposable compositeDisposables = new CompositeDisposable();
private SelectCahootsType.type selectedCahootsType = SelectCahootsType.type.NONE;
private int account = 0;
private int multiplier = 10000;
private DecimalFormat decimalFormat = new DecimalFormat("##.00");

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -214,6 +218,8 @@ protected void onCreate(Bundle savedInstanceState) {
tvTotalFee = sendTransactionDetailsView.getTransactionReview().findViewById(R.id.total_fee);
btnSend = sendTransactionDetailsView.getTransactionReview().findViewById(R.id.send_btn);
feeSeekBar = sendTransactionDetailsView.getTransactionReview().findViewById(R.id.fee_seekbar);
feeManualEditText = sendTransactionDetailsView.getTransactionReview().findViewById(R.id.fee_manual_edittext);
feeOverrideSwitch = sendTransactionDetailsView.getTransactionReview().findViewById(R.id.fee_override_switch);
tvEstimatedBlockWait = sendTransactionDetailsView.getTransactionReview().findViewById(R.id.est_block_time);
feeSeekBar = sendTransactionDetailsView.getTransactionReview().findViewById(R.id.fee_seekbar);
cahootsGroup = sendTransactionDetailsView.findViewById(R.id.cohoots_options);
Expand Down Expand Up @@ -241,6 +247,22 @@ protected void onCreate(Bundle savedInstanceState) {

tvTotalFee.setOnClickListener(clipboardCopy);
tvSelectedFeeRate.setOnClickListener(clipboardCopy);
feeManualEditText.setFilters(new InputFilter[]{
(source, start, end, dest, dstart, dend) -> {
double value = Double.parseDouble(dest.toString() + source.toString());

// Sane limit 10x from max estimated fee
double maxValue = (((double) feeSeekBar.getMax() / multiplier) + 1) * 10;

if (value < 1) {
return "1";
} else if (value > maxValue) {
return "";
}

return null;
}
});

if (getIntent().getExtras() != null && getIntent().getExtras().containsKey("_account")) {
if (getIntent().getExtras().getInt("_account") == WhirlpoolMeta.getInstance(SendActivity.this).getWhirlpoolPostmix()) {
Expand Down Expand Up @@ -450,9 +472,6 @@ private void enableReviewButton(boolean enable) {

private void setUpFee() {


int multiplier = 10000;

FEE_TYPE = PrefsUtil.getInstance(this).getValue(PrefsUtil.CURRENT_FEE_TYPE, FEE_NORMAL);


Expand Down Expand Up @@ -511,38 +530,12 @@ private void setUpFee() {
tvSelectedFeeRate.setText((String.valueOf((int) feeMed).concat(" sats/b")));

feeSeekBar.setProgress((feeMedSliderValue - multiplier) + 1);
DecimalFormat decimalFormat = new DecimalFormat("##.00");

setFeeLabels();
feeSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

double value = ((double) i + multiplier) / (double) multiplier;

tvSelectedFeeRate.setText(String.valueOf(decimalFormat.format(value)).concat(" sats/b"));
if (value == 0.0) {
value = 1.0;
}
double pct = 0.0;
int nbBlocks = 6;
if (value <= (double) feeLow) {
pct = ((double) feeLow / value);
nbBlocks = ((Double) Math.ceil(pct * 24.0)).intValue();
} else if (value >= (double) feeHigh) {
pct = ((double) feeHigh / value);
nbBlocks = ((Double) Math.ceil(pct * 2.0)).intValue();
if (nbBlocks < 1) {
nbBlocks = 1;
}
} else {
pct = ((double) feeMed / value);
nbBlocks = ((Double) Math.ceil(pct * 6.0)).intValue();
}
tvEstimatedBlockWait.setText(nbBlocks + " blocks");
setFee(value);
setFeeLabels();

restoreChangeIndexes();
onSliderChange(i);
}

@Override
Expand All @@ -556,6 +549,38 @@ public void onStopTrackingTouch(SeekBar seekBar) {
}
});

feeOverrideSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String feeText = String.valueOf(
decimalFormat.format(((double)feeSeekBar.getProgress() + multiplier) / (double) multiplier)
);

feeManualEditText.setText(feeText);
feeManualEditText.setVisibility(isChecked ? View.VISIBLE : View.INVISIBLE);
feeSeekBar.setVisibility(isChecked ? View.INVISIBLE : View.VISIBLE);
setFeeLabels();
}
});

feeManualEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {
if (s.toString() == null || s.toString().isEmpty()) return;
double value = Double.parseDouble(s.toString());
onSliderChange((int) ((double) multiplier * (value - 1)));
}
});

switch (FEE_TYPE) {
case FEE_LOW:
Expand All @@ -571,22 +596,52 @@ public void onStopTrackingTouch(SeekBar seekBar) {
FeeUtil.getInstance().sanitizeFee();
break;
}
}

private void onSliderChange(int progress) {
double value = ((double) progress + multiplier) / (double) multiplier;

tvSelectedFeeRate.setText(String.valueOf(decimalFormat.format(value)).concat(" sats/b"));
if (value == 0.0) {
value = 1.0;
}
double pct = 0.0;
int nbBlocks = 6;
if (value <= (double) feeLow) {
pct = ((double) feeLow / value);
nbBlocks = ((Double) Math.ceil(pct * 24.0)).intValue();
} else if (value >= (double) feeHigh) {
pct = ((double) feeHigh / value);
nbBlocks = ((Double) Math.ceil(pct * 2.0)).intValue();
if (nbBlocks < 1) {
nbBlocks = 1;
}
} else {
pct = ((double) feeMed / value);
nbBlocks = ((Double) Math.ceil(pct * 6.0)).intValue();
}
tvEstimatedBlockWait.setText(nbBlocks + " blocks");
setFee(value);
setFeeLabels();

restoreChangeIndexes();
}

private void setFeeLabels() {
float sliderValue = (((float) feeSeekBar.getProgress()) / feeSeekBar.getMax());

float sliderInPercentage = sliderValue * 100;
if (feeManualEditText.isShown()) {
tvSelectedFeeRateLayman.setText(R.string.manual);
} else {
float sliderValue = (((float) feeSeekBar.getProgress()) / feeSeekBar.getMax());

if (sliderInPercentage < 33) {
tvSelectedFeeRateLayman.setText(R.string.low);
} else if (sliderInPercentage > 33 && sliderInPercentage < 66) {
tvSelectedFeeRateLayman.setText(R.string.normal);
} else if (sliderInPercentage > 66) {
tvSelectedFeeRateLayman.setText(R.string.urgent);
float sliderInPercentage = sliderValue * 100;

if (sliderInPercentage < 33) {
tvSelectedFeeRateLayman.setText(R.string.low);
} else if (sliderInPercentage > 33 && sliderInPercentage < 66) {
tvSelectedFeeRateLayman.setText(R.string.normal);
} else if (sliderInPercentage > 66) {
tvSelectedFeeRateLayman.setText(R.string.urgent);
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/drawable/fee_override_switch_green.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

</selector>
51 changes: 48 additions & 3 deletions app/src/main/res/layout/fee_selector.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
android:layout_marginRight="8dp"
tools:showIn="@layout/send_transaction_review">


<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
Expand Down Expand Up @@ -44,6 +45,22 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView6" />

<EditText
android:id="@+id/fee_manual_edittext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:progressDrawable="@drawable/seekbar_green_bg"
android:ems="10"
android:inputType="number"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="@+id/total_miner_fee_group"
app:layout_constraintEnd_toStartOf="@+id/linearLayout2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView6" />

<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="70dp"
Expand Down Expand Up @@ -104,6 +121,35 @@

</LinearLayout>

<LinearLayout
android:id="@+id/fee_override_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:gravity="center_horizontal"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="@+id/linearLayout3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/linearLayout3"
app:layout_constraintVertical_bias="0.0">

<TextView
android:id="@+id/fee_override_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Manual Override"
android:textSize="12sp" />

<Switch
android:id="@+id/fee_override_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:checked="false" />
</LinearLayout>

<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="wrap_content"
Expand Down Expand Up @@ -132,6 +178,5 @@
tools:text="1 Block"
android:textColor="@color/white" />
</LinearLayout>


</android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,7 @@
<string name="urgent">Urgent</string>
<string name="normal">Normal</string>
<string name="low">Low</string>
<string name="manual">Manual</string>

<string name="ricochet_nlocktime_ok">Your staggered Ricochet has been forwarded for broadcast. Please wait until the block height permits the broadcasting of all the transactions in the series.</string>
<string name="ricochet_nlocktime_ko">Your staggered Ricochet could not be forwarded for broadcast. Please try again.</string>
Expand Down