Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
Add on-chain balance to wallet fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbertoBoldrini committed Oct 21, 2022
1 parent acffc0e commit c922c37
Show file tree
Hide file tree
Showing 5 changed files with 259 additions and 23 deletions.
114 changes: 100 additions & 14 deletions app/src/main/java/zapsolutions/zap/fragments/WalletFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
Expand Down Expand Up @@ -55,10 +56,27 @@ public class WalletFragment extends Fragment implements SharedPreferences.OnShar

private static final String LOG_TAG = WalletFragment.class.getName();

private TextView mTvPrimaryBalance;
private TextView mTvPrimaryBalanceUnit;
private TextView mTvSecondaryBalance;
private TextView mTvSecondaryBalanceUnit;
private LinearLayout mTotalBalancePrimaryLayout;
private TextView mTvTotalBalancePrimary;
private TextView mTvTotalBalancePrimaryUnit;
private LinearLayout mTotalBalanceSecondaryLayout;
private TextView mTvTotalBalanceSecondary;
private TextView mTvTotalBalanceSecondaryUnit;

private LinearLayout mChannelBalancePrimaryLayout;
private TextView mTvChannelBalancePrimary;
private TextView mTvChannelBalancePrimaryUnit;
private LinearLayout mChannelBalanceSecondaryLayout;
private TextView mTvChannelBalanceSecondary;
private TextView mTvChannelBalanceSecondaryUnit;

private LinearLayout mOnChainBalancePrimaryLayout;
private TextView mTvOnChainBalancePrimary;
private TextView mTvOnChainBalancePrimaryUnit;
private LinearLayout mOnChainBalanceSecondaryLayout;
private TextView mTvOnChainBalanceSecondary;
private TextView mTvOnChainBalanceSecondaryUnit;

private TextView mTvMode;
private ConstraintLayout mClBalanceLayout;
private ImageView mIvLogo;
Expand Down Expand Up @@ -98,10 +116,28 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
mClBalanceLayout = view.findViewById(R.id.BalanceLayout);
mIvLogo = view.findViewById(R.id.logo);
mIvSwitchButton = view.findViewById(R.id.switchButtonImage);
mTvPrimaryBalance = view.findViewById(R.id.BalancePrimary);
mTvPrimaryBalanceUnit = view.findViewById(R.id.BalancePrimaryUnit);
mTvSecondaryBalance = view.findViewById(R.id.BalanceSecondary);
mTvSecondaryBalanceUnit = view.findViewById(R.id.BalanceSecondaryUnit);

mTotalBalancePrimaryLayout = view.findViewById(R.id.TotalBalancePrimaryLayout);
mTvTotalBalancePrimary = view.findViewById(R.id.TotalBalancePrimary);
mTvTotalBalancePrimaryUnit = view.findViewById(R.id.TotalBalancePrimaryUnit);
mTotalBalanceSecondaryLayout = view.findViewById(R.id.TotalBalanceSecondaryLayout);
mTvTotalBalanceSecondary = view.findViewById(R.id.TotalBalanceSecondary);
mTvTotalBalanceSecondaryUnit = view.findViewById(R.id.TotalBalanceSecondaryUnit);

mChannelBalancePrimaryLayout = view.findViewById(R.id.ChannelBalancePrimaryLayout);
mTvChannelBalancePrimary = view.findViewById(R.id.ChannelBalancePrimary);
mTvChannelBalancePrimaryUnit = view.findViewById(R.id.ChannelBalancePrimaryUnit);
mChannelBalanceSecondaryLayout = view.findViewById(R.id.ChannelBalanceSecondaryLayout);
mTvChannelBalanceSecondary = view.findViewById(R.id.ChannelBalanceSecondary);
mTvChannelBalanceSecondaryUnit = view.findViewById(R.id.ChannelBalanceSecondaryUnit);

mOnChainBalancePrimaryLayout = view.findViewById(R.id.OnChainBalancePrimaryLayout);
mTvOnChainBalancePrimary = view.findViewById(R.id.OnChainBalancePrimary);
mTvOnChainBalancePrimaryUnit = view.findViewById(R.id.OnChainBalancePrimaryUnit);
mOnChainBalanceSecondaryLayout = view.findViewById(R.id.OnChainBalanceSecondaryLayout);
mTvOnChainBalanceSecondary = view.findViewById(R.id.OnChainBalanceSecondary);
mTvOnChainBalanceSecondaryUnit = view.findViewById(R.id.OnChainBalanceSecondaryUnit);

mTvMode = view.findViewById(R.id.mode);
mBalanceFadeOutAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.balance_fade_out);
mLogoFadeInAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.logo_fade_in);
Expand Down Expand Up @@ -169,6 +205,13 @@ public void onAnimationEnd(Animation arg0) {
hideBalance();
}

