Coder Social home page Coder Social logo

andfun-kotlin-sleep-tracker's Introduction

Room - SleepQualityTracker app

This is the toy app for Lesson 6 of the Android App Development in Kotlin course on Udacity.

SleepQualityTracker

The SleepQualityTracker app is a demo app that helps you collect information about your sleep.

  • Start time
  • End time
  • Quality
  • Time slept

This app demonstrates the following views and techniques:

  • Room database
  • DAO
  • Coroutines

It also uses and builds on the following techniques from previous lessons:

  • Transformation map
  • Data Binding in XML files
  • ViewModel Factory
  • Using Backing Properties to protect MutableLiveData
  • Observable state LiveData variables to trigger navigation

Screenshots

Screenshot1 Screenshot2 Screenshot3

How to use this repo while taking the course

Each code repository in this class has a chain of commits that looks like this:

listofcommits

These commits show every step you'll take to create the app. Each commit contains instructions for completing the that step.

Each commit also has a branch associated with it of the same name as the commit message, as seen below:

branches Access all branches from this tab.

listofbranches

branchesdropdown

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

Working with the Course Code

Here are the basic steps for working with and completing exercises in the repo.

The basic steps are:

  1. Clone the repo.
  2. Check out the branch corresponding to the step you want to attempt.
  3. Find and complete the TODOs.
  4. Optionally commit your code changes.
  5. Compare your code with the solution.
  6. Repeat steps 2-5 until you've gone trough all the steps to complete the toy app.

Step 1: Clone the repo

As you go through the course, you'll be instructed to clone the different exercise repositories, so you don't need to set these up now. You can clone a repository from github in a folder of your choice with the command:

git clone https://github.com/udacity/REPOSITORY_NAME.git

Step 2: Check out the step branch

As you go through different steps in the code, you'll be told which step you're on, as well as a link to the corresponding branch.

You'll want to check out the branch associated with that step. The command to check out a branch would be:

git checkout BRANCH_NAME

Step 3: Find and complete the TODOs

Once you've checked out the branch, you'll have the code in the exact state you need. You'll even have TODOs, which are special comments that tell you all the steps you need to complete the exercise. You can easily navigate to all the TODOs using Android Studio's TODO tool. To open the TODO tool, click the button at the bottom of the screen that says TODO. This will display a list of all comments with TODO in the project.

We've numbered the TODO steps so you can do them in order: todos

Step 4: Commit your code changes

After You've completed the TODOs, you can optionally commit your changes. This will allow you to see the code you wrote whenever you return to the branch. The following git code will add and save all your changes.

git add .
git commit -m "Your commit message"

Step 5: Compare with the solution

Most exercises will have a list of steps for you to check off in the classroom. Once you've checked these off, you'll see a pop up window with a link to the solution code. Note the Diff link:

solutionwindow

The Diff link will take you to a Github diff as seen below: diff

All of the code that was added in the solution is in green, and the removed code (which will usually be the TODO comments) is in red.

You can also compare your code locally with the branch of the following step.

Report Issues

Notice any issues with a repository? Please file a github issue in the repository.

andfun-kotlin-sleep-tracker's People

Contributors

apigeoneer avatar badr-1 avatar dnbit avatar mir47 avatar rwarner avatar sudkul avatar uanjali avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

andfun-kotlin-sleep-tracker's Issues

App crashes on navigation after orientation change

When user tries to navigate after rotating the phone, app crashes.
The following error is shown in the logcat:

com.example.android.trackmysleepquality E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.android.trackmysleepquality, PID: 10884
    java.lang.IllegalStateException: no current navigation node
        at androidx.navigation.NavController.navigate(NavController.java:765)
        at androidx.navigation.NavController.navigate(NavController.java:745)
        at androidx.navigation.NavController.navigate(NavController.java:731)
        at androidx.navigation.NavController.navigate(NavController.java:863)
        at com.example.android.trackmysleepquality.sleeptracker.SleepTrackerFragment$onCreateView$2.onChanged(SleepTrackerFragment.kt:94)
        at com.example.android.trackmysleepquality.sleeptracker.SleepTrackerFragment$onCreateView$2.onChanged(SleepTrackerFragment.kt:38)
        at androidx.lifecycle.LiveData.considerNotify(LiveData.java:131)
        at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:149)
        at androidx.lifecycle.LiveData.setValue(LiveData.java:307)
        at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50)
        at com.example.android.trackmysleepquality.sleeptracker.SleepTrackerViewModel$onStopTracking$1.invokeSuspend(SleepTrackerViewModel.kt:185)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Error testing the database

Hi, I am getting the following error when testing the database:

Suspend function 'insertAndGetNight' should be called only from a coroutine or another suspend function

Testing the room database - Test Failed

I tried the following test function in my SleepDatabaseTest.kt:

@Test
@Throws(Exception::class)
fun updateAndGetNight() {
    val night = SleepNight()
    sleepDao.insert(night)
    val tonight = sleepDao.get(night.nightId)
    assertEquals(-1, tonight?.sleepQuality)
}

But something unexpected happened:

java.lang.AssertionError: expected:<-1> but was:<null>

Could you plz help me figure out what's wrong with my usage of the Dao function get(), or if not, where lies the problem?

Error in testing room database

in app/build.gradle if the following line

    testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'

is not there under defaultConfig then we get the following error

junit.framework.AssertionFailedError: No tests found in com.example.android.trackmysleepquality.SleepDatabaseTest

Lesson 6, Exercise 10 - Testing the room database

I am getting error, following are logs:

Testing started at 08:08 ...

08/29 08:08:51: Launching 'AppDatabaseTest' on samsung SM-M205F.
Running tests

