Skip to content

Commit

Permalink
Merge pull request #55 from Team-Shaka/feat/#53-scrap-data
Browse files Browse the repository at this point in the history
[ feat ] scrap field 수정 + livedata -> flow
  • Loading branch information
kimwest00 authored Dec 23, 2023
2 parents ace5c08 + 37069de commit b28ce92
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.dev.briefing.data.datasource

import com.dev.briefing.data.model.response.common.CommonResponse
import com.dev.briefing.data.model.SocialLoginResponse
import com.dev.briefing.data.model.ScrapResponse
import com.dev.briefing.data.model.SetScrapResponse
import com.dev.briefing.data.model.TokenRequest
import com.dev.briefing.data.model.UnScrapResponse

interface ScrapDataSource {
Expand Down
14 changes: 4 additions & 10 deletions app/src/main/java/com/dev/briefing/data/model/Scrap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,8 @@ data class ScrapResponse(
val subtitle: String,
@SerializedName("date")
val date: String,
@SerializedName("gptModel")
val gptModel: String,
@SerializedName("timeOfDay")
val timeOfDay: String,
)
data class ScrapViewData(
@SerializedName("briefingId")
val briefingId: Int,
@SerializedName("ranks")
val ranks: Int,
@SerializedName("title")
val title: String,
@SerializedName("subtitle")
val subtitle: String,
)
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.dev.briefing.data.respository

import com.dev.briefing.data.model.response.common.CommonResponse
import com.dev.briefing.data.model.SocialLoginResponse
import com.dev.briefing.data.model.ScrapResponse
import com.dev.briefing.data.model.SetScrapResponse
import com.dev.briefing.data.model.TokenRequest
import com.dev.briefing.data.model.UnScrapResponse
import com.dev.briefing.data.model.response.common.CommonResponse
import com.dev.briefing.model.Scrap

interface ScrapRepository {
suspend fun getScrap(memberId: Int): CommonResponse<List<ScrapResponse>>

suspend fun getScrap(memberId: Int): List<Scrap>

suspend fun setScrap(memberId: Int, articleId: Long): CommonResponse<SetScrapResponse>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,29 @@ package com.dev.briefing.data.respository

import com.dev.briefing.data.datasource.ScrapDataSource
import com.dev.briefing.data.model.response.common.CommonResponse
import com.dev.briefing.data.model.SocialLoginResponse
import com.dev.briefing.data.model.ScrapResponse
import com.dev.briefing.data.model.SetScrapResponse
import com.dev.briefing.data.model.TokenRequest
import com.dev.briefing.data.model.UnScrapResponse
import com.dev.briefing.model.Scrap
import com.dev.briefing.model.toScrap

class ScrapRepositoryImpl(private val scrapDataSource: ScrapDataSource): ScrapRepository {
override suspend fun getScrap(memberId: Int): CommonResponse<List<ScrapResponse>> {
return scrapDataSource.getScrap(memberId)
override suspend fun getScrap(memberId: Int): List<Scrap> {
val tmpList = mutableListOf<Scrap>()
scrapDataSource.getScrap(memberId).result.forEach {
tmpList.add(it.toScrap())
}
return tmpList
}

override suspend fun setScrap(memberId: Int, articleId: Long): CommonResponse<SetScrapResponse> {
override suspend fun setScrap(
memberId: Int,
articleId: Long
): CommonResponse<SetScrapResponse> {
return scrapDataSource.setScrap(memberId, articleId)
}

override suspend fun unScrap(memberId: Int, articleId: Long): CommonResponse<UnScrapResponse> {
return scrapDataSource.unScrap(memberId, articleId)

}
}
14 changes: 11 additions & 3 deletions app/src/main/java/com/dev/briefing/model/Scrap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,29 @@ data class Scrap(
val ranks: Int,
val title: String,
val subtitle: String,
val date: String
val date: String,
val gptModel: String,
val timeOfDay: String
)

val tmpScrap = Scrap(
briefingId = 1,
title = "title",
subtitle = "subtitle",
ranks = 1,
date = "2023-1-1"
date = "2023-1-1",
gptModel = "gpt-3.5-turbo",
timeOfDay = "Morning"
)

fun ScrapResponse.toScrap(): Scrap {
return Scrap(
briefingId = this.briefingId,
ranks = this.ranks,
title = this.title,
subtitle = this.subtitle,
date = this.date
date = this.date,
gptModel = if (this.gptModel == "gpt-3.5-turbo") "GPT-3" else "GPT-4",
timeOfDay = if (this.timeOfDay == "MORNING") "아침" else "저녁"
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.Divider
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
Expand Down Expand Up @@ -42,7 +42,7 @@ fun ScrapScreen(
) {
val context = LocalContext.current
val viewModel: ScrapViewModel = getViewModel<ScrapViewModel>()
val scrap = viewModel.scrap.observeAsState()
val scrap = viewModel.scrap.collectAsState()

Column(
modifier = modifier
Expand All @@ -57,17 +57,17 @@ fun ScrapScreen(
onBackClick = onBackClick,
color = BriefingTheme.color.BackgroundWhite
)
if (scrap.value.isNullOrEmpty()) {
if (scrap.value.isEmpty()) {
ScrapDefaultScreen()
} else {
LazyColumn(
modifier = Modifier.padding(vertical = 20.dp),
) {
items(scrap.value!!.size){idx->
ScrapItem(scrap = scrap.value!![idx], onItemClick = { id ->
items(scrap.value.size){idx->
ScrapItem(scrap = scrap.value[idx], onItemClick = { id ->
navController.navigate("${HomeScreen.Detail.route}/$id")
})
Divider(color = BriefingTheme.color.SeperatorGray)
HorizontalDivider(color = BriefingTheme.color.SeperatorGray)
}

}
Expand Down Expand Up @@ -106,13 +106,7 @@ fun ScrapDefaultScreen() {
@Composable
fun PreviewScrapItem() {
ScrapItem(
scrap = Scrap(
briefingId = 1,
title = "title",
subtitle = "subtitle",
ranks = 1,
date = "2023-1-1"
),
scrap = tmpScrap,
onItemClick = {}
)
}
Expand All @@ -139,8 +133,7 @@ fun ScrapItem(
horizontalAlignment = Alignment.Start
) {
Text(
//TODO: GPT 모델 번호 field 추가
text = "${scrap.date} | 이슈 #${scrap.ranks} | GPT-3로 생성됨",
text = "${scrap.date} ${scrap.timeOfDay}| 이슈 #${scrap.ranks} | ${scrap.gptModel}로 생성됨",
style = BriefingTheme.typography.DetailStyleRegular.copy(
color = BriefingTheme.color.TextGray
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import com.dev.briefing.model.toScrap
import com.dev.briefing.util.MainApplication
import com.dev.briefing.util.SERVER_TAG
import com.dev.briefing.util.preference.AuthPreferenceHelper
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

class ScrapViewModel(private val repository: ScrapRepository, private val authPreferenceHelper: AuthPreferenceHelper) : ViewModel() {

private val _scrap: MutableLiveData<List<Scrap>> =
MutableLiveData<List<Scrap>>(listOf())
val scrap: LiveData<List<Scrap>>
private val _scrap: MutableStateFlow<List<Scrap>> =
MutableStateFlow(listOf())
val scrap: StateFlow<List<Scrap>>
get() = _scrap

val memberId: Int = authPreferenceHelper.getMemberId()
Expand All @@ -38,20 +40,18 @@ class ScrapViewModel(private val repository: ScrapRepository, private val authPr
val response = repository.getScrap(
memberId = memberId
)
response.result.let {scrapList ->
scrapList.forEach {scrap->
if(_scrap.value == null || _scrap.value!!.isEmpty()){
_scrap.value = listOf(scrap.toScrap())
}else{
_scrap.value = _scrap.value!! + listOf(scrap.toScrap())
}
Log.d(SERVER_TAG, _scrap.value.toString())

response.let { scrapList ->
val tmpScrapList: MutableList<Scrap> = mutableListOf()
scrapList.forEach { scrap ->
tmpScrapList.add(scrap)
}
_scrap.value = tmpScrapList
}
} catch (e: Throwable) {
Log.d(SERVER_TAG, e.toString())
}
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import com.dev.briefing.navigation.HomeScreen
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
Expand All @@ -19,7 +20,6 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavController
import com.dev.briefing.BuildConfig
import com.dev.briefing.R
import com.dev.briefing.navigation.HomeScreen
import com.dev.briefing.presentation.login.SignInActivity
import com.dev.briefing.presentation.login.SignInViewModel
import com.dev.briefing.presentation.setting.component.SettingMenu
Expand Down

0 comments on commit b28ce92

Please sign in to comment.