// Hide balance if the setting was chosen
if (PrefsUtil.getPrefs().getBoolean("detailedBalance", false)) {
showDetailedBalance();
} else {
showTotalBalance();
}

// Action when clicked on menu button
mDrawerMenuButton.setOnClickListener(new OnSingleClickListener() {
@Override
Expand Down Expand Up @@ -355,9 +398,13 @@ private void updateTotalBalanceDisplay() {
public void run() {
// Adapt unit text size depending on its length
if (MonetaryUtil.getInstance().getPrimaryDisplayUnit().length() > 2) {
mTvPrimaryBalanceUnit.setTextSize(20);
mTvTotalBalancePrimaryUnit.setTextSize(20);
mTvChannelBalancePrimaryUnit.setTextSize(20);
mTvOnChainBalancePrimaryUnit.setTextSize(20);
} else {
mTvPrimaryBalanceUnit.setTextSize(32);
mTvTotalBalancePrimaryUnit.setTextSize(32);
mTvChannelBalancePrimaryUnit.setTextSize(32);
mTvOnChainBalancePrimaryUnit.setTextSize(32);
}

Balances balances;
Expand All @@ -367,10 +414,20 @@ public void run() {
balances = Wallet.getInstance().getDemoBalances();
}

mTvPrimaryBalance.setText(MonetaryUtil.getInstance().getPrimaryDisplayAmount(balances.total()));
mTvPrimaryBalanceUnit.setText(MonetaryUtil.getInstance().getPrimaryDisplayUnit());
mTvSecondaryBalance.setText(MonetaryUtil.getInstance().getSecondaryDisplayAmount(balances.total()));
mTvSecondaryBalanceUnit.setText(MonetaryUtil.getInstance().getSecondaryDisplayUnit());
mTvTotalBalancePrimary.setText(MonetaryUtil.getInstance().getPrimaryDisplayAmount(balances.total()));
mTvTotalBalancePrimaryUnit.setText(MonetaryUtil.getInstance().getPrimaryDisplayUnit());
mTvTotalBalanceSecondary.setText(MonetaryUtil.getInstance().getSecondaryDisplayAmount(balances.total()));
mTvTotalBalanceSecondaryUnit.setText(MonetaryUtil.getInstance().getSecondaryDisplayUnit());

mTvChannelBalancePrimary.setText("⚡ " + MonetaryUtil.getInstance().getPrimaryDisplayAmount(balances.channelTotal()));
mTvChannelBalancePrimaryUnit.setText(MonetaryUtil.getInstance().getPrimaryDisplayUnit());
mTvChannelBalanceSecondary.setText(MonetaryUtil.getInstance().getSecondaryDisplayAmount(balances.channelTotal()));
mTvChannelBalanceSecondaryUnit.setText(MonetaryUtil.getInstance().getSecondaryDisplayUnit());

mTvOnChainBalancePrimary.setText("❄ " + MonetaryUtil.getInstance().getPrimaryDisplayAmount(balances.onChainTotal()));
mTvOnChainBalancePrimaryUnit.setText(MonetaryUtil.getInstance().getPrimaryDisplayUnit());
mTvOnChainBalanceSecondary.setText(MonetaryUtil.getInstance().getSecondaryDisplayAmount(balances.onChainTotal()));
mTvOnChainBalanceSecondaryUnit.setText(MonetaryUtil.getInstance().getSecondaryDisplayUnit());

ZapLog.v(LOG_TAG, "Total balance display updated");
}
Expand All @@ -391,6 +448,13 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
showBalance();
}
}
if (key.equals("detailedBalance")) {
if (PrefsUtil.getPrefs().getBoolean("detailedBalance", false)) {
showDetailedBalance();
} else {
showTotalBalance();
}
}
}
}

Expand Down Expand Up @@ -551,6 +615,28 @@ private void showBalance() {
mIvLogo.setVisibility(View.INVISIBLE);
}

private void showTotalBalance() {
mTotalBalancePrimaryLayout.setVisibility(View.VISIBLE);
mTotalBalanceSecondaryLayout.setVisibility(View.VISIBLE);

mChannelBalancePrimaryLayout.setVisibility(View.GONE);
mChannelBalanceSecondaryLayout.setVisibility(View.GONE);

mOnChainBalancePrimaryLayout.setVisibility(View.GONE);
mOnChainBalanceSecondaryLayout.setVisibility(View.GONE);
}

private void showDetailedBalance() {
mTotalBalancePrimaryLayout.setVisibility(View.GONE);
mTotalBalanceSecondaryLayout.setVisibility(View.GONE);

mChannelBalancePrimaryLayout.setVisibility(View.VISIBLE);
mChannelBalanceSecondaryLayout.setVisibility(View.VISIBLE);

mOnChainBalancePrimaryLayout.setVisibility(View.VISIBLE);
mOnChainBalanceSecondaryLayout.setVisibility(View.VISIBLE);
}

