Skip to content

Commit

Permalink
Merge pull request #69 from Team-Umbba/feat/#63
Browse files Browse the repository at this point in the history
[Feat/#63] 메인-문답-답변 API 연결
  • Loading branch information
ss99x2002 authored Jul 17, 2023
2 parents c00015f + 5cabda2 commit 9d03b5c
Show file tree
Hide file tree
Showing 39 changed files with 319 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import com.sopt.umbba_android.data.model.ServicePool
class HomeRemoteDataSource {
private val homeService = ServicePool.homeService
suspend fun getHomeData() = homeService.getHomeData()
suspend fun getResponseCase() = homeService.getResponseCase()
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.sopt.umbba_android.data.datasource

import com.sopt.umbba_android.data.model.ServicePool
import com.sopt.umbba_android.data.model.request.AnswerRequestDto
import retrofit2.http.Header

class QuestionAnswerRemoteDataSource {
private val questionAnswerService = ServicePool.questionAnswerService
suspend fun getQuestionAnswer() = questionAnswerService.getQuestionAnswer()
suspend fun postAnswer(answerRequestDto : AnswerRequestDto) = questionAnswerService.postAnswer(answerRequestDto)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.sopt.umbba_android.data.model.request

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

@Serializable
data class AnswerRequestDto(
val answer: String
@SerialName("answer")
val answer: String?
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.sopt.umbba_android.data.model.response

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

@Serializable
data class AnswerResponseDto(
val answer: String
@SerialName("status")
val status: Int,
@SerialName("message")
val message: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.sopt.umbba_android.data.model.response

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

@Serializable
data class HomeCaseResponseDto(
@SerialName("status")
val status: Int,
@SerialName("message")
val message: String,
@SerialName("data")
val data: HomeCaseData
) {
@Serializable
data class HomeCaseData(
@SerialName("response_case")
val responseCase: Int,
@SerialName("invite_code")
val inviteCode: String?,
@SerialName("invite_username")
val inviteUserName: String?,
@SerialName("install_url")
val installUrl: String?,
@SerialName("relative_user_active")
val relativeUserActive: Boolean?
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ data class HomeResponseDto(
val message: String,
@SerialName("data")
val data: HomeData
){
) {
@Serializable
data class HomeData(
@SerialName("section")
val section:String,
val section: String,
@SerialName("topic")
val topic:String,
val topic: String,
@SerialName("index")
val index:Int
val index: Int
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ data class QuestionAnswerResponseDto(
) {
@Serializable
data class QnaData(
@SerialName("response_case")
val responseCase: Int,
@SerialName("qna_id")
val qnaId: Long?,
@SerialName("section")
Expand All @@ -37,14 +35,6 @@ data class QuestionAnswerResponseDto(
@SerialName("opponent_username")
val opponentUsername: String?,
@SerialName("my_username")
val myUsername: String?,
@SerialName("invite_code")
val inviteCode:String?,
@SerialName("invite_username")
val inviteUserName:String?,
@SerialName("install_url")
val installUrl:String?,
@SerialName("relative_user_active")
val relativeUserActivate:Boolean?
val myUsername: String?
)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.sopt.umbba_android.data.repository

import android.util.Log
import com.sopt.umbba_android.data.datasource.HomeRemoteDataSource
import com.sopt.umbba_android.data.model.response.HomeCaseResponseDto
import com.sopt.umbba_android.data.model.response.HomeResponseDto
import com.sopt.umbba_android.domain.repository.HomeRepository
import timber.log.Timber

class HomeRepositoryImpl(private val homeRemoteDataSource: HomeRemoteDataSource):HomeRepository {
class HomeRepositoryImpl(private val homeRemoteDataSource: HomeRemoteDataSource) : HomeRepository {
override suspend fun getHomeData(): Result<HomeResponseDto> =
runCatching {
homeRemoteDataSource.getHomeData()
Expand All @@ -15,4 +15,13 @@ class HomeRepositoryImpl(private val homeRemoteDataSource: HomeRemoteDataSource)
}.onFailure {
Timber.e("get home data 실패")
}

override suspend fun getResponseCase(): Result<HomeCaseResponseDto> =
runCatching {
homeRemoteDataSource.getResponseCase()
}.onSuccess {
Timber.e("get response case 성공~")
}.onFailure {
Timber.e("get response case 실패")
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.sopt.umbba_android.data.repository

import com.sopt.umbba_android.data.datasource.QuestionAnswerRemoteDataSource
import com.sopt.umbba_android.data.model.request.AnswerRequestDto
import com.sopt.umbba_android.data.model.response.AnswerResponseDto
import com.sopt.umbba_android.data.model.response.QuestionAnswerResponseDto
import com.sopt.umbba_android.domain.repository.QuestionAnswerRepository
import timber.log.Timber
Expand All @@ -15,7 +17,12 @@ class QuestionAnswerRepositoryImpl(private val questionAnswerRemoteDataSource: Q
}.onFailure {
Timber.e("문답 data get 실패 크라잉. . . ")
}
override suspend fun postAnswer() {
TODO("Not yet implemented")
}
override suspend fun postAnswer(answerRequestDto: AnswerRequestDto):Result<AnswerResponseDto> =
runCatching {
questionAnswerRemoteDataSource.postAnswer(answerRequestDto)
}.onSuccess {
Timber.e("답변 data post 성공")
}.onFailure {
Timber.e("답변 data post 실패 크라잉. . . ")
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.sopt.umbba_android.data.service

import com.sopt.umbba_android.data.model.response.HomeCaseResponseDto
import com.sopt.umbba_android.data.model.response.HomeResponseDto
import retrofit2.http.GET
import retrofit2.http.Header
import retrofit2.http.Headers

interface HomeService {
@Headers("Content-Type: application/json")
@GET("/home")
suspend fun getHomeData(): HomeResponseDto
@Headers("Content-Type: application/json")
@GET("/home/case")
suspend fun getResponseCase() : HomeCaseResponseDto
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.sopt.umbba_android.domain.repository

import com.sopt.umbba_android.data.model.response.HomeCaseResponseDto
import com.sopt.umbba_android.data.model.response.HomeResponseDto

interface HomeRepository {
suspend fun getHomeData():Result<HomeResponseDto>
suspend fun getResponseCase():Result<HomeCaseResponseDto>
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.sopt.umbba_android.domain.repository

import com.sopt.umbba_android.data.model.request.AnswerRequestDto
import com.sopt.umbba_android.data.model.response.AnswerResponseDto
import com.sopt.umbba_android.data.model.response.QuestionAnswerResponseDto

interface QuestionAnswerRepository {
suspend fun getQuestionAnswer(): Result<QuestionAnswerResponseDto>
suspend fun postAnswer()
suspend fun postAnswer(answerRequestDto: AnswerRequestDto): Result<AnswerResponseDto>
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package com.sopt.umbba_android.presentation.home
import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import coil.load
import com.sopt.umbba_android.R
import com.sopt.umbba_android.data.model.response.HomeCaseResponseDto
import com.sopt.umbba_android.data.model.response.HomeResponseDto
import com.sopt.umbba_android.databinding.FragmentHomeBinding
import com.sopt.umbba_android.presentation.home.viewmodel.HomeViewModel
import com.sopt.umbba_android.presentation.qna.NoOpponentDialogFragment
import com.sopt.umbba_android.presentation.qna.QuestionAnswerActivity
import com.sopt.umbba_android.util.ViewModelFactory
import com.sopt.umbba_android.util.binding.BindingFragment
Expand All @@ -19,15 +20,15 @@ class HomeFragment : BindingFragment<FragmentHomeBinding>(R.layout.fragment_home
private val viewModel: HomeViewModel by viewModels { ViewModelFactory(requireActivity()) }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.getHomeData()
observeData()
setClickEvent()
}

private fun setClickEvent() {
with(binding) {
btnAnswer.setOnClickListener {
startActivity(Intent(requireActivity(), QuestionAnswerActivity::class.java))
private fun setClickEvent(responseCaseDto: HomeCaseResponseDto.HomeCaseData) {
binding.btnAnswer.setOnClickListener {
when (responseCaseDto.responseCase) {
1 -> startActivity(Intent(requireActivity(), QuestionAnswerActivity::class.java))
2 -> showInviteDialog(responseCaseDto.inviteCode.toString())
3 -> showNoOpponentDialog()
}
}
}
Expand All @@ -37,6 +38,9 @@ class HomeFragment : BindingFragment<FragmentHomeBinding>(R.layout.fragment_home
setData(it)
setBackground(it.section)
}
viewModel.responseCaseData.observe(requireActivity()) {
setClickEvent(it)
}
}

@SuppressLint("StringFormatMatches")
Expand All @@ -46,6 +50,20 @@ class HomeFragment : BindingFragment<FragmentHomeBinding>(R.layout.fragment_home
}
}

private fun showInviteDialog(inviteCode: String) {
InviteCodeDialogFragment(inviteCode).show(
requireActivity().supportFragmentManager,
"InviteCodeDialogFragment"
)
}

private fun showNoOpponentDialog() {
NoOpponentDialogFragment().show(
requireActivity().supportFragmentManager,
"NoOpponentDialogFragment"
)
}

private fun setBackground(section: String) {
binding.ivBackground.load(
when (section) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package com.sopt.umbba_android.presentation.home

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Context.CLIPBOARD_SERVICE
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.Toast
import androidx.core.content.ContextCompat.getSystemService
import androidx.fragment.app.DialogFragment
import com.sopt.umbba_android.R
import com.sopt.umbba_android.databinding.FragmentInviteCodeDialogBinding

class InviteCodeDialogFragment : DialogFragment() {
class InviteCodeDialogFragment(private val inviteCode: String) : DialogFragment() {

private var _binding: FragmentInviteCodeDialogBinding? = null
private val binding get() = requireNotNull(_binding) { "InviteCodeDialogFragment is null" }
Expand All @@ -24,8 +32,10 @@ class InviteCodeDialogFragment : DialogFragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

copyInviteCode()
closeDialog()
setBackgroundDesign()
setInviteCodeText(inviteCode)
}

private fun closeDialog() {
Expand All @@ -34,6 +44,24 @@ class InviteCodeDialogFragment : DialogFragment() {
}
}

private fun setBackgroundDesign() {
dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
}

private fun setInviteCodeText(inviteCode: String) {
binding.tvInviteCode.text = inviteCode
}

private fun copyInviteCode() {
binding.clCopyInviteCode.setOnClickListener {
val clipboard: ClipboardManager =
requireActivity().getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText("label", binding.tvInviteCode.text)
clipboard.setPrimaryClip(clip)
Toast.makeText(requireActivity(), "초대 코드가 복사되었습니다", Toast.LENGTH_SHORT).show()
}
}

override fun onResume() {
super.onResume()
dialog?.window?.setLayout(
Expand Down
Loading

0 comments on commit 9d03b5c

Please sign in to comment.