-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Androapp 5497 mobile UI implement image block (#81)
* add component * add image block component * update component to make more generic * fix lint error * fix image block scale type to crop --------- Co-authored-by: Siddharth Agarwal <[email protected]>
- Loading branch information
Showing
10 changed files
with
180 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
common/src/commonMain/kotlin/org/hisp/dhis/common/screens/ImageBlockScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.hisp.dhis.common.screens | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.graphics.painter.Painter | ||
import org.hisp.dhis.mobile.ui.designsystem.component.ImageBlock | ||
import org.jetbrains.compose.resources.ExperimentalResourceApi | ||
import org.jetbrains.compose.resources.painterResource | ||
|
||
@Composable | ||
fun ImageBlockScreen() { | ||
val sampleImage = provideSampleImage() | ||
ImageBlock( | ||
load = { sampleImage }, | ||
painterFor = { remember { it } }, | ||
onClick = {}, | ||
) | ||
} | ||
|
||
@OptIn(ExperimentalResourceApi::class) | ||
@Composable | ||
private fun provideSampleImage(): Painter = | ||
painterResource("drawable/sample.png") |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions
14
...ain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/internal/image/ImageProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.hisp.dhis.mobile.ui.designsystem.component.internal.image | ||
|
||
import android.graphics.BitmapFactory | ||
import androidx.compose.ui.graphics.ImageBitmap | ||
import androidx.compose.ui.graphics.asImageBitmap | ||
import java.io.File | ||
|
||
actual fun provideImage(file: File): ImageBitmap? { | ||
return try { | ||
BitmapFactory.decodeFile(file.absolutePath).asImageBitmap() | ||
} catch (ex: Exception) { | ||
null | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
...system/src/commonMain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/ImageBlock.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package org.hisp.dhis.mobile.ui.designsystem.component | ||
|
||
import androidx.compose.foundation.Image | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.shape.RoundedCornerShape | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.outlined.FileDownload | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.produceState | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.clip | ||
import androidx.compose.ui.graphics.painter.Painter | ||
import androidx.compose.ui.layout.ContentScale | ||
import androidx.compose.ui.platform.testTag | ||
import androidx.compose.ui.unit.dp | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import org.hisp.dhis.mobile.ui.designsystem.theme.Radius | ||
import org.hisp.dhis.mobile.ui.designsystem.theme.Spacing | ||
import java.io.IOException | ||
|
||
/** | ||
* DHIS2 Image Block. Wraps compose [Image]. | ||
* @param load to load an image stored in the resource, device memory or from network | ||
* we can use loadPainter, loadImageBitmap, loadSvgPainter or loadXmlImageVector | ||
* @param painterFor is a composable function which controls how to paint the load param, | ||
* @param modifier allows a modifier to be passed externally | ||
* @param downloadButtonVisible controls whether the download button is visible or not | ||
* @param onClick is a callback to notify when the download button is clicked. | ||
*/ | ||
@Composable | ||
fun <T> ImageBlock( | ||
load: suspend () -> T, | ||
painterFor: @Composable (T) -> Painter, | ||
modifier: Modifier = Modifier, | ||
downloadButtonVisible: Boolean = true, | ||
onClick: () -> Unit, | ||
) { | ||
val image: T? by produceState<T?>(null) { | ||
value = withContext(Dispatchers.IO) { | ||
try { | ||
load() | ||
} catch (e: IOException) { | ||
null | ||
} | ||
} | ||
} | ||
|
||
if (image != null) { | ||
Box( | ||
modifier = modifier | ||
.padding(vertical = Spacing.Spacing8) | ||
.testTag("IMAGE_BLOCK_CONTAINER"), | ||
) { | ||
Image( | ||
painter = painterFor(image!!), | ||
contentDescription = null, | ||
contentScale = ContentScale.Crop, | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.clip(shape = RoundedCornerShape(Radius.S)) | ||
.height(160.dp), | ||
) | ||
if (downloadButtonVisible) { | ||
SquareIconButton( | ||
enabled = true, | ||
modifier = Modifier | ||
.align(Alignment.BottomEnd) | ||
.padding(Spacing.Spacing4), | ||
icon = { | ||
Icon( | ||
imageVector = Icons.Outlined.FileDownload, | ||
contentDescription = "File download Button", | ||
) | ||
}, | ||
) { | ||
onClick.invoke() | ||
} | ||
} | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...ain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/internal/image/ImageProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package org.hisp.dhis.mobile.ui.designsystem.component.internal.image | ||
|
||
import androidx.compose.ui.graphics.ImageBitmap | ||
import java.io.File | ||
|
||
expect fun provideImage(file: File): ImageBitmap? |
14 changes: 14 additions & 0 deletions
14
...ain/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/internal/image/ImageProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.hisp.dhis.mobile.ui.designsystem.component.internal.image | ||
|
||
import androidx.compose.ui.graphics.ImageBitmap | ||
import androidx.compose.ui.graphics.toComposeImageBitmap | ||
import org.jetbrains.skia.Image | ||
import java.io.File | ||
|
||
actual fun provideImage(file: File): ImageBitmap? { | ||
return try { | ||
Image.makeFromEncoded(file.readBytes()).toComposeImageBitmap() | ||
} catch (ex: Exception) { | ||
null | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...m/src/desktopTest/kotlin/org/hisp/dhis/mobile/ui/designsystem/component/ImageBlockTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.hisp.dhis.mobile.ui.designsystem.component | ||
|
||
import androidx.compose.ui.graphics.painter.BitmapPainter | ||
import androidx.compose.ui.test.junit4.createComposeRule | ||
import androidx.compose.ui.test.onNodeWithTag | ||
import org.hisp.dhis.mobile.ui.designsystem.component.internal.image.provideImage | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import java.io.File | ||
|
||
class ImageBlockTest { | ||
|
||
@get:Rule | ||
val rule = createComposeRule() | ||
|
||
@Test | ||
fun shouldNotRenderImageBlockIfFileIsNotValid() { | ||
rule.setContent { | ||
ImageBlock( | ||
load = { provideImage(File("")) }, | ||
painterFor = { BitmapPainter(it!!) }, | ||
onClick = {}, | ||
) | ||
} | ||
|
||
rule.onNodeWithTag("IMAGE_BLOCK_CONTAINER").assertDoesNotExist() | ||
} | ||
} |