Skip to content

Commit

Permalink
Refactor PostImage
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcoding810 committed Dec 27, 2024
1 parent 7c5f0b1 commit 29e6f8b
Showing 1 changed file with 39 additions and 34 deletions.
73 changes: 39 additions & 34 deletions app/src/main/java/com/paulcoding/hviewer/ui/page/post/PostPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ fun PostPage(appViewModel: AppViewModel, navToWebView: (String) -> Unit, goBack:
val uiState by viewModel.stateFlow.collectAsState()
var selectedImage by remember { mutableStateOf<String?>(null) }
val listState = rememberLazyListState()
val context = LocalContext.current as MainActivity

LaunchedEffect(uiState.error) {
uiState.error?.let {
Expand All @@ -93,39 +92,8 @@ fun PostPage(appViewModel: AppViewModel, navToWebView: (String) -> Unit, goBack:
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
items(uiState.images, key = { it }) { image ->
val showMenu = remember { mutableStateOf(false) }
val menuOffset = remember { mutableStateOf(Pair(0f, 0f)) }

Box(modifier = Modifier.pointerInput(Unit) {
detectTapGestures(
onLongPress = { offset ->
println("pressed $image")
showMenu.value = true
menuOffset.value = Pair(offset.x, offset.y)
},
onTap = {
selectedImage = image
}
)
}) {
HImage(
url = image
)

DropdownMenu(
expanded = showMenu.value,
onDismissRequest = { showMenu.value = false },
) {
DropdownMenuItem(
onClick = {
showMenu.value = false
context.openInBrowser(image)
},
text = {
Text("Open in browser")
}
)
}
PostImage(url = image) {
selectedImage = image
}
}
if (uiState.isLoading)
Expand Down Expand Up @@ -201,4 +169,41 @@ fun ImageModal(url: String, dismiss: () -> Unit) {
}
}
}
}

@Composable
fun PostImage(url: String, onTap: () -> Unit = {}) {
val showMenu = remember { mutableStateOf(false) }
val menuOffset = remember { mutableStateOf(Pair(0f, 0f)) }
val context = LocalContext.current as MainActivity

Box(modifier = Modifier.pointerInput(Unit) {
detectTapGestures(
onLongPress = { offset ->
println("pressed $url")
showMenu.value = true
menuOffset.value = Pair(offset.x, offset.y)
},
onTap = { onTap() }
)
}) {
HImage(
url = url
)

DropdownMenu(
expanded = showMenu.value,
onDismissRequest = { showMenu.value = false },
) {
DropdownMenuItem(
onClick = {
showMenu.value = false
context.openInBrowser(url)
},
text = {
Text("Open in browser")
}
)
}
}
}

0 comments on commit 29e6f8b

Please sign in to comment.