Skip to content

Commit

Permalink
#76 [feat] 행복루틴 추가 API 연결 및 뷰페이저 subRoutineId 옵저빙
Browse files Browse the repository at this point in the history
  • Loading branch information
pump9918 committed Jan 17, 2024
1 parent 5ebb953 commit c8b4f8e
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.sopetit.softie.data.entity.request

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class PostMemberHappyRoutineRequest(
@SerialName("subRoutineId")
val subRoutineId: Int
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.sopetit.softie.data.entity.response

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class MemberHappyRoutinePostResponse(
@SerialName("routineId")
val routineId: Int
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.sopetit.softie.data.repositoryImpl

import com.sopetit.softie.data.entity.request.PostMemberHappyRoutineRequest
import com.sopetit.softie.data.source.MemberHappinessRoutineDataSource
import com.sopetit.softie.domain.repository.MemberHappinessRoutineRepository
import javax.inject.Inject

class MemberHappinessRoutineRepositoryImpl @Inject constructor(
private val memberHappinessRoutineDataSource: MemberHappinessRoutineDataSource
) : MemberHappinessRoutineRepository {
override suspend fun postMemberHappinessRoutine(request: PostMemberHappyRoutineRequest): Result<Int> =
runCatching { memberHappinessRoutineDataSource.postMemberHappyRoutine(request) }.map {
requireNotNull(it.data).routineId
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.sopetit.softie.data.service

import com.sopetit.softie.data.entity.BaseResponse
import com.sopetit.softie.data.entity.request.PostMemberHappyRoutineRequest
import com.sopetit.softie.data.entity.response.MemberHappyRoutinePostResponse
import retrofit2.http.Body
import retrofit2.http.POST

interface MemberHappinessRoutineService {
@POST("api/v1/routines/happiness/member")
suspend fun postMemberHappyRoutine(@Body request: PostMemberHappyRoutineRequest): BaseResponse<MemberHappyRoutinePostResponse>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.sopetit.softie.data.source

import com.sopetit.softie.data.entity.BaseResponse
import com.sopetit.softie.data.entity.request.PostMemberHappyRoutineRequest
import com.sopetit.softie.data.entity.response.MemberHappyRoutinePostResponse
import com.sopetit.softie.data.service.MemberHappinessRoutineService
import javax.inject.Inject

class MemberHappinessRoutineDataSource @Inject constructor(
private val memberHappinessRoutineService: MemberHappinessRoutineService
) {
suspend fun postMemberHappyRoutine(request: PostMemberHappyRoutineRequest): BaseResponse<MemberHappyRoutinePostResponse> =
memberHappinessRoutineService.postMemberHappyRoutine(request)
}
8 changes: 8 additions & 0 deletions app/src/main/java/com/sopetit/softie/di/RepositoryModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package com.sopetit.softie.di
import com.sopetit.softie.data.repositoryImpl.DailyRoutineRepositoryImpl
import com.sopetit.softie.data.repositoryImpl.DollRepositoryImpl
import com.sopetit.softie.data.repositoryImpl.HappinessRoutineRepositoryImpl
import com.sopetit.softie.data.repositoryImpl.MemberHappinessRoutineRepositoryImpl
import com.sopetit.softie.data.repositoryImpl.MemberRepositoryImpl
import com.sopetit.softie.domain.repository.DailyRoutineRepository
import com.sopetit.softie.domain.repository.DollRepository
import com.sopetit.softie.domain.repository.HappinessRoutineRepository
import com.sopetit.softie.domain.repository.MemberHappinessRoutineRepository
import com.sopetit.softie.domain.repository.MemberRepository
import dagger.Binds
import dagger.Module
Expand Down Expand Up @@ -40,4 +42,10 @@ abstract class RepositoryModule {
abstract fun bindToHappinessRoutineRepository(
happinessRoutineRepositoryImpl: HappinessRoutineRepositoryImpl
): HappinessRoutineRepository

@Binds
@Singleton
abstract fun bindToMemberHappinessRoutineRepository(
memberHappinessRoutineRepositoryImpl: MemberHappinessRoutineRepositoryImpl
): MemberHappinessRoutineRepository
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.sopetit.softie.di
import com.sopetit.softie.data.service.DailyRoutineService
import com.sopetit.softie.data.service.DollService
import com.sopetit.softie.data.service.HappinessRoutineService
import com.sopetit.softie.data.service.MemberHappinessRoutineService
import com.sopetit.softie.data.service.MemberService
import dagger.Module
import dagger.Provides
Expand All @@ -28,4 +29,8 @@ object RetrofitServiceModule {
@Provides
fun providesDollService(@RetrofitModule.SoftieType retrofit: Retrofit): DollService =
retrofit.create(DollService::class.java)

@Provides
fun providesMemberHappinessRoutineService(@RetrofitModule.SoftieType retrofit: Retrofit): MemberHappinessRoutineService =
retrofit.create(MemberHappinessRoutineService::class.java)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.sopetit.softie.domain.repository

import com.sopetit.softie.data.entity.request.PostMemberHappyRoutineRequest

interface MemberHappinessRoutineRepository {
suspend fun postMemberHappinessRoutine(request: PostMemberHappyRoutineRequest): Result<Int>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.sopetit.softie.domain.usecase

import com.sopetit.softie.data.entity.request.PostMemberHappyRoutineRequest
import com.sopetit.softie.domain.repository.MemberHappinessRoutineRepository
import javax.inject.Inject

class PostMemberHappyRoutineUseCase @Inject constructor(
private val memberHappinessRoutineRepository: MemberHappinessRoutineRepository
) {
suspend operator fun invoke(request: PostMemberHappyRoutineRequest) =
memberHappinessRoutineRepository.postMemberHappinessRoutine(request)
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ import kotlinx.coroutines.launch
class HappyDetailActivity :
BindingActivity<ActivityHappyAddDetailBinding>(R.layout.activity_happy_add_detail) {
private lateinit var viewPager: ViewPager2
private lateinit var happyRoutineAddCardPagerAdapter: HappyDetailCardPagerAdapter

private val happyRoutineAddCardPagerAdapter = HappyDetailCardPagerAdapter()
private val viewModel by viewModels<HappyDetailCardViewModel>()

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -41,8 +40,19 @@ class HappyDetailActivity :
setStatusBarColorFromResource(R.color.background)

val routineId = intent.getIntExtra(ID, -1).toString()
viewModel.getHappyCard(routineId)

setInitBinding(routineId)
setCurrentCard()
setBackEnter()
setupAdapter(routineId)
setIndicator()
initSetLoading()
initViewPager()
initPagerDiv(0, 90)
}

private fun setInitBinding(routineId: String) {
viewModel.getHappyCard(routineId)
viewModel.happyCardResponse.observe(this) { happyCard ->
happyCard?.let {
with(binding) {
Expand All @@ -51,16 +61,29 @@ class HappyDetailActivity :
tvHappyAddDetailSubtitle.text = happyCard.title
tvHappyAddDetailTitle.setTextColor(Color.parseColor(happyCard.nameColor))
}
setBottomSheetEnter(happyCard.iconImageUrl)
viewModel.mySubroutineId.observe(this) { mySubRoutineId ->
mySubRoutineId?.let {
setBottomSheetEnter(happyCard.iconImageUrl, mySubRoutineId)
}
}
}
}
}

initSetLoading()
setBackEnter()
setupAdapter(routineId)
setIndicator()
initViewPager()
initPagerDiv(0, 90)
private fun setCurrentCard() {
viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
super.onPageSelected(position)
val currentSubRoutineId = getCurrentSelectedSubRoutineId()
viewModel.setSubRoutineId(currentSubRoutineId)
}
})
}

private fun getCurrentSelectedSubRoutineId(): Int {
val currentItem = binding.vpHappyAddDetailCard.currentItem
val itemId = happyRoutineAddCardPagerAdapter.getItemId(currentItem)
return itemId.toInt()
}

private fun setBackEnter() {
Expand All @@ -69,13 +92,13 @@ class HappyDetailActivity :
}
}

private fun setBottomSheetEnter(icon: String) {
private fun setBottomSheetEnter(icon: String, subRoutineId: Int) {
binding.btnHappyDetailAdd.setOnClickListener {
initHappyRoutineAddBottomSheet(icon)
initHappyRoutineAddBottomSheet(icon, subRoutineId)
}
}

private fun initHappyRoutineAddBottomSheet(icon: String) {
private fun initHappyRoutineAddBottomSheet(icon: String, subRoutineId: Int) {
BindingBottomSheet.Builder().build(
isDrawable = false,
imageDrawable = 0,
Expand All @@ -90,6 +113,7 @@ class HappyDetailActivity :
backBtnAction = {},
doBtnAction = {
moveToProgress()
viewModel.postAddRoutine(subRoutineId)
}
).show(this.supportFragmentManager, OriginalBottomSheet.BOTTOM_SHEET_TAG)
}
Expand All @@ -104,8 +128,6 @@ class HappyDetailActivity :

private fun setupAdapter(routineId: String) {
with(binding) {
happyRoutineAddCardPagerAdapter =
HappyDetailCardPagerAdapter()
vpHappyAddDetailCard.adapter = happyRoutineAddCardPagerAdapter
}
viewModel.happyCardResponse.observe(this) { happyCard ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ class HappyDetailCardPagerAdapter() :
}
}

override fun getItemId(position: Int): Long {
val itemCount = currentList.size
if (position in 0 until itemCount) {
return currentList[position].subRoutineId.toLong()
} else {
return RecyclerView.NO_ID
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HappyPagerViewHolder {
val binding = ItemHappyAddDetailCardBinding.inflate(
LayoutInflater.from(parent.context),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,32 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.sopetit.softie.data.entity.request.PostMemberHappyRoutineRequest
import com.sopetit.softie.domain.entity.HappyCard
import com.sopetit.softie.domain.usecase.GetHappyCardUseCase
import com.sopetit.softie.domain.usecase.PostMemberHappyRoutineUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject

@HiltViewModel
class HappyDetailCardViewModel @Inject constructor(
private val getHappyCardUseCase: GetHappyCardUseCase
private val getHappyCardUseCase: GetHappyCardUseCase,
private val postMemberHappyRoutineUseCase: PostMemberHappyRoutineUseCase
) : ViewModel() {
private val _happyCardResponse = MutableLiveData<HappyCard>()
val happyCardResponse: LiveData<HappyCard> get() = _happyCardResponse


private val _isPostAddRoutine: MutableLiveData<Boolean> = MutableLiveData()
val isPostAddRoutine: LiveData<Boolean>
get() = _isPostAddRoutine

private val _mySubroutineId: MutableLiveData<Int> = MutableLiveData()
val mySubroutineId: LiveData<Int>
get() = _mySubroutineId

fun getHappyCard(routineId: String) {
viewModelScope.launch {
getHappyCardUseCase(routineId)
Expand All @@ -29,4 +41,23 @@ class HappyDetailCardViewModel @Inject constructor(
}
}
}

fun postAddRoutine(subRoutineId: Int) {
viewModelScope.launch {
postMemberHappyRoutineUseCase.invoke(
PostMemberHappyRoutineRequest(
subRoutineId
)
).onSuccess {
_isPostAddRoutine.value = true
}.onFailure { throwable ->
_isPostAddRoutine.value = false
Timber.e("서버 통신 실패 ${throwable.message}")
}
}
}

fun setSubRoutineId(subRoutineId: Int) {
_mySubroutineId.value = subRoutineId
}
}

0 comments on commit c8b4f8e

Please sign in to comment.