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

[둘리] 2단계 재화 미션 제출합니다. #30

Merged
merged 58 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
908f44b
feat: 서버 선택 액티비티 구현
hyemdooly May 28, 2023
82842ac
refactor: ProductListActivity Retrofit, Domain에 Pagination 생성하여 리팩터링
hyemdooly May 30, 2023
b810530
refactor: Repository들 Retrofit으로 수정, RecentViewedDB 수정 및 리팩터링
hyemdooly May 31, 2023
4d98c1c
refactor: ProductList 리팩터링
hyemdooly May 31, 2023
2057624
refactor: ProductList, ProductDetail 리팩터링
hyemdooly May 31, 2023
80935fb
refactor: Cart 리팩터링
hyemdooly Jun 1, 2023
46cf174
refactor: Cart 스켈레톤 추가
hyemdooly Jun 1, 2023
d824e50
refactor: Product 스켈레톤 View로 요소 변경
hyemdooly Jun 1, 2023
3f5b4de
refactor: Mypage 액티비티 생성
hyemdooly Jun 1, 2023
60a207c
refactor: 카트에서 수정 후 백버튼 눌렀을 때 반영 안되는 버그 수정
hyemdooly Jun 1, 2023
a94c2ad
feat: Mypage Activity 구현
hyemdooly Jun 1, 2023
c2481fa
refactor: AppCompatButton -> Button 수정
hyemdooly Jun 2, 2023
158c5ef
feat: OrderAcitvity View xml 구현
hyemdooly Jun 2, 2023
4f0dab6
feat: CartActivity, OrderActivity 주문 기능 구현
hyemdooly Jun 3, 2023
0a8e0cd
feat: OrderDetail, OrderHistory 구현
hyemdooly Jun 3, 2023
007d3c0
refactor: databinding 리팩터링
hyemdooly Jun 3, 2023
454da81
refactor: Log 삭제
hyemdooly Jun 3, 2023
0973f31
refactor: ktlintFormat
hyemdooly Jun 3, 2023
8c1c90a
refactor: onFailure시 실행할 콜백 함수 추가, 최근본 상품 서버별로 분리
hyemdooly Jun 3, 2023
5cee35e
refactor: callback 대소문자 통일
hyemdooly Jun 3, 2023
bac916c
fix: CartPagination 버그 수정
hyemdooly Jun 3, 2023
0b43a65
fix: CartPagination 버그 수정
hyemdooly Jun 3, 2023
575beb5
refactor: 500 에러 대응
hyemdooly Jun 3, 2023
9fcbe88
Merge remote-tracking branch 'origin/step2' into step2
hyemdooly Jun 3, 2023
496197f
refactor: 사용하지 않는 함수 제거, products가 비었을 경우 대응
hyemdooly Jun 3, 2023
ba14236
refactor: 함수 중복 호출 제거, 사용하지 않는 import 제거
hyemdooly Jun 3, 2023
79db6d7
refactor: 기존 Presenter Test 수정
hyemdooly Jun 3, 2023
0391439
fix: 캐시 음수 안걸러지는 버그 수정
hyemdooly Jun 3, 2023
cf5243d
refactor: OrderPresenter 접근제어자 변경, 로그 삭제
hyemdooly Jun 3, 2023
0ed0447
feat: OrderPresenter 테스트 작성
hyemdooly Jun 3, 2023
69e74d8
feat: OrderHistoryPresenter 테스트 작성
hyemdooly Jun 3, 2023
06bbd3a
feat: OrderDetailPresenter 테스트 작성
hyemdooly Jun 3, 2023
454a11d
feat: 테스트를 위한 Fixture 작성
hyemdooly Jun 3, 2023
5ed1a89
refactor: 로그 삭제
hyemdooly Jun 3, 2023
ae88747
refactor: 로그 삭제
hyemdooly Jun 3, 2023
3908c3a
Merge remote-tracking branch 'origin/step2' into step2
hyemdooly Jun 3, 2023
c9e0bd1
refactor: Repository 반복 코드 삭제
hyemdooly Jun 4, 2023
5f0ec65
refactor: failureCallback 삭제
hyemdooly Jun 4, 2023
f2af726
refactor: okhttp interceptor을 이용한 헤더 추가
hyemdooly Jun 5, 2023
b9e9c4f
refactor: adapter에서 데이터 변경을 notify하도록 수정
hyemdooly Jun 5, 2023
3278cd7
refactor: adapter에서 데이터 변경을 notify하도록 수정
hyemdooly Jun 5, 2023
7361d41
refactor: DataResult Class 생성하여 에러처리를 Presenter에서 하도록 수정
hyemdooly Jun 5, 2023
a65ef2e
Merge remote-tracking branch 'origin/step2' into step2
hyemdooly Jun 5, 2023
45e5e39
refactor: UserAuthorization 수정
hyemdooly Jun 6, 2023
ac354f6
refactor: ProductListAdapter 업데이트 함수 DiffUtil 사용하여 리팩터링
hyemdooly Jun 6, 2023
a0a1d31
refactor: CartAdapter 업데이트 함수 DiffUtil 사용하여 리팩터링
hyemdooly Jun 6, 2023
10b1b03
refactor: Product companion object 삭제
hyemdooly Jun 6, 2023
b680c33
refactor: 에러별로 다른 토스트 띄우도록 수정
hyemdooly Jun 6, 2023
ae57e08
refactor: retrofitService 네이밍 수정
hyemdooly Jun 6, 2023
f13fb24
refactor: response body값들이 null일 경우 처리
hyemdooly Jun 6, 2023
71e8eab
refactor: DataSource - Repository 분리
hyemdooly Jun 7, 2023
c9c2644
refactor: 프레젠터 테스트 수정
hyemdooly Jun 7, 2023
b238322
refactor: ProductRemoteDataSourceTest 작성
hyemdooly Jun 7, 2023
4332aaa
refactor: CartRemoteDataSourceTest 작성
hyemdooly Jun 7, 2023
06bdf53
refactor: OrderRemoteDataSourceTest 작성
hyemdooly Jun 7, 2023
a148344
refactor: ProductRemoteDataSourceTest 로그 삭제
hyemdooly Jun 7, 2023
36f7277
refactor: MypageRemoteDataSourceTest 작성
hyemdooly Jun 7, 2023
00e847e
refactor: ktlintFormat
hyemdooly Jun 7, 2023
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
Prev Previous commit
Next Next commit
feat: Mypage Activity 구현
  • Loading branch information
