Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT/#36] 홈 뷰 / 서버통신 구현 #39

Merged
merged 9 commits into from
Jul 11, 2024
6 changes: 6 additions & 0 deletions app/src/main/java/co/orange/ddanzi/di/DataSourceModule.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package co.orange.ddanzi.di

import co.orange.data.dataSource.AuthDataSource
import co.orange.data.dataSource.HomeDataSource
import co.orange.data.dataSourceImpl.AuthDataSourceImpl
import co.orange.data.dataSourceImpl.HomeDataSourceImpl
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand All @@ -14,4 +16,8 @@ object DataSourceModule {
@Provides
@Singleton
fun provideAuthDataSource(authDataSourceImpl: AuthDataSourceImpl): AuthDataSource = authDataSourceImpl

@Provides
@Singleton
fun provideHomeDataSource(homeDataSourceImpl: HomeDataSourceImpl): HomeDataSource = homeDataSourceImpl
}
6 changes: 6 additions & 0 deletions app/src/main/java/co/orange/ddanzi/di/RepositoryModule.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package co.orange.ddanzi.di

import co.orange.data.repositoryImpl.AuthRepositoryImpl
import co.orange.data.repositoryImpl.HomeRepositoryImpl
import co.orange.domain.repository.AuthRepository
import co.orange.domain.repository.HomeRepository
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand All @@ -14,4 +16,8 @@ object RepositoryModule {
@Provides
@Singleton
fun provideAuthRepository(authRepositoryImpl: AuthRepositoryImpl): AuthRepository = authRepositoryImpl

@Provides
@Singleton
fun provideHomeRepository(homeRepositoryImpl: HomeRepositoryImpl): HomeRepository = homeRepositoryImpl
}
8 changes: 8 additions & 0 deletions app/src/main/java/co/orange/ddanzi/di/ServiceModule.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package co.orange.ddanzi.di

