Skip to content

Commit

Permalink
Merge pull request #718 from KotlinGeekDev/aa/feature/hide-balance
Browse files Browse the repository at this point in the history
Privacy feature: Hide Balance.
  • Loading branch information
tomastiminskas authored Feb 20, 2024
2 parents 98a5ae1 + ad815be commit 1b2fad6
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package chat.sphinx.wrapper_common

object HideBalance {

const val ENABLED = 1
const val DISABLED = 0

const val HIDE_BALANCE_SHARED_PREFERENCES = "general_settings"
const val HIDE_BALANCE_ENABLED_KEY = "hide-balance-enabled"

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.motion.widget.MotionLayout
Expand Down Expand Up @@ -34,8 +35,10 @@ import chat.sphinx.kotlin_response.Response
import chat.sphinx.menu_bottom_scanner.BottomScannerMenu
import chat.sphinx.resources.databinding.LayoutPodcastPlayerFooterBinding
import chat.sphinx.swipe_reveal_layout.SwipeRevealLayout
import chat.sphinx.wrapper_common.HideBalance
import chat.sphinx.wrapper_common.chat.PushNotificationLink
import chat.sphinx.wrapper_common.lightning.*
import chat.sphinx.wrapper_lightning.NodeBalance
import chat.sphinx.wrapper_view.Px
import com.google.android.material.tabs.TabLayoutMediator
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -47,6 +50,7 @@ import io.matthewnelson.concept_views.viewstate.collect
import io.matthewnelson.concept_views.viewstate.value
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.launch
import javax.inject.Inject

Expand Down Expand Up @@ -277,6 +281,10 @@ internal class DashboardFragment : MotionLayoutFragment<
header.textViewDashboardHeaderNetwork.setOnClickListener {
viewModel.toastIfNetworkConnected()
}

header.textViewDashboardHeaderBalance.setOnClickListener {
viewModel.toggleHideBalanceState()
}
}
}

Expand Down Expand Up @@ -372,6 +380,10 @@ internal class DashboardFragment : MotionLayoutFragment<

navDrawer.layoutConstraintDashboardNavDrawer.setOnClickListener { viewModel }

navDrawer.navDrawerTextViewSatsBalance.setOnClickListener {
viewModel.toggleHideBalanceState()
}

navDrawer.navDrawerButtonContacts.setOnClickListener {
lifecycleScope.launch { viewModel.navDrawerNavigator.toAddressBookScreen() }
}
Expand Down Expand Up @@ -479,12 +491,38 @@ internal class DashboardFragment : MotionLayoutFragment<
}

