Skip to content

Commit 473f770

Browse files
committed
[mod] 아이템 등록 로직 수정
1 parent c6c2998 commit 473f770

File tree

4 files changed

+39
-15
lines changed

4 files changed

+39
-15
lines changed

app/src/main/java/com/hyeeyoung/wishboard/presentation/wishitem/viewmodels/WishItemRegistrationViewModel.kt

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import com.hyeeyoung.wishboard.presentation.folder.FolderListAdapter
1616
import com.hyeeyoung.wishboard.presentation.folder.types.FolderListViewType
1717
import com.hyeeyoung.wishboard.presentation.noti.types.NotiType
1818
import com.hyeeyoung.wishboard.util.ContentUriRequestBody
19+
import com.hyeeyoung.wishboard.util.extension.addSourceList
1920
import com.hyeeyoung.wishboard.util.extension.toPlainNullableRequestBody
2021
import com.hyeeyoung.wishboard.util.extension.toPlainRequestBody
2122
import com.hyeeyoung.wishboard.util.getBitmapFromURL
@@ -81,18 +82,27 @@ class WishItemRegistrationViewModel @Inject constructor(
8182
private val folderListSquareAdapter =
8283
FolderListAdapter(FolderListViewType.SQUARE_VIEW_TYPE)
8384

85+
val isEnabledUploadButton = MediatorLiveData<Boolean>().apply {
86+
addSourceList(itemName, itemPrice) { checkValidItemInfoInput() }
87+
}
88+
89+
private fun checkValidItemInfoInput(): Boolean {
90+
return !(itemName.value.isNullOrBlank() || itemPrice.value.isNullOrBlank() || token == null)
91+
}
92+
8493
init {
8594
initEnabledSaveButton()
8695
fetchFolderList()
8796
}
8897

8998
/** 오픈그래프 메타태그 파싱을 통해 아이템 정보 가져오기 */
90-
suspend fun getWishItemInfo(url: String) {
91-
val result = wishRepository.getItemParsingInfo(url)
92-
if (result?.first == null) return
93-
itemName.value = result.first!!.name
94-
itemPrice.value = result.first!!.price.toString()
95-
itemImage.value = result.first!!.image
99+
fun getWishItemInfo(url: String) {
100+
viewModelScope.launch {
101+
val result = wishRepository.getItemParsingInfo(url)
102+
itemName.value = result?.first?.name
103+
itemPrice.value = if (result?.first?.price == null || result.first?.price == "0") null else result.first?.price
104+
itemImage.value = result?.first?.image
105+
}
96106
}
97107

98108
suspend fun uploadWishItemByLinkSharing() {
@@ -207,7 +217,8 @@ class WishItemRegistrationViewModel @Inject constructor(
207217
private suspend fun updateWishItem(trimmedItemName: String) { // TODO need refactoring, uploadWishItemByBasics()와 합치기
208218
// 파싱으로 아이템 이미지 불러온 경우 비트맵이미지로 이미지 파일 만들기
209219
val folderId: RequestBody? =
210-
(folderItem.value?.id ?: wishItemDetail?.folderId)?.toString()?.toPlainNullableRequestBody()
220+
(folderItem.value?.id ?: wishItemDetail?.folderId)?.toString()
221+
?.toPlainNullableRequestBody()
211222
val itemName: RequestBody = trimmedItemName.toPlainRequestBody()
212223
val itemPrice: RequestBody? = itemPrice.value?.replace(",", "")?.toIntOrNull()?.toString()
213224
?.toPlainNullableRequestBody()
@@ -291,9 +302,7 @@ class WishItemRegistrationViewModel @Inject constructor(
291302
if (!isValid) return
292303

293304
selectedGalleryImageUri.value = null // 파싱한 이미지를 적용할 것이기 때문에 기존에 선택된 이미지를 제거
294-
viewModelScope.launch {
295-
getWishItemInfo(url!!)
296-
}
305+
getWishItemInfo(url!!)
297306
}
298307

299308
/** url 유효성 검증 */
@@ -437,7 +446,8 @@ class WishItemRegistrationViewModel @Inject constructor(
437446

438447
fun setSelectedGalleryImage(imageUri: Uri, imageFile: File) {
439448
// 갤러리 이미지를 적용할 것이기 때문에 기존에 파싱한 이미지를 제거
440-
itemImage.value = null // TODO need refactoring, 아이템 정보 파싱 후 갤러리에서 이미지 선택 하지 않아도 이전에 갤러리에서 이미지를 선택한 적이 있는 경우, 갤러리 이미지가 보이는 버그를 방지하기 위함
449+
itemImage.value =
450+
null // TODO need refactoring, 아이템 정보 파싱 후 갤러리에서 이미지 선택 하지 않아도 이전에 갤러리에서 이미지를 선택한 적이 있는 경우, 갤러리 이미지가 보이는 버그를 방지하기 위함
441451
selectedGalleryImageUri.value = imageUri
442452
this.imageFile = imageFile
443453
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.hyeeyoung.wishboard.util.extension
2+
3+
import androidx.lifecycle.MediatorLiveData
4+
import androidx.lifecycle.MutableLiveData
5+
6+
fun <T> MediatorLiveData<T>.addSourceList(
7+
vararg liveDataArgument: MutableLiveData<*>,
8+
onChanged: () -> T?,
9+
) {
10+
liveDataArgument.forEach {
11+
this.addSource(it) { value = onChanged() }
12+
}
13+
}

app/src/main/res/layout/activity_wish_link_sharing.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@
163163
android:layout_width="match_parent"
164164
android:layout_marginHorizontal="@dimen/spacingBase"
165165
android:layout_marginBottom="@dimen/spacingBase"
166-
android:enabled="@{viewModel.itemName.length() > 0 ? true : false}"
167-
android:text="@{viewModel.registrationStatus == ProcessStatus.IN_PROGRESS ? `` : context.getString(R.string.wish_list_registration_button_text)}"
166+
android:enabled="@{viewModel.isEnabledUploadButton}"
167+
android:text="@{viewModel.registrationStatus == ProcessStatus.IN_PROGRESS ? `` : context.getString(viewModel.token == null ? R.string.wish_list_registration_button_text_for_unregister_user : R.string.wish_list_registration_button_text)}"
168168
app:layout_constraintBottom_toBottomOf="parent"
169169
tools:text="@string/wish_list_registration_button_text" />
170170

@@ -190,11 +190,11 @@
190190
android:layout_gravity="center_horizontal"
191191
android:layout_marginBottom="-40dp"
192192
android:scaleType="centerCrop"
193-
app:shapeAppearanceOverlay="@style/Style.CircleImageView.Half"
194193
app:layout_constraintBottom_toTopOf="@id/container"
195194
app:layout_constraintEnd_toEndOf="parent"
196195
app:layout_constraintStart_toStartOf="parent"
197-
tools:src="@mipmap/ic_main" />
196+
app:shapeAppearanceOverlay="@style/Style.CircleImageView.Half"
197+
tools:src="@drawable/ic_app_logo_black" />
198198

199199
<TextView
200200
android:id="@+id/networkView"

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130

131131
<string name="item_non_update_dialog_description">앗, 삭제된 아이템은 수정할 수 없어요.\n홈탭에서 새로고침을 시도해 주세요!</string>
132132
<string name="item_non_update_dialog_title">아이템 수정 오류</string>
133+
<string name="wish_list_registration_button_text_for_unregister_user">로그인 후 위시보드에 아이템을 담아보세요!</string>
133134

134135
<!-- Detail -->
135136
<string name="go_to_folder">%s></string>

0 commit comments

Comments
 (0)