DevModeView.kt 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package com.develop.airfryer.ui
  2. import android.content.Context
  3. import android.content.Intent
  4. import android.util.AttributeSet
  5. import android.view.LayoutInflater
  6. import android.view.View
  7. import androidx.constraintlayout.widget.ConstraintLayout
  8. import com.blankj.utilcode.util.ActivityUtils.startActivity
  9. import com.blankj.utilcode.util.AppUtils
  10. import com.develop.airfryer.databinding.DevModeViewBinding
  11. import com.kuyuntech.cofarcooking.device.sdk.constant.core.CommonEventTypes
  12. import com.kuyuntech.cofarcooking.device.sdk.constant.core.DevModes
  13. import com.kuyuntech.cofarcooking.device.sdk.eventbus.event.DevCommonEvent
  14. import com.kuyuntech.cofarcooking.device.sdk.util.core.CofarSDK
  15. import org.greenrobot.eventbus.Subscribe
  16. class DevModeView(context: Context, attrs: AttributeSet) : ConstraintLayout(context, attrs){
  17. private var mode = CofarSDK.devInfo().devMode;
  18. private val binding: DevModeViewBinding
  19. init {
  20. binding = DevModeViewBinding.inflate(LayoutInflater.from(context), this, true)
  21. setupClickListener()
  22. CofarSDK.register(this)
  23. updateUI()
  24. if(!AppUtils.getAppVersionName().split(".")[0].startsWith("5")){
  25. this.visibility = GONE
  26. }
  27. }
  28. override fun onDetachedFromWindow() {
  29. super.onDetachedFromWindow()
  30. CofarSDK.unregister(this);
  31. }
  32. @Subscribe
  33. fun onDevModeChange(event: DevCommonEvent){
  34. if(CommonEventTypes.DEV_MODE_CHANGE == event.type){
  35. mode = CofarSDK.devInfo().devMode;
  36. updateUI();
  37. val intent = Intent(this.context, Class.forName("com.develop.main.ui.ModeEntranceActivity"))
  38. intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
  39. this.context.startActivity(intent)
  40. }
  41. }
  42. private fun updateUI(){
  43. //判断模式
  44. if(mode == DevModes.AIR_FRYER){
  45. binding.dfbAtive.visibility = View.GONE;
  46. binding.dfbUnactive.visibility = View.VISIBLE;
  47. binding.kqzgActive.visibility = View.VISIBLE;
  48. binding.kqzgUnactive.visibility = View.GONE;
  49. }else{
  50. binding.dfbAtive.visibility = View.VISIBLE;
  51. binding.dfbUnactive.visibility = View.GONE;
  52. binding.kqzgActive.visibility = View.GONE;
  53. binding.kqzgUnactive.visibility = View.VISIBLE;
  54. }
  55. }
  56. private fun setupClickListener(){
  57. setOnClickListener {
  58. CofarSDK.changeDevMode(if (mode ==DevModes.SOUP) DevModes.AIR_FRYER else DevModes.SOUP)
  59. }
  60. }
  61. }