1 /* <lambda>null2 * Copyright 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.supportv4.app 18 19 import android.os.Bundle 20 import android.view.View 21 import android.widget.Button 22 import android.widget.ProgressBar 23 import androidx.fragment.app.Fragment 24 import androidx.fragment.app.FragmentActivity 25 import androidx.fragment.app.viewModels 26 import androidx.lifecycle.LiveData 27 import androidx.lifecycle.MutableLiveData 28 import androidx.lifecycle.ViewModel 29 import androidx.lifecycle.viewModelScope 30 import com.example.android.supportv4.R 31 import kotlinx.coroutines.delay 32 import kotlinx.coroutines.launch 33 34 class FragmentViewModelSupport : FragmentActivity() { 35 override fun onCreate(savedInstanceState: Bundle?) { 36 super.onCreate(savedInstanceState) 37 38 // First time init, create the UI. 39 if (savedInstanceState == null) { 40 supportFragmentManager 41 .beginTransaction() 42 .add(android.R.id.content, UiFragment()) 43 .commit() 44 } 45 } 46 47 /** 48 * This is a fragment showing UI that will be updated from work done in the ProgressViewModel. 49 */ 50 class UiFragment : Fragment(R.layout.fragment_view_model) { 51 52 private val progressViewModel: ProgressViewModel by viewModels() 53 54 override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 55 super.onViewCreated(view, savedInstanceState) 56 view.findViewById<Button>(R.id.restart).setOnClickListener { 57 progressViewModel.restart() 58 } 59 60 val progresssBar = view.findViewById<ProgressBar>(R.id.progress_horizontal) 61 progressViewModel.max = progresssBar.max 62 progressViewModel.progressLiveData.observe(viewLifecycleOwner) { progress -> 63 progresssBar.progress = progress 64 } 65 } 66 } 67 68 /** 69 * This is the ViewModel implementation that will be retained across activity instances. It 70 * represents some ongoing work, here a Job we have that sits around incrementing a progress 71 * indicator. 72 */ 73 class ProgressViewModel : ViewModel() { 74 private var progress = 0 75 private val _progressLiveData = MutableLiveData<Int>(progress) 76 val progressLiveData: LiveData<Int> 77 get() = _progressLiveData 78 79 var max = 10000 80 81 init { 82 // Using viewModelScope ensures that the Job is automatically cancelled 83 // when the ViewModel is cleared 84 viewModelScope.launch { 85 while (true) { 86 if (progress < max) { 87 _progressLiveData.value = progress++ 88 } 89 delay(50) 90 } 91 } 92 } 93 94 fun restart() { 95 progress = 0 96 } 97 } 98 } 99