-
Notifications
You must be signed in to change notification settings - Fork 0
task/hw8 todo_list #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package com.example.androidpractice2020.database | ||
|
|
||
| import androidx.room.Insert | ||
| import androidx.room.OnConflictStrategy | ||
| import androidx.room.Query | ||
| import androidx.room.Update | ||
| import com.example.androidpractice2020.notes.Note | ||
| import kotlinx.coroutines.flow.Flow | ||
|
|
||
| interface NoteDao { | ||
| @Query("SELECT * FROM note") | ||
| fun getListNotes(): Flow<List<Note>> | ||
|
|
||
| @Insert(onConflict = OnConflictStrategy.REPLACE) | ||
| suspend fun insert(note: Note) | ||
|
|
||
| @Query("DELETE FROM note") | ||
| suspend fun deleteAll() | ||
|
|
||
| @Update() | ||
| suspend fun update() | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package com.example.androidpractice2020.database | ||
|
|
||
| import androidx.annotation.WorkerThread | ||
| import com.example.androidpractice2020.notes.Note | ||
| import kotlinx.coroutines.flow.Flow | ||
|
|
||
| class NoteRepository(private val noteDao: NoteDao) { | ||
|
|
||
| val allNotes: Flow<List<Note>> = noteDao.getListNotes() | ||
|
|
||
| @Suppress("RedundantSuspendModifier") | ||
| @WorkerThread | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А смысл?, у тебя дао само его уже на ио запустит, нет смысла указывать тред на верхние уровни |
||
| suspend fun insert(note: Note) { | ||
| noteDao.insert(note) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package com.example.androidpractice2020.database | ||
|
|
||
| import android.content.Context | ||
| import androidx.room.* | ||
| import androidx.sqlite.db.SupportSQLiteDatabase | ||
| import com.example.androidpractice2020.notes.Note | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.launch | ||
|
|
||
| @Database(entities = [Note::class], version = 1, exportSchema = false) | ||
| abstract class NoteRoomDataBase : RoomDatabase() { | ||
|
|
||
| abstract fun noteDao(): NoteDao | ||
|
|
||
| companion object { | ||
| @Volatile | ||
| private var INSTANCE: NoteRoomDataBase? = null | ||
|
|
||
| fun getDatabase( | ||
| context: Context, | ||
| scope: CoroutineScope | ||
| ): NoteRoomDataBase { | ||
| return INSTANCE | ||
| ?: synchronized(this) { | ||
| val instance = Room.databaseBuilder( | ||
| context.applicationContext, | ||
| NoteRoomDataBase::class.java, | ||
| "note" | ||
| ) | ||
| .fallbackToDestructiveMigration() | ||
| .addCallback( | ||
| NoteDatabaseCallback( | ||
| scope | ||
| ) | ||
| ) | ||
| .build() | ||
| INSTANCE = instance | ||
| instance | ||
| } | ||
| } | ||
|
|
||
| private class NoteDatabaseCallback( | ||
| private val scope: CoroutineScope | ||
| ) : RoomDatabase.Callback() { | ||
| override fun onCreate(db: SupportSQLiteDatabase) { | ||
| super.onCreate(db) | ||
|
|
||
| INSTANCE?.let { database -> | ||
| scope.launch(Dispatchers.IO) { | ||
| populateDatabase( | ||
| database.noteDao() | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| suspend fun populateDatabase(noteDao: NoteDao) { | ||
| noteDao.deleteAll() | ||
|
|
||
| var example = Note(0, "hqweqweo", "hqweqwe") | ||
| noteDao.insert(example) | ||
| example = Note(1, "qweqweqwe","qweqwqweqwewq00") | ||
| noteDao.insert(example) | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package com.example.androidpractice2020.database | ||
|
|
||
| import androidx.lifecycle.* | ||
| import com.example.androidpractice2020.notes.Note | ||
| import kotlinx.coroutines.launch | ||
|
|
||
| class NoteViewModel(private val repository: NoteRepository): ViewModel() { | ||
|
|
||
| val allNotes: LiveData<List<Note>> = repository.allNotes.asLiveData() | ||
|
|
||
| fun insert(note: Note) = viewModelScope.launch { | ||
| repository.insert(note) | ||
| } | ||
| } | ||
|
|
||
| class NoteViewModelFactory(private val repository: NoteRepository) : ViewModelProvider.Factory { | ||
| override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
| if (modelClass.isAssignableFrom(NoteViewModel::class.java)) { | ||
| @Suppress("UNCHECKED_CAST") | ||
| return NoteViewModel( | ||
| repository | ||
| ) as T | ||
| } | ||
| throw IllegalArgumentException("Unknown ViewModel class") | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package com.example.androidpractice2020.database | ||
|
|
||
| import android.app.Application | ||
| import com.example.androidpractice2020.database.NoteRepository | ||
| import com.example.androidpractice2020.database.NoteRoomDataBase | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.SupervisorJob | ||
|
|
||
| class NotesApplication : Application() { | ||
|
|
||
| val applicationScope = CoroutineScope(SupervisorJob()) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. не оч понятно зачем это. Наверное, чтоб при первом запуске что-то было в бд, но выглядит как-то мега костыльно. Смысл скоупа в том что он зависит от жц элементов, а ты создаешь applicationScope на все приложение. Тогда мог бы и глобалскоуп использовать. Вообщем очень сомнительное решение, которое в принципе рушит идею самих скоупов |
||
|
|
||
|
|
||
| val database by lazy { NoteRoomDataBase.getDatabase(this, applicationScope) } | ||
| val repository by lazy { | ||
| NoteRepository( | ||
| database.noteDao() | ||
| ) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package com.example.androidpractice2020.fragment | ||
|
|
||
| import android.os.Bundle | ||
| import android.view.LayoutInflater | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import androidx.fragment.app.Fragment | ||
| import com.example.androidpractice2020.R | ||
|
|
||
| class NoteEditAddFragment : Fragment() { | ||
|
|
||
| override fun onCreateView( | ||
| inflater: LayoutInflater, | ||
| container: ViewGroup?, | ||
| savedInstanceState: Bundle? | ||
| ): View? { | ||
|
|
||
| return inflater.inflate(R.layout.activity_note_details, container, false) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package com.example.androidpractice2020.fragment | ||
|
|
||
| import android.os.Bundle | ||
| import android.view.LayoutInflater | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import androidx.fragment.app.Fragment | ||
| import com.example.androidpractice2020.R | ||
|
|
||
| class NoteListFragment : Fragment() { | ||
| override fun onCreateView( | ||
| inflater: LayoutInflater, | ||
| container: ViewGroup?, | ||
| savedInstanceState: Bundle? | ||
| ): View? { | ||
| return inflater.inflate(R.layout.activity_todolist, container, false) | ||
| } | ||
|
|
||
| interface OnSelectedButtonListener { | ||
| fun onButtonSelected(buttonIndex: Int) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package com.example.androidpractice2020.interfacecell | ||
|
|
||
| interface CellClickListener { | ||
| fun onCellClickListener() | ||
| fun onDeleteClickListener() | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.example.androidpractice2020.notes | ||
|
|
||
| import android.os.Parcelable | ||
| import androidx.room.ColumnInfo | ||
| import androidx.room.Entity | ||
| import androidx.room.PrimaryKey | ||
| import kotlinx.android.parcel.Parcelize | ||
|
|
||
| @Parcelize | ||
| @Entity(tableName = "note") | ||
| data class Note( | ||
| @PrimaryKey(autoGenerate = true) | ||
| val id: Int = 0, | ||
| @ColumnInfo(name = "title") | ||
| var title: String, | ||
| @ColumnInfo(name = "description") | ||
| var description: String | ||
| ) : Parcelable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
а тебе точно эт надо?