forked from googlesamples/glass-enterprise-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added persistent storage to the NotesSample application (googlesamples#8
- Loading branch information
1 parent
0ff739c
commit a596d36
Showing
7 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
NotesSample/app/src/main/java/com/example/glass/notessample/model/Note.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
54 changes: 54 additions & 0 deletions
54
NotesSample/app/src/main/java/com/example/glass/notessample/model/NoteViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
40 changes: 40 additions & 0 deletions
40
NotesSample/app/src/main/java/com/example/glass/notessample/storage/NoteDao.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>> | ||
} |
34 changes: 34 additions & 0 deletions
34
NotesSample/app/src/main/java/com/example/glass/notessample/storage/NoteRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
56 changes: 56 additions & 0 deletions
56
NotesSample/app/src/main/java/com/example/glass/notessample/storage/NotesDatabase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters