@@ -2,15 +2,13 @@ package com.google.samples.quickstart.canonical
2
2
3
3
import android.os.Bundle
4
4
import android.os.SystemClock
5
- import android.util.Log
6
5
import androidx.fragment.app.Fragment
7
6
import android.view.LayoutInflater
8
7
import android.view.View
9
8
import android.view.ViewGroup
10
9
import android.widget.Button
11
10
import android.widget.Chronometer
12
11
import androidx.databinding.DataBindingUtil
13
- import androidx.databinding.ViewDataBinding
14
12
import androidx.lifecycle.ViewModelProviders
15
13
import com.google.samples.quickstart.canonical.databinding.FragmentRunBinding
16
14
import kotlinx.android.synthetic.main.fragment_run.*
@@ -30,24 +28,19 @@ class RunFragment : Fragment() {
30
28
private var param1: String? = null
31
29
private var param2: String? = null
32
30
33
- private lateinit var stopwatchViewModel: StopwatchView
34
- private var pauseOffset = 0L
35
- private var isWorking = false
36
- private var fragmentPauseStartTime = 0L
31
+ private lateinit var stopwatchVM: StopwatchViewModel
32
+ private lateinit var binding : FragmentRunBinding
37
33
38
34
private fun startStopTimer (chronometer : Chronometer ) {
39
- isWorking = if (! isWorking) {
40
- chronometer.base = SystemClock .elapsedRealtime() - pauseOffset
35
+ if (! stopwatchVM. isWorking.value !! ) {
36
+ chronometer.base = SystemClock .elapsedRealtime() - stopwatchVM. pauseOffset.value !!
41
37
chronometer.start()
42
38
chronometer.showContextMenu()
43
- stopwatchViewModel.setWorkingStatus(true )
44
- true
39
+ stopwatchVM.setWorkingStatus(true )
45
40
} else {
46
- pauseOffset = SystemClock .elapsedRealtime() - chronometer.base
47
41
chronometer.stop()
48
- stopwatchViewModel.setPauseOffset(SystemClock .elapsedRealtime() - chronometer.base)
49
- stopwatchViewModel.setWorkingStatus(false )
50
- false
42
+ stopwatchVM.setPauseOffset(SystemClock .elapsedRealtime() - chronometer.base)
43
+ stopwatchVM.setWorkingStatus(false )
51
44
}
52
45
}
53
46
@@ -58,22 +51,18 @@ class RunFragment : Fragment() {
58
51
param2 = it.getString(ARG_PARAM2 )
59
52
}
60
53
61
- stopwatchViewModel = activity?.run {
62
- ViewModelProviders .of(this )[StopwatchView ::class .java]
54
+ stopwatchVM = activity?.run {
55
+ ViewModelProviders .of(this )[StopwatchViewModel ::class .java]
63
56
} ? : throw Exception (" Null Activity" )
64
-
65
- pauseOffset = stopwatchViewModel.pauseOffset.value!!
66
- isWorking = stopwatchViewModel.isWorking.value!!
67
- fragmentPauseStartTime = stopwatchViewModel.fragmentPauseStartTime.value!!
68
57
}
69
58
70
59
override fun onCreateView (
71
60
inflater : LayoutInflater , container : ViewGroup ? ,
72
61
savedInstanceState : Bundle ?
73
62
): View ? {
74
- val binding : FragmentRunBinding = DataBindingUtil .inflate(inflater, R .layout.fragment_run, container, false )
63
+ binding = DataBindingUtil .inflate(inflater, R .layout.fragment_run, container, false )
75
64
binding.lifecycleOwner = this
76
- binding.stopwatchViewModel = stopwatchViewModel
65
+ binding.stopwatchViewModel = stopwatchVM
77
66
return binding.root
78
67
}
79
68
@@ -99,33 +88,29 @@ class RunFragment : Fragment() {
99
88
100
89
override fun onViewCreated (view : View , savedInstanceState : Bundle ? ) {
101
90
super .onViewCreated(view, savedInstanceState)
102
- val chronometer = view.findViewById<Chronometer >(R .id.chronometer)
103
- val runBtn = view.findViewById<Button >(R .id.run_btn)
104
91
105
- runBtn.setOnClickListener {
106
- startStopTimer(chronometer)
92
+ binding. runBtn.setOnClickListener {
93
+ startStopTimer(binding. chronometer)
107
94
}
108
95
109
96
}
110
97
111
98
override fun onResume () {
112
99
super .onResume()
113
- if (isWorking) {
114
- chronometer?.base = fragmentPauseStartTime - pauseOffset
100
+ if (stopwatchVM. isWorking.value !! ) {
101
+ chronometer?.base = stopwatchVM. fragmentPauseStartTime.value !! - stopwatchVM. pauseOffset.value !!
115
102
chronometer?.start()
116
103
} else {
117
- chronometer?.base = SystemClock .elapsedRealtime() - pauseOffset
104
+ chronometer?.base = SystemClock .elapsedRealtime() - stopwatchVM. pauseOffset.value !!
118
105
chronometer?.stop()
119
106
}
120
107
}
121
108
122
109
override fun onPause () {
123
110
super .onPause()
124
- if (isWorking) {
125
- pauseOffset = SystemClock .elapsedRealtime() - chronometer.base
126
- fragmentPauseStartTime = SystemClock .elapsedRealtime()
127
- stopwatchViewModel.setPauseOffset(pauseOffset)
128
- stopwatchViewModel.setFragmentPauseStartTime(fragmentPauseStartTime)
111
+ if (stopwatchVM.isWorking.value!! ) {
112
+ stopwatchVM.setPauseOffset(SystemClock .elapsedRealtime() - chronometer.base)
113
+ stopwatchVM.setFragmentPauseStartTime(SystemClock .elapsedRealtime())
129
114
}
130
115
}
131
116
}
0 commit comments