-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiaryHolder.kt
209 lines (201 loc) · 7.72 KB
/
DiaryHolder.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package com.example.util
import android.annotation.SuppressLint
import android.net.Uri
import android.widget.Toast
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.spring
import androidx.compose.animation.expandVertically
import androidx.compose.animation.fadeIn
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.ui.R
import com.example.ui.theme.Elevation
import com.example.util.model.Diary
import com.example.util.model.Mood
import io.realm.kotlin.ext.realmListOf
import java.text.SimpleDateFormat
import java.time.Instant
import java.util.*
// 동적으로 화면 크기를 구현
@Composable
fun DiaryHolder(diary: Diary, onClick: (String) -> Unit) {
// Density 를 제공하며, Dp,Sp,Px 등 서로 단위간에 변환을 쉽게 할 수 있음
val localDensity = LocalDensity.current
val context = LocalContext.current
var componentHeight by remember { mutableStateOf(0.dp) }
var galleryOpened by remember { mutableStateOf(false) }
var galleryLoading by remember { mutableStateOf(false) }
val downloadedImages = remember { mutableStateListOf<Uri>() }
LaunchedEffect(key1 = galleryOpened) {
if (galleryOpened && downloadedImages.isEmpty()) {
galleryLoading = true
fetchImageFromFirebase(
remoteImagePaths = diary.images,
onImageDownload = { image ->
downloadedImages.add(image)
},
onImageDownloadFailed = {
Toast.makeText(
context, "Images not uploaded yet." +
"Wait a little bit, or try uploading again.", Toast.LENGTH_SHORT
).show()
galleryLoading = false
galleryOpened = false
},
onReadyToDisplay = {
galleryLoading = false
galleryOpened = true
}
)
}
}
Row(modifier = Modifier
.clickable(
indication = null,
interactionSource = remember { MutableInteractionSource() }
) { onClick(diary._id.toHexString()) })
{
Spacer(modifier = Modifier.width(14.dp))
Surface(
modifier = Modifier
.width(2.dp)
.height(componentHeight + 14.dp),
//TODO 기존 elevation 과의 차이점 학습
tonalElevation = Elevation.Level1
) {}
Spacer(modifier = Modifier.width(20.dp))
Surface(
modifier = Modifier
.clip(shape = Shapes().medium)
.onGloballyPositioned {
componentHeight = with(localDensity) { it.size.height.toDp() }
},
tonalElevation = Elevation.Level1
) {
Column(modifier = Modifier.fillMaxWidth()) {
DiaryHeader(moodName = diary.mood, time = diary.date.toInstant())
Text(
modifier = Modifier.padding(all = 14.dp),
text = diary.description,
style = TextStyle(fontSize = MaterialTheme.typography.bodyLarge.fontSize),
maxLines = 4,
overflow = TextOverflow.Ellipsis
)
if (diary.images.isNotEmpty()) {
ShowGalleryButton(
galleryOpened = galleryOpened,
galleryLoading = galleryLoading,
onClick = {
galleryOpened = !galleryOpened
}
)
}
// animatedVisibility composable function will be visible
// only if gallery is opened
// add optional spring bouncy animation
AnimatedVisibility(
visible = galleryOpened && !galleryLoading,
enter = fadeIn() + expandVertically(
animationSpec = spring(
dampingRatio = Spring.DampingRatioMediumBouncy,
stiffness = Spring.StiffnessLow
)
)
) {
Column(modifier = Modifier.padding(all = 14.dp)) {
Gallery(images = downloadedImages)
}
}
}
}
}
}
@SuppressLint("NewApi")
@Composable
fun DiaryHeader(
moodName: String,
time: Instant
) {
val mood by remember { mutableStateOf(Mood.valueOf(moodName)) }
Row(
modifier = Modifier
.fillMaxWidth()
.background(mood.containerColor)
.padding(horizontal = 14.dp, vertical = 7.dp),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Row(verticalAlignment = Alignment.CenterVertically) {
Image(
modifier = Modifier.size(18.dp),
painter = painterResource(id = mood.icon),
contentDescription = "Mood Icon"
)
Spacer(modifier = Modifier.width(7.dp))
Text(
text = mood.name,
color = mood.containerColor,
style = TextStyle(fontSize = MaterialTheme.typography.bodyMedium.fontSize)
)
}
Text(
text = SimpleDateFormat("hh:mm a", Locale.US).format(Date.from(time)),
color = mood.contentColor,
style = TextStyle(fontSize = MaterialTheme.typography.bodyMedium.fontSize)
)
}
}
@Composable
fun ShowGalleryButton(
galleryOpened: Boolean,
galleryLoading: Boolean,
onClick: () -> Unit
) {
TextButton(onClick = onClick) {
Text(
text = if (galleryOpened) {
if (galleryLoading) {
stringResource(R.string.loading)
} else {
stringResource(R.string.hide_gallery)
}
} else {
stringResource(R.string.show_gallery)
},
style = TextStyle(fontSize = MaterialTheme.typography.bodySmall.fontSize),
)
}
}
@Composable
@Preview
fun DiaryHolderPreview() {
DiaryHolder(
diary = Diary().apply {
title = "My Diary"
description =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
mood = Mood.Happy.name
images = realmListOf("", "")
},
// empty lambda
onClick = {}
)
}