onStopSupervisor.scope.launch(viewModel.mainImmediate) {
viewModel.getAccountBalance().collect { nodeBalance ->
if (nodeBalance == null) return@collect

nodeBalance.balance.asFormattedString().let { balance ->
binding.layoutDashboardHeader.textViewDashboardHeaderBalance.text = balance
binding.layoutDashboardNavDrawer.navDrawerTextViewSatsBalance.text = balance
combine(
viewModel.hideBalanceStateFlow,
viewModel.getAccountBalance()
) { hideBalanceState: Int, nodeBalance: NodeBalance? ->
BalanceState(nodeBalance, hideBalanceState)
}.collect { balanceState ->

Log.d(
"DashboardFrag",
"onStart: ShouldHide Balance: ${balanceState.hideBalanceState == HideBalance.ENABLED}")
if (balanceState.nodeBalance == null) return@collect
balanceState.nodeBalance.balance.asFormattedString().let { balance ->
with(binding.layoutDashboardHeader.textViewDashboardHeaderBalance){
if (balanceState.hideBalanceState == HideBalance.ENABLED){
layoutParams.height = resources.getDimensionPixelSize(R.dimen.hidden_balance_placeholder)
text = "*****"
}
else {
layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT
text = balance
}
}
with(binding.layoutDashboardNavDrawer.navDrawerTextViewSatsBalance){
if (balanceState.hideBalanceState == HideBalance.ENABLED){
layoutParams.height = resources.getDimensionPixelSize(R.dimen.hidden_balance_placeholder)
text = "*****"
}
else {
layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT
text = balance
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import chat.sphinx.dashboard.navigation.DashboardNavigator
import chat.sphinx.dashboard.ui.viewstates.*
import chat.sphinx.kotlin_response.*
import chat.sphinx.logger.SphinxLogger
import chat.sphinx.logger.d
import chat.sphinx.menu_bottom.ui.MenuBottomViewState
import chat.sphinx.menu_bottom_scanner.ScannerMenuHandler
import chat.sphinx.menu_bottom_scanner.ScannerMenuViewModel
Expand Down Expand Up @@ -70,14 +69,16 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import io.matthewnelson.android_feature_navigation.util.navArgs
import io.matthewnelson.android_feature_viewmodel.MotionLayoutViewModel
import io.matthewnelson.android_feature_viewmodel.submitSideEffect
import io.matthewnelson.android_feature_viewmodel.updateViewState
import io.matthewnelson.build_config.BuildConfigVersionCode
import io.matthewnelson.concept_coroutines.CoroutineDispatchers
import io.matthewnelson.concept_views.viewstate.ViewStateContainer
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import javax.inject.Inject


data class BalanceState(val nodeBalance: NodeBalance?, val hideBalanceState: Int)

@HiltViewModel
internal class DashboardViewModel @Inject constructor(
private val app: Application,
Expand Down Expand Up @@ -146,6 +147,13 @@ internal class DashboardViewModel @Inject constructor(
val networkStateFlow: StateFlow<Pair<LoadResponse<Boolean, ResponseError>, Boolean>>
get() = _networkStateFlow.asStateFlow()

private val _hideBalanceStateFlow: MutableStateFlow<Int> by lazy {
MutableStateFlow(HideBalance.DISABLED)
}

val hideBalanceStateFlow: StateFlow<Int>
get() = _hideBalanceStateFlow.asStateFlow()


private lateinit var signerManager: SignerManager

Expand All @@ -156,6 +164,8 @@ internal class DashboardViewModel @Inject constructor(
}
}

getHideBalanceState()

syncFeedRecommendationsState()

getRelayKeys()
Expand All @@ -167,6 +177,42 @@ internal class DashboardViewModel @Inject constructor(

networkRefresh(true)
}

private fun getHideBalanceState() {
viewModelScope.launch(mainImmediate) {
val appContext: Context = app.applicationContext
val hideSharedPreferences = appContext.getSharedPreferences(HideBalance.HIDE_BALANCE_SHARED_PREFERENCES, Context.MODE_PRIVATE)

val shouldHide = hideSharedPreferences.getInt(HideBalance.HIDE_BALANCE_ENABLED_KEY, HideBalance.DISABLED)
_hideBalanceStateFlow.value = shouldHide
}
}

fun toggleHideBalanceState(){
viewModelScope.launch(mainImmediate) {
val newState = if(_hideBalanceStateFlow.value == HideBalance.DISABLED){
HideBalance.ENABLED
} else {
HideBalance.DISABLED
}
_hideBalanceStateFlow.value = newState

delay(50L)

val appContext: Context = app.applicationContext
val hideSharedPreferences = appContext.getSharedPreferences(HideBalance.HIDE_BALANCE_SHARED_PREFERENCES, Context.MODE_PRIVATE)

withContext(dispatchers.io) {
hideSharedPreferences.edit()
.putInt(HideBalance.HIDE_BALANCE_ENABLED_KEY, newState)
.let { editor ->
if (!editor.commit()) {
editor.apply()
}
}
}
}
}

private fun getRelayKeys() {
repositoryDashboard.getAndSaveTransportKey(forceGet = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
<dimen name="dashboard_chip_height">38dp</dimen>
<dimen name="dashboard_chip_vertical_spacing">22dp</dimen>
<dimen name="dashboard_chip_spacing">5dp</dimen>
<dimen name="hidden_balance_placeholder">16dp</dimen>
</resources>

0 comments on commit 1b2fad6

Please sign in to comment.