hxMac 1 miesiąc temu
rodzic
commit
8704a6817f

+ 40 - 0
UtilsApplication/app/app/src/main/java/com/hx/utils/application/base/BaseFragment.kt

@@ -0,0 +1,40 @@
+package com.hx.utils.application.base
+
+import android.os.Bundle
+import android.view.LayoutInflater
+import android.view.View
+import android.view.ViewGroup
+import androidx.fragment.app.Fragment
+import androidx.lifecycle.ViewModel
+import androidx.viewbinding.ViewBinding
+
+/**
+ * author: hx
+ * created on: 2024/12/6 13:28
+ * description:
+ */
+abstract class BaseFragment<Binding : ViewBinding, VM : ViewModel> : Fragment() {
+    private var _binding: Binding? = null
+    protected val binding get() = _binding!!
+    protected lateinit var viewModel: VM
+
+    abstract fun setupViewModel()
+    abstract fun setupView()
+    abstract fun setupBinding(inflater: LayoutInflater, container: ViewGroup?): Binding
+
+    override fun onCreateView(
+        inflater: LayoutInflater, container: ViewGroup?,
+        savedInstanceState: Bundle?
+    ): View? {
+        _binding = setupBinding(inflater, container)
+        setupViewModel()
+        setupView()
+        return binding.root
+    }
+
+    override fun onDestroyView() {
+        super.onDestroyView()
+        _binding = null
+    }
+
+}

+ 14 - 25
UtilsApplication/app/app/src/main/java/com/hx/utils/application/ui/dashboard/DashboardFragment.kt

@@ -1,40 +1,29 @@
 package com.hx.utils.application.ui.dashboard
 
-import android.os.Bundle
 import android.view.LayoutInflater
-import android.view.View
 import android.view.ViewGroup
-import android.widget.TextView
-import androidx.fragment.app.Fragment
 import androidx.lifecycle.ViewModelProvider
+import com.hx.utils.application.base.BaseFragment
 import com.hx.utils.application.databinding.FragmentDashboardBinding
 