$ adb shell am instrument -w -r -e debug false -e class 'np.com.yubarajshrestha.retrofitnroom.AppDatabaseTest' np.com.yubarajshrestha.retrofitnroom.test/androidx.test.runner.AndroidJUnitRunner
Waiting for process to come online...
Connected to process 7023 on device 'samsung-sm_m205f-320110e6b40a16e5'.

Started running tests

java.lang.RuntimeException: cannot find implementation for np.com.yubarajshrestha.retrofitnroom.database.AppDatabase. AppDatabase_Impl does not exist at androidx.room.Room.getGeneratedImplementation(Room.java:94) at androidx.room.RoomDatabase$Builder.build(RoomDatabase.java:667) at np.com.yubarajshrestha.retrofitnroom.AppDatabaseTest.createDb(AppDatabaseTest.kt:29) at java.lang.reflect.Method.invoke(Native Method) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at androidx.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:76) at androidx.test.internal.runner.junit4.statement.RunAfters.evaluate(RunAfters.java:61) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at androidx.test.ext.junit.runners.AndroidJUnit4.run(AndroidJUnit4.java:104) at org.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:27) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at org.junit.runner.JUnitCore.run(JUnitCore.java:115) at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56) at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:392) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2160)

Tests ran to completion.

Test runner - no tests found

Unlike other issues reported (#33, #18), I see no error when trying to run the tests, rather no tests are found at all

04/05 22:13:01: Launching 'SleepDatabaseTest (1)' on Pixel 5 API 30.
Install successfully finished in 758 ms.
Running tests

$ adb shell am instrument -w -m    -e debug false -e class 'com.example.android.trackmysleepquality.SleepDatabaseTest' com.example.android.trackmysleepquality.test/android.test.InstrumentationTestRunner

image

based upon branch Step.03-Solution-Create-RoomDatabase

Android Studio Bumblebee | 2021.1.1 Patch 1
Build #AI-211.7628.21.2111.8139111, built on February 1, 2022
Runtime version: 11.0.11+0-b60-7590822 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Linux 5.13.0-39-generic
GC: G1 Young Generation, G1 Old Generation
Memory: 1024M
Cores: 8
Registry: external.system.auto.import.disabled=true, ide.instant.shutdown=false
Non-Bundled Plugins: IdeaVIM (1.9.2)
Current Desktop: ubuntu:GNOME

Missing string value

error: resource string/how_was_your_sleep

there's a typo in strings
it's written how_was_hour_sleep

Application crashes on launch

Hi
Implemented the code as of this part (Solution 5 - Coroutines). The build was without any errors, but on application launch on the physical device (running Android 10), the app crashes, without showing the screen.

Logcat is attached herewith for your reference.

Please help.

Thanks.
Logcat.txt

10. Testing the Room database

I uncomment the code and when I ran the test case it keeps saying that the test framework quit unexpectedly. Has to add the suspend keyword as it seems that you need to use a coroutine to insert and query the database.

@test
@throws(Exception::class)
suspend fun insertAndGetNight() {
val night = SleepNight()
sleepDao.insert(night)
val tonight = sleepDao.getTonight()
assertEquals(tonight?.sleepQuality, -1)
}

coroutine running in the main thread.

why are the private database access functions in the SleepTrackerViewModel.kt running on the main thread in new commit? Shouldn't they be running in the Dispatcher.IO thread as shown in the original course.

Issue with tonight.value (Cannot invoke setValue on a background thread)

Hi i found an error in the SleepTrackerViewModel.kt

If i use the code:
private fun initializeTonight() { uiScope.launch { tonight.value = getTonightFromDatabase() } }
i get an Error:

java.lang.IllegalStateException: Cannot invoke setValue on a background thread

To fix the Problem replace all
tonight.value = getTonightFromDatabase()
with
tonight.postValue(getTonightFromDatabase())

Test running failed: Unable to find instrumentation

Hi, in order to run the JUnit test for SleepDatabaseTest, I had to add the following line of code inside the app's build.gradle android, defaultConfig:

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

Error Fixed:

Testing started at 3:32 PM ...

11/03 15:32:28: Launching 'SleepDatabaseTest' on Pixel 3a API 30.
Running tests

$ adb shell am instrument -w -r -e debug false -e class 'com.example.android.trackmysleepquality.SleepDatabaseTest' com.example.android.trackmysleepquality.test/android.test.InstrumentationTestRunner

Started running tests

Test running failed: Unable to find instrumentation info for: ComponentInfo{com.example.android.trackmysleepquality.test/android.test.InstrumentationTestRunner}. onError: commandError=true message=INSTRUMENTATION_FAILED: com.example.android.trackmysleepquality.test/android.test.InstrumentationTestRunner
Tests ran to completion.

"Step.07*" branches do not compile

Both exercise and solution branches do not compile with error:

Cannot find a setter for <android.widget.TextView android:text> that accepts parameter type 'error.NonExistentClass'

If a binding adapter provides the setter, check that the adapter is annotated correctly and that the parameter type matches.

I did not change the code at all.

No tests were found

I got the following error message when running the test:
Test running failed: Unable to find instrumentation info for: ComponentInfo{com.example.android.trackmysleepquality.test/android.test.InstrumentationTestRunner}. onError: commandError=true message=INSTRUMENTATION_FAILED: com.example.android.trackmysleepquality.test/android.test.InstrumentationTestRunner

Help!

Missing part about relations in this lesson

We are using this course in University College Ghent to learn our students how to program Android apps.

Although the lesson covers the basics on Room for single Entities, Relations are omitted while they are essential when coding more complex applications. Because of the design choice of Room not to structure relations like traditional ORM's (e.g. ObjectBox), this is a very difficult part to learn on your own. It would be great that this application/lesson also covers this aspect of Room.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.