|
1 | 1 | package to.bitkit.ui.components |
2 | 2 |
|
3 | | -import android.graphics.Bitmap |
4 | | -import androidx.compose.foundation.Image |
5 | 3 | import androidx.compose.foundation.background |
6 | 4 | import androidx.compose.foundation.layout.Box |
7 | 5 | import androidx.compose.foundation.layout.size |
8 | 6 | import androidx.compose.foundation.shape.CircleShape |
9 | 7 | import androidx.compose.material3.CircularProgressIndicator |
10 | 8 | import androidx.compose.material3.Icon |
11 | 9 | import androidx.compose.runtime.Composable |
12 | | -import androidx.compose.runtime.LaunchedEffect |
13 | | -import androidx.compose.runtime.getValue |
14 | 10 | import androidx.compose.ui.Alignment |
15 | 11 | import androidx.compose.ui.Modifier |
16 | 12 | import androidx.compose.ui.draw.clip |
17 | | -import androidx.compose.ui.graphics.asImageBitmap |
18 | 13 | import androidx.compose.ui.layout.ContentScale |
19 | 14 | import androidx.compose.ui.res.painterResource |
20 | 15 | import androidx.compose.ui.unit.Dp |
21 | 16 | import androidx.compose.ui.unit.dp |
22 | | -import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel |
23 | | -import androidx.lifecycle.ViewModel |
24 | | -import androidx.lifecycle.compose.collectAsStateWithLifecycle |
25 | | -import androidx.lifecycle.viewModelScope |
26 | | -import dagger.hilt.android.lifecycle.HiltViewModel |
27 | | -import kotlinx.coroutines.flow.MutableStateFlow |
28 | | -import kotlinx.coroutines.flow.asStateFlow |
29 | | -import kotlinx.coroutines.flow.update |
30 | | -import kotlinx.coroutines.launch |
| 17 | +import coil3.compose.SubcomposeAsyncImage |
31 | 18 | import to.bitkit.R |
32 | | -import to.bitkit.repositories.PubkyRepo |
33 | 19 | import to.bitkit.ui.theme.Colors |
34 | | -import to.bitkit.utils.Logger |
35 | | -import javax.inject.Inject |
36 | | - |
37 | | -private const val TAG = "PubkyImage" |
38 | | - |
39 | | -@HiltViewModel |
40 | | -class PubkyImageViewModel @Inject constructor( |
41 | | - private val pubkyRepo: PubkyRepo, |
42 | | -) : ViewModel() { |
43 | | - |
44 | | - private val _images = MutableStateFlow<Map<String, PubkyImageState>>(emptyMap()) |
45 | | - val images = _images.asStateFlow() |
46 | | - |
47 | | - fun loadImage(uri: String) { |
48 | | - val current = _images.value[uri] |
49 | | - if (current is PubkyImageState.Loaded || current is PubkyImageState.Loading) return |
50 | | - |
51 | | - val cached = pubkyRepo.cachedImage(uri) |
52 | | - if (cached != null) { |
53 | | - _images.update { it + (uri to PubkyImageState.Loaded(cached)) } |
54 | | - return |
55 | | - } |
56 | | - |
57 | | - _images.update { it + (uri to PubkyImageState.Loading) } |
58 | | - viewModelScope.launch { |
59 | | - pubkyRepo.fetchImage(uri) |
60 | | - .onSuccess { bitmap -> |
61 | | - _images.update { it + (uri to PubkyImageState.Loaded(bitmap)) } |
62 | | - } |
63 | | - .onFailure { |
64 | | - Logger.error("Failed to load pubky image '$uri'", it, context = TAG) |
65 | | - _images.update { it + (uri to PubkyImageState.Failed) } |
66 | | - } |
67 | | - } |
68 | | - } |
69 | | -} |
70 | | - |
71 | | -sealed interface PubkyImageState { |
72 | | - data object Loading : PubkyImageState |
73 | | - data class Loaded(val bitmap: Bitmap) : PubkyImageState |
74 | | - data object Failed : PubkyImageState |
75 | | -} |
76 | 20 |
|
77 | 21 | @Composable |
78 | 22 | fun PubkyImage( |
79 | 23 | uri: String, |
80 | 24 | size: Dp, |
81 | 25 | modifier: Modifier = Modifier, |
82 | 26 | ) { |
83 | | - val viewModel: PubkyImageViewModel = hiltViewModel() |
84 | | - val images by viewModel.images.collectAsStateWithLifecycle() |
85 | | - val state = images[uri] |
86 | | - |
87 | | - LaunchedEffect(uri) { |
88 | | - viewModel.loadImage(uri) |
89 | | - } |
90 | | - |
91 | | - Box( |
92 | | - contentAlignment = Alignment.Center, |
93 | | - modifier = modifier |
94 | | - .size(size) |
95 | | - .clip(CircleShape) |
96 | | - ) { |
97 | | - when (state) { |
98 | | - is PubkyImageState.Loaded -> { |
99 | | - Image( |
100 | | - bitmap = state.bitmap.asImageBitmap(), |
101 | | - contentDescription = null, |
102 | | - contentScale = ContentScale.Crop, |
103 | | - modifier = Modifier.matchParentSize() |
104 | | - ) |
105 | | - } |
106 | | - is PubkyImageState.Failed -> { |
107 | | - Box( |
108 | | - contentAlignment = Alignment.Center, |
109 | | - modifier = Modifier |
110 | | - .matchParentSize() |
111 | | - .background(Colors.Gray5, CircleShape) |
112 | | - ) { |
113 | | - Icon( |
114 | | - painter = painterResource(R.drawable.ic_user_square), |
115 | | - contentDescription = null, |
116 | | - tint = Colors.White32, |
117 | | - modifier = Modifier.size(size / 2) |
118 | | - ) |
119 | | - } |
120 | | - } |
121 | | - else -> { |
| 27 | + SubcomposeAsyncImage( |
| 28 | + model = uri, |
| 29 | + contentDescription = null, |
| 30 | + contentScale = ContentScale.Crop, |
| 31 | + loading = { |
| 32 | + Box(contentAlignment = Alignment.Center) { |
122 | 33 | CircularProgressIndicator( |
123 | 34 | strokeWidth = 2.dp, |
124 | 35 | color = Colors.White32, |
125 | | - modifier = Modifier.size(size / 3) |
| 36 | + modifier = Modifier.size(size / 3), |
126 | 37 | ) |
127 | 38 | } |
128 | | - } |
129 | | - } |
| 39 | + }, |
| 40 | + error = { |
| 41 | + Box( |
| 42 | + contentAlignment = Alignment.Center, |
| 43 | + modifier = Modifier |
| 44 | + .matchParentSize() |
| 45 | + .background(Colors.Gray5, CircleShape), |
| 46 | + ) { |
| 47 | + Icon( |
| 48 | + painter = painterResource(R.drawable.ic_user_square), |
| 49 | + contentDescription = null, |
| 50 | + tint = Colors.White32, |
| 51 | + modifier = Modifier.size(size / 2), |
| 52 | + ) |
| 53 | + } |
| 54 | + }, |
| 55 | + modifier = modifier |
| 56 | + .size(size) |
| 57 | + .clip(CircleShape), |
| 58 | + ) |
130 | 59 | } |
0 commit comments