hyemdooly committed Jun 1, 2023
commit a94c2ad0f92a9f091bb80a4f5a266bade3ab2146
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package woowacourse.shopping.data.repository

import retrofit2.Call
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import woowacourse.shopping.data.retrofit.MypageApi
import woowacourse.shopping.domain.model.TotalCashDTO
import woowacourse.shopping.domain.repository.MypageRepository

class MypageRemoteRepository(baseUrl: String) : MypageRepository {
private val retrofitService = Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(MypageApi::class.java)

override fun getCash(callback: (Int) -> Unit) {
retrofitService.requestCash().enqueue(object : retrofit2.Callback<TotalCashDTO> {
override fun onResponse(call: Call<TotalCashDTO>, response: Response<TotalCashDTO>) {
response.body()?.let {
callback(it.totalCash)
}
}

override fun onFailure(call: Call<TotalCashDTO>, t: Throwable) {
throw t
}
})
}

override fun chargeCash(cash: Int, callback: (Int) -> Unit) {
retrofitService.requestChargeCash(cash).enqueue(object : retrofit2.Callback<TotalCashDTO> {
override fun onResponse(call: Call<TotalCashDTO>, response: Response<TotalCashDTO>) {
response.body()?.let {
callback(it.totalCash)
}
}

override fun onFailure(call: Call<TotalCashDTO>, t: Throwable) {
throw t
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,6 @@ class ProductRemoteRepository(baseUrl: String) : ProductRepository {
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(ProductApi::class.java)
// "Authorization", "Basic ZG9vbHlAZG9vbHkuY29tOjEyMzQ="
// override fun getAll(callBack: (List<Product>) -> Unit) {
// executeRequest(request, callBack)
// }
//
// override fun getProduct(id: Int, callBack: (Product?) -> Unit) {
// fun callBackWrapper(products: List<Product>) {
// callBack(products.find { it.id == id })
// }
// executeRequest(request, ::callBackWrapper)
// }

override fun getProductsByRange(lastId: Int, pageItemCount: Int, callback: (ProductsWithCartItemDTO) -> Unit) {
retrofitService.requestProductsByRange(lastId, pageItemCount)
Expand Down Expand Up @@ -61,7 +50,7 @@ class ProductRemoteRepository(baseUrl: String) : ProductRepository {
retrofitService.requestProductById(id).enqueue(object : retrofit2.Callback<ProductWithCartInfo> {
override fun onResponse(
call: Call<ProductWithCartInfo>,
response: Response<ProductWithCartInfo>
response: Response<ProductWithCartInfo>,
) {
response.body()?.let { productWithCartInfo ->
callback(productWithCartInfo)
Expand All @@ -73,21 +62,4 @@ class ProductRemoteRepository(baseUrl: String) : ProductRepository {
}
})
}

// private fun executeRequest(request: Request, callBack: (List<Product>) -> Unit) {
// var responseBody: String?
// client.newCall(request).enqueue(
// object : Callback {
// override fun onFailure(call: Call, e: IOException) {
// throw java.lang.RuntimeException("Request Failed", e)
// }
//
// override fun onResponse(call: Call, response: Response) {
// responseBody = response.body?.string()
// callBack(parseResponse(responseBody))
// response.close()
// }
// },
// )
// }
}
15 changes: 15 additions & 0 deletions app/src/main/java/woowacourse/shopping/data/retrofit/MypageApi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package woowacourse.shopping.data.retrofit

import retrofit2.Call
import retrofit2.http.*
import woowacourse.shopping.domain.model.TotalCashDTO

interface MypageApi {
@Headers("Authorization: Basic ZG9vbHlAZG9vbHkuY29tOjEyMzQ=")
@GET("/members/cash")
fun requestCash(): Call<TotalCashDTO>

@Headers("Authorization: Basic ZG9vbHlAZG9vbHkuY29tOjEyMzQ=")
@POST("/members/cash")
fun requestChargeCash(@Body cashToCharge: Int): Call<TotalCashDTO>
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,50 @@ package woowacourse.shopping.view.mypage
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import woowacourse.shopping.R
import woowacourse.shopping.data.repository.MypageRemoteRepository
import woowacourse.shopping.data.repository.ServerPreferencesRepository
import woowacourse.shopping.databinding.ActivityMypageBinding

class MypageActivity : AppCompatActivity() {
private val binding: ActivityMypageBinding by lazy {
ActivityMypageBinding.inflate(
layoutInflater,
)
}
private val presenter: MypageContract.Presenter by lazy {
MypagePresenter(
MypageRemoteRepository(ServerPreferencesRepository(this).getServerUrl()),
)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_mypage)
setContentView(binding.root)
setUpBinding()
setUpActionBar()
presenter.fetchCash()
}

private fun setUpBinding() {
binding.presenter = presenter
binding.lifecycleOwner = this
binding.btnSubmit.setOnClickListener {
presenter.chargeCash(Integer.parseInt(binding.editCash.text.toString()))
}
}

private fun setUpActionBar() {
supportActionBar?.setDisplayHomeAsUpEnabled(true)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
android.R.id.home -> {
finish()
}
}
return super.onOptionsItemSelected(item)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package woowacourse.shopping.view.mypage

import androidx.lifecycle.LiveData

interface MypageContract {
interface Presenter {
val cash: LiveData<Int>
fun fetchCash()
fun chargeCash(cash: Int)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package woowacourse.shopping.view.mypage

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import woowacourse.shopping.domain.repository.MypageRepository

class MypagePresenter(private val mypageRepository: MypageRepository) : MypageContract.Presenter {
private val _cash = MutableLiveData(0)
override val cash: LiveData<Int>
get() = _cash
Comment on lines +9 to +11

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

둘리님은 MVP패턴에서 LiveData를 사용해보니 어떠신거 같나요?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MVP가 아닌 것 같습니다...🥲
LiveData가 편해서 사용하긴 했지만, interface에 LiveData 변수를 선언해야 View에서 사용할 수 있어서 MVP에 어긋난다고 느꼈습니다!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

추후에 mvvm패턴을 적용해보시면 장점을 느끼실수 있으실거예요! 👍


override fun fetchCash() {
mypageRepository.getCash {
Log.d("get", it.toString())
_cash.value = it
}
}

override fun chargeCash(cash: Int) {
mypageRepository.chargeCash(cash) {
Log.d("post", it.toString())
_cash.value = it
}
}
}
53 changes: 53 additions & 0 deletions app/src/main/res/layout/activity_mypage.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<variable
name="presenter"
type="woowacourse.shopping.view.mypage.MypageContract.Presenter" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.mypage.MypageActivity">

<TextView
android:id="@+id/text_cash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{@string/cash_on_hand(presenter.cash)}"
android:textColor="@color/black"
android:textSize="22sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="보유한 캐시 : 10,000 캐시" />

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="@id/text_cash"
app:layout_constraintStart_toStartOf="@id/text_cash"
app:layout_constraintTop_toBottomOf="@id/text_cash">

<EditText
android:id="@+id/edit_cash"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="number"
app:layout_constraintEnd_toStartOf="@id/btn_submit"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/btn_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/mint"
android:text="@string/charge"
android:textSize="17sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/edit_cash"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

</layout>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@
<string name="sungha_server">성하 서버</string>
<string name="logan_server">로건 서버</string>
<string name="mypage">마이페이지</string>
<string name="charge">충전</string>
<string name="cash_on_hand">보유한 캐시 : %,d캐시</string>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package woowacourse.shopping.domain.model

data class TotalCashDTO(val totalCash: Int)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package woowacourse.shopping.domain.repository

interface MypageRepository {
fun getCash(callback: (Int) -> Unit)
fun chargeCash(cash: Int, callback: (Int) -> Unit)
}