private void updateStatusDot(String walletAlias) {
DisplayMetrics metrics = getResources().getDisplayMetrics();
if (PrefsUtil.isTorEnabled()) {
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/zapsolutions/zap/util/Balances.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public long total() {
return mOnChainBalanceTotal + mChannelBalance + mChannelBalancePendingOpen + mChannelBalanceLimbo;
}

public long channelTotal() {
return mChannelBalance + mChannelBalancePendingOpen + mChannelBalanceLimbo;
}

public long onChainTotal() {
return mOnChainBalanceTotal;
}
Expand Down
156 changes: 147 additions & 9 deletions app/src/main/res/layout/fragment_wallet.xml
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
app:layout_constraintVertical_bias="0.4">

<LinearLayout
android:id="@+id/BalancePrimaryLayout"
android:id="@+id/TotalBalancePrimaryLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
Expand All @@ -153,7 +153,7 @@
app:layout_constraintStart_toStartOf="parent">

<TextView
android:id="@+id/BalancePrimary"
android:id="@+id/TotalBalancePrimary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
Expand All @@ -166,7 +166,7 @@
tools:text="Primary" />

<zapsolutions.zap.customView.NonClippingTextView
android:id="@+id/BalancePrimaryUnit"
android:id="@+id/TotalBalancePrimaryUnit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
Expand All @@ -179,28 +179,28 @@
</LinearLayout>

<LinearLayout
android:id="@+id/BalanceSecondaryLayout"
android:id="@+id/TotalBalanceSecondaryLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/BalancePrimaryLayout">
app:layout_constraintTop_toBottomOf="@+id/TotalBalancePrimaryLayout">

<TextView
android:id="@+id/BalanceSecondary"
android:id="@+id/TotalBalanceSecondary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="@color/gray"
android:textSize="15sp"
app:layout_constraintTop_toBottomOf="@id/BalancePrimary"
app:layout_constraintTop_toBottomOf="@id/TotalBalancePrimary"
tools:text="Secondary" />

<zapsolutions.zap.customView.NonClippingTextView
android:id="@+id/BalanceSecondaryUnit"
android:id="@+id/TotalBalanceSecondaryUnit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
Expand All @@ -211,6 +211,144 @@
tools:text="sat" />
</LinearLayout>

<LinearLayout
android:id="@+id/ChannelBalancePrimaryLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">

<TextView
android:id="@+id/ChannelBalancePrimary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="38sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Primary" />

<zapsolutions.zap.customView.NonClippingTextView
android:id="@+id/ChannelBalancePrimaryUnit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="italic"
tools:text="BTC" />

</LinearLayout>

<LinearLayout
android:id="@+id/ChannelBalanceSecondaryLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ChannelBalancePrimaryLayout">

<TextView
android:id="@+id/ChannelBalanceSecondary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="@color/gray"
android:textSize="15sp"
app:layout_constraintTop_toBottomOf="@id/TotalBalancePrimary"
tools:text="Secondary" />

<zapsolutions.zap.customView.NonClippingTextView
android:id="@+id/ChannelBalanceSecondaryUnit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:textAlignment="center"
android:textColor="@color/gray"
android:textSize="15sp"
android:textStyle="italic"
tools:text="sat" />
</LinearLayout>

<LinearLayout
android:id="@+id/OnChainBalancePrimaryLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20pt"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ChannelBalanceSecondaryLayout">

<TextView
android:id="@+id/OnChainBalancePrimary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="38sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Primary" />

<zapsolutions.zap.customView.NonClippingTextView
android:id="@+id/OnChainBalancePrimaryUnit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="italic"
tools:text="BTC" />

</LinearLayout>

<LinearLayout
android:id="@+id/OnChainBalanceSecondaryLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/OnChainBalancePrimaryLayout">

<TextView
android:id="@+id/OnChainBalanceSecondary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textColor="@color/gray"
android:textSize="15sp"
app:layout_constraintTop_toBottomOf="@id/OnChainBalancePrimary"
tools:text="Secondary" />

<zapsolutions.zap.customView.NonClippingTextView
android:id="@+id/OnChainBalanceSecondaryUnit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:textAlignment="center"
android:textColor="@color/gray"
android:textSize="15sp"
android:textStyle="italic"
tools:text="sat" />
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Expand Down Expand Up @@ -282,7 +420,7 @@
app:layout_constraintBottom_toBottomOf="@+id/BalanceLayout"
app:layout_constraintStart_toEndOf="@+id/BalanceLayout"
app:layout_constraintTop_toTopOf="@+id/BalanceLayout"
app:layout_constraintVertical_bias="0.6"
app:layout_constraintVertical_bias="0.5"
app:tint="@color/lightningOrange" />

<Button
Expand Down
Loading

0 comments on commit c922c37

Please sign in to comment.