Browse Source

ImageViewer fix simple warnings and code style issues (#3025)

pull/3027/head
dima.avdeev 2 years ago committed by GitHub
parent
commit
52601254ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      examples/imageviewer/shared/src/androidMain/kotlin/example/imageviewer/storage/AndroidImageStorage.kt
  2. 2
      examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/ImageViewer.common.kt
  3. 7
      examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/NameAndDescription.common.kt
  4. 1
      examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/platform.common.kt
  5. 28
      examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/style/Palette.kt
  6. 7
      examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/view/CameraScreen.kt
  7. 6
      examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/view/CameraView.common.kt
  8. 6
      examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/view/MemoryScreen.kt
  9. 13
      examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/view/ScalableImage.common.kt
  10. 7
      examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/view/ThumbnailImage.kt
  11. 1
      examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/view/Tooltip.common.kt
  12. 1
      examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/view/ZoomControllerView.common.kt
  13. 1
      examples/imageviewer/shared/src/desktopMain/kotlin/example/imageviewer/DesktopImageStorage.kt
  14. 3
      examples/imageviewer/shared/src/desktopMain/kotlin/example/imageviewer/platform.desktop.kt
  15. 1
      examples/imageviewer/shared/src/desktopMain/kotlin/example/imageviewer/view/CameraView.desktop.kt
  16. 11
      examples/imageviewer/shared/src/iosMain/kotlin/example/imageviewer/filter/BitmapFilter.ios.kt
  17. 1
      examples/imageviewer/shared/src/iosMain/kotlin/example/imageviewer/main.ios.kt
  18. 1
      examples/imageviewer/shared/src/iosMain/kotlin/example/imageviewer/platform.ios.kt
  19. 10
      examples/imageviewer/shared/src/iosMain/kotlin/example/imageviewer/storage/FileExtensions.kt
  20. 38
      examples/imageviewer/shared/src/iosMain/kotlin/example/imageviewer/storage/IosImageStorage.ios.kt

2
examples/imageviewer/shared/src/androidMain/kotlin/example/imageviewer/storage/AndroidImageStorage.kt

@ -23,8 +23,6 @@ import kotlinx.serialization.json.Json
import org.jetbrains.compose.resources.ExperimentalResourceApi
import org.jetbrains.compose.resources.resource
import java.io.File
import java.util.UUID
private const val maxStorableImageSizePx = 2000
private const val storableThumbnailSizePx = 200

2
examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/ImageViewer.common.kt

@ -67,7 +67,7 @@ fun ImageViewerWithProvidedDependencies(
slideInHorizontally { w -> multiplier * w } with
slideOutHorizontally { w -> multiplier * -1 * w }
}
}) { (index, page) ->
}) { (_, page) ->
when (page) {
is GalleryPage -> {
GalleryScreen(

7
examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/NameAndDescription.common.kt

@ -2,7 +2,12 @@ package example.imageviewer
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import kotlinx.datetime.*
import kotlinx.datetime.Clock
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.Month
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toInstant
import kotlinx.datetime.toLocalDateTime
class NameAndDescription(
val name: String,

1
examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/platform.common.kt

@ -2,7 +2,6 @@ package example.imageviewer
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import example.imageviewer.model.PictureData
import kotlinx.coroutines.CoroutineDispatcher
expect fun Modifier.notchPadding(): Modifier

28
examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/style/Palette.kt

@ -5,46 +5,18 @@ import androidx.compose.material.LocalTextStyle
import androidx.compose.material.MaterialTheme
import androidx.compose.material.ProvideTextStyle
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.sp
object ImageviewerColors {
val Gray = Color.DarkGray
val LightGray = Color(100, 100, 100)
val DarkGray = Color(32, 32, 32)
val PreviewImageAreaHoverColor = Color(45, 45, 45)
val ToastBackground = Color(23, 23, 23)
val MiniatureColor = Color(50, 50, 50)
val MiniatureHoverColor = Color(55, 55, 55)
val Foreground = Color(210, 210, 210)
val TranslucentBlack = Color(0, 0, 0, 60)
val TranslucentWhite = Color(255, 255, 255, 20)
val Transparent = Color.Transparent
val background = Color(0xFFFFFFFF)
val onBackground = Color(0xFF19191C)
val fullScreenImageBackground = Color(0xFF19191C)
val filterButtonsBackground = fullScreenImageBackground.copy(alpha = 0.7f)
val uiLightBlack = Color(25, 25, 28).copy(alpha = 0.7f)
val textOnImage = Color.White
val noteBlockBackground = Color(0xFFF3F3F4)
val KotlinGradient0 = Color(0xFF7F52FF)
val KotlinGradient50 = Color(0xFFC811E2)
val KotlinGradient100 = Color(0xFFE54857)
val kotlinHorizontalGradientBrush = Brush.horizontalGradient(
colors = listOf(
KotlinGradient0,
KotlinGradient50,
KotlinGradient100
)
)
fun buttonBackground(isHover: Boolean) = if (isHover) TranslucentBlack else Transparent
}
@Composable

7
examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/view/CameraScreen.kt

@ -3,7 +3,12 @@ package example.imageviewer.view
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import example.imageviewer.LocalImageProvider

6
examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/view/CameraView.common.kt

@ -2,9 +2,11 @@ package example.imageviewer.view
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import example.imageviewer.ImageStorage
import example.imageviewer.PlatformStorableImage
import example.imageviewer.model.PictureData
@Composable
expect fun CameraView(modifier: Modifier, onCapture: (picture: PictureData.Camera, image: PlatformStorableImage)->Unit)
expect fun CameraView(
modifier: Modifier,
onCapture: (picture: PictureData.Camera, image: PlatformStorableImage) -> Unit
)

6
examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/view/MemoryScreen.kt

@ -37,7 +37,6 @@ import example.imageviewer.isShareFeatureSupported
import example.imageviewer.model.*
import example.imageviewer.shareIcon
import example.imageviewer.style.ImageviewerColors
import org.jetbrains.compose.resources.ExperimentalResourceApi
@Composable
fun MemoryScreen(
@ -197,7 +196,6 @@ private fun MemoryHeader(bitmap: ImageBitmap, picture: PictureData, onClick: ()
}
}
@OptIn(ExperimentalResourceApi::class)
@Composable
fun BoxScope.MagicButtonOverlay(onClick: () -> Unit) {
Column(
@ -248,7 +246,7 @@ fun BoxScope.MemoryTextOverlay(picture: PictureData) {
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun Collapsible(s: String, onEdit: () -> Unit) {
val interctionSource = remember { MutableInteractionSource() }
val interactionSource = remember { MutableInteractionSource() }
var isCollapsed by remember { mutableStateOf(true) }
val text = if (isCollapsed) s.lines().first() + "... (see more)" else s
Text(
@ -265,7 +263,7 @@ fun Collapsible(s: String, onEdit: () -> Unit) {
stiffness = Spring.StiffnessLow
)
).combinedClickable(
interactionSource = interctionSource, indication = null,
interactionSource = interactionSource, indication = null,
onClick = {
isCollapsed = !isCollapsed
},

13
examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/view/ScalableImage.common.kt

@ -5,7 +5,8 @@ import androidx.compose.foundation.gestures.detectTransformGestures
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.BoxWithConstraintsScope
import androidx.compose.runtime.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.geometry.Offset
@ -44,8 +45,14 @@ fun ScalableImage(scalableState: ScalableState, image: ImageBitmap, modifier: Mo
drawIntoCanvas {
it.withSave {
it.translate(areaCenter.x, areaCenter.y)
it.translate(scalableState.transformation.offset.x, scalableState.transformation.offset.y)
it.scale(scalableState.transformation.scale, scalableState.transformation.scale)
it.translate(
scalableState.transformation.offset.x,
scalableState.transformation.offset.y
)
it.scale(
scalableState.transformation.scale,
scalableState.transformation.scale
)
it.translate(-imageCenter.x, -imageCenter.y)
drawImage(image)
}

7
examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/view/ThumbnailImage.kt

@ -1,7 +1,12 @@
package example.imageviewer.view
import androidx.compose.foundation.Image
import androidx.compose.runtime.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.layout.ContentScale

1
examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/view/Tooltip.common.kt

@ -1,7 +1,6 @@
package example.imageviewer.view
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
@Composable
expect fun Tooltip(

1
examples/imageviewer/shared/src/commonMain/kotlin/example/imageviewer/view/ZoomControllerView.common.kt

@ -1,6 +1,5 @@
package example.imageviewer.view
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import example.imageviewer.model.ScalableState

1
examples/imageviewer/shared/src/desktopMain/kotlin/example/imageviewer/DesktopImageStorage.kt

@ -2,7 +2,6 @@ package example.imageviewer
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.toAwtImage
import androidx.compose.ui.graphics.toComposeImageBitmap
import example.imageviewer.filter.scaleBitmapAspectRatio

3
examples/imageviewer/shared/src/desktopMain/kotlin/example/imageviewer/platform.desktop.kt

@ -7,9 +7,8 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.unit.dp
import example.imageviewer.model.PictureData
import kotlinx.coroutines.Dispatchers
import java.util.*
import java.util.UUID
actual fun Modifier.notchPadding(): Modifier = Modifier.padding(top = 12.dp)

1
examples/imageviewer/shared/src/desktopMain/kotlin/example/imageviewer/view/CameraView.desktop.kt

@ -6,7 +6,6 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember

11
examples/imageviewer/shared/src/iosMain/kotlin/example/imageviewer/filter/BitmapFilter.ios.kt

@ -4,7 +4,16 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asComposeImageBitmap
import androidx.compose.ui.graphics.asSkiaBitmap
import org.jetbrains.skia.*
import org.jetbrains.skia.Bitmap
import org.jetbrains.skia.Canvas
import org.jetbrains.skia.ColorAlphaType
import org.jetbrains.skia.ColorInfo
import org.jetbrains.skia.ColorType
import org.jetbrains.skia.FilterTileMode
import org.jetbrains.skia.Image
import org.jetbrains.skia.ImageFilter
import org.jetbrains.skia.ImageInfo
import org.jetbrains.skia.Paint
actual fun grayScaleFilter(bitmap: ImageBitmap, context: PlatformContext): ImageBitmap {
return applyGrayScaleFilter(bitmap.asSkiaBitmap()).asComposeImageBitmap()

1
examples/imageviewer/shared/src/iosMain/kotlin/example/imageviewer/main.ios.kt

@ -3,6 +3,7 @@ package example.imageviewer
import androidx.compose.ui.window.ComposeUIViewController
import platform.UIKit.UIViewController
@Suppress("FunctionName", "unused")
fun MainViewController(): UIViewController =
ComposeUIViewController {
ImageViewerIos()

1
examples/imageviewer/shared/src/iosMain/kotlin/example/imageviewer/platform.ios.kt

@ -15,7 +15,6 @@ import platform.CoreFoundation.CFUUIDCreateString
import platform.Foundation.CFBridgingRelease
import platform.UIKit.UIApplication
import platform.UIKit.UIImage
import platform.UIKit.safeAreaInsets
private val iosNotchInset = object : WindowInsets {
override fun getTop(density: Density): Int {

10
examples/imageviewer/shared/src/iosMain/kotlin/example/imageviewer/storage/FileExtensions.kt

@ -3,18 +3,16 @@ package example.imageviewer.storage
import kotlinx.cinterop.*
import kotlinx.coroutines.yield
import platform.Foundation.*
import platform.UIKit.UIImage
import platform.UIKit.UIImageJPEGRepresentation
import platform.posix.memcpy
val NSFileManager.DocumentDirectory get() = URLForDirectory(
val NSFileManager.DocumentDirectory
get() = URLForDirectory(
directory = NSDocumentDirectory,
inDomain = NSUserDomainMask,
create = true,
appropriateForURL = null,
error = null
)!!
)!!
// Mimic to java's File class
@Suppress("FunctionName")
@ -78,5 +76,3 @@ fun NSURL.writeText(text: String) {
error = null
)
}

38
examples/imageviewer/shared/src/iosMain/kotlin/example/imageviewer/storage/IosImageStorage.ios.kt

@ -2,26 +2,33 @@ package example.imageviewer.storage
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.toComposeImageBitmap
import example.imageviewer.ImageStorage
import example.imageviewer.PlatformStorableImage
import example.imageviewer.model.PictureData
import example.imageviewer.toImageBitmap
import kotlinx.cinterop.CValue
import kotlinx.cinterop.addressOf
import kotlinx.cinterop.useContents
import kotlinx.cinterop.usePinned
import kotlinx.coroutines.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.jetbrains.skia.Image
import platform.CoreGraphics.CGRectMake
import platform.CoreGraphics.CGSize
import platform.CoreGraphics.CGSizeMake
import platform.Foundation.*
import platform.UIKit.*
import platform.posix.memcpy
import platform.Foundation.NSBundle
import platform.Foundation.NSData
import platform.Foundation.NSFileManager
import platform.Foundation.NSURL
import platform.Foundation.writeToURL
import platform.UIKit.UIGraphicsBeginImageContextWithOptions
import platform.UIKit.UIGraphicsEndImageContext
import platform.UIKit.UIGraphicsGetImageFromCurrentImageContext
import platform.UIKit.UIImage
import platform.UIKit.UIImageJPEGRepresentation
private const val maxStorableImageSizePx = 1200
private const val storableThumbnailSizePx = 180
@ -31,11 +38,18 @@ class IosImageStorage(
private val pictures: SnapshotStateList<PictureData>,
private val ioScope: CoroutineScope
) : ImageStorage {
private val savePictureDir = File(NSFileManager.defaultManager.DocumentDirectory, "ImageViewer/takenPhotos/")
private val PictureData.Camera.jpgFile get() = File(savePictureDir, "$id.jpg")
private val PictureData.Camera.thumbnailJpgFile get() = File(savePictureDir, "$id-thumbnail.jpg")
private val PictureData.Camera.jsonFile get() = File(savePictureDir, "$id.json")
private val savePictureDir =
File(NSFileManager.defaultManager.DocumentDirectory, "ImageViewer/takenPhotos/")
private val PictureData.Camera.jpgFile
get() = File(savePictureDir, "$id.jpg")
private val PictureData.Camera.thumbnailJpgFile
get() = File(savePictureDir, "$id-thumbnail.jpg")
private val PictureData.Camera.jsonFile
get() = File(savePictureDir, "$id.json")
init {
if (savePictureDir.isDirectory) {

Loading…
Cancel
Save