参加0元购逻辑
This commit is contained in:
parent
68f1119b4a
commit
66e47c4328
|
|
@ -11,6 +11,7 @@ data class ZeroBuyResp (
|
|||
var mCurrentList: List<ZeroBuyItem>?,
|
||||
var mFinishedList: List<ZeroBuyItem>?,
|
||||
val Content: String,
|
||||
var contentObj: ZeroBuyItem?
|
||||
)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ import com.ama.core.architecture.appBase.AppViewsEmptyViewModelActivity
|
|||
import com.ama.core.architecture.util.CommonItemDecoration
|
||||
import com.ama.core.architecture.util.setOnClickBatch
|
||||
import com.gamedog.vididin.R
|
||||
import com.gamedog.vididin.beans.ZeroBuyItem
|
||||
import com.gamedog.vididin.beans.ZeroBuyResp
|
||||
import com.gamedog.vididin.core.login.login.AccountManager
|
||||
import com.gamedog.vididin.features.zero.dialogs.ZeroBuyRulesDialog
|
||||
|
|
@ -54,7 +55,9 @@ class ZeroBuyActivity : AppViewsEmptyViewModelActivity<ViewBinding>() {
|
|||
|
||||
|
||||
with(recyclerView) {
|
||||
mAdapter = ZeroItemAdapter()
|
||||
mAdapter = ZeroItemAdapter({ itemId->
|
||||
requestParticipateActivity(itemId)
|
||||
})
|
||||
adapter = mAdapter
|
||||
layoutManager = GridLayoutManager(this@ZeroBuyActivity, 2)
|
||||
addItemDecoration(
|
||||
|
|
@ -108,11 +111,36 @@ class ZeroBuyActivity : AppViewsEmptyViewModelActivity<ViewBinding>() {
|
|||
}
|
||||
|
||||
|
||||
private fun requestParticipateActivity(itemId: Int) {
|
||||
lifecycleScope.launch {
|
||||
repeatOnLifecycle(Lifecycle.State.STARTED) {
|
||||
viewModel.ZeroBuyJoinResult.collect { result ->
|
||||
when (result) {
|
||||
is Result.Loading -> { }
|
||||
is Result.Success -> updateItemUI(result.data)
|
||||
is Result.Error -> { }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
viewModel.requestJoinZeroBuy(itemId)
|
||||
}
|
||||
|
||||
|
||||
private fun updateUIs(data: ZeroBuyResp) {
|
||||
mAdapter.submitList(data.mCurrentList)
|
||||
}
|
||||
|
||||
private fun updateItemUI(joinedItem: ZeroBuyItem?) {
|
||||
val currentList = mAdapter.currentList.toMutableList()
|
||||
val indexToUpdate = currentList.indexOfFirst { it.id == joinedItem?.id }
|
||||
if (indexToUpdate != -1) {
|
||||
currentList.removeAt(indexToUpdate)
|
||||
currentList.add(indexToUpdate, joinedItem)
|
||||
mAdapter.submitList(currentList)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -21,27 +21,56 @@ class ZeroBuyViewModel : ViewModel() {
|
|||
private val _ZeroBuyListData = MutableStateFlow<Result<ZeroBuyResp>>(Result.Loading)
|
||||
val ZeroBuyListData: StateFlow<Result<ZeroBuyResp>> = _ZeroBuyListData.asStateFlow()
|
||||
|
||||
private val _ZeroBuySecretData = MutableStateFlow<Result<String>>(Result.Loading)
|
||||
val ZeroBuySecretData: StateFlow<Result<String>> = _ZeroBuySecretData.asStateFlow()
|
||||
private val _ZeroBuyJoinResult = MutableStateFlow<Result<ZeroBuyItem?>>(Result.Loading)
|
||||
val ZeroBuyJoinResult: StateFlow<Result<ZeroBuyItem?>> = _ZeroBuyJoinResult.asStateFlow()
|
||||
|
||||
|
||||
fun requestZeroBuySecret() {
|
||||
fun requestJoinZeroBuy(itemId: Int) {
|
||||
viewModelScope.launch {
|
||||
_ZeroBuySecretData.value = Result.Loading
|
||||
val operationVal = 1
|
||||
_ZeroBuyJoinResult.value = Result.Loading
|
||||
|
||||
val operationVal = 10
|
||||
val curTimeSec = System.currentTimeMillis()/1000
|
||||
val signStr = RequestUtil.getZeroBuyRequestSign(curTimeSec, operationVal)
|
||||
val requestHeaders = mapOf("Operation" to operationVal.toString(), "Timestamp" to curTimeSec.toString(), "Sign" to signStr)
|
||||
val requestParams = mutableMapOf("AppId" to VidiConst.ZEROBUY_APPID, "DeviceId" to DeviceUtil.generateDeviceId())
|
||||
val userId = AccountManager.getAccount()?.userId?: 0
|
||||
if (userId > 0) {
|
||||
requestParams.put("UserId", userId.toString())
|
||||
}
|
||||
val joinZeroBuyItemIds = SpUtil.instance().getList<Int>(SpUtil.KEY_ZEROBUY_JOINED_ACTIVITY_IDS)
|
||||
if (joinZeroBuyItemIds.isNotEmpty()) {
|
||||
requestParams.put("JoinedPurchaseIds", AndroidUtil.object2Json(joinZeroBuyItemIds))
|
||||
}
|
||||
requestParams.put("ActivityId", itemId.toString())
|
||||
|
||||
val result = NetworkUtil.get("${VidiConst.URL_ZERO_BUY}/anynameisok", requestHeaders)
|
||||
|
||||
val result = NetworkUtil.get("${VidiConst.URL_ZERO_BUY}/anynameisok", requestHeaders, requestParams)
|
||||
when (result) {
|
||||
is Result.Success -> {
|
||||
val respObj = AndroidUtil.json2Object<String>(result.data.string())
|
||||
_ZeroBuySecretData.value = Result.Success(respObj!!)
|
||||
val respObj = AndroidUtil.json2Object<ZeroBuyResp>(result.data.string())?.apply {
|
||||
mCurrentList = AndroidUtil.json2Object<List<ZeroBuyItem>>(CurrentPurchases)
|
||||
mFinishedList = AndroidUtil.json2Object<List<ZeroBuyItem>>(FinishedPurchases)
|
||||
contentObj = AndroidUtil.json2Object<ZeroBuyItem>(Content)
|
||||
}
|
||||
|
||||
|
||||
respObj?.contentObj?.let {
|
||||
val itemId = respObj?.contentObj?.id
|
||||
if (respObj.Code == 0 && itemId != null && itemId > 0 && !joinZeroBuyItemIds.contains(itemId)) {
|
||||
val mutableJoinedIdList = if(joinZeroBuyItemIds == null) mutableListOf<Int>() else joinZeroBuyItemIds.toMutableList()
|
||||
mutableJoinedIdList.add(itemId)
|
||||
SpUtil.instance().putList(SpUtil.KEY_ZEROBUY_JOINED_ACTIVITY_IDS, mutableJoinedIdList)
|
||||
|
||||
_ZeroBuyJoinResult.value = Result.Success(respObj?.contentObj)
|
||||
return@launch
|
||||
}
|
||||
}
|
||||
|
||||
_ZeroBuyJoinResult.value = Result.Error(Throwable("empty response"))
|
||||
}
|
||||
is Result.Error -> {
|
||||
_ZeroBuySecretData.value = Result.Error(result.exception, result.message)
|
||||
_ZeroBuyJoinResult.value = Result.Error(result.exception, result.message)
|
||||
}
|
||||
else -> { }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,20 @@
|
|||
package com.gamedog.vididin.features.zero
|
||||
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import androidx.recyclerview.widget.ListAdapter
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.ama.core.architecture.util.SpUtil
|
||||
import com.ama.core.architecture.util.setOnClickBatch
|
||||
import com.gamedog.vididin.R
|
||||
import com.gamedog.vididin.beans.ZeroBuyItem
|
||||
import com.gamedog.vididin.core.login.login.AccountManager
|
||||
import com.gamedog.vididin.databinding.LayoutItemZerobuyBinding as ViewBinding
|
||||
|
||||
class ZeroItemAdapter : ListAdapter<ZeroBuyItem, ZeroItemAdapter.ViewHolder>(DiffCallback()) {
|
||||
class ZeroItemAdapter(private val joinCallback: (itemId: Int)->Unit) : ListAdapter<ZeroBuyItem, ZeroItemAdapter.ViewHolder>(DiffCallback()) {
|
||||
|
||||
private val mBgResList = listOf<Int>(R.mipmap.zero_bg_item_sub1, R.mipmap.zero_bg_item_sub2, R.mipmap.zero_bg_item_sub3, R.mipmap.zero_bg_item_sub4)
|
||||
|
||||
|
|
@ -30,6 +35,28 @@ class ZeroItemAdapter : ListAdapter<ZeroBuyItem, ZeroItemAdapter.ViewHolder>(Dif
|
|||
binding.tvJoinGoldNum.text = item.cost.toString()
|
||||
binding.ivBgType.setImageResource(mBgResList[item.image])
|
||||
binding.tvRewardCashNum.text = item.price
|
||||
|
||||
with(binding) {
|
||||
setOnClickBatch(flBottomBut) {
|
||||
when (this) {
|
||||
flBottomBut-> {
|
||||
if (!item.completed && (item.current_users == null || !item.current_users!!.contains(AccountManager.getAccount()?.userId))) {
|
||||
joinCallback(item.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// judge state
|
||||
val joinedIds = SpUtil.instance().getList<Int>(SpUtil.KEY_ZEROBUY_JOINED_ACTIVITY_IDS)
|
||||
val hasJoined = joinedIds.contains(item.id)
|
||||
val hasCompleted = item.completed
|
||||
flBottomBut.isVisible = !hasJoined
|
||||
tvParticipateAlready.isVisible = hasJoined
|
||||
tvRemainTime.isVisible = hasJoined && !hasCompleted
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -110,5 +110,6 @@ object AccountManager {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -91,7 +91,28 @@
|
|||
/>
|
||||
</LinearLayout>
|
||||
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
android:id="@+id/tv_participate_already"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textColor="@color/green_39"
|
||||
android:textStyle="bold"
|
||||
android:textSize="14sp"
|
||||
android:text="@string/participar_ed"
|
||||
/>
|
||||
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
android:id="@+id/tv_remain_time"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textColor="@color/red_28"
|
||||
android:textStyle="bold"
|
||||
android:textSize="12sp"
|
||||
android:text="@string/remain_time"
|
||||
/>
|
||||
|
||||
<FrameLayout
|
||||
android:id="@+id/fl_bottom_but"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="5dp">
|
||||
|
|
@ -112,7 +133,6 @@
|
|||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textColor="@color/white"
|
||||
android:layout_marginStart="5dp"
|
||||
android:textSize="14sp"
|
||||
android:text="@string/participar"
|
||||
/>
|
||||
|
|
|
|||
|
|
@ -117,5 +117,7 @@
|
|||
<string name="record_cash_title">Dinheiro</string>
|
||||
<string name="record_gold_title">Moedas</string>
|
||||
<string name="record_cash_title_hint">Total sacado até o momento:</string>
|
||||
<string name="participar_ed">Já participou</string>
|
||||
<string name="remain_time">Reembolso em</string>
|
||||
|
||||
</resources>
|
||||
Loading…
Reference in New Issue