Skip to content
Draft
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
4 changes: 3 additions & 1 deletion app/src/main/java/to/bitkit/ui/ContentView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1204,9 +1204,11 @@ private fun NavGraphBuilder.lightningConnections(
composableWithDefaultTransitions<Routes.ChannelDetail> {
val parentEntry = remember(it) { navController.getBackStackEntry(Routes.ConnectionsNav) }
val viewModel = hiltViewModel<LightningConnectionsViewModel>(parentEntry)
val route = it.toRoute<Routes.ChannelDetail>()
ChannelDetailScreen(
navController = navController,
viewModel = viewModel,
channelId = route.channelId,
)
}
composableWithDefaultTransitions<Routes.CloseConnection> {
Expand Down Expand Up @@ -1836,7 +1838,7 @@ sealed interface Routes {
data object LightningConnections : Routes

@Serializable
data object ChannelDetail : Routes
data class ChannelDetail(val channelId: String) : Routes

@Serializable
data object CloseConnection : Routes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.pulltorefresh.PullToRefreshBox
Expand Down Expand Up @@ -85,19 +86,32 @@ import java.util.Locale
fun ChannelDetailScreen(
navController: NavController,
viewModel: LightningConnectionsViewModel,
channelId: String,
) {
val context = LocalContext.current
val app = appViewModel ?: return
val wallet = walletViewModel ?: return

LaunchedEffect(channelId) {
viewModel.findAndSelectChannel(channelId)
}

val selectedChannel by viewModel.selectedChannel.collectAsStateWithLifecycle()
val channel = selectedChannel ?: return
val channel = selectedChannel

val uiState by viewModel.uiState.collectAsStateWithLifecycle()
val paidOrders by viewModel.blocktankRepo.blocktankState.collectAsStateWithLifecycle()
val lightningState by wallet.lightningState.collectAsStateWithLifecycle()

if (channel == null) {
Content(
channel = null,
onBack = { navController.popBackStack() },
)
return
}

val isClosedChannel = uiState.closedChannels.any { it.details.channelId == channel.details.channelId }
val lightningState by wallet.lightningState.collectAsStateWithLifecycle()

// Fetch transaction details for funding transaction if available
LaunchedEffect(channel.details.fundingTxo?.txid) {
Expand All @@ -108,8 +122,8 @@ fun ChannelDetailScreen(

// Fetch activity timestamp for transfer activity with matching channel ID
LaunchedEffect(channel.details.channelId) {
channel.details.channelId?.let { channelId ->
viewModel.fetchActivityTimestamp(channelId)
channel.details.channelId.let { id ->
viewModel.fetchActivityTimestamp(id)
}
}

Expand Down Expand Up @@ -148,7 +162,7 @@ fun ChannelDetailScreen(
@Suppress("CyclomaticComplexMethod")
@Composable
private fun Content(
channel: ChannelUi,
channel: ChannelUi?,
blocktankOrders: List<IBtOrder> = emptyList(),
cjitEntries: List<IcJitEntry> = emptyList(),
txTime: ULong? = null,
Expand All @@ -161,36 +175,46 @@ private fun Content(
onSupport: (Any) -> Unit = {},
onCloseConnection: () -> Unit = {},
) {
// Check if the channel was opened via CJIT
val cjitEntry = cjitEntries.find { entry ->
entry.channel?.fundingTx?.id == channel.details.fundingTxo?.txid
}

// Check if the channel was opened via blocktank order
val blocktankOrder = blocktankOrders.find { order ->
// real channel
if (channel.details.fundingTxo?.txid != null) {
order.channel?.fundingTx?.id == channel.details.fundingTxo?.txid
} else {
// fake channel
order.id == channel.details.channelId
}
}

val order = blocktankOrder ?: cjitEntry

val capacity = channel.details.channelValueSats.toLong()
val localBalance = channel.details.amountOnClose.toLong()
val remoteBalance = (channel.details.inboundCapacityMsat / 1000u).toLong()
val reserveBalance = (channel.details.unspendablePunishmentReserve ?: 0u).toLong()

ScreenColumn {
AppTopBar(
titleText = channel.name,
titleText = channel?.name ?: "",
onBackClick = onBack,
actions = { DrawerNavIcon() },
)

if (channel == null) {
Box(
contentAlignment = Alignment.Center,
modifier = Modifier.fillMaxSize(),
) {
CircularProgressIndicator()
}
return@ScreenColumn
}

// Check if the channel was opened via CJIT
val cjitEntry = cjitEntries.find { entry ->
entry.channel?.fundingTx?.id == channel.details.fundingTxo?.txid
}

// Check if the channel was opened via blocktank order
val blocktankOrder = blocktankOrders.find { order ->
// real channel
if (channel.details.fundingTxo?.txid != null) {
order.channel?.fundingTx?.id == channel.details.fundingTxo?.txid
} else {
// fake channel
order.id == channel.details.channelId
}
}

val order = blocktankOrder ?: cjitEntry

val capacity = channel.details.channelValueSats.toLong()
val localBalance = channel.details.amountOnClose.toLong()
val remoteBalance = (channel.details.inboundCapacityMsat / 1000u).toLong()
val reserveBalance = (channel.details.unspendablePunishmentReserve ?: 0u).toLong()

PullToRefreshBox(
isRefreshing = isRefreshing,
onRefresh = onRefresh,
Expand Down Expand Up @@ -628,6 +652,14 @@ private fun createSupportEmailIntent(
return Intent(Intent.ACTION_SENDTO, uri)
}

@Preview
@Composable
private fun PreviewLoadingState() {
AppThemeSurface {
Content(channel = null)
}
}

@Preview
@Composable
private fun PreviewOpenChannel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,13 @@ fun LightningConnectionsScreen(
viewModel.refreshObservedState()
viewModel.clearSelectedChannel()
viewModel.clearTransactionDetails()
}

LaunchedEffect(navController.currentBackStackEntry) {
val selectedChannelId = navController.previousBackStackEntry?.savedStateHandle?.get<String>("selectedChannelId")
if (selectedChannelId == null) return@LaunchedEffect

navController.previousBackStackEntry?.savedStateHandle?.remove<String>("selectedChannelId")
delay(CHANNEL_SELECTION_DELAY_MS)
if (viewModel.findAndSelectChannel(selectedChannelId)) {
navController.navigate(Routes.ChannelDetail) {
if (selectedChannelId != null) {
navController.previousBackStackEntry?.savedStateHandle?.remove<String>("selectedChannelId")
delay(CHANNEL_SELECTION_DELAY_MS)
navController.navigate(Routes.ChannelDetail(selectedChannelId)) {
launchSingleTop = true
popUpTo(Routes.ConnectionsNav) { inclusive = false }
}
}
}
Expand All @@ -112,8 +107,7 @@ fun LightningConnectionsScreen(
viewModel.zipLogsForSharing { uri -> context.shareZipFile(uri) }
},
onClickChannel = { channelUi ->
viewModel.setSelectedChannel(channelUi)
navController.navigate(Routes.ChannelDetail)
navController.navigate(Routes.ChannelDetail(channelUi.details.channelId))
},
onRefresh = {
viewModel.onPullToRefresh()
Expand Down
Loading