diff --git a/DessertClicker-Starter/README.md b/DessertClicker-Starter/README.md index fcb675109..4cfe4abdd 100644 --- a/DessertClicker-Starter/README.md +++ b/DessertClicker-Starter/README.md @@ -46,4 +46,5 @@ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under -the License. \ No newline at end of file +the License. + diff --git a/DessertClicker-Starter/app/build.gradle b/DessertClicker-Starter/app/build.gradle index cbbc6e2b8..7ab8fb41a 100644 --- a/DessertClicker-Starter/app/build.gradle +++ b/DessertClicker-Starter/app/build.gradle @@ -44,4 +44,5 @@ dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.0-rc1' + implementation 'com.jakewharton.timber:timber:4.7.1' } diff --git a/DessertClicker-Starter/app/src/main/AndroidManifest.xml b/DessertClicker-Starter/app/src/main/AndroidManifest.xml index fc3f9b373..a04354690 100644 --- a/DessertClicker-Starter/app/src/main/AndroidManifest.xml +++ b/DessertClicker-Starter/app/src/main/AndroidManifest.xml @@ -17,10 +17,27 @@ + + diff --git a/DessertClicker-Starter/app/src/main/java/com/example/android/dessertclicker/ClickerApplication.kt b/DessertClicker-Starter/app/src/main/java/com/example/android/dessertclicker/ClickerApplication.kt new file mode 100644 index 000000000..0f71875dd --- /dev/null +++ b/DessertClicker-Starter/app/src/main/java/com/example/android/dessertclicker/ClickerApplication.kt @@ -0,0 +1,11 @@ +package com.example.android.dessertclicker + +import android.app.Application +import timber.log.Timber + +class ClickerApplication : Application() { + override fun onCreate() { + super.onCreate() + Timber.plant(Timber.DebugTree()) + } +} \ No newline at end of file diff --git a/DessertClicker-Starter/app/src/main/java/com/example/android/dessertclicker/DessertTimer.kt b/DessertClicker-Starter/app/src/main/java/com/example/android/dessertclicker/DessertTimer.kt index 9a20fae45..709ee1a6c 100644 --- a/DessertClicker-Starter/app/src/main/java/com/example/android/dessertclicker/DessertTimer.kt +++ b/DessertClicker-Starter/app/src/main/java/com/example/android/dessertclicker/DessertTimer.kt @@ -1,72 +1,88 @@ -///* -// * Copyright 2019, The Android Open Source Project -// * -// * Licensed under the Apache License, Version 2.0 (the "License"); -// * you may not use this file except in compliance with the License. -// * You may obtain a copy of the License at -// * -// * http://www.apache.org/licenses/LICENSE-2.0 -// * -// * Unless required by applicable law or agreed to in writing, software -// * distributed under the License is distributed on an "AS IS" BASIS, -// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// * See the License for the specific language governing permissions and -// * limitations under the License. -// */ -// -//package com.example.android.dessertclicker -// -//import android.os.Handler -//import timber.log.Timber -//import android.os.Looper -///** -// * This is a class representing a timer that you can start or stop. The secondsCount outputs a count of -// * how many seconds since it started, every one second. -// * -// * ----- -// * -// * Handler and Runnable are beyond the scope of this lesson. This is in part because they deal with -// * threading, which is a complex topic that will be covered in a later lesson. -// * -// * If you want to learn more now, you can take a look on the Android Developer documentation on -// * threading: -// * -// * https://developer.android.com/guide/components/processes-and-threads -// * -// */ -//class DessertTimer { -// -// // The number of seconds counted since the timer started -// var secondsCount = 0 -// -// /** -// * [Handler] is a class meant to process a queue of messages (known as [android.os.Message]s) -// * or actions (known as [Runnable]s) -// */ -// private var handler = Handler(Looper.getMainLooper()) -// private lateinit var runnable: Runnable -// -// -// fun startTimer() { -// // Create the runnable action, which prints out a log and increments the seconds counter -// runnable = Runnable { -// secondsCount++ -// Timber.i("Timer is at : $secondsCount") -// // postDelayed re-adds the action to the queue of actions the Handler is cycling -// // through. The delayMillis param tells the handler to run the runnable in -// // 1 second (1000ms) -// handler.postDelayed(runnable, 1000) -// } -// -// // This is what initially starts the timer -// handler.postDelayed(runnable, 1000) -// -// // Note that the Thread the handler runs on is determined by a class called Looper. -// } -// -// fun stopTimer() { -// // Removes all pending posts of runnable from the handler's queue, effectively stopping the -// // timer -// handler.removeCallbacks(runnable) -// } -//} \ No newline at end of file +/* + * Copyright 2019, The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.dessertclicker + +import android.os.Handler +import timber.log.Timber +import android.os.Looper +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.LifecycleObserver +import androidx.lifecycle.OnLifecycleEvent + +/** + * This is a class representing a timer that you can start or stop. The secondsCount outputs a count of + * how many seconds since it started, every one second. + * + * ----- + * + * Handler and Runnable are beyond the scope of this lesson. This is in part because they deal with + * threading, which is a complex topic that will be covered in a later lesson. + * + * If you want to learn more now, you can take a look on the Android Developer documentation on + * threading: + * + * https://developer.android.com/guide/components/processes-and-threads + * + */ +class DessertTimer(lifecycle: Lifecycle) : LifecycleObserver { + + init { + lifecycle.addObserver(this) + } + + //@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) + //fun dummyMethod(){ + //Timber.i("I was called") + //} + + // The number of seconds counted since the timer started + var secondsCount = 0 + + /** + * [Handler] is a class meant to process a queue of messages (known as [android.os.Message]s) + * or actions (known as [Runnable]s) + */ + private var handler = Handler(Looper.getMainLooper()) + private lateinit var runnable: Runnable + + @OnLifecycleEvent(Lifecycle.Event.ON_START) + fun startTimer() { + // Create the runnable action, which prints out a log and increments the seconds counter + runnable = Runnable { + secondsCount++ + Timber.i("Timer is at : $secondsCount") + // postDelayed re-adds the action to the queue of actions the Handler is cycling + // through. The delayMillis param tells the handler to run the runnable in + // 1 second (1000ms) + handler.postDelayed(runnable, 1000) + } + + // This is what initially starts the timer + handler.postDelayed(runnable, 1000) + + // Note that the Thread the handler runs on is determined by a class called Looper. + } + + @OnLifecycleEvent(Lifecycle.Event.ON_STOP) + fun stopTimer() { + // Removes all pending posts of runnable from the handler's queue, effectively stopping the + // timer + handler.removeCallbacks(runnable) + } + + +} \ No newline at end of file diff --git a/DessertClicker-Starter/app/src/main/java/com/example/android/dessertclicker/MainActivity.kt b/DessertClicker-Starter/app/src/main/java/com/example/android/dessertclicker/MainActivity.kt index 82262b06a..7bea201f4 100644 --- a/DessertClicker-Starter/app/src/main/java/com/example/android/dessertclicker/MainActivity.kt +++ b/DessertClicker-Starter/app/src/main/java/com/example/android/dessertclicker/MainActivity.kt @@ -25,11 +25,18 @@ import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ShareCompat import androidx.databinding.DataBindingUtil import com.example.android.dessertclicker.databinding.ActivityMainBinding +//import com.example.android.dessertpusher.R +import timber.log.Timber + +const val KEY_REVENUE = "key_revenue" +const val KEY_DESSERT_SOLD = "dessert_sold" +const val KEY_TIMER_SECONDS = "key_timer_seconds" class MainActivity : AppCompatActivity() { private var revenue = 0 private var dessertsSold = 0 + private lateinit var dessertTimer: DessertTimer // Contains all the views private lateinit var binding: ActivityMainBinding @@ -62,7 +69,10 @@ class MainActivity : AppCompatActivity() { private var currentDessert = allDesserts[0] override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) + super.onCreate(savedInstanceState) //we are overriding any lifecycles + + Timber.i("onCreate called") + //Log.i("MainActivity", "onCreate called") // Use Data Binding to get reference to the views binding = DataBindingUtil.setContentView(this, R.layout.activity_main) @@ -71,6 +81,16 @@ class MainActivity : AppCompatActivity() { onDessertClicked() } + //Create a Dessert Timer + dessertTimer = DessertTimer(this.lifecycle) + + if (savedInstanceState != null) { + revenue = savedInstanceState.getInt(KEY_REVENUE, 0) + dessertsSold = savedInstanceState.getInt(KEY_DESSERT_SOLD, 0) + dessertTimer.secondsCount = savedInstanceState.getInt(KEY_TIMER_SECONDS) + + } + // Set the TextViews to the right values binding.revenue = revenue binding.amountSold = dessertsSold @@ -145,4 +165,49 @@ class MainActivity : AppCompatActivity() { } return super.onOptionsItemSelected(item) } + + //LIFE CYCLE METHODS + override fun onSaveInstanceState(outState: Bundle) { + super.onSaveInstanceState(outState) + outState.putInt(KEY_REVENUE, revenue) + outState.putInt(KEY_DESSERT_SOLD, dessertsSold) + outState.putInt(KEY_TIMER_SECONDS, dessertTimer.secondsCount) + Timber.i("onSaveInstanceState Called") + } + + override fun onStart() { + super.onStart() + //dessertsSold = 0 + Timber.i("onStart Called") + } + + override fun onPause() { + super.onPause() + Timber.i("onPause Called") + } + + override fun onResume() { + super.onResume() + Timber.i("onResume Called") + + } + + override fun onDestroy() { + super.onDestroy() + Timber.i("onDestroy Called") + + } + + override fun onRestart() { + super.onRestart() + Timber.i("onRestart Called") + + } + + override fun onStop() { + super.onStop() + //dessertTimer.stopTimer() + Timber.i("onStop Called") + + } }