Skip to content

Commit

Permalink
fix: deprecated 된 함수 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
easyhooon committed Apr 23, 2023
1 parent 533038c commit c9cc1a3
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 18 deletions.
2 changes: 2 additions & 0 deletions core/util/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@ dependencies {

implementation libs.realm.sync
implementation libs.coroutines.core

implementation(libs.timber)
}
6 changes: 4 additions & 2 deletions core/util/src/main/java/com/example/util/DiaryHolder.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.util

import android.annotation.SuppressLint
import android.net.Uri
import android.widget.Toast
import androidx.compose.animation.AnimatedVisibility
Expand Down Expand Up @@ -75,7 +76,7 @@ fun DiaryHolder(diary: Diary, onClick: (String) -> Unit) {
.clickable(
indication = null,
interactionSource = remember { MutableInteractionSource() }
) { onClick(diary._id.toString()) })
) { onClick(diary._id.toHexString()) })
{
Spacer(modifier = Modifier.width(14.dp))
Surface(
Expand Down Expand Up @@ -133,6 +134,7 @@ fun DiaryHolder(diary: Diary, onClick: (String) -> Unit) {
}
}

@SuppressLint("NewApi")
@Composable
fun DiaryHeader(
moodName: String,
Expand Down Expand Up @@ -185,7 +187,7 @@ fun ShowGalleryButton(
} else {
stringResource(R.string.show_gallery)
},
style = TextStyle(fontSize = MaterialTheme.typography.bodySmall.fontSize)
style = TextStyle(fontSize = MaterialTheme.typography.bodySmall.fontSize),
)
}
}
Expand Down
21 changes: 19 additions & 2 deletions core/util/src/main/java/com/example/util/Gallery.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,22 @@ import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CornerBasedShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material3.*
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Shapes
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.remember
Expand Down Expand Up @@ -38,6 +49,8 @@ fun Gallery(
spaceBetween: Dp = 10.dp,
imageShape: CornerBasedShape = Shapes().small
) {
// val context = LocalContext.current

// TODO 눈으로 확인했을 때 이미지를 분명 한 칸 더 넣을 수 있을 것 같은데 (5장), 4장 밖에 넣지 못하는 이유 찾기
BoxWithConstraints(modifier = modifier) {
val numberOfVisibleImages = remember {
Expand All @@ -53,6 +66,7 @@ fun Gallery(
)
}
}

val remainingImages = remember {
derivedStateOf {
images.size - numberOfVisibleImages.value
Expand Down Expand Up @@ -82,6 +96,9 @@ fun Gallery(
imagesShape = imageShape,
remainingImages = remainingImages.value,
)
// 여기서 토스트 출력하면 오랫동안 반복 호출되다가 사라짐
// Toast.makeText(context, "${numberOfVisibleImages.value}, ${remainingImages.value}", Toast.LENGTH_SHORT).show()
// Timber.d("GalleryImages", "numberOfVisibleImages = ${numberOfVisibleImages.value}, remainingImages = ${remainingImages.value}")
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions core/util/src/main/java/com/example/util/model/Diary.kt
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
package com.example.util.model

import android.annotation.SuppressLint
import com.example.util.toRealmInstant
import io.realm.kotlin.ext.realmListOf
import io.realm.kotlin.types.ObjectId
import io.realm.kotlin.types.RealmInstant
import io.realm.kotlin.types.RealmList
import io.realm.kotlin.types.RealmObject
import io.realm.kotlin.types.annotations.PrimaryKey
import org.mongodb.kbson.ObjectId
import java.time.Instant

// Mongo DB model schema(model class)
// not support enum class type
open class Diary : RealmObject {
@PrimaryKey
var _id: ObjectId = ObjectId.create() // automatically generated
var _id: ObjectId = ObjectId.invoke() // automatically generated
var ownerId: String = ""
var mood: String = Mood.Neutral.name
var title: String = ""
var description: String = ""
var images: RealmList<String> = realmListOf()
@SuppressLint("NewApi")
var date: RealmInstant = Instant.now().toRealmInstant() // defaultValue
}
14 changes: 7 additions & 7 deletions data/mongo/src/main/java/com/example/mongo/repository/MongoDB.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import io.realm.kotlin.log.LogLevel
import io.realm.kotlin.mongodb.App
import io.realm.kotlin.mongodb.sync.SyncConfiguration
import io.realm.kotlin.query.Sort
import io.realm.kotlin.types.ObjectId
import io.realm.kotlin.types.RealmInstant
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import org.mongodb.kbson.ObjectId
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.ZoneId
Expand All @@ -38,7 +38,7 @@ object MongoDB : MongoRepository {
val config = SyncConfiguration.Builder(user, setOf(Diary::class))
.initialSubscriptions { sub ->
add(
query = sub.query<Diary>(query = "ownerId == $0", user.identity),
query = sub.query<Diary>(query = "ownerId == $0", user.id),
name = "User's Diaries" // optional parameter
)
}
Expand All @@ -52,7 +52,7 @@ object MongoDB : MongoRepository {
override fun getAllDiaries(): Flow<Diaries> {
return if (user != null) {
try {
realm.query<Diary>(query = "ownerId == $0", user.identity)
realm.query<Diary>(query = "ownerId == $0", user.id)
.sort(property = "date", sortOrder = Sort.DESCENDING)
.asFlow()
.map { result ->
Expand All @@ -78,7 +78,7 @@ object MongoDB : MongoRepository {
try {
realm.query<Diary>(
"ownerId == $0 AND date < $1 And date > $2",
user.identity,
user.id,
RealmInstant.from(
LocalDateTime.of(
zonedDateTime.toLocalDate().plusDays(1),
Expand Down Expand Up @@ -128,7 +128,7 @@ object MongoDB : MongoRepository {
return if (user != null) {
realm.write {
try {
val addedDiary = copyToRealm(diary.apply { ownerId = user.identity })
val addedDiary = copyToRealm(diary.apply { ownerId = user.id })
RequestState.Success(data = addedDiary)
} catch (e: Exception) {
RequestState.Error(e)
Expand Down Expand Up @@ -163,7 +163,7 @@ object MongoDB : MongoRepository {
override suspend fun deleteDiary(id: ObjectId): RequestState<Diary> {
return if (user != null) {
realm.write {
val diary = query<Diary>(query = "_id == $0 AND ownerId == $1", id, user.identity)
val diary = query<Diary>(query = "_id == $0 AND ownerId == $1", id, user.id)
.first().find()
if (diary != null) {
try {
Expand All @@ -184,7 +184,7 @@ object MongoDB : MongoRepository {
override suspend fun deleteAllDiaries(): RequestState<Boolean> {
return if (user != null) {
realm.write {
val diaries = this.query<Diary>("ownerId == $0", user.identity).find()
val diaries = this.query<Diary>("ownerId == $0", user.id).find()
try {
delete(diaries)
RequestState.Success(data = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package com.example.mongo.repository

import com.example.util.model.Diary
import com.example.util.model.RequestState
import io.realm.kotlin.types.ObjectId
import kotlinx.coroutines.flow.Flow
import org.mongodb.kbson.ObjectId
import java.time.LocalDate
import java.time.ZonedDateTime

Expand Down
11 changes: 7 additions & 4 deletions feature/write/src/main/java/com/example/write/WriteViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase
import com.google.firebase.storage.FirebaseStorage
import dagger.hilt.android.lifecycle.HiltViewModel
import io.realm.kotlin.types.ObjectId
import io.realm.kotlin.types.RealmInstant
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.mongodb.kbson.ObjectId
import java.time.ZonedDateTime
import javax.inject.Inject

Expand Down Expand Up @@ -72,7 +72,10 @@ internal class WriteViewModel @Inject constructor(
private fun fetchSelectedDiary() {
if (uiState.selectedDiaryId != null) {
viewModelScope.launch {
MongoDB.getSelectedDiary(diaryId = ObjectId.Companion.from(uiState.selectedDiaryId!!))
// ObjectId.from() is deprecated
// Object.invoke() <- 파라미터가 없는 함수는 새로운 id 를 생성함
// Object.invoke(hexString: String) <- 파라미터가 존재하는 경우, hexString 으로 부터 새로운 id 를 생성
MongoDB.getSelectedDiary(diaryId = ObjectId.invoke(uiState.selectedDiaryId!!))
.catch {
emit(RequestState.Error(Exception("Diary is already deleted.")))
}
Expand Down Expand Up @@ -168,7 +171,7 @@ internal class WriteViewModel @Inject constructor(
onError: (String) -> Unit
) {
val result = MongoDB.updateDiary(diary = diary.apply {
_id = ObjectId.from(uiState.selectedDiaryId!!)
_id = ObjectId.invoke(uiState.selectedDiaryId!!)
// update 할때 기존에 등록된 diary 의 date, time 정보가 갱신 되지 않도록
date = if (uiState.updatedDateTime != null) {
uiState.updatedDateTime!!
Expand Down Expand Up @@ -196,7 +199,7 @@ internal class WriteViewModel @Inject constructor(
) {
viewModelScope.launch(Dispatchers.IO) {
if (uiState.selectedDiaryId != null) {
val result = MongoDB.deleteDiary(id = ObjectId.from(uiState.selectedDiaryId!!))
val result = MongoDB.deleteDiary(id = ObjectId.invoke(uiState.selectedDiaryId!!))
if (result is RequestState.Success) {
withContext(Dispatchers.Main) {
uiState.selectedDiary?.let {
Expand Down

0 comments on commit c9cc1a3

Please sign in to comment.