import co.orange.data.service.AuthService
import co.orange.data.service.HomeService
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand All @@ -16,4 +17,11 @@ object ServiceModule {
fun provideAuthService(
@RetrofitQualifier.NOTOKEN retrofit: Retrofit,
): AuthService = retrofit.create(AuthService::class.java)

// TODO 추후 수정
@Provides
@Singleton
fun provideHomeService(
@RetrofitQualifier.NOTOKEN retrofit: Retrofit,
): HomeService = retrofit.create(HomeService::class.java)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package co.orange.data.dataSource

import co.orange.data.dto.BaseResponse
import co.orange.data.dto.response.HomeDto

interface HomeDataSource {
suspend fun getHomeData(): BaseResponse<HomeDto>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package co.orange.data.dataSourceImpl

import co.orange.data.dataSource.HomeDataSource
import co.orange.data.dto.BaseResponse
import co.orange.data.dto.response.HomeDto
import co.orange.data.service.HomeService
import javax.inject.Inject

data class HomeDataSourceImpl
@Inject
constructor(
private val homeService: HomeService,
) : HomeDataSource {
override suspend fun getHomeData(): BaseResponse<HomeDto> = homeService.getHomeData()
}
16 changes: 8 additions & 8 deletions data/src/main/java/co/orange/data/dto/BaseResponse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import kotlinx.serialization.Serializable

@Serializable
data class BaseResponse<T>(
@SerialName("success")
val success: Boolean,
@SerialName("response")
val response: T,
@SerialName("code")
val code: String,
@SerialName("message")
val message: String,
@SerialName("status")
val status: Int,
@SerialName("message")
val message: String,
@SerialName("timestamp")
val timestamp: String,
@SerialName("path")
val path: String,
@SerialName("data")
val data: T,
)
14 changes: 0 additions & 14 deletions data/src/main/java/co/orange/data/dto/NonDataBaseResponse.kt

This file was deleted.

15 changes: 15 additions & 0 deletions data/src/main/java/co/orange/data/dto/response/HomeDto.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package co.orange.data.dto.response

import co.orange.domain.entity.response.HomeModel
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class HomeDto(
@SerialName("homeImgUrl")
val homeImgUrl: String,
@SerialName("productList")
val productList: List<ProductDto>,
) {
fun toModel() = HomeModel(homeImgUrl, productList.map { it.toModel() })
}
25 changes: 25 additions & 0 deletions data/src/main/java/co/orange/data/dto/response/ProductDto.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package co.orange.data.dto.response

import co.orange.domain.entity.response.ProductModel
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class ProductDto(
@SerialName("productId")
val productId: String,
@SerialName("kakaoProductId")
val kakaoProductId: Long,
@SerialName("name")
val name: String,
@SerialName("imgUrl")
val imgUrl: String,
@SerialName("originPrice")
val originPrice: Int,
@SerialName("salePrice")
val salePrice: Int,
@SerialName("interestCount")
val interestCount: Int,
) {
fun toModel() = ProductModel(productId, kakaoProductId, name, imgUrl, originPrice, salePrice, interestCount)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ class AuthRepositoryImpl
authDataSource.postReissueTokens(
authorization,
request.toDto(),
).response.toModel()
).data.toModel()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package co.orange.data.repositoryImpl

import co.orange.data.dataSource.HomeDataSource
import co.orange.domain.entity.response.HomeModel
import co.orange.domain.repository.HomeRepository
import javax.inject.Inject

class HomeRepositoryImpl
@Inject
constructor(
private val homeDataSource: HomeDataSource,
) : HomeRepository {
override suspend fun getHomeData(): Result<HomeModel> = runCatching { homeDataSource.getHomeData().data.toModel() }
}
10 changes: 10 additions & 0 deletions data/src/main/java/co/orange/data/service/HomeService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package co.orange.data.service

import co.orange.data.dto.BaseResponse
import co.orange.data.dto.response.HomeDto
import retrofit2.http.GET

interface HomeService {
@GET("/api/v1/home")
suspend fun getHomeData(): BaseResponse<HomeDto>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package co.orange.domain.entity.response

data class HomeModel(
val homeImgUrl: String,
val productList: List<ProductModel>,
)
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package co.orange.domain.entity.response

data class ProductModel(
val productId: Long,
val productId: String,
val kakaoProductId: Long,
val name: String,
val imgUrl: String,
val originPrice: Int,
val salePrice: Int,
val interestCount: Int,
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package co.orange.domain.repository

import co.orange.domain.entity.response.HomeModel

interface HomeRepository {
suspend fun getHomeData(): Result<HomeModel>
}
Binary file modified presentation/src/main/ic_launcher-playstore.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class DetailActivity : BaseActivity<ActivityDetailBinding>(R.layout.activity_det
@JvmStatic
fun createIntent(
context: Context,
productId: Long,
productId: String,
productUrl: String,
originPrice: Int,
salePrice: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class HomeAdapter(
private val likeClick: (Unit) -> (Unit),
) : ListAdapter<ProductModel, RecyclerView.ViewHolder>(diffUtil) {
private var itemList = mutableListOf<ProductModel>()
private var bannerItem: String? = null

override fun onCreateViewHolder(
parent: ViewGroup,
Expand Down Expand Up @@ -52,7 +53,7 @@ class HomeAdapter(
) {
when (holder) {
is HomeBannerViewHolder -> {
holder.onBind()
bannerItem?.let { holder.onBind(it) }
}

is HomeProductViewHolder -> {
Expand All @@ -73,8 +74,8 @@ class HomeAdapter(
else -> VIEW_TYPE_PRODUCT
}

fun addItemList(newItems: List<ProductModel>) {
this.itemList.addAll(newItems)
fun addBannerItem(bannerUrl: String) {
this.bannerItem = bannerUrl
notifyDataSetChanged()
}

Expand All @@ -83,14 +84,6 @@ class HomeAdapter(
notifyDataSetChanged()
}

fun removeItem(position: Int) {
if (this.itemList.isNotEmpty()) {
this.itemList.removeAt(position)
notifyItemRemoved(position + HEADER_COUNT)
notifyItemRangeChanged(position + HEADER_COUNT, itemCount)
}
}

companion object {
private val diffUtil =
ItemDiffCallback<ProductModel>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@ package co.orange.presentation.main.home

import androidx.recyclerview.widget.RecyclerView
import co.orange.core.extension.setOnSingleClickListener
import coil.load
import kr.genti.presentation.databinding.ItemHomeBannerBinding

class HomeBannerViewHolder(
val binding: ItemHomeBannerBinding,
val bannerClick: (Unit) -> (Unit),
) :
RecyclerView.ViewHolder(binding.root) {
fun onBind() {
binding.root.setOnSingleClickListener {
bannerClick
fun onBind(item: String) {
with(binding) {
ivHomeBanner.load(item)
root.setOnSingleClickListener {
bannerClick
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import co.orange.core.base.BaseFragment
import co.orange.core.extension.dpToPx
import co.orange.core.extension.initOnBackPressedListener
import co.orange.core.extension.setOnSingleClickListener
import co.orange.core.extension.stringOf
import co.orange.core.extension.toast
import co.orange.core.state.UiState
import co.orange.domain.entity.response.ProductModel
import co.orange.presentation.auth.login.LoginActivity
import co.orange.presentation.detail.DetailActivity
Expand Down Expand Up @@ -51,8 +54,8 @@ class HomeFragment() : BaseFragment<FragmentHomeBinding>(R.layout.fragment_home)
searchSellProduct()
setGridRecyclerView()
setRecyclerViewDeco()
setItemList()
observeCheckedAgainState()
observeGetHomeDataState()
}

private fun initView() {
Expand Down Expand Up @@ -145,10 +148,6 @@ class HomeFragment() : BaseFragment<FragmentHomeBinding>(R.layout.fragment_home)
)
}

private fun setItemList() {
adapter.setItemList(viewModel.mockItemList)
}

private fun observeCheckedAgainState() {
viewModel.isCheckedAgain.flowWithLifecycle(lifecycle).onEach { isChecked ->
if (isChecked) {
Expand All @@ -158,6 +157,20 @@ class HomeFragment() : BaseFragment<FragmentHomeBinding>(R.layout.fragment_home)
}.launchIn(lifecycleScope)
}

private fun observeGetHomeDataState() {
viewModel.getHomeDataState.flowWithLifecycle(lifecycle).onEach { state ->
when (state) {
is UiState.Success -> {
adapter.addBannerItem(state.data.homeImgUrl)
adapter.setItemList(state.data.productList)
}

is UiState.Failure -> toast(stringOf(R.string.error_msg))
else -> return@onEach
}
}.launchIn(lifecycleScope)
}

override fun onDestroyView() {
super.onDestroyView()
_adapter = null
Expand Down
Loading
Loading