Skip to content
This repository was archived by the owner on Jul 2, 2025. It is now read-only.

Commit 0bfd15c

Browse files
Fix code reviews
Still have some automatically generated unused code blocks and comments. Will delete them in the future, in case they will be used.
1 parent 54ce5cc commit 0bfd15c

File tree

6 files changed

+31
-54
lines changed

6 files changed

+31
-54
lines changed

android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/MainActivity.kt

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,11 @@ import com.google.android.material.bottomnavigation.BottomNavigationView
77

88
class MainActivity : AppCompatActivity() {
99

10-
private lateinit var runFragment: RunFragment
11-
private lateinit var mapsFragment: MapsFragment
12-
private lateinit var meFragment: MeFragment
13-
14-
1510
override fun onCreate(savedInstanceState: Bundle?) {
1611
super.onCreate(savedInstanceState)
1712
setContentView(R.layout.activity_main)
1813

19-
runFragment = RunFragment()
14+
val runFragment = RunFragment()
2015
supportFragmentManager
2116
.beginTransaction()
2217
.replace(R.id.fragment_container, runFragment)
@@ -28,7 +23,7 @@ class MainActivity : AppCompatActivity() {
2823
when (item.itemId) {
2924

3025
R.id.bottom_navigation_item_run -> {
31-
runFragment = RunFragment()
26+
val runFragment = RunFragment()
3227
supportFragmentManager
3328
.beginTransaction()
3429
.replace(R.id.fragment_container, runFragment)
@@ -38,7 +33,7 @@ class MainActivity : AppCompatActivity() {
3833
}
3934

4035
R.id.bottom_navigation_item_map -> {
41-
mapsFragment = MapsFragment()
36+
val mapsFragment = MapsFragment()
4237
supportFragmentManager
4338
.beginTransaction()
4439
.replace(R.id.fragment_container, mapsFragment)
@@ -48,7 +43,7 @@ class MainActivity : AppCompatActivity() {
4843
}
4944

5045
R.id.bottom_navigation_item_profile -> {
51-
meFragment = MeFragment()
46+
val meFragment = MeFragment()
5247
supportFragmentManager
5348
.beginTransaction()
5449
.replace(R.id.fragment_container, meFragment)

android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/RunFragment.kt

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ package com.google.samples.quickstart.canonical
22

33
import android.os.Bundle
44
import android.os.SystemClock
5-
import android.util.Log
65
import androidx.fragment.app.Fragment
76
import android.view.LayoutInflater
87
import android.view.View
98
import android.view.ViewGroup
109
import android.widget.Button
1110
import android.widget.Chronometer
1211
import androidx.databinding.DataBindingUtil
13-
import androidx.databinding.ViewDataBinding
1412
import androidx.lifecycle.ViewModelProviders
1513
import com.google.samples.quickstart.canonical.databinding.FragmentRunBinding
1614
import kotlinx.android.synthetic.main.fragment_run.*
@@ -30,24 +28,19 @@ class RunFragment : Fragment() {
3028
private var param1: String? = null
3129
private var param2: String? = null
3230

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
3733

3834
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!!
4137
chronometer.start()
4238
chronometer.showContextMenu()
43-
stopwatchViewModel.setWorkingStatus(true)
44-
true
39+
stopwatchVM.setWorkingStatus(true)
4540
} else {
46-
pauseOffset = SystemClock.elapsedRealtime() - chronometer.base
4741
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)
5144
}
5245
}
5346

@@ -58,22 +51,18 @@ class RunFragment : Fragment() {
5851
param2 = it.getString(ARG_PARAM2)
5952
}
6053

61-
stopwatchViewModel = activity?.run {
62-
ViewModelProviders.of(this)[StopwatchView::class.java]
54+
stopwatchVM = activity?.run {
55+
ViewModelProviders.of(this)[StopwatchViewModel::class.java]
6356
} ?: throw Exception("Null Activity")
64-
65-
pauseOffset = stopwatchViewModel.pauseOffset.value!!
66-
isWorking = stopwatchViewModel.isWorking.value!!
67-
fragmentPauseStartTime = stopwatchViewModel.fragmentPauseStartTime.value!!
6857
}
6958

7059
override fun onCreateView(
7160
inflater: LayoutInflater, container: ViewGroup?,
7261
savedInstanceState: Bundle?
7362
): 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)
7564
binding.lifecycleOwner = this
76-
binding.stopwatchViewModel = stopwatchViewModel
65+
binding.stopwatchViewModel = stopwatchVM
7766
return binding.root
7867
}
7968

@@ -99,33 +88,29 @@ class RunFragment : Fragment() {
9988

10089
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
10190
super.onViewCreated(view, savedInstanceState)
102-
val chronometer = view.findViewById<Chronometer>(R.id.chronometer)
103-
val runBtn = view.findViewById<Button>(R.id.run_btn)
10491

105-
runBtn.setOnClickListener {
106-
startStopTimer(chronometer)
92+
binding.runBtn.setOnClickListener {
93+
startStopTimer(binding.chronometer)
10794
}
10895

10996
}
11097

11198
override fun onResume() {
11299
super.onResume()
113-
if (isWorking) {
114-
chronometer?.base = fragmentPauseStartTime - pauseOffset
100+
if (stopwatchVM.isWorking.value!!) {
101+
chronometer?.base = stopwatchVM.fragmentPauseStartTime.value!! - stopwatchVM.pauseOffset.value!!
115102
chronometer?.start()
116103
} else {
117-
chronometer?.base = SystemClock.elapsedRealtime() - pauseOffset
104+
chronometer?.base = SystemClock.elapsedRealtime() - stopwatchVM.pauseOffset.value!!
118105
chronometer?.stop()
119106
}
120107
}
121108

122109
override fun onPause() {
123110
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())
129114
}
130115
}
131116
}

android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/StopwatchView.kt renamed to android/canonical/app/src/main/java/com/google/samples/quickstart/canonical/StopwatchViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.google.samples.quickstart.canonical
33
import androidx.lifecycle.MutableLiveData
44
import androidx.lifecycle.ViewModel
55

6-
class StopwatchView : ViewModel() {
6+
class StopwatchViewModel : ViewModel() {
77

88
var pauseOffset = MutableLiveData<Long>(0L)
99
var isWorking = MutableLiveData<Boolean>(false)

android/canonical/app/src/main/res/layout/fragment_run.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<data>
77
<variable
88
name="stopwatchViewModel"
9-
type="com.google.samples.quickstart.canonical.StopwatchView" />
9+
type="com.google.samples.quickstart.canonical.StopwatchViewModel" />
1010
</data>
1111

1212
<androidx.constraintlayout.widget.ConstraintLayout
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
3-
<color name="color_primary">#6200EE</color>
4-
<color name="color_primary_dark">#3700B3</color>
5-
<color name="color_accent">#03DAC5</color>
3+
<color name="colorPrimary">#6200EE</color>
4+
<color name="colorPrimaryDark">#3700B3</color>
5+
<color name="colorAccent">#03DAC5</color>
66
</resources>
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
<resources>
2-
<!-- Base application theme. -->
32
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
4-
<!-- Customize your theme here. -->
5-
<item name="colorPrimary">@color/color_primary</item>
6-
<item name="colorPrimaryDark">@color/color_primary_dark</item>
7-
<item name="colorAccent">@color/color_accent</item>
3+
<item name="colorPrimary">@color/colorPrimary</item>
4+
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
5+
<item name="colorAccent">@color/colorAccent</item>
86
</style>
9-
107
</resources>

0 commit comments

Comments
 (0)