Skip to content
Open
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ Access all branches from this tab.

The branches are also accessible from the drop-down in the "Code" tab.

## Requirements

1. Android Studio (Jellyfish or above)
2. JDK 21 with `JAVA_HOME` environment variable set. If you don't have JDK 21 installed or `JAVA_HOME` is not set, consider using a tool like `sdkman` to simplify the process. Refer to the sdkman documentation for installation instructions: [sdkman installation](https://sdkman.io/install)

## Working with the Course Code

Expand Down
43 changes: 29 additions & 14 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,20 @@

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
// Kotlin Android Extensions is deprecated, which means that using Kotlin synthetics for view binding is no longer supported.
// apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'androidx.navigation.safeargs'

android {
compileSdkVersion 28
compileSdk 34
defaultConfig {
applicationId "com.example.android.trackmysleepquality"
minSdkVersion 19
targetSdkVersion 28
minSdkVersion 21
targetSdkVersion 34
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
vectorDrawables.useSupportLibrary = true
}
buildTypes {
Expand All @@ -39,15 +40,21 @@ android {
}

// Enables data binding.
dataBinding {
enabled = true
buildFeatures {
dataBinding true
}
namespace = "com.example.android.trackmysleepquality"

compileOptions {
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$version_kotlin"
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$version_kotlin"

// Support libraries
implementation "androidx.constraintlayout:constraintlayout:$version_constraint_layout"
Expand All @@ -58,19 +65,27 @@ dependencies {
// Room and Lifecycle dependencies
implementation "androidx.room:room-runtime:$version_room"
kapt "androidx.room:room-compiler:$version_room"
implementation "androidx.lifecycle:lifecycle-extensions:$version_lifecycle_extensions"
// The APIs in lifecycle-extensions have been deprecated. Instead, add dependencies for the specific Lifecycle artifacts you need.
// implementation "androidx.lifecycle:lifecycle-extensions:$version_lifecycle_extensions"

// Coroutines
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$version_coroutine"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$version_coroutine"

// Navigation
implementation "android.arch.navigation:navigation-fragment-ktx:$version_navigation"
implementation "android.arch.navigation:navigation-ui-ktx:$version_navigation"
implementation "androidx.navigation:navigation-fragment-ktx:$version_navigation"
implementation "androidx.navigation:navigation-ui-ktx:$version_navigation"

// ViewModel and LiveData
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$version_lifecycle"

// Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:$version_room"


// Testing
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
android:roundIcon="@mipmap/ic_launcher_sleep_tracker_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import androidx.room.Update
interface SleepDatabaseDao {

@Insert
fun insert(night: SleepNight)
suspend fun insert(night: SleepNight)

/**
* When updating a row with a value already set in a column,
Expand All @@ -38,23 +38,23 @@ interface SleepDatabaseDao {
* @param night new value to write
*/
@Update
fun update(night: SleepNight)
suspend fun update(night: SleepNight)

/**
* Selects and returns the row that matches the supplied start time, which is our key.
*
* @param key startTimeMilli to match
*/
@Query("SELECT * from daily_sleep_quality_table WHERE nightId = :key")
fun get(key: Long): SleepNight?
suspend fun get(key: Long): SleepNight?

/**
* Deletes all values from the table.
*
* This does not delete the table, only its contents.
*/
@Query("DELETE FROM daily_sleep_quality_table")
fun clear()
suspend fun clear()

/**
* Selects and returns all rows in the table,
Expand All @@ -68,7 +68,7 @@ interface SleepDatabaseDao {
* Selects and returns the latest night.
*/
@Query("SELECT * FROM daily_sleep_quality_table ORDER BY nightId DESC LIMIT 1")
fun getTonight(): SleepNight?
suspend fun getTonight(): SleepNight?

}

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.example.android.trackmysleepquality.sleeptracker

import android.app.Application
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.AndroidViewModel
import com.example.android.trackmysleepquality.database.SleepDatabaseDao

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SleepTrackerViewModelFactory(
private val dataSource: SleepDatabaseDao,
private val application: Application) : ViewModelProvider.Factory {
@Suppress("unchecked_cast")
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(SleepTrackerViewModel::class.java)) {
return SleepTrackerViewModel(dataSource, application) as T
}
Expand Down
23 changes: 12 additions & 11 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,26 @@
buildscript {

ext {
version_core = "1.0.1"
version_coroutine = "1.1.0"
version_constraint_layout = "1.1.3"
version_gradle = '3.3.0'
version_kotlin = "1.3.21"
version_lifecycle_extensions = "2.0.0"
version_navigation = '1.0.0-beta02'
version_room = "2.0.0"
version_core = "1.13.1"
version_coroutine = "1.8.1"
version_constraint_layout = "2.1.4"
version_gradle = '8.4.2'
version_kotlin = '1.9.23'
// version_lifecycle_extensions = "2.2.0"
version_lifecycle = '2.8.2'
version_navigation = '2.7.7'
version_room = '2.6.1'
}

repositories {
mavenCentral()
google()
jcenter()
}

dependencies {
classpath "com.android.tools.build:gradle:$version_gradle"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$version_kotlin"
classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:$version_navigation"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$version_navigation"

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand All @@ -46,8 +47,8 @@ buildscript {

allprojects {
repositories {
mavenCentral()
google()
jcenter()
}
}

Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue Oct 02 13:39:27 PDT 2018
#Tue Aug 11 17:39:48 PDT 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip