Skip to content
Merged
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
1 change: 0 additions & 1 deletion app/src/main/java/to/bitkit/data/AppStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.serialization.encodeToString
import to.bitkit.di.json
import to.bitkit.models.BalanceState
import to.bitkit.utils.Logger
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/to/bitkit/di/HttpModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ object HttpModule {
fun provideHttpClient(json: Json): HttpClient {
return HttpClient {
install(HttpTimeout) {
requestTimeoutMillis = 60_000 // 30 seconds
connectTimeoutMillis = 30_000 // 10 seconds
socketTimeoutMillis = 30_000 // 10 seconds
requestTimeoutMillis = 60_000
connectTimeoutMillis = 30_000
socketTimeoutMillis = 30_000
}
install(Logging) {
logger = Logger.ANDROID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package to.bitkit.models
import android.content.Context
import android.content.SharedPreferences
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import to.bitkit.data.APP_PREFS
import to.bitkit.di.json
import to.bitkit.utils.Logger
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package to.bitkit.ui.screens.transfer

import android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
import androidx.activity.compose.LocalActivity
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
Expand All @@ -15,6 +17,7 @@ import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand Down Expand Up @@ -51,6 +54,7 @@ fun SavingsProgressScreen(
onContinueClick: () -> Unit = {},
onCloseClick: () -> Unit = {},
) {
val window = LocalActivity.current?.window
val transfer = transferViewModel ?: return
val wallet = walletViewModel ?: return
var progressState by remember { mutableStateOf(SavingsProgressState.PROGRESS) }
Expand All @@ -60,6 +64,8 @@ fun SavingsProgressScreen(
val channelsFailedToCoopClose = transfer.closeSelectedChannels()

if (channelsFailedToCoopClose.isEmpty()) {
window?.clearFlags(FLAG_KEEP_SCREEN_ON)

wallet.refreshState()
delay(5000)
progressState = SavingsProgressState.SUCCESS
Expand All @@ -70,6 +76,14 @@ fun SavingsProgressScreen(
}
}

// Keeps screen on while this view is active
DisposableEffect(Unit) {
window?.addFlags(FLAG_KEEP_SCREEN_ON)
onDispose {
window?.clearFlags(FLAG_KEEP_SCREEN_ON)
}
}

SavingsProgressScreen(
progressState = progressState,
onContinueClick = { onContinueClick() },
Expand Down
35 changes: 35 additions & 0 deletions app/src/main/java/to/bitkit/ui/screens/transfer/SettingUpScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
Expand All @@ -26,7 +28,11 @@ import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.delay
import org.lightningdevkit.ldknode.Network
import to.bitkit.R
import to.bitkit.env.Env
import to.bitkit.ui.appViewModel
import to.bitkit.ui.components.BodyM
import to.bitkit.ui.components.Display
import to.bitkit.ui.components.PrimaryButton
Expand All @@ -38,15 +44,44 @@ import to.bitkit.ui.theme.AppThemeSurface
import to.bitkit.ui.theme.Colors
import to.bitkit.ui.utils.localizedRandom
import to.bitkit.ui.utils.withAccent
import to.bitkit.utils.Logger
import to.bitkit.viewmodels.TransferViewModel
import uniffi.bitkitcore.regtestMine

@Composable
fun SettingUpScreen(
viewModel: TransferViewModel,
onContinueClick: () -> Unit = {},
onCloseClick: () -> Unit = {},
) {
val app = appViewModel ?: return
val lightningSetupStep by viewModel.lightningSetupStep.collectAsState()

LaunchedEffect(Unit) {
Logger.debug("SettingUp view appeared - TransferViewModel is handling order updates")

// Auto-mine a block on regtest after a 5-seconds delay
if (Env.network == Network.REGTEST) {
delay(5000)

try {
Logger.debug("Auto-mining a block", context = "SettingUpScreen")
regtestMine(1u)
Logger.debug("Successfully mined a block", context = "SettingUpScreen")
} catch (e: Throwable) {
Logger.error("Failed to mine block: $e", context = "SettingUpScreen")
}
}
}

// Effect to disable new transaction sheet for channel purchase
DisposableEffect(Unit) {
app.setNewTransactionSheetEnabled(false)
onDispose {
app.setNewTransactionSheetEnabled(true)
}
}

SettingUpScreen(
lightningSetupStep = lightningSetupStep,
onContinueClick = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ fun HomeScreen(
) {
val hasSeenSavingsIntro by appViewModel.hasSeenSavingsIntro.collectAsState()
SpendingWalletScreen(
uiState = uiState,
onAllActivityButtonClick = { rootNavController.navigateToAllActivity() },
onActivityItemClick = { rootNavController.navigateToActivityItem(it) },
onTransferToSavingsClick = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ import to.bitkit.ui.screens.wallets.activity.ActivityListWithHeaders
import to.bitkit.ui.theme.AppThemeSurface
import to.bitkit.ui.theme.Colors
import to.bitkit.ui.utils.withAccent
import to.bitkit.viewmodels.MainUiState

@Composable
fun SpendingWalletScreen(
uiState: MainUiState,
onAllActivityButtonClick: () -> Unit,
onActivityItemClick: (String) -> Unit,
onTransferToSavingsClick: () -> Unit,
Expand All @@ -50,8 +52,11 @@ fun SpendingWalletScreen(
// TODO use && hasLnActivity + LN spendingSats
mutableStateOf(balances.totalLightningSats == 0uL)
}
val canTransfer by remember(balances.totalLightningSats) {
mutableStateOf(balances.totalLightningSats > 0uL)
val canTransfer by remember(balances.totalLightningSats, uiState.channels.size) {
val hasLnBalance = balances.totalLightningSats > 0uL
val hasChannels = uiState.channels.isNotEmpty()

mutableStateOf(hasLnBalance && hasChannels)
}

Box(
Expand Down Expand Up @@ -124,6 +129,7 @@ fun SpendingWalletScreen(
private fun SpendingWalletScreenPreview() {
AppThemeSurface {
SpendingWalletScreen(
uiState = MainUiState(),
onAllActivityButtonClick = {},
onActivityItemClick = {},
onTransferToSavingsClick = {},
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,9 @@ class AppViewModel @Inject constructor(
// endregion

// region TxSheet
var isNewTransactionSheetEnabled = true
private set

var showNewTransaction by mutableStateOf(false)
private set

Expand All @@ -629,7 +632,16 @@ class AppViewModel @Inject constructor(
)
)

fun setNewTransactionSheetEnabled(enabled: Boolean) {
isNewTransactionSheetEnabled = enabled
}

fun showNewTransactionSheet(details: NewTransactionSheetDetails) {
if (!isNewTransactionSheetEnabled) {
Logger.debug("NewTransactionSheet display blocked by isNewTransactionSheetEnabled=false")
return
}

newTransaction = details
showNewTransaction = true
}
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/to/bitkit/viewmodels/TransferViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import kotlin.math.max
import kotlin.math.min
import kotlin.math.roundToLong

const val RETRY_INTERVAL = 5 * 60 * 1000L // 5 minutes in ms
const val GIVE_UP = 30 * 60 * 1000L // 30 minutes in ms
const val RETRY_INTERVAL_MS = 1 * 60 * 1000L // 1 minutes in ms
const val GIVE_UP_MS = 30 * 60 * 1000L // 30 minutes in ms

@HiltViewModel
class TransferViewModel @Inject constructor(
Expand Down Expand Up @@ -300,12 +300,12 @@ class TransferViewModel @Inject constructor(
private var coopCloseRetryJob: Job? = null

/** Retry to coop close the channel(s) for 30 min */
fun startCoopCloseRetries(channels: List<ChannelDetails>, startTime: Long) {
fun startCoopCloseRetries(channels: List<ChannelDetails>, startTimeMs: Long) {
channelsToClose = channels
coopCloseRetryJob?.cancel()

coopCloseRetryJob = viewModelScope.launch {
val giveUpTime = startTime + GIVE_UP
val giveUpTime = startTimeMs + GIVE_UP_MS

while (isActive && System.currentTimeMillis() < giveUpTime) {
Logger.info("Trying coop close...")
Expand All @@ -320,7 +320,7 @@ class TransferViewModel @Inject constructor(
Logger.info("Coop close failed: ${channelsFailedToCoopClose.map { it.channelId }}")
}

delay(RETRY_INTERVAL)
delay(RETRY_INTERVAL_MS)
}

Logger.info("Giving up on coop close.")
Expand Down