-class DashboardFragment : Fragment() {
+class DashboardFragment : BaseFragment<FragmentDashboardBinding, DashboardViewModel>() {
 
-    private var _binding: FragmentDashboardBinding? = null
+    override fun setupViewModel() {
+        viewModel = ViewModelProvider(this)[DashboardViewModel::class.java]
 
-    private val binding get() = _binding!!
-
-    override fun onCreateView(
-        inflater: LayoutInflater,
-        container: ViewGroup?,
-        savedInstanceState: Bundle?
-    ): View {
-        val dashboardViewModel =
-            ViewModelProvider(this).get(DashboardViewModel::class.java)
-
-        _binding = FragmentDashboardBinding.inflate(inflater, container, false)
-        val root: View = binding.root
+    }
 
-        val textView: TextView = binding.textDashboard
-        dashboardViewModel.text.observe(viewLifecycleOwner) {
-            textView.text = it
+    override fun setupView() {
+        viewModel.text.observe(viewLifecycleOwner) {
+            binding.textDashboard.text = it
         }
-        return root
     }
 
-    override fun onDestroyView() {
-        super.onDestroyView()
-        _binding = null
+    override fun setupBinding(
+        inflater: LayoutInflater,
+        container: ViewGroup?
+    ): FragmentDashboardBinding {
+        return FragmentDashboardBinding.inflate(inflater, container, false)
     }
+
 }

+ 19 - 23
UtilsApplication/app/app/src/main/java/com/hx/utils/application/ui/home/HomeFragment.kt

@@ -1,39 +1,40 @@
 package com.hx.utils.application.ui.home
 
-import android.os.Bundle
 import android.view.LayoutInflater
-import android.view.View
 import android.view.ViewGroup
 import androidx.core.os.bundleOf
-import androidx.fragment.app.Fragment
 import androidx.lifecycle.ViewModelProvider
 import androidx.navigation.Navigation.findNavController
 import androidx.recyclerview.widget.GridLayoutManager
 import com.blankj.utilcode.util.ToastUtils
 import com.hx.utils.application.R
 import com.hx.utils.application.base.BaseAdapter
+import com.hx.utils.application.base.BaseFragment
 import com.hx.utils.application.databinding.FragmentHomeBinding
 import com.hx.utils.application.databinding.ItemMenuBinding
 
-class HomeFragment : Fragment() {
+class HomeFragment : BaseFragment<FragmentHomeBinding, HomeViewModel>() {
     private var fyOrderAdapter: BaseAdapter<String, ItemMenuBinding>? = null
-    private var _binding: FragmentHomeBinding? = null
-    private val binding get() = _binding!!
-    private lateinit var homeViewModel: HomeViewModel
-    override fun onCreateView(
-        inflater: LayoutInflater,
-        container: ViewGroup?,
-        savedInstanceState: Bundle?
-    ): View {
 
-        _binding = FragmentHomeBinding.inflate(inflater, container, false)
-        homeViewModel = ViewModelProvider(this)[HomeViewModel::class.java]
-        homeViewModel.items.observe(viewLifecycleOwner) {
+    override fun setupViewModel() {
+        viewModel = ViewModelProvider(this)[HomeViewModel::class.java]
+
+    }
+
+    override fun setupView() {
+        viewModel.items.observe(viewLifecycleOwner) {
             initList(it)
         }
-        return binding.root
     }
 
+    override fun setupBinding(
+        inflater: LayoutInflater,
+        container: ViewGroup?
+    ): FragmentHomeBinding {
+        return FragmentHomeBinding.inflate(inflater, container, false)
+    }
+
+
     private fun initList(newList: MutableList<String>) {
         fyOrderAdapter?.let {
             it.setList(newList)
@@ -47,7 +48,7 @@ class HomeFragment : Fragment() {
 
                 override fun onBindView(bind: ItemMenuBinding, data: String, position: Int) {
                     bind.name.text = data
-                    homeViewModel.itemsImg.value?.get(position)
+                    viewModel.itemsImg.value?.get(position)
                         ?.let { bind.name.setCompoundDrawablesWithIntrinsicBounds(0, it, 0, 0) }
                 }
             }.also {
@@ -70,7 +71,7 @@ class HomeFragment : Fragment() {
                             ).navigate(R.id.navigation_sms, bundle)
                         }
 
-                        2 -> {
+                        else -> {
                             ToastUtils.showLong(item)
                         }
                     }
@@ -83,9 +84,4 @@ class HomeFragment : Fragment() {
     }
 
 
-    override fun onDestroyView() {
-        super.onDestroyView()
-        _binding = null
-    }
-
 }

+ 22 - 31
UtilsApplication/app/app/src/main/java/com/hx/utils/application/ui/sms/SmsFragment.kt

@@ -9,18 +9,17 @@ import android.app.role.RoleManager
 import android.content.ContentValues
 import android.content.Intent
 import android.os.Build
-import android.os.Bundle
 import android.provider.Telephony
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
 import androidx.appcompat.app.AppCompatActivity
-import androidx.fragment.app.Fragment
 import androidx.lifecycle.ViewModelProvider
 import com.blankj.utilcode.util.AppUtils
 import com.blankj.utilcode.util.PermissionUtils
 import com.blankj.utilcode.util.TimeUtils
 import com.blankj.utilcode.util.ToastUtils
+import com.hx.utils.application.base.BaseFragment
 import com.hx.utils.application.databinding.FragmentSmsBinding
 import kotlinx.coroutines.DelicateCoroutinesApi
 import kotlinx.coroutines.Dispatchers
@@ -29,25 +28,17 @@ import kotlinx.coroutines.delay
 import kotlinx.coroutines.launch
 import java.util.Calendar
 
-class SmsFragment : Fragment() {
-    private var _binding: FragmentSmsBinding? = null
-    private val binding get() = _binding!!
-    private lateinit var smsViewModel: SmsViewModel
-
-    override fun onCreateView(
-        inflater: LayoutInflater,
-        container: ViewGroup?,
-        savedInstanceState: Bundle?
-    ): View {
-        _binding = FragmentSmsBinding.inflate(inflater, container, false)
-        val root: View = binding.root
-        smsViewModel = ViewModelProvider(this)[SmsViewModel::class.java]
-        binding.loading.visibility = View.GONE
+class SmsFragment : BaseFragment<FragmentSmsBinding, SmsViewModel>() {
+
+    override fun setupViewModel() {
+        viewModel = ViewModelProvider(this)[SmsViewModel::class.java]
+    }
 
-        smsViewModel.showTime.observe(viewLifecycleOwner) {
+    override fun setupView() {
+        binding.loading.visibility = View.GONE
+        viewModel.showTime.observe(viewLifecycleOwner) {
             binding.time.setText(it)
         }
-
         binding.insertMms.setOnClickListener {
             val smsPackageName = Telephony.Sms.getDefaultSmsPackage(context)
             val currentPackageName = context?.packageName
@@ -99,22 +90,22 @@ class SmsFragment : Fragment() {
         }
 
         binding.radioGroup.setOnCheckedChangeListener { _, checkedId ->
-            smsViewModel.read.value = binding.radioButton1.id == checkedId
+            viewModel.read.value = binding.radioButton1.id == checkedId
         }
 
         (activity as? AppCompatActivity)?.supportActionBar?.title = arguments?.getString("data")
-        return root
     }
 
+    override fun setupBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentSmsBinding {
+        return FragmentSmsBinding.inflate(inflater, container, false)
+    }
+
+
     override fun onStart() {
         super.onStart()
         getPermissions()
     }
 
-    override fun onDestroyView() {
-        super.onDestroyView()
-        _binding = null
-    }
 
     fun getPermissions() {
         val permissions = listOf(SEND_SMS, RECEIVE_SMS, READ_SMS).toTypedArray()
@@ -144,9 +135,9 @@ class SmsFragment : Fragment() {
         try {
             val values = ContentValues().apply {
                 put(Telephony.Sms.ADDRESS, binding.name.text.toString())
-                put(Telephony.Sms.DATE, smsViewModel.time.value)
-                put(Telephony.Sms.DATE_SENT, smsViewModel.time.value?.minus(5000))
-                put(Telephony.Sms.READ, smsViewModel.read.value)
+                put(Telephony.Sms.DATE, viewModel.time.value)
+                put(Telephony.Sms.DATE_SENT, viewModel.time.value?.minus(5000))
+                put(Telephony.Sms.READ, viewModel.read.value)
                 put(Telephony.Sms.TYPE, 1)
                 put(Telephony.Sms.BODY, binding.message.text.toString())
                 put(Telephony.Sms.TYPE, Telephony.Sms.MESSAGE_TYPE_INBOX)
@@ -155,8 +146,8 @@ class SmsFragment : Fragment() {
 
             binding.name.text.clear()
             binding.message.text.clear()
-            smsViewModel.time.postValue(0)
-            smsViewModel.showTime.postValue("")
+            viewModel.time.postValue(0)
+            viewModel.showTime.postValue("")
 
         } catch (e: Exception) {
             e.message?.let {
@@ -196,8 +187,8 @@ class SmsFragment : Fragment() {
     }
 
     private fun handleSelectedTimestamp(timestamp: Long, showTxt: String) {
-        smsViewModel.time.postValue(timestamp)
-        smsViewModel.showTime.postValue(showTxt)
+        viewModel.time.postValue(timestamp)
+        viewModel.showTime.postValue(showTxt)
 
 
     }

+ 24 - 31
UtilsApplication/app/app/src/main/java/com/hx/utils/application/ui/web/WebFragment.kt

@@ -1,40 +1,34 @@
 package com.hx.utils.application.ui.web
 
-import android.os.Bundle
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
 import android.widget.LinearLayout
 import androidx.appcompat.app.AlertDialog
 import androidx.appcompat.app.AppCompatActivity
-import androidx.fragment.app.Fragment
 import androidx.lifecycle.ViewModelProvider
 import com.hx.utils.application.BuildConfig
+import com.hx.utils.application.base.BaseFragment
 import com.hx.utils.application.base.BaseWebView
 import com.hx.utils.application.databinding.FragmentWebBinding
 
-class WebFragment : Fragment() {
-    private var _binding: FragmentWebBinding? = null
+class WebFragment : BaseFragment<FragmentWebBinding, WebViewModel>() {
     private var webView: BaseWebView? = null
-    private val binding get() = _binding!!
-    private lateinit var webViewModel: WebViewModel
-    override fun onCreateView(
-        inflater: LayoutInflater,
-        container: ViewGroup?,
-        savedInstanceState: Bundle?
-    ): View {
-        _binding = FragmentWebBinding.inflate(inflater, container, false)
-        val root: View = binding.root
-        webViewModel = ViewModelProvider(this)[WebViewModel::class.java]
-
-        webViewModel.urlFormState.observe(viewLifecycleOwner) {
-            binding.fab.show()
-            binding.loading.visibility = View.GONE
-            if (it.success == true)
-                successTip()
-            else
-                errorTip()
+    override fun setupViewModel() {
+        viewModel = ViewModelProvider(this)[WebViewModel::class.java]
+    }
 
+    override fun setupView() {
+        viewModel.urlFormState.observe(viewLifecycleOwner) {
+            binding.apply {
+                fab.show()
+                loading.visibility = View.GONE
+            }
+            when (it.success) {
+                true -> successTip()
+                false -> errorTip()
+                null -> {}
+            }
         }
         webView?.loadUrl(BuildConfig.testUrl) ?: run {
             webView = BaseWebView(requireActivity()).apply {
@@ -50,25 +44,24 @@ class WebFragment : Fragment() {
         binding.fab.setOnClickListener {
             binding.fab.hide()
             binding.loading.visibility = View.VISIBLE
-            webViewModel.startListening()
+            viewModel.startListening()
         }
         (activity as? AppCompatActivity)?.supportActionBar?.title = arguments?.getString("data")
-        return root
     }
 
-    override fun onDestroyView() {
-        super.onDestroyView()
-        _binding = null
+    override fun setupBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentWebBinding {
+        return FragmentWebBinding.inflate(inflater, container, false)
     }
 
+
     private fun successTip() {
         val dialog = AlertDialog.Builder(requireActivity())
             .setTitle("WebTestBot Message")
             .setMessage(
-                "URL: ${webViewModel.urlFormState.value!!.url}\nName: ${webViewModel.urlFormState.value!!.username}\nFirst Name: ${webViewModel.urlFormState.value!!.firstName}"
+                "URL: ${viewModel.urlFormState.value!!.url}\nName: ${viewModel.urlFormState.value!!.username}\nFirst Name: ${viewModel.urlFormState.value!!.firstName}"
             )
             .setPositiveButton("Go") { dialogInterface, _ ->
-                webView?.loadUrl(webViewModel.urlFormState.value!!.url)
+                webView?.loadUrl(viewModel.urlFormState.value!!.url)
                 dialogInterface.dismiss()
             }
             .setNegativeButton("Cancel") { dialogInterface, _ ->
@@ -82,10 +75,10 @@ class WebFragment : Fragment() {
         val dialog = AlertDialog.Builder(requireActivity())
             .setTitle("Error Info")
             .setMessage(
-                webViewModel.urlFormState.value!!.error
+                viewModel.urlFormState.value!!.error
             )
             .setPositiveButton("Ok") { dialogInterface, _ ->
-                webView?.loadUrl(webViewModel.urlFormState.value!!.url)
+                webView?.loadUrl(viewModel.urlFormState.value!!.url)
                 dialogInterface.dismiss()
             }
             .create()