Skip to content
This repository has been archived by the owner on Jan 11, 2024. It is now read-only.

Mywork #64

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DessertClicker-Starter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
the License.

1 change: 1 addition & 0 deletions DessertClicker-Starter/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
19 changes: 18 additions & 1 deletion DessertClicker-Starter/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,27 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.dessertclicker">

<!-- <application
android:allowBackup="true"
android:icon="@mipmap/ic_dessert_clicker"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_dessert_clicker_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.example.android.dessertclicker.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> -->

<application
android:name=".ClickerApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_dessert_clicker"
android:label="@string/app_name"
android:label="Dessert Pusher"
android:roundIcon="@mipmap/ic_dessert_clicker_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
Expand Down
Original file line number Diff line number Diff line change
@@ -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())
}
}
Original file line number Diff line number Diff line change
@@ -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)
// }
//}
/*
* 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)
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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")

}
}