Skip to content

Commit

Permalink
Added persistent storage to the NotesSample application (googlesamples#8
Browse files Browse the repository at this point in the history
)
  • Loading branch information
idzkowski-google committed Jul 7, 2020
1 parent 0ff739c commit a596d36
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 0 deletions.
7 changes: 7 additions & 0 deletions NotesSample/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ dependencies {
implementation "androidx.fragment:fragment-ktx:1.2.4"
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.0-alpha03'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.google.android.material:material:1.1.0'

// Room persistence library imports
implementation "androidx.room:room-runtime:$room_version"
implementation "androidx.room:room-ktx:$room_version"
implementation "androidx.room:room-rxjava2:$room_version"
kapt "androidx.room:room-compiler:$room_version"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2020 Google LLC
*
* 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.glass.notessample.model

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey

/**
* Model for the single note.
*/
@Entity(tableName = "notes")
data class Note(
@ColumnInfo val title: String,
@ColumnInfo val content: String,
@ColumnInfo val daytime: String
) {
@PrimaryKey(autoGenerate = true)
var id: Int = 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2020 Google LLC
*
* 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.glass.notessample.model

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.viewModelScope
import com.example.glass.notessample.storage.NoteRepository
import com.example.glass.notessample.storage.NotesDatabase
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
* View model for notes. Uses {@link NoteRepository} to communicate with the persistent storage.
*/
class NoteViewModel(application: Application) : AndroidViewModel(application) {

private val repository: NoteRepository
val allNotes: LiveData<List<Note>>

init {
val wordsDao = NotesDatabase.getDatabase(
application,
viewModelScope
).wordDao()
repository = NoteRepository(wordsDao)
allNotes = repository.allNotes()
}

fun insert(note: Note) = viewModelScope.launch(Dispatchers.IO) {
repository.insert(note)
}

fun delete(note: Note) = viewModelScope.launch(Dispatchers.IO) {
repository.delete(note)
}

fun find(text: String) = repository.find(text)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2020 Google LLC
*
* 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.glass.notessample.storage

import androidx.lifecycle.LiveData
import androidx.room.*
import com.example.glass.notessample.model.Note

/**
* Dao for the notes.
*/
@Dao
interface NoteDao {

@Query("SELECT * from notes")
fun getAll(): LiveData<List<Note>>

@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insert(note: Note)

@Delete
suspend fun delete(note: Note)

@Query("SELECT * FROM notes WHERE content LIKE :text")
fun find(text: String): LiveData<List<Note>>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2020 Google LLC
*
* 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.glass.notessample.storage

import androidx.lifecycle.LiveData
import com.example.glass.notessample.model.Note

/**
* Repository to communicate between database and view model
*/
class NoteRepository(private val noteDao: NoteDao) {

fun allNotes() = noteDao.getAll()

suspend fun insert(note: Note) = noteDao.insert(note)

suspend fun delete(note: Note) = noteDao.delete(note)

fun find(text: String) = noteDao.find(text)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2020 Google LLC
*
* 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.glass.notessample.storage

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.example.glass.notessample.model.Note
import kotlinx.coroutines.CoroutineScope

/**
* Room database for notes.
*/
@Database(entities = [Note::class], version = 1, exportSchema = false)
public abstract class NotesDatabase : RoomDatabase() {

abstract fun wordDao(): NoteDao

companion object {
private const val DATABASE_NAME = "notes"

@Volatile
private var INSTANCE: NotesDatabase? = null

fun getDatabase(context: Context, scope: CoroutineScope): NotesDatabase {
val tempInstance = INSTANCE
if (tempInstance != null) {
return tempInstance
}
synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext,
NotesDatabase::class.java,
DATABASE_NAME
).build()
INSTANCE = instance
return instance
}
}
}
}
1 change: 1 addition & 0 deletions NotesSample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

buildscript {
ext.kotlin_version = '1.3.41'
ext.room_version = '2.2.5'
repositories {
google()
jcenter()
Expand Down

0 comments on commit a596d36

Please sign in to comment.