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

#70 / 홈 GET 통신 #71

Merged
merged 8 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/src/main/java/sopt/uni/data/datasource/local/SparkleStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ object SparkleStorage {
get() = pref.getString(REFRESH_TOKEN, null)
set(value) = pref.edit { putString(REFRESH_TOKEN, value).apply() }

var userId: Int?
get() = pref.getInt(USER_ID, -1)
set(value) = pref.edit { putInt(USER_ID, value ?: -1).apply() }

var partnerId: Int?
get() = pref.getInt(PARTNER_ID, -1)
set(value) = pref.edit { putInt(PARTNER_ID, value ?: -1).apply() }

fun clear() {
pref.edit().clear().apply()
}
Expand All @@ -47,3 +55,5 @@ object SparkleStorage {
const val ACCESS_TOKEN = "accessToken"
const val REFRESH_TOKEN = "refreshToken"
const val AUTH = "auth"
const val USER_ID = "userId"
const val PARTNER_ID = "partnerId"
10 changes: 10 additions & 0 deletions app/src/main/java/sopt/uni/data/entity/home/HomeInfo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package sopt.uni.data.entity.home

data class HomeInfo(
val roundGameId: Int? = 0,
val myScore: Int? = 0,
val partnerScore: Int? = 0,
val drawCount: Int? = 0,
val dDay: Int? = 0,
val enable: Boolean? = false,
)

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package sopt.uni.data.repository.home

import sopt.uni.data.source.remote.response.ResponseHomeDto

interface HomeRepository {
suspend fun getHomeInfo(): Result<ResponseHomeDto>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package sopt.uni.data.repository.home

import sopt.uni.data.service.HomeService
import sopt.uni.data.source.remote.response.ResponseHomeDto
import javax.inject.Inject

class HomeRepositoryImpl @Inject constructor(
private val homeService: HomeService,
) : HomeRepository {
override suspend fun getHomeInfo(): Result<ResponseHomeDto> =
kotlin.runCatching {
homeService.getHome()
}.fold({
Result.success(it)
}, {
Result.failure(it)
})
}
13 changes: 0 additions & 13 deletions app/src/main/java/sopt/uni/data/service/ExampleService.kt

This file was deleted.

10 changes: 10 additions & 0 deletions app/src/main/java/sopt/uni/data/service/HomeService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package sopt.uni.data.service

import retrofit2.http.GET
import sopt.uni.data.source.remote.response.ResponseHomeDto

interface HomeService {

@GET("api/home")
suspend fun getHome(): ResponseHomeDto
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package sopt.uni.data.source.remote.response

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import sopt.uni.data.entity.home.HomeInfo

@Serializable
data class ResponseHomeDto(
@SerialName("couple")
val couple: Couple,
@SerialName("dDay")
val dDay: Int,
@SerialName("drawCount")
val drawCount: Int,
@SerialName("myScore")
val myScore: Int,
@SerialName("partnerId")
val partnerId: Int,
@SerialName("partnerScore")
val partnerScore: Int,
@SerialName("roundGameId")
val roundGameId: Int?,
@SerialName("shortGame")
val shortGame: ShortGame?,
@SerialName("userId")
val userId: Int,
) {
@Serializable
data class Couple(
@SerialName("heartToken")
val heartToken: Int,
@SerialName("id")
val id: Int,
@SerialName("startDate")
val startDate: String,
)

@Serializable
data class ShortGame(
@SerialName("enable")
val enable: Boolean,
@SerialName("finishAt")
val finishAt: String?,
@SerialName("id")
val id: Int,
)

fun toHomeInfo() = HomeInfo(
roundGameId = roundGameId,
myScore = myScore,
partnerScore = partnerScore,
drawCount = drawCount,
dDay = dDay,
enable = shortGame?.enable,
)
}
7 changes: 7 additions & 0 deletions app/src/main/java/sopt/uni/di/RepositoryModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import sopt.uni.data.repository.auth.AuthRepository
import sopt.uni.data.repository.auth.AuthRepositoryImpl
import sopt.uni.data.repository.history.HistoryRepository
import sopt.uni.data.repository.history.HistoryRepositoryImpl
import sopt.uni.data.repository.home.HomeRepository
import sopt.uni.data.repository.home.HomeRepositoryImpl
import sopt.uni.data.repository.mypage.MypageRepository
import sopt.uni.data.repository.mypage.MypageRepositoryImpl
import sopt.uni.data.repository.onboarding.OnBoardingRepository
Expand Down Expand Up @@ -43,4 +45,9 @@ object RepositoryModule {
@Singleton
fun providesMyPageRepository(mypageRepository: MypageRepositoryImpl): MypageRepository =
mypageRepository

@Provides
@Singleton
fun providesHomeRepository(homeRepository: HomeRepositoryImpl): HomeRepository =
homeRepository
}
5 changes: 5 additions & 0 deletions app/src/main/java/sopt/uni/di/ServiceModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import dagger.hilt.components.SingletonComponent
import retrofit2.Retrofit
import sopt.uni.data.service.AuthService
import sopt.uni.data.service.HistoryService
import sopt.uni.data.service.HomeService
import sopt.uni.data.service.MyPageService
import sopt.uni.data.service.OnBoardingService
import sopt.uni.data.service.ShortGameService
Expand Down Expand Up @@ -38,4 +39,8 @@ object ServiceModule {
@Provides
@Singleton
fun provideShortGameService(retrofit: Retrofit): ShortGameService = retrofit.create()

@Provides
@Singleton
fun provideHomeService(retrofit: Retrofit): HomeService = retrofit.create()
}
7 changes: 6 additions & 1 deletion app/src/main/java/sopt/uni/presentation/SplashActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import kotlinx.coroutines.launch
import sopt.uni.R
import sopt.uni.data.datasource.local.SparkleStorage
import sopt.uni.databinding.ActivitySplashBinding
import sopt.uni.presentation.home.HomeActivity
import sopt.uni.presentation.invite.NickNameActivity
import sopt.uni.presentation.onboarding.OnBoardingActivity
import sopt.uni.util.binding.BindingActivity
Expand All @@ -22,7 +23,11 @@ class SplashActivity : BindingActivity<ActivitySplashBinding>(R.layout.activity_
lifecycleScope.launch {
delay(2000)
if (SparkleStorage.accessToken != null) {
startActivity<NickNameActivity>()
if (SparkleStorage.partnerId != -1) {
startActivity<HomeActivity>()
} else {
startActivity<NickNameActivity>()
}
} else {
startActivity<OnBoardingActivity>()
}
Expand Down
13 changes: 12 additions & 1 deletion app/src/main/java/sopt/uni/presentation/home/HomeActivity.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package sopt.uni.presentation.home

import android.os.Bundle
import androidx.activity.viewModels
import dagger.hilt.android.AndroidEntryPoint
import sopt.uni.R
import sopt.uni.databinding.ActivityHomeBinding
import sopt.uni.presentation.history.HistoryActivity
import sopt.uni.presentation.mypage.MypageSettingActivity
import sopt.uni.presentation.shortgame.createshortgame.CreateShortGameActivity
import sopt.uni.presentation.wish.WishActivity
import sopt.uni.util.binding.BindingActivity
Expand All @@ -13,13 +15,16 @@ import sopt.uni.util.extension.startActivity

@AndroidEntryPoint
class HomeActivity : BindingActivity<ActivityHomeBinding>(R.layout.activity_home) {
private val homeViewModel by viewModels<HomeViewModel>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)

binding.viewModel = homeViewModel

moveToHistory()
moveToShortGame()
moveToWish()
moveToMyPage()
}

private fun moveToShortGame() {
Expand All @@ -40,4 +45,10 @@ class HomeActivity : BindingActivity<ActivityHomeBinding>(R.layout.activity_home
startActivity<HistoryActivity>()
}
}

private fun moveToMyPage() {
binding.ivProfile.setOnSingleClickListener {
startActivity<MypageSettingActivity>()
}
}
}
38 changes: 38 additions & 0 deletions app/src/main/java/sopt/uni/presentation/home/HomeViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package sopt.uni.presentation.home

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
import sopt.uni.data.datasource.local.SparkleStorage
import sopt.uni.data.entity.home.HomeInfo
import sopt.uni.data.repository.home.HomeRepository
import timber.log.Timber
import javax.inject.Inject

@HiltViewModel
class HomeViewModel @Inject constructor(
private val homeRepository: HomeRepository,
) : ViewModel() {

val homeInfo = MutableStateFlow(HomeInfo())

init {
fetchHomeInfo()
}

fun fetchHomeInfo() {
viewModelScope.launch {
homeRepository.getHomeInfo().onSuccess {
Timber.e(it.toString())
SparkleStorage.userId = it.userId
SparkleStorage.partnerId = it.partnerId
Timber.e("${SparkleStorage.userId} ${SparkleStorage.partnerId}")
homeInfo.value = it.toHomeInfo()
}.onFailure {
Timber.e(it)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ class EnterInviteCodeActivity :
private val enterInviteCodeViewModel by viewModels<EnterInviteCodeViewModel>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

binding.viewModel = enterInviteCodeViewModel

moveToPrevPage()
checkInviteCode()
changeBoxStrokeColor()
Expand All @@ -33,8 +35,10 @@ class EnterInviteCodeActivity :
}

private fun changeBoxStrokeColor() {
if (binding.etInviteCode.text.isNotBlank()) {
if (enterInviteCodeViewModel.inviteCode.value.isNotBlank()) {
binding.etInviteCode.background = getDrawable(R.drawable.bg_mypage_edit_text)
} else {
binding.etInviteCode.background = getDrawable(R.drawable.bg_mypage_edit_text_error)
}
}

Expand All @@ -45,7 +49,8 @@ class EnterInviteCodeActivity :
enterInviteCodeViewModel.connectState.collect { responseCode ->
if (responseCode == "204") {
[email protected](getString(R.string.couple_connect_success))
binding.etInviteCode.background = getDrawable(R.drawable.bg_mypage_edit_text)
binding.etInviteCode.background =
getDrawable(R.drawable.bg_mypage_edit_text)
binding.tvInviteCodeErrorMessage.visibility = View.INVISIBLE
startActivity<HomeActivity>()
finishAffinity()
Expand Down
12 changes: 2 additions & 10 deletions app/src/main/java/sopt/uni/presentation/invite/NickNameActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import android.graphics.Rect
import android.os.Bundle
import android.view.MotionEvent
import android.view.MotionEvent.ACTION_DOWN
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import androidx.activity.viewModels
import dagger.hilt.android.AndroidEntryPoint
import sopt.uni.R
import sopt.uni.databinding.ActivityNicknameBinding
import sopt.uni.presentation.login.LoginActivity
import sopt.uni.util.binding.BindingActivity
import sopt.uni.util.extension.setOnSingleClickListener
import sopt.uni.util.extension.startActivity
Expand All @@ -24,7 +24,6 @@ class NickNameActivity : BindingActivity<ActivityNicknameBinding>(R.layout.activ

binding.viewModel = nickNameViewModel

blockSpaceNickName()
moveToInviteHub()
moveToPrevPage()
}
Expand All @@ -38,18 +37,11 @@ class NickNameActivity : BindingActivity<ActivityNicknameBinding>(R.layout.activ

private fun moveToPrevPage() {
binding.ivBackArrow.setOnSingleClickListener {
startActivity<LoginActivity>()
finish()
}
}

private fun blockSpaceNickName() {
if (nickNameViewModel.nickName.value.filterNot { it.isWhitespace() }.isEmpty()) {
binding.etNickname.background = getDrawable(R.drawable.bg_mypage_edit_text_error)
binding.tvNicknameErrorMessage.text = "공백으로 시작할 수 없어요"
binding.tvNicknameErrorMessage.visibility = View.VISIBLE
}
}

override fun dispatchTouchEvent(event: MotionEvent?): Boolean {
if (event?.action === ACTION_DOWN) {
val v = currentFocus
Expand Down
Loading
Loading