iT邦幫忙

2021 iThome 鐵人賽

DAY 19
0
自我挑戰組

Android kotlin &MVVM系列 第 19

Android學習筆記19

今天建立了dialog的viewmodel去實作原本的動作

class Dvm(application: Application): AndroidViewModel(application) {

    val account = MutableLiveData<String>()
    //若要監聽輸入值時使用
    val progressbar = View.VISIBLE
    val progress = MutableLiveData<Int>()

    private val myScope = object : CoroutineScope {
        override val coroutineContext: CoroutineContext
            get() = job
    }
    private val job = Job()

    fun coroutine() {
        myScope.launch(Dispatchers.Main) {
            showprogress()
        }
    }

    private suspend fun showprogress() = withContext(Dispatchers.IO) {
        for (i in 0..100){
            progress.postValue(i)
            SystemClock.sleep(500)
        }
    }
}

那原本我在fragment中我適用Thread去跑的那如果在viewmodel中記得要使用coroutine
原本的fragment就變成這樣

class FragmentDialog: DialogFragment() {

    private val viewModel: DialogViewModel<MainDialogResult> by activityViewModels()

    private val mviewModel by lazy {
        initViewModel(Application(), Dvm::class.java)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val binding = DialogfragmentBinding.inflate(inflater,container,false)
        binding.lifecycleOwner = this
        binding.viewmodel = mviewModel
        binding.login.setOnClickListener {
            viewModel.result.postValue(MainDialogResult.Ok(binding.account.text.toString() , binding.password.text.toString()))
            dismiss()
        }

        binding.cancel.setOnClickListener {
            viewModel.result.postValue(MainDialogResult.Cancel)
            dismiss()
        }

        mviewModel.coroutine()

        return binding.root
    }
    
}

是不是簡潔了不少,雖然我也想把onclickk寫在viewmodel但是因為他的資料值是跟activity連結的,所以想過去雖然不會報錯,但就無法轉移資料
最後是xml

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android">
   <data>
      <variable
          name="viewmodel"
          type="com.example.myapplication.Dvm" />

      <variable
          name="data"
          type="com.example.myapplication.ViewModel" />
   </data>
   <LinearLayout
       android:orientation="vertical"
       android:layout_height="wrap_content"
       android:layout_width="match_parent">
      <EditText
          android:id="@+id/account"
          android:text="@={viewmodel.account}"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:hint="account"/>
      <EditText
          android:id="@+id/password"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:hint="password"/>
      <ProgressBar
          android:visibility="@{viewmodel.progressbar}"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          style="?android:attr/progressBarStyle"
          android:layout_gravity="center" />
      <ProgressBar
          android:max="100"
          android:progress="@{viewmodel.progress}"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          style="?android:attr/progressBarStyleHorizontal"
          android:layout_gravity="center"
          android:id="@+id/progressBar2" />
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal">
         <Button
             android:id="@+id/login"
             android:layout_width="0dp"
             android:layout_height="wrap_content"
             android:layout_weight="0.5"
             android:text="login"/>
         <Button
             android:id="@+id/cancel"
             android:layout_width="0dp"
             android:layout_height="wrap_content"
             android:layout_weight="0.5"
             android:text="cancel"/>

      </LinearLayout>
   </LinearLayout>

</layout>

上一篇
Android學習筆記18
下一篇
Android學習筆記20
系列文
Android kotlin &MVVM30

尚未有邦友留言

立即登入留言