Skip to content

Commit

Permalink
Merge pull request #54 from snuhcs-course/feat/fetch-emoji-list
Browse files Browse the repository at this point in the history
Feat/fetch emoji list
  • Loading branch information
thisisWooyeol authored Nov 16, 2023
2 parents 9f9f179 + 81f574f commit 1145323
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import retrofit2.http.Query
interface EmojiApi {
@GET("emoji")
suspend fun fetchEmojiList(
@Body body: FetchEmojiListDto
@Query("sortByDate") sortByDate: Int,
@Query("index") index: Int,
@Query("count") count: Int
): Response<List<EmojiDto>>

@GET("emoji")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.goliath.emojihub.extensions

fun String.toEmoji(): String {
return String(Character.toChars(this.substring(2).toInt(16)))
return try {
this.trim().split(" ").map { it.removePrefix("U+").toInt(16) }
.joinToString("") { Character.toChars(it).joinToString("") }
} catch (e: Exception){
"\u2764\uFE0F"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.goliath.emojihub.repositories.remote
import android.util.Log
import com.goliath.emojihub.data_sources.api.EmojiApi
import com.goliath.emojihub.models.EmojiDto
import com.goliath.emojihub.models.FetchEmojiListDto
import com.goliath.emojihub.models.UploadEmojiDto
import com.google.gson.Gson
import okhttp3.MediaType
Expand All @@ -29,7 +30,21 @@ class EmojiRepositoryImpl @Inject constructor(
private val emojiApi: EmojiApi
): EmojiRepository {
override suspend fun fetchEmojiList(numLimit: Int): List<EmojiDto> {
TODO("Not yet implemented")
// val fetchEmojiListDto = FetchEmojiListDto(1, 0, 10)
try {
val response = emojiApi.fetchEmojiList(1, 1, 10)

if(response.isSuccessful && response.body() != null) {
Log.d("Fetch_E_L", "Successfully fetched ${response.body()!!.size} emojis")
return response.body()!!
} else {
val errorBody = response.errorBody()?.string() ?: "Unknown error"
Log.d("Fetch_E_L", "Failed to fetch emojis: $errorBody")
}
} catch(e: Exception) {
Log.e("Fetch_E_L", "Error fetching emojis", e)
}
return listOf()
}

override suspend fun getEmojiWithId(id: String): EmojiDto? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ package com.goliath.emojihub.usecases
import android.net.Uri
import android.util.Log
import com.goliath.emojihub.data_sources.ApiErrorController
import com.goliath.emojihub.models.Emoji
import com.goliath.emojihub.models.EmojiDto

import com.goliath.emojihub.models.UploadEmojiDto
import com.goliath.emojihub.repositories.local.X3dRepository
import com.goliath.emojihub.repositories.remote.EmojiRepository
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import java.io.File
import javax.inject.Inject
import javax.inject.Singleton

interface EmojiUseCase {
val emojiListState: StateFlow<List<EmojiDto>>
suspend fun fetchEmojiList(numInt: Int)
fun createEmoji(videoUri: Uri): Pair<String, String>?

Expand All @@ -25,8 +32,19 @@ class EmojiUseCaseImpl @Inject constructor(
private val model: X3dRepository,
private val errorController: ApiErrorController
): EmojiUseCase {

private val _emojiListState = MutableStateFlow<List<EmojiDto>>(emptyList())
override val emojiListState: StateFlow<List<EmojiDto>>
get() = _emojiListState.asStateFlow()

override suspend fun fetchEmojiList(numInt: Int) {
repository.fetchEmojiList(numInt)
try{
val emojiList = repository.fetchEmojiList(numInt)
_emojiListState.emit(emojiList)
Log.d("Fetch_E_L", "USECASE DONE: $emojiList")
} catch (e: Exception) {
errorController.setErrorState(-1)
}
}

override fun createEmoji(videoUri: Uri): Pair<String, String>? {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package com.goliath.emojihub.viewmodels

import android.net.Uri
import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.goliath.emojihub.models.Emoji
import com.goliath.emojihub.models.EmojiDto
import com.goliath.emojihub.usecases.EmojiUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import java.io.File
import javax.inject.Inject

Expand All @@ -19,16 +26,22 @@ class EmojiViewModel @Inject constructor(
var currentEmoji: Emoji? = null
var isBottomSheetShown by mutableStateOf(false)

private val _emojiList = MutableStateFlow<List<Emoji>>(emptyList())
val emojiList: StateFlow<List<Emoji>> = _emojiList.asStateFlow()

fun fetchEmojiList(numInt: Int)
{
viewModelScope.launch {
emojiUseCase.fetchEmojiList(numInt)

val emojis = emojiUseCase.emojiListState.value.map { dto -> Emoji(dto) }
_emojiList.emit(emojis)
Log.d("Fetch_E_L", "VIEWMODEL DONE: $emojis")
}
}

fun createEmoji(videoUri: Uri): Pair<String, String>? {
val (emojiName, emojiUnicode) = emojiUseCase.createEmoji(videoUri)?: return null

val emojiCharArray = emojiUnicode.replace("U+", "")
.split(" ").map {
val codePoint = it.toInt(16)
codePoint.toChar()
}.toCharArray()
val emoji = String(emojiCharArray)
return Pair(emojiName, emoji)
return emojiUseCase.createEmoji(videoUri)
}

suspend fun uploadEmoji(emojiUnicode: String, emojiLabel: String, videoFile: File): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fun BottomNavigationBar(
}

composable(PageItem.Emoji.screenRoute) {
EmojiPage((1..10).map{ createDummyEmoji() })
EmojiPage()
}

composable(PageItem.Profile.screenRoute) {
Expand Down
21 changes: 15 additions & 6 deletions android/app/src/main/java/com/goliath/emojihub/views/EmojiPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import androidx.compose.foundation.layout.height
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
Expand All @@ -41,7 +43,7 @@ import com.goliath.emojihub.views.components.TopNavigationBar

@Composable
fun EmojiPage(
emojiList: List<Emoji>
// emojiList: List<Emoji>
) {
val context = LocalContext.current
val navController = LocalNavController.current
Expand All @@ -62,6 +64,13 @@ fun EmojiPage(
}
}

LaunchedEffect(Unit)
{
viewModel.fetchEmojiList(10)
}

val emojiList = viewModel.emojiList.collectAsState().value

Column(Modifier.background(White)) {
TopNavigationBar("Emoji", shouldNavigate = false) {
IconButton(onClick = {
Expand Down Expand Up @@ -109,8 +118,8 @@ fun EmojiPage(
}
}

@Preview
@Composable
fun EmojiPagePreview() {
EmojiPage(emojiList = (1..10).map { createDummyEmoji() })
}
//@Preview
//@Composable
//fun EmojiPagePreview() {
// EmojiPage(emojiList = (1..10).map { createDummyEmoji() })
//}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import androidx.media3.common.Player
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.ui.PlayerView
import com.goliath.emojihub.LocalNavController
import com.goliath.emojihub.extensions.toEmoji
import com.goliath.emojihub.viewmodels.EmojiViewModel
import com.goliath.emojihub.views.components.CustomDialog
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -130,7 +131,7 @@ fun TransformVideoPage(
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
text = resultEmoji!!.second,
text = resultEmoji!!.second.toEmoji(),
fontSize = 48.sp
)
Text (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.goliath.emojihub.views.components

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -32,6 +33,8 @@ fun EmojiCell (
emoji: Emoji,
onSelected: (Emoji) -> Unit
) {
Log.d("Fetch_E_L", "Emoji passed: ${emoji.id}")
Log.d("Fetch_E_L", "unicode is: ${emoji.unicode}")
Card (
modifier = Modifier.fillMaxWidth().height(292.dp).clickable { onSelected(emoji) },
shape = RoundedCornerShape(4.dp),
Expand Down

0 comments on commit 1145323

Please sign in to comment.