Selaa lähdekoodia

fix: 商城bug

tongmengxiao 4 kuukautta sitten
vanhempi
commit
c2a5510012

+ 104 - 0
user/src/main/java/com/swago/user/store/BaseStoreDFragment.kt

@@ -0,0 +1,104 @@
+package com.swago.user.store
+
+import android.os.Bundle
+import android.view.Gravity
+import android.view.LayoutInflater
+import android.view.View
+import android.view.ViewGroup
+import androidx.fragment.app.DialogFragment
+import androidx.fragment.app.FragmentManager
+import androidx.viewbinding.ViewBinding
+import com.gyf.immersionbar.ImmersionBar
+import com.swago.baseswago.R
+import java.lang.reflect.ParameterizedType
+
+/**
+ *@date 2021/8/27 17:00
+ *description:
+ */
+abstract class BaseStoreDFragment<T : ViewBinding> : DialogFragment() {
+
+    private var _binding: T? = null
+    val binding get() = _binding
+
+    private var mAmount = 0.5f
+    private var gravity = Gravity.CENTER
+    private var canCancel = true
+    private var mHeight = ViewGroup.LayoutParams.WRAP_CONTENT
+
+    override fun onCreate(savedInstanceState: Bundle?) {
+        super.onCreate(savedInstanceState)
+        setStyle(STYLE_NORMAL, R.style.cusDialogStyle)
+    }
+
+    override fun onStart() {
+        super.onStart()
+        //设置宽度
+        val attrs = dialog?.window?.attributes
+        attrs?.width = ViewGroup.LayoutParams.MATCH_PARENT
+        attrs?.height = mHeight
+        dialog?.window?.attributes = attrs
+        dialog?.window?.setGravity(gravity)
+        dialog?.window?.setDimAmount(mAmount)
+        dialog?.setCanceledOnTouchOutside(canCancel)
+    }
+
+    override fun onCreateView(
+        inflater: LayoutInflater,
+        container: ViewGroup?,
+        savedInstanceState: Bundle?
+    ): View? {
+        _binding = getViewBindingForFragment(inflater, container)
+        return binding?.root
+    }
+
+    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
+        super.onViewCreated(view, savedInstanceState)
+        ImmersionBar.with(this).init()
+        initOther()
+        initLiveData()
+    }
+    abstract fun initOther()
+    abstract fun initLiveData()
+    fun setGravity(gravity: Int){
+        this.gravity = gravity
+    }
+
+    fun setDimAmount(amount: Float) {
+        mAmount = amount
+    }
+
+    fun setCanCancel(canCancel:Boolean){
+        this.canCancel = canCancel
+    }
+
+    fun setHeightMatchParent(){
+        mHeight = ViewGroup.LayoutParams.MATCH_PARENT
+    }
+
+    @Suppress("UNCHECKED_CAST")
+    fun getViewBindingForFragment(layoutInflater: LayoutInflater, container: ViewGroup?): T {
+        val type = javaClass.genericSuperclass as ParameterizedType
+        val aClass = type.actualTypeArguments[0] as Class<*>
+        val method = aClass.getDeclaredMethod("inflate", LayoutInflater::class.java)
+        return method.invoke(null, layoutInflater) as T
+    }
+
+    override fun show(manager: FragmentManager, tag: String?) {
+        try {
+            //在每个add事务前增加一个remove事务,防止连续的add
+            manager.beginTransaction().remove(this).commit()
+            super.show(manager,tag)
+        } catch (e: Exception) {
+            //同一实例使用不同的tag会异常,这里捕获一下
+            e.printStackTrace()
+        }
+    }
+
+
+
+    override fun onDestroyView() {
+        super.onDestroyView()
+        _binding = null
+    }
+}

+ 41 - 39
user/src/main/java/com/swago/user/store/StoreBuyDialog.kt

@@ -14,6 +14,7 @@ import com.opensource.svgaplayer.SVGAVideoEntity
 import com.swago.baseswago.dialog.BaseXDFragment
 import com.swago.baseswago.model.mine.StoreItemBean
 import com.swago.baseswago.util.AppContext
+import com.swago.baseswago.util.LogUtil
 import com.swago.baseswago.util.NoDoubleClickListener
 import com.swago.loadUrl
 import com.swago.user.R
@@ -22,25 +23,26 @@ import com.swago.user.vm.StoreVm
 import java.io.File
 import java.net.URL
 
-class StoreBuyDialog : BaseXDFragment<DialogStoreDetailBinding>(){
+class StoreBuyDialog : BaseStoreDFragment<DialogStoreDetailBinding>() {
     private val storeVm by viewModels<StoreVm>()
 
     private var svgaParser: SVGAParser? = null
-    var storeBean: StoreItemBean ?= null
-    var storeType:Int = 0
-    var buySuccessFun:(()->Unit)? = null
+    var storeBean: StoreItemBean? = null
+    var storeType: Int = 0
+    var buySuccessFun: (() -> Unit)? = null
+
     init {
         setGravity(Gravity.BOTTOM)
         setDimAmount(0.5f)
         setCanCancel(true)
     }
 
-    companion object{
-        fun newInstance(bean: StoreItemBean,storeType:Int): StoreBuyDialog {
+    companion object {
+        fun newInstance(bean: StoreItemBean, storeType: Int): StoreBuyDialog {
             val dialog = StoreBuyDialog()
             val bundle = Bundle()
-            bundle.putParcelable("data" , bean)
-            bundle.putInt("storeType",storeType)
+            bundle.putParcelable("data", bean)
+            bundle.putInt("storeType", storeType)
             dialog.arguments = bundle
             return dialog
         }
@@ -51,63 +53,63 @@ class StoreBuyDialog : BaseXDFragment<DialogStoreDetailBinding>(){
         svgaParser = SVGAParser(activity)
         arguments?.let {
             storeBean = it.getParcelable("data")
-            storeType = it.getInt("storeType",0)
+            storeType = it.getInt("storeType", 0)
         }
         storeBean?.let {
-            binding.tvName.text = it.product_name
-            binding.tvCoin.text = "${it.product_coin}"
-            if (storeType == 1){
-                binding.ivIcon.visibility = View.GONE
-                binding.svgContent.visibility = View.VISIBLE
-                binding.svgAvatar.visibility = View.GONE
+            binding?.tvName?.text = it.product_name
+            binding?.tvCoin?.text = "${it.product_coin}"
+            if (storeType == 1) {
+                binding?.ivIcon?.visibility = View.GONE
+                binding?.svgContent?.visibility = View.VISIBLE
+                binding?.svgAvatar?.visibility = View.GONE
                 it.product_svg_url?.let { svgUrl ->
-                    svgaParser?.decodeFromURL(URL(svgUrl), object : SVGAParser.ParseCompletion{
+                    svgaParser?.decodeFromURL(URL(svgUrl), object : SVGAParser.ParseCompletion {
                         override fun onComplete(videoItem: SVGAVideoEntity) {
                             val drawable = SVGADrawable(videoItem)
-                            binding.svgContent.visibility = View.VISIBLE
-                            binding.svgAvatar.visibility = View.GONE
-                            binding.svgContent.setImageDrawable(drawable)
-                            binding.svgContent.startAnimation()
+                            binding?.svgContent?.setImageDrawable(drawable)
+                            binding?.svgContent?.startAnimation()
+
+
                         }
+
                         override fun onError() {
                         }
                     })
                 }
 
-            } else if (storeType == 2){
-                binding.ivIcon.visibility = View.GONE
-                binding.svgContent.visibility = View.GONE
-                binding.svgAvatar.visibility = View.VISIBLE
+            } else if (storeType == 2) {
+                binding?.ivIcon?.visibility = View.GONE
+                binding?.svgContent?.visibility = View.GONE
+                binding?.svgAvatar?.visibility = View.VISIBLE
                 it.product_svg_url?.let { svgUrl ->
-                    svgaParser?.decodeFromURL(URL(svgUrl), object : SVGAParser.ParseCompletion{
+                    svgaParser?.decodeFromURL(URL(svgUrl), object : SVGAParser.ParseCompletion {
                         override fun onComplete(videoItem: SVGAVideoEntity) {
                             val drawable = SVGADrawable(videoItem)
-                            binding.svgContent.visibility = View.GONE
-                            binding.svgAvatar.visibility = View.VISIBLE
-                            binding.svgAvatar.setImageDrawable(drawable)
-                            binding.svgAvatar.startAnimation()
+                            binding?.svgAvatar?.setImageDrawable(drawable)
+                            binding?.svgAvatar?.startAnimation()
                         }
+
                         override fun onError() {
                         }
                     })
                 }
 
-            }else {
-                binding.svgContent.visibility = View.GONE
-                binding.svgAvatar.visibility = View.GONE
-                binding.ivIcon.visibility = View.VISIBLE
+            } else {
+                binding?.svgContent?.visibility = View.GONE
+                binding?.svgAvatar?.visibility = View.GONE
+                binding?.ivIcon?.visibility = View.VISIBLE
                 it.product_url?.let { imageUrl ->
-                    binding.ivIcon.loadUrl(AppContext.getContext(),imageUrl)
+                    binding?.ivIcon?.loadUrl(AppContext.getContext(), imageUrl)
                 }
             }
         }
     }
 
     override fun initLiveData() {
-        binding.tvBuy.setOnClickListener(object: NoDoubleClickListener(){
+        binding?.tvBuy?.setOnClickListener(object : NoDoubleClickListener() {
             override fun onClick() {
                 storeBean?.let {
-                    if (it.product_status ==1){
+                    if (it.product_status == 1) {
                         Toast.makeText(
                             AppContext.getContext(),
                             AppContext.getContext().resources.getString(R.string.this_product_has_been_purchased),
@@ -120,12 +122,12 @@ class StoreBuyDialog : BaseXDFragment<DialogStoreDetailBinding>(){
 
             }
         })
-        binding.svgContent.setOnClickListener {
+        binding?.svgContent?.setOnClickListener {
             dismissAllowingStateLoss()
         }
 
-        storeVm.buyProductSuccess.observe(this){
-            if (it){
+        storeVm.buyProductSuccess.observe(this) {
+            if (it) {
                 buySuccessFun?.invoke()
             }
             dismissAllowingStateLoss()