Skip to content

Commit

Permalink
Merge pull request #24 from f-lab-edu/feature/23
Browse files Browse the repository at this point in the history
[#23] 검색화면 Card에 Click Event 추가
  • Loading branch information
ksw4015 authored Nov 12, 2024
2 parents fb9ab57 + dd7ea38 commit 990aa64
Show file tree
Hide file tree
Showing 16 changed files with 137 additions and 109 deletions.
12 changes: 2 additions & 10 deletions app/src/main/java/kr/ksw/visitkorea/data/remote/api/DetailApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,25 @@ import retrofit2.http.Query
interface DetailApi {
@GET("detailCommon1")
suspend fun getDetailCommon(
@Query("numOfRows") numOfRows: Int = 10,
@Query("pageNo") pageNo: Int = 1,
@Query("defaultYN") defaultYN: String = "Y",
@Query("overviewYN") overviewYN: String = "Y",
@Query("contentId") contentId: String
): ApiResponse<DetailCommonDTO>

@GET("detailIntro1")
suspend fun getDetailIntro(
@Query("numOfRows") numOfRows: Int = 10,
@Query("pageNo") pageNo: Int = 1,
@Query("contentId") contentId: String,
@Query("contentTypeId") contentTypeId: String
): ApiResponse<DetailIntroDTO>
/*

@GET("detailInfo1")
suspend fun getDetailInfo(
@Query("numOfRows") numOfRows: Int = 10,
@Query("pageNo") pageNo: Int = 1,
@Query("contentId") contentId: Int,
@Query("contentTypeId") contentTypeId: Int
)
*/

@GET("detailImage1")
suspend fun getDetailImage(
@Query("numOfRows") numOfRows: Int = 10,
@Query("pageNo") pageNo: Int = 1,
@Query("subImageYN") subImageYN: String = "Y",
@Query("imageYN") imageYN: String = "Y",
@Query("contentId") contentId: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package kr.ksw.visitkorea.data.remote.api

import android.util.Log
import kr.ksw.visitkorea.BuildConfig
import okhttp3.Interceptor
import okhttp3.Protocol
import okhttp3.Response
import okhttp3.ResponseBody.Companion.toResponseBody
import okhttp3.internal.http2.ConnectionShutdownException
import java.io.IOException
import java.net.SocketTimeoutException
import java.net.UnknownHostException
import javax.inject.Inject


import kotlin.jvm.Throws

class RetrofitInterceptor @Inject constructor() : Interceptor {

@Throws(Exception::class)
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
val requestUrl = request.url
Expand All @@ -18,12 +26,33 @@ class RetrofitInterceptor @Inject constructor() : Interceptor {
}
}
.build()
Log.d("RetrofitInterceptor", requestUrl.toString())

return chain.proceed(request
val newRequest = request
.newBuilder()
.url(requestUrl)
.build()
)

try {
return chain.proceed(newRequest)
} catch (e: Exception) {
e.printStackTrace()
val msg = when (e) {
is SocketTimeoutException -> "Timeout - Please check your internet connection"
is UnknownHostException -> "Unable to make a connection. Please check your internet"
is ConnectionShutdownException -> "Connection shutdown. Please check your internet"
is IOException -> "Server is unreachable, please try again later."
is IllegalStateException -> "${e.message}"
else -> "${e.message}"
}
return Response.Builder()
.request(newRequest)
.protocol(Protocol.HTTP_1_1)
.code(999)
.message(msg)
.body("{${e}}".toResponseBody(null))
.build()
}
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ fun LocationBasedDTO.toCommonCardModel(): CommonCardModel = CommonCardModel(
address,
firstImage.toImageUrl(),
title,
contentId
dist,
contentId,
contentTypeId
)

fun SearchFestivalDTO.toFestival(): Festival = Festival(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ data class CommonCardModel(
val address: String = "",
val firstImage: String = "",
val title: String = "",
val dist: String? = null,
val contentId: String = "",
)
val contentTypeId: String = "",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package kr.ksw.visitkorea.presentation.core

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.launch

open class BaseViewModel<EFFECT> : ViewModel() {
private val _uiEffect = MutableSharedFlow<EFFECT>(replay = 0)
val uiEffect: SharedFlow<EFFECT>
get() = _uiEffect.asSharedFlow()

protected fun postUIEffect(effect: EFFECT) {
viewModelScope.launch {
_uiEffect.emit(effect)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,6 @@ private fun DetailScreen(
.data(detailState.firstImage)
.size(Size.ORIGINAL)
.build(),
colorFilter = if(detailState.firstImage.isNotEmpty()) {
ColorFilter.tint(Color.LightGray, blendMode = BlendMode.Darken)
}
else {
null
},
contentDescription = "Detail Image",
contentScale = ContentScale.Crop,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ fun FestivalScreen(
val context = LocalContext.current
val hotelState by viewModel.festivalState.collectAsState()
val lazyItem = hotelState.festivalModelFlow.collectAsLazyPagingItems()
LaunchedEffect(viewModel.festivalUiEffect) {
viewModel.festivalUiEffect.collectLatest { effect ->
LaunchedEffect(viewModel.uiEffect) {
viewModel.uiEffect.collectLatest { effect ->
when(effect) {
is FestivalUiEffect.StartDetailActivity -> {
context.startActivity(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,36 @@
package kr.ksw.visitkorea.presentation.festival.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.cachedIn
import androidx.paging.map
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kr.ksw.visitkorea.domain.usecase.festival.GetFestivalListUseCase
import kr.ksw.visitkorea.domain.usecase.mapper.toFestival
import kr.ksw.visitkorea.presentation.common.DetailParcel
import kr.ksw.visitkorea.presentation.home.viewmodel.HomeUiEffect
import kr.ksw.visitkorea.presentation.core.BaseViewModel
import javax.inject.Inject

@HiltViewModel
class FestivalViewModel @Inject constructor(
private val getFestivalListUseCase: GetFestivalListUseCase
) : ViewModel() {
) : BaseViewModel<FestivalUiEffect>() {
private val _festivalState = MutableStateFlow(FestivalState())
val festivalState: StateFlow<FestivalState>
get() = _festivalState.asStateFlow()

private val _festivalUiEffect = MutableSharedFlow<FestivalUiEffect>(replay = 0)
val festivalUiEffect: SharedFlow<FestivalUiEffect>
get() = _festivalUiEffect.asSharedFlow()

init {
getFestivalList()
}

fun onAction(action: FestivalActions) {
when(action) {
is FestivalActions.ClickFestivalCardItem -> {
startDetailActivity(action.data)
postUIEffect(FestivalUiEffect.StartDetailActivity(action.data))
}
}
}
Expand Down Expand Up @@ -67,10 +58,4 @@ class FestivalViewModel @Inject constructor(
}
}
}

private fun startDetailActivity(data: DetailParcel) {
viewModelScope.launch {
_festivalUiEffect.emit(FestivalUiEffect.StartDetailActivity(data))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ fun HomeScreen(
) {
val homeState by homeViewModel.homeState.collectAsState()
val context = LocalContext.current
LaunchedEffect(homeViewModel.homeUiEffect) {
homeViewModel.homeUiEffect.collectLatest { effect ->
LaunchedEffect(homeViewModel.uiEffect) {
homeViewModel.uiEffect.collectLatest { effect ->
when(effect) {
is HomeUiEffect.StartMoreActivity -> {
context.startActivity(Intent(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
package kr.ksw.visitkorea.presentation.home.viewmodel

import android.annotation.SuppressLint
import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.Priority
import com.google.android.gms.tasks.CancellationTokenSource
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kr.ksw.visitkorea.domain.usecase.home.GetCultureCenterForHomeUseCase
import kr.ksw.visitkorea.domain.usecase.home.GetLeisureSportsForHomeUseCase
import kr.ksw.visitkorea.domain.usecase.home.GetRestaurantForHomeUseCase
import kr.ksw.visitkorea.domain.usecase.home.GetTouristSpotForHomeUseCase
import kr.ksw.visitkorea.presentation.common.ContentType
import kr.ksw.visitkorea.presentation.common.DetailParcel
import kr.ksw.visitkorea.presentation.core.BaseViewModel
import javax.inject.Inject

@SuppressLint("MissingPermission")
Expand All @@ -32,15 +26,11 @@ class HomeViewModel @Inject constructor(
private val getLeisureSportsForHomeUseCase: GetLeisureSportsForHomeUseCase,
private val getRestaurantForHomeUseCase: GetRestaurantForHomeUseCase,
private val fusedLocationProviderClient: FusedLocationProviderClient
): ViewModel() {
): BaseViewModel<HomeUiEffect>() {
private val _homeState = MutableStateFlow(HomeState())
val homeState: StateFlow<HomeState>
get() = _homeState.asStateFlow()

private val _homeUiEffect = MutableSharedFlow<HomeUiEffect>(replay = 0)
val homeUiEffect: SharedFlow<HomeUiEffect>
get() = _homeUiEffect.asSharedFlow()

init {
fusedLocationProviderClient.getCurrentLocation(
Priority.PRIORITY_HIGH_ACCURACY,
Expand All @@ -58,10 +48,14 @@ class HomeViewModel @Inject constructor(
fun onAction(action: HomeActions) {
when(action) {
is HomeActions.ClickMoreButton -> {
startMoreActivity(action.contentType)
postUIEffect(
HomeUiEffect.StartMoreActivity(action.contentType)
)
}
is HomeActions.ClickCardItem -> {
startDetailActivity(action.data)
postUIEffect(
HomeUiEffect.StartDetailActivity(action.data)
)
}
}
}
Expand Down Expand Up @@ -142,16 +136,4 @@ class HomeViewModel @Inject constructor(
}
}
}

private fun startMoreActivity(contentType: ContentType) {
viewModelScope.launch {
_homeUiEffect.emit(HomeUiEffect.StartMoreActivity(contentType))
}
}

private fun startDetailActivity(data: DetailParcel) {
viewModelScope.launch {
_homeUiEffect.emit(HomeUiEffect.StartDetailActivity(data))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ fun MoreScreen(
val state = rememberPullToRefreshState()
val context = LocalContext.current

LaunchedEffect(viewModel.moreUiEffect) {
viewModel.moreUiEffect.collectLatest { effect ->
LaunchedEffect(viewModel.uiEffect) {
viewModel.uiEffect.collectLatest { effect ->
when(effect) {
is MoreUiEffect.StartDetailActivity -> {
context.startActivity(Intent(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package kr.ksw.visitkorea.presentation.more.viewmodel

import android.annotation.SuppressLint
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.cachedIn
import androidx.paging.map
Expand All @@ -10,41 +9,36 @@ import com.google.android.gms.location.Priority
import com.google.android.gms.tasks.CancellationTokenSource
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kr.ksw.visitkorea.domain.usecase.mapper.toMoreCardModel
import kr.ksw.visitkorea.domain.usecase.more.GetMoreListUseCase
import kr.ksw.visitkorea.presentation.common.DetailParcel
import kr.ksw.visitkorea.presentation.common.latitudeToStringOrDefault
import kr.ksw.visitkorea.presentation.common.longitudeToStringOrDefault
import kr.ksw.visitkorea.presentation.core.BaseViewModel
import javax.inject.Inject

@SuppressLint("MissingPermission")
@HiltViewModel
class MoreViewModel @Inject constructor(
private val getMoreListUseCase: GetMoreListUseCase,
private val fusedLocationProviderClient: FusedLocationProviderClient,
): ViewModel() {
): BaseViewModel<MoreUiEffect>() {
private val _moreState = MutableStateFlow(MoreState())
val moreState: StateFlow<MoreState>
get() = _moreState.asStateFlow()

private val _moreUiEffect = MutableSharedFlow<MoreUiEffect>(replay = 0)
val moreUiEffect: SharedFlow<MoreUiEffect>
get() = _moreUiEffect.asSharedFlow()

fun onAction(action: MoreActions) {
when(action) {
is MoreActions.ClickCardItem -> {
startDetailActivity(action.data)
postUIEffect(
MoreUiEffect.StartDetailActivity(action.data)
)
}
}
}
Expand Down Expand Up @@ -99,10 +93,4 @@ class MoreViewModel @Inject constructor(
}
}
}

private fun startDetailActivity(data: DetailParcel) {
viewModelScope.launch {
_moreUiEffect.emit(MoreUiEffect.StartDetailActivity(data))
}
}
}
Loading

0 comments on commit 990aa64

Please sign in to comment.