Skip to content

Commit

Permalink
Merge pull request #57 from dahyun1226/feature/baepo1
Browse files Browse the repository at this point in the history
배포 업데이트 1
  • Loading branch information
dahyun1226 authored Sep 14, 2022
2 parents ca6d627 + e13e4b2 commit edf9541
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import androidx.compose.animation.*
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
Expand Down Expand Up @@ -47,28 +47,30 @@ fun DamgleStoryReactionBox(
.width(320.dp),
contentAlignment = Alignment.Center
) {
var isOne by remember { mutableStateOf(false) }

when (reactions.filter { it.value.count > 0 }.count()) {
0 -> {
isOne = false
EmptyReaction()
}
1 -> {
isOne = true
AnimatedVisibility(
visible = isOne,
enter = fadeIn(initialAlpha = 0.3f),
exit = fadeOut()
) {
SingleReaction(reactions.entries.first().key, reactions.entries.first().value.count)
}
}
else -> {
isOne = false
MultiReaction(modifier = Modifier.fillMaxSize(), reactions)
val count = reactions.filter { it.value.count > 0 }.count()
AnimatedVisibility(
visible = count == 0,
enter = fadeIn(initialAlpha = 0.3f),
exit = fadeOut()
) {
EmptyReaction()
}
AnimatedVisibility(
visible = count == 1,
enter = fadeIn(initialAlpha = 0.3f),
exit = fadeOut()
) {
reactions.entries.firstOrNull()?.let {
SingleReaction(it.key, it.value.count)
}
}
AnimatedVisibility(
visible = count > 1,
enter = fadeIn(initialAlpha = 0.3f),
exit = fadeOut()
) {
MultiReaction(modifier = Modifier.fillMaxSize(), reactions)
}
if (!isMine)
Text(
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
Expand Down Expand Up @@ -62,6 +61,9 @@ fun AllDamgleListSuccessScreen(navController: NavController, viewModel: AllDamgl
item {
Spacer(modifier = Modifier.height(16.dp))
Row(modifier = Modifier.padding(horizontal = 20.dp)) {

val strategy by viewModel.damgleSortStrategy.collectAsState()

DamgleStorySort.values().forEach {
val interactionSource = remember { MutableInteractionSource() }
Text(
Expand All @@ -72,8 +74,8 @@ fun AllDamgleListSuccessScreen(navController: NavController, viewModel: AllDamgl
indication = null
) { viewModel.changeDamgleSortStrategy(it) },
text = it.key,
color = if (viewModel.damgleSortStrategy.value == it) Black else Gray800,
fontWeight = if (viewModel.damgleSortStrategy.value == it) FontWeight.Bold else FontWeight.Normal,
color = if (strategy == it) Black else Gray800,
fontWeight = if (strategy == it) FontWeight.Bold else FontWeight.Normal,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.mashup.damgledamgle.presentation.feature.all_damgle_list

import android.annotation.SuppressLint
import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.mashup.damgledamgle.domain.entity.base.launchWithResult
import com.mashup.damgledamgle.domain.usecase.damgle.*
import com.mashup.damgledamgle.domain.usecase.user.GetUserProfileUserCase
import com.mashup.damgledamgle.domain.usecase.user.GetMyIdUseCase
import com.mashup.damgledamgle.enumerate.*
import com.mashup.damgledamgle.presentation.common.ViewState
import com.mashup.damgledamgle.presentation.common.successOrNull
Expand All @@ -20,30 +21,33 @@ import javax.inject.Inject
@HiltViewModel
class AllDamgleListViewModel @Inject constructor(
private val getDamgleStoryUseCase: GetDamgleStoriesUseCase,
private val getUserProfileUserCase: GetUserProfileUserCase,
private val getMyIdUseCase: GetMyIdUseCase,
private val createDamgleReactionUseCase: CreateDamgleReactionUseCase,
private val deleteDamgleReactionUseCase: DeleteDamgleReactionUseCase,
private val reportDamgleUseCase: ReportDamgleUseCase
) : ViewModel() {

private val myId = MutableStateFlow(-1)

private val _damgleSortStrategy = MutableStateFlow(DamgleStorySort.LATEST)
val damgleSortStrategy: StateFlow<DamgleStorySort> = _damgleSortStrategy

private val _damgleFeedState = MutableStateFlow<ViewState<MutableMap<String, DamgleStoryBoxModel>>>(ViewState.Loading)
val damgleFeedState: StateFlow<ViewState<List<DamgleStoryBoxModel>>> =
_damgleFeedState.combine(_damgleSortStrategy) { stories, strategy ->
combine(_damgleFeedState, _damgleSortStrategy, myId) { stories, strategy, id ->
when (stories) {
is ViewState.Loading -> ViewState.Loading
is ViewState.Error -> stories
is ViewState.Error -> ViewState.Error(stories.error)
is ViewState.Success -> ViewState.Success(
stories.data.values
.toList()
.sortedBy { model ->
when (strategy) {
DamgleStorySort.LATEST -> model.dateTime
DamgleStorySort.POPULAR -> model.reactions.values.sumOf { it.count }.toLong()
DamgleStorySort.LATEST -> -model.dateTime
DamgleStorySort.POPULAR -> -model.reactions.values.sumOf { it.count }.toLong()
}
}
.filter { !it.reports.contains(id) }
)
}
}.stateIn(viewModelScope, SharingStarted.Eagerly, ViewState.Loading)
Expand All @@ -55,6 +59,12 @@ class AllDamgleListViewModel @Inject constructor(
right: Double
) {
viewModelScope.launch {
// TODO 합쳐야함
launchWithResult(
getMyIdUseCase(),
{ id -> myId.emit(id) },
{}
)
launchWithResult(
getDamgleStoryUseCase(top, bottom, left, right),
{ data ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ object DamgleStoryBoxMapper {
}
map
},
reports = entity.reports.map { it.userNo },
myReaction = entity.reactionOfMine?.type?.toReaction(),
reactionBoxState = reactionBoxState
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ data class DamgleStoryBoxModel(
val dateTime: Long,
val content: String,
val reactions: Map<Reaction, DamgleStoryReactionState>,
val reports: List<Int>,
val myReaction: Reaction?,
val reactionBoxState: ReactionBoxState
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class DataStoreRepositoryImpl @Inject constructor(

private const val KEY_ACCESS_TOKEN = "KEY_ACCESS_TOKEN"
private const val KEY_REFRESH_TOKEN = "KEY_REFRESH_TOKEN"
private const val KEY_ID = "KEY_ID"

}

private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = DATASTORE_FILE_NAME)
Expand All @@ -34,6 +36,14 @@ class DataStoreRepositoryImpl @Inject constructor(
setStringPreference(KEY_REFRESH_TOKEN, refreshToken)
}

override suspend fun setId(id: Int) {
return setIntPreference(KEY_ID, id)
}

override suspend fun getId(): Int {
return getIntPreferenceOnce(KEY_ID) ?: -1
}

override suspend fun getAccessToken(): String {
return getStringPreferenceOnce(KEY_ACCESS_TOKEN) ?: ""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ class DamgleRepositoryImpl @Inject constructor(
override suspend fun getMyDamgleList(): Result<List<Damgle>> {
return try {
val resultData = damgleApi.getMyDamgleList()
Result.Success(resultData.stories.map { damgleMapper.mapToEntity(it) })
Result.Success(resultData.stories
.map { damgleMapper.mapToEntity(it) }
// TODO 임시로직
.filter { !it.address1.isNullOrEmpty() })
} catch (e: Exception) {
Result.Error(e)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,33 @@ import javax.inject.Inject

class MapRepositoryImpl @Inject constructor(
private val naverApi: NaverApi,
private val damgleApi : DamgleApi,
private val damgleMapper : DamgleMapper,
private val damgleApi: DamgleApi,
private val damgleMapper: DamgleMapper,
@ApplicationContext private val context: Context
) : MapRepository {
) : MapRepository {

private val prefs =
context.getSharedPreferences(PREFERENCE_FILE_NAME, Context.MODE_PRIVATE)

override fun getLastEntryDamgleDay(): String {
return prefs.getString(LAST_ENTRY_DAY, "") ?: ""
return prefs.getString(LAST_ENTRY_DAY, "") ?: ""
}

override fun setLastEntryDamgleDay(date: String) {
prefs.edit().putString(LAST_ENTRY_DAY, date).apply()
}

override suspend fun getReverseGeocoding(
coors: String): Result<GeoResult> {
coors: String
): Result<GeoResult> {
return try {
val result = naverApi.getReverseGeocoding(
BuildConfig.NAVER_CLIENT_ID,
BuildConfig.NAVER_CLIENT_KEY,
coors,
"roadaddr",
"json")
"json"
)
Result.Success(geocodeMapper(result))
} catch (e: Exception) {
Result.Error(e)
Expand All @@ -61,8 +63,9 @@ class MapRepositoryImpl @Inject constructor(
)
Result.Success(storyFeedResult.stories.map {
damgleMapper.mapToEntity(it)
})
} catch (e : Exception) {
}// TODO 임시로직
.filter { !it.address1.isNullOrEmpty() })
} catch (e: Exception) {
Result.Error(e)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.mashup.damgledamgle.domain.repository

import kotlinx.coroutines.flow.Flow

/**
* DataStoreRepository.kt
*
Expand All @@ -14,15 +12,6 @@ interface DataStoreRepository {
suspend fun getAccessToken(): String
suspend fun setToken(accessToken: String, refreshToken: String)

// fun getIntPreference(key: String): Flow<Int?>
// fun getBooleanPreference(key: String): Flow<Boolean?>
// fun getStringPreference(key: String): Flow<String?>
//
// suspend fun getIntPreferenceOnce(key: String): Int?
// suspend fun getBooleanPreferenceOnce(key: String): Boolean?
// suspend fun getStringPreferenceOnce(key: String): String?
//
// suspend fun setIntPreference(key: String, value: Int)
// suspend fun setBooleanPreference(key: String, value: Boolean)
// suspend fun setStringPreference(key: String, value: String)
suspend fun setId(id: Int)
suspend fun getId(): Int
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.mashup.damgledamgle.domain.usecase.user

import com.mashup.damgledamgle.domain.entity.base.Result
import com.mashup.damgledamgle.domain.repository.DataStoreRepository
import com.mashup.damgledamgle.domain.repository.UserRepository
import javax.inject.Inject

class GetMyIdUseCase @Inject constructor(
private val dataStoreRepository: DataStoreRepository,
private val userRepository: UserRepository
) {
suspend operator fun invoke(): Result<Int> {
val id = dataStoreRepository.getId()
return if (id != -1) {
Result.Success(id)
} else {
val result = userRepository.getUserProfile()

return if (result is Result.Success) {
dataStoreRepository.setId(result.data.userNo)
Result.Success(result.data.userNo)
} else {
result as Result.Error
}
}
}
}

0 comments on commit edf9541

Please sign in to comment.