-
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.
- Loading branch information
Siddharth Agarwal
committed
Oct 3, 2023
1 parent
6ac006c
commit 8e30520
Showing
7 changed files
with
122 additions
and
32 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
package org.hisp.dhis.common.screens | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.res.painterResource | ||
import org.hisp.dhis.mobile.ui.designsystem.component.ImageBlock | ||
import java.io.File | ||
|
||
@Composable | ||
fun ImageBlockScreen() { | ||
ImageBlock( | ||
painter = painterResource("image/sample.png"), | ||
file = File("/data/data/org.hisp.dhis.android/files/sample.jpg"), | ||
onClick = {}, | ||
) | ||
} |
16 changes: 16 additions & 0 deletions
16
...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,16 @@ | ||
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 | ||
|
||
internal actual class ImageProvider { | ||
actual fun provideImageFromFile(file: File): ImageBitmap? { | ||
return try { | ||
BitmapFactory.decodeFile(file.absolutePath).asImageBitmap() | ||
} catch (ex: Exception) { | ||
null | ||
} | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
...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,28 @@ | ||
package org.hisp.dhis.mobile.ui.designsystem.component.internal.image | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.State | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.staticCompositionLocalOf | ||
import androidx.compose.ui.graphics.ImageBitmap | ||
import java.io.File | ||
|
||
@Composable | ||
internal fun rememberImageProvider(file: File): State<ImageBitmap?> { | ||
val imageProvider = LocalImageProvider.current | ||
val result = remember(file) { mutableStateOf<ImageBitmap?>(null) } | ||
|
||
LaunchedEffect(file) { | ||
result.value = imageProvider.provideImageFromFile(file) | ||
} | ||
|
||
return result | ||
} | ||
|
||
internal expect class ImageProvider() { | ||
fun provideImageFromFile(file: File): ImageBitmap? | ||
} | ||
|
||
internal val LocalImageProvider = staticCompositionLocalOf { ImageProvider() } |
Binary file not shown.
16 changes: 16 additions & 0 deletions
16
...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,16 @@ | ||
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 | ||
|
||
internal actual class ImageProvider { | ||
actual fun provideImageFromFile(file: File): ImageBitmap? { | ||
return try { | ||
Image.makeFromEncoded(file.readBytes()).toComposeImageBitmap() | ||
} catch (ex: Exception) { | ||
null | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...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,22 @@ | ||
package org.hisp.dhis.mobile.ui.designsystem.component | ||
|
||
import androidx.compose.ui.test.junit4.createComposeRule | ||
import androidx.compose.ui.test.onNodeWithTag | ||
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(file = File("")) {} | ||
} | ||
|
||
rule.onNodeWithTag("IMAGE_BLOCK_CONTAINER").assertDoesNotExist() | ||
} | ||
} |