Skip to content

Commit

Permalink
[Feat] #39 나의 지갑 내역 조회 API 연동
Browse files Browse the repository at this point in the history
  • Loading branch information
PIYUJIN committed Nov 30, 2024
1 parent b279cce commit 194b0a2
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/src/main/java/com/project/jangburich/api/ApiService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.project.jangburich.api.response.group.CreateGroupResponse
import com.project.jangburich.api.response.group.GetGroupInfoWithCodeResponse
import com.project.jangburich.api.response.group.GetGroupResponse
import com.project.jangburich.api.response.home.GetHomeDataResponse
import com.project.jangburich.api.response.home.GetWalletDataResponse
import com.project.jangburich.api.response.login.LoginResponse
import com.project.jangburich.api.response.login.MessageResponse
import com.project.jangburich.api.response.store.GetStoreDetailResponse
Expand Down Expand Up @@ -43,6 +44,11 @@ interface ApiService {
@Header("Authorization") token: String
): Call<BaseResponse<GetHomeDataResponse>>

// 지갑 내역 조회
@GET("/user/wallet")
fun getWalletData(
@Header("Authorization") token: String
): Call<BaseResponse<GetWalletDataResponse>>
// 그룹 생성
@POST("/teams")
fun createGroup(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.project.jangburich.api.response.home

data class GetWalletDataResponse(
val point: Int,
val purchaseHistories: List<PurchaseHistory>
)

data class PurchaseHistory(
val date: String,
val amount: Int,
val transactionTitle: String,
val transactionType: String
)
23 changes: 23 additions & 0 deletions app/src/main/java/com/project/jangburich/ui/home/WalletFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class WalletFragment : Fragment() {
mainActivity = activity as MainActivity
viewModel = ViewModelProvider(mainActivity)[WalletViewModel::class.java]

initAdapter()
observeViewModel()

binding.run {
buttonCharge.setOnClickListener {
Expand All @@ -47,6 +49,27 @@ class WalletFragment : Fragment() {
initView()
}

fun initAdapter() {
walletAdapter = WalletAdapter(mainActivity, getPaymentHistory)

binding.recyclerViewPayment.apply {
adapter = walletAdapter
layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
}
}

fun observeViewModel() {
viewModel.run {
userPoint.observe(mainActivity) {
binding.textViewUserPoint.text = "${it}"
}

purchaseHistoryList.observe(mainActivity) {
getPaymentHistory = it
walletAdapter.updateList(getPaymentHistory)
}
}
}

fun initView() {
viewModel.getWalletData(mainActivity)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.project.jangburich.ui.home.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.project.jangburich.api.response.home.PurchaseHistory
import com.project.jangburich.databinding.RowWalletPaymentBinding
import com.project.jangburich.ui.MainActivity

class WalletAdapter(
private var activity: MainActivity,
private var purchaseHistories: List<PurchaseHistory>
) :
RecyclerView.Adapter<WalletAdapter.ViewHolder>() {

private var onItemClickListener: ((Int) -> Unit)? = null
private var context: Context? = null
private var selectedPosition: Int = 0

fun setOnItemClickListener(listener: (Int) -> Unit) {
onItemClickListener = listener
}

fun updateList(newPurchaseHistories: List<PurchaseHistory>) {
purchaseHistories = newPurchaseHistories
notifyDataSetChanged()
}


interface OnItemClickListener {
fun onItemClick(position: Int) {}
}

var itemClickListener: OnItemClickListener? = null

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
context = parent.context
val binding =
RowWalletPaymentBinding.inflate(LayoutInflater.from(parent.context), parent, false)

return ViewHolder(binding)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.title.text = purchaseHistories[position].transactionTitle
holder.description.text = purchaseHistories[position].transactionType
holder.paymentValue.text = purchaseHistories[position].amount.toString()
holder.date.text = purchaseHistories[position].date
}

override fun getItemCount() = purchaseHistories.size

inner class ViewHolder(val binding: RowWalletPaymentBinding) :
RecyclerView.ViewHolder(binding.root) {
val title = binding.textViewTitle
val description = binding.textViewDescription
val date = binding.textViewDate
val paymentValue = binding.textViewPaymentValue
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.project.jangburich.ui.home.viewModel

import android.content.Intent
import android.net.Uri
import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.project.jangburich.api.ApiClient
import com.project.jangburich.api.TokenManager
import com.project.jangburich.api.request.home.ReadyKakaoPayRequest
import com.project.jangburich.api.response.BaseResponse
import com.project.jangburich.api.response.home.GetWalletDataResponse
import com.project.jangburich.api.response.home.PurchaseHistory
import com.project.jangburich.api.response.home.ReadyKakaoPayResponse
import com.project.jangburich.ui.MainActivity
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class WalletViewModel: ViewModel() {

var userPoint = MutableLiveData<Int>()

var purchaseHistoryList = MutableLiveData<MutableList<PurchaseHistory>>()

init {
purchaseHistoryList.value = mutableListOf<PurchaseHistory>()
}

fun getWalletData(activity: MainActivity) {

var tempPurchaseHistory = mutableListOf<PurchaseHistory>()

val apiClient = ApiClient(activity)
val tokenManager = TokenManager(activity)

apiClient.apiService.getWalletData("Bearer ${tokenManager.getAccessToken()}").enqueue(object :
Callback<BaseResponse<GetWalletDataResponse>> {
override fun onResponse(
call: Call<BaseResponse<GetWalletDataResponse>>,
response: Response<BaseResponse<GetWalletDataResponse>>
) {
Log.d("##", "onResponse 성공: " + response.body().toString())
if (response.isSuccessful) {
// 정상적으로 통신이 성공된 경우
val result: BaseResponse<GetWalletDataResponse>? = response.body()
Log.d("##", "onResponse 성공: " + result?.toString())

userPoint.value = result?.data?.point

if (!(result?.data?.purchaseHistories.isNullOrEmpty())) {
for (i in 0 until result?.data?.purchaseHistories?.size!!) {
var date = result.data.purchaseHistories[i].date
var amount = result.data.purchaseHistories[i].amount
var transactionTitle = result.data.purchaseHistories[i].transactionTitle
var transactionType = result.data.purchaseHistories[i].transactionType

var t1 = PurchaseHistory(date, amount, transactionTitle, transactionType)

tempPurchaseHistory.add(t1)
}
}

purchaseHistoryList.value = tempPurchaseHistory

} else {
// 통신이 실패한 경우(응답코드 3xx, 4xx 등)
var result: BaseResponse<GetWalletDataResponse>? = response.body()
Log.d("##", "onResponse 실패")
Log.d("##", "onResponse 실패: " + response.code())
Log.d("##", "onResponse 실패: " + response.body())
val errorBody = response.errorBody()?.string() // 에러 응답 데이터를 문자열로 얻음
Log.d("##", "Error Response: $errorBody")

}
}

override fun onFailure(call: Call<BaseResponse<GetWalletDataResponse>>, t: Throwable) {
// 통신 실패
Log.d("##", "onFailure 에러: " + t.message.toString())
}
})
}
}

0 comments on commit 194b0a2

Please sign in to comment.