├── .gitignore
├── .idea
├── .name
├── codeStyles
│ ├── Project.xml
│ └── codeStyleConfig.xml
├── gradle.xml
├── misc.xml
└── runConfigurations.xml
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── codingwithmitch
│ │ └── mvvm_koin_room
│ │ └── ExampleInstrumentedTest.kt
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── codingwithmitch
│ │ │ └── mvvm_koin_room
│ │ │ ├── MyApplication.kt
│ │ │ ├── custom_views
│ │ │ └── LinedEditText.kt
│ │ │ ├── di
│ │ │ └── AppModule.kt
│ │ │ ├── models
│ │ │ └── Note.kt
│ │ │ ├── persistence
│ │ │ ├── NoteDao.kt
│ │ │ └── NoteDatabase.kt
│ │ │ ├── repositories
│ │ │ ├── NoteRepository.kt
│ │ │ └── NoteRepositoryImpl.kt
│ │ │ ├── ui
│ │ │ ├── CreateNoteFragment.kt
│ │ │ ├── DetailNoteFragment.kt
│ │ │ ├── IMainActivity.kt
│ │ │ ├── INoteViewModel.kt
│ │ │ ├── MainActivity.kt
│ │ │ ├── NoteListAdapter.kt
│ │ │ ├── NoteListFragment.kt
│ │ │ ├── NoteViewModel.kt
│ │ │ └── ViewExtensions.kt
│ │ │ └── util
│ │ │ ├── AbsentLiveData.kt
│ │ │ ├── DateUtils.kt
│ │ │ └── TopSpacingItemDecoration.kt
│ └── res
│ │ ├── anim
│ │ ├── slide_in_left.xml
│ │ ├── slide_in_right.xml
│ │ ├── slide_out_left.xml
│ │ └── slide_out_right.xml
│ │ ├── drawable-v24
│ │ └── ic_launcher_foreground.xml
│ │ ├── drawable
│ │ ├── ic_add_white_24dp.xml
│ │ ├── ic_arrow_back_black_24dp.xml
│ │ ├── ic_check_black_24dp.xml
│ │ └── ic_launcher_background.xml
│ │ ├── layout
│ │ ├── activity_main.xml
│ │ ├── fragment_create_note.xml
│ │ ├── fragment_detail_note.xml
│ │ ├── fragment_note_list.xml
│ │ ├── layout_create_note_toolbar.xml
│ │ ├── layout_note_list_item.xml
│ │ └── layout_note_toolbar.xml
│ │ ├── mipmap-anydpi-v26
│ │ ├── ic_launcher.xml
│ │ └── ic_launcher_round.xml
│ │ ├── mipmap-hdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ ├── ic_launcher.png
│ │ └── ic_launcher_round.png
│ │ ├── navigation
│ │ └── notes_nav_graph.xml
│ │ └── values
│ │ ├── colors.xml
│ │ ├── dimen.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── com
│ └── codingwithmitch
│ └── mvvm_koin_room
│ └── ExampleUnitTest.kt
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/caches
5 | /.idea/libraries
6 | /.idea/modules.xml
7 | /.idea/workspace.xml
8 | /.idea/navEditor.xml
9 | /.idea/assetWizardSettings.xml
10 | .DS_Store
11 | /build
12 | /captures
13 | .externalNativeBuild
14 | .cxx
15 |
--------------------------------------------------------------------------------
/.idea/.name:
--------------------------------------------------------------------------------
1 | MVVM-Koin-Room
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 | xmlns:android
20 |
21 | ^$
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 | xmlns:.*
31 |
32 | ^$
33 |
34 |
35 | BY_NAME
36 |
37 |
38 |
39 |
40 |
41 |
42 | .*:id
43 |
44 | http://schemas.android.com/apk/res/android
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 | .*:name
54 |
55 | http://schemas.android.com/apk/res/android
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 | name
65 |
66 | ^$
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 | style
76 |
77 | ^$
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 | .*
87 |
88 | ^$
89 |
90 |
91 | BY_NAME
92 |
93 |
94 |
95 |
96 |
97 |
98 | .*
99 |
100 | http://schemas.android.com/apk/res/android
101 |
102 |
103 | ANDROID_ATTRIBUTE_ORDER
104 |
105 |
106 |
107 |
108 |
109 |
110 | .*
111 |
112 | .*
113 |
114 |
115 | BY_NAME
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
--------------------------------------------------------------------------------
/.idea/codeStyles/codeStyleConfig.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
18 |
19 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Experimental TODO app
2 | This app is for learning purposes. I'll be using it for experimentation.
3 |
4 | ## Features:
5 | 1. MVVM
6 | 2. Repository Pattern
7 | 3. Koin
8 | 4. Room
9 | 5. Retrofit2 (adding later)
10 |
11 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | apply plugin: 'kotlin-android'
4 |
5 | apply plugin: 'kotlin-android-extensions'
6 |
7 | apply plugin: 'kotlin-kapt'
8 |
9 | android {
10 | compileSdkVersion 29
11 | buildToolsVersion "29.0.2"
12 | defaultConfig {
13 | applicationId "com.codingwithmitch.mvvm_koin_room"
14 | minSdkVersion 21
15 | targetSdkVersion 29
16 | versionCode 1
17 | versionName "1.0"
18 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
19 | }
20 | buildTypes {
21 | release {
22 | minifyEnabled false
23 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
24 | }
25 | }
26 | }
27 |
28 | dependencies {
29 | implementation fileTree(dir: 'libs', include: ['*.jar'])
30 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
31 | implementation 'androidx.appcompat:appcompat:1.0.2'
32 | implementation 'androidx.core:core-ktx:1.0.2'
33 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
34 | implementation 'androidx.legacy:legacy-support-v4:1.0.0'
35 | testImplementation 'junit:junit:4.12'
36 | androidTestImplementation 'androidx.test.ext:junit:1.1.0'
37 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
38 |
39 | // -- Coroutines
40 | def coroutines_version = "1.2.1"
41 | implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
42 | implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
43 |
44 | // Room
45 | def room_version = "2.2.3"
46 | implementation "androidx.room:room-runtime:$room_version"
47 | kapt "androidx.room:room-compiler:$room_version"
48 |
49 | // Kotlin Extensions and Coroutines support for Room
50 | implementation "androidx.room:room-ktx:$room_version"
51 |
52 | // Recyclerview
53 | def recyclerview_version = "1.2.0-alpha01"
54 | implementation "androidx.recyclerview:recyclerview:$recyclerview_version"
55 |
56 | def material_version = "1.2.0-alpha03"
57 | implementation "com.google.android.material:material:$material_version"
58 |
59 | // Navigation Component
60 | def nav_version = "2.2.0-rc04"
61 | implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
62 | implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
63 |
64 | // Koin
65 | def koin_version = "2.0.1"
66 | implementation "org.koin:koin-android:$koin_version"
67 | implementation "org.koin:koin-android-viewmodel:$koin_version"
68 | }
69 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/codingwithmitch/mvvm_koin_room/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | package com.codingwithmitch.mvvm_koin_room
2 |
3 | import androidx.test.platform.app.InstrumentationRegistry
4 | import androidx.test.ext.junit.runners.AndroidJUnit4
5 |
6 | import org.junit.Test
7 | import org.junit.runner.RunWith
8 |
9 | import org.junit.Assert.*
10 |
11 | /**
12 | * Instrumented test, which will execute on an Android device.
13 | *
14 | * See [testing documentation](http://d.android.com/tools/testing).
15 | */
16 | @RunWith(AndroidJUnit4::class)
17 | class ExampleInstrumentedTest {
18 | @Test
19 | fun useAppContext() {
20 | // Context of the app under test.
21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22 | assertEquals("com.codingwithmitch.mvvm_koin_room", appContext.packageName)
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/app/src/main/java/com/codingwithmitch/mvvm_koin_room/MyApplication.kt:
--------------------------------------------------------------------------------
1 | package com.codingwithmitch.mvvm_koin_room
2 |
3 | import android.app.Application
4 | import com.codingwithmitch.mvvm_koin_room.di.appModule
5 | import org.koin.android.ext.koin.androidContext
6 | import org.koin.android.ext.koin.androidLogger
7 | import org.koin.core.context.startKoin
8 |
9 | class MyApplication : Application(){
10 |
11 | override fun onCreate() {
12 | super.onCreate()
13 | // Start Koin
14 | startKoin{
15 | androidLogger()
16 | androidContext(this@MyApplication)
17 | modules(appModule)
18 | }
19 | }
20 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/codingwithmitch/mvvm_koin_room/custom_views/LinedEditText.kt:
--------------------------------------------------------------------------------
1 | package com.codingwithmitch.mvp_room_koin.custom_views
2 |
3 | import android.content.Context
4 | import android.graphics.Canvas
5 | import android.graphics.Paint
6 | import android.graphics.Rect
7 | import android.util.AttributeSet
8 | import android.view.View
9 | import androidx.appcompat.widget.AppCompatEditText
10 |
11 | class LinedEditText(
12 | context: Context?,
13 | attrs: AttributeSet?
14 | ) :
15 | AppCompatEditText(context, attrs) {
16 |
17 | private val rect: Rect
18 |
19 | private val paint: Paint
20 |
21 | override fun onDraw(canvas: Canvas) { // get the height of the view
22 | val height = (this.getParent() as View).height
23 | val lineHeight: Int = getLineHeight()
24 | val numberOfLines = height / lineHeight
25 | val r = rect
26 | val paint = paint
27 | var baseline: Int = getLineBounds(0, r)
28 | for (i in 0 until numberOfLines) {
29 | canvas.drawLine(
30 | r.left.toFloat(),
31 | baseline + 1.toFloat(),
32 | r.right.toFloat(),
33 | baseline + 1.toFloat(),
34 | paint
35 | )
36 | baseline += lineHeight
37 | }
38 | super.onDraw(canvas)
39 | }
40 |
41 | companion object {
42 | private const val TAG = "LinedEditText"
43 | }
44 |
45 | // we need this constructor for LayoutInflater
46 | init {
47 | rect = Rect()
48 | paint = Paint()
49 | paint.style = Paint.Style.STROKE
50 | paint.strokeWidth = 2f
51 | paint.color = -0x269a // Color of the lines on paper
52 | }
53 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/codingwithmitch/mvvm_koin_room/di/AppModule.kt:
--------------------------------------------------------------------------------
1 | package com.codingwithmitch.mvvm_koin_room.di
2 |
3 | import androidx.room.Room
4 | import com.codingwithmitch.mvvm_koin_room.persistence.NOTE_DATABASE_NAME
5 | import com.codingwithmitch.mvvm_koin_room.persistence.NoteDatabase
6 | import com.codingwithmitch.mvvm_koin_room.repositories.NoteRepository
7 | import com.codingwithmitch.mvvm_koin_room.repositories.NoteRepositoryImpl
8 | import com.codingwithmitch.mvvm_koin_room.ui.NoteViewModel
9 | import org.koin.android.viewmodel.dsl.viewModel
10 | import org.koin.dsl.module
11 |
12 |
13 | /**
14 | * See https://start.insert-koin.io/#/quickstart/android
15 | **/
16 | val appModule = module{
17 |
18 | // Provide NoteDatabase
19 | single{ Room.databaseBuilder(get(), NoteDatabase::class.java, NOTE_DATABASE_NAME).build() }
20 |
21 | // Provide NoteDao
22 | single{ get().noteDao() }
23 |
24 | // Provide NoteRepository
25 | single { NoteRepositoryImpl(get()) }
26 |
27 | // Provide NoteViewModel
28 | viewModel { NoteViewModel(get()) }
29 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/codingwithmitch/mvvm_koin_room/models/Note.kt:
--------------------------------------------------------------------------------
1 | package com.codingwithmitch.mvp_room_koin.models
2 |
3 | import android.os.Parcelable
4 | import androidx.room.ColumnInfo
5 | import androidx.room.Entity
6 | import androidx.room.PrimaryKey
7 | import kotlinx.android.parcel.Parcelize
8 |
9 | const val SINGLE_NOTE_BUNDLE_KEY = "com.codingwithmitch.mvp_room_koin.models.create_note"
10 |
11 | @Parcelize
12 | @Entity(tableName = "notes")
13 | class Note(
14 |
15 | @PrimaryKey(autoGenerate = true)
16 | var id: Int? = null,
17 |
18 | @ColumnInfo(name = "title")
19 | var title: String,
20 |
21 | @ColumnInfo(name = "content")
22 | var content: String? = "",
23 |
24 | @ColumnInfo(name = "updated")
25 | var updated: Long = System.currentTimeMillis() // default is current time
26 | ) : Parcelable{
27 |
28 | override fun toString(): String {
29 | return "Note(id=$id, title='$title', content=$content, updated=$updated)"
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/app/src/main/java/com/codingwithmitch/mvvm_koin_room/persistence/NoteDao.kt:
--------------------------------------------------------------------------------
1 | package com.codingwithmitch.mvvm_koin_room.persistence
2 |
3 | import androidx.lifecycle.LiveData
4 | import androidx.room.*
5 | import com.codingwithmitch.mvp_room_koin.models.Note
6 |
7 | @Dao
8 | interface NoteDao {
9 |
10 | @Insert
11 | suspend fun insertNote(note: Note)
12 |
13 | @Update
14 | suspend fun updateNote(note: Note)
15 |
16 | @Delete
17 | suspend fun deleteNote(note: Note)
18 |
19 | @Query("SELECT * FROM notes")
20 | suspend fun getAllNotes(): List
21 |
22 | @Query("DELETE FROM notes")
23 | suspend fun deleteAllNotes()
24 |
25 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/codingwithmitch/mvvm_koin_room/persistence/NoteDatabase.kt:
--------------------------------------------------------------------------------
1 | package com.codingwithmitch.mvvm_koin_room.persistence
2 |
3 | import androidx.room.Database
4 | import androidx.room.RoomDatabase
5 | import com.codingwithmitch.mvp_room_koin.models.Note
6 |
7 | const val NOTE_DATABASE_NAME = "notes_database"
8 |
9 | @Database(entities = [Note::class], version = 1)
10 | abstract class NoteDatabase: RoomDatabase(){
11 |
12 | abstract fun noteDao(): NoteDao
13 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/codingwithmitch/mvvm_koin_room/repositories/NoteRepository.kt:
--------------------------------------------------------------------------------
1 | package com.codingwithmitch.mvvm_koin_room.repositories
2 |
3 | import com.codingwithmitch.mvp_room_koin.models.Note
4 |
5 | interface NoteRepository {
6 |
7 | suspend fun insertNote(note: Note)
8 |
9 | suspend fun updateNote(note: Note)
10 |
11 | suspend fun deleteNote(note: Note)
12 |
13 | suspend fun deleteAllNotes()
14 |
15 | suspend fun getAllNotes(): List
16 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/codingwithmitch/mvvm_koin_room/repositories/NoteRepositoryImpl.kt:
--------------------------------------------------------------------------------
1 | package com.codingwithmitch.mvvm_koin_room.repositories
2 |
3 | import android.util.Log
4 | import com.codingwithmitch.mvp_room_koin.models.Note
5 | import com.codingwithmitch.mvvm_koin_room.persistence.NoteDao
6 | import kotlinx.coroutines.delay
7 |
8 | class NoteRepositoryImpl(
9 | private val noteDao: NoteDao
10 | ) : NoteRepository{
11 |
12 | private val TAG: String = "AppDebug"
13 |
14 | override suspend fun insertNote(note: Note) {
15 | noteDao.insertNote(note)
16 | }
17 |
18 | override suspend fun updateNote(note: Note) {
19 | noteDao.updateNote(note)
20 | }
21 |
22 | override suspend fun deleteNote(note: Note) {
23 | noteDao.deleteNote(note)
24 | }
25 |
26 | override suspend fun deleteAllNotes() {
27 | noteDao.deleteAllNotes()
28 | }
29 |
30 | override suspend fun getAllNotes(): List {
31 | return noteDao.getAllNotes()
32 | }
33 |
34 | }
35 |
36 |
37 |
--------------------------------------------------------------------------------
/app/src/main/java/com/codingwithmitch/mvvm_koin_room/ui/CreateNoteFragment.kt:
--------------------------------------------------------------------------------
1 | package com.codingwithmitch.mvvm_koin_room.ui
2 |
3 |
4 | import android.content.Context
5 | import android.os.Bundle
6 | import android.util.Log
7 | import androidx.fragment.app.Fragment
8 | import android.view.LayoutInflater
9 | import android.view.View
10 | import android.view.ViewGroup
11 | import androidx.lifecycle.Observer
12 | import com.codingwithmitch.mvp_room_koin.models.Note
13 |
14 | import com.codingwithmitch.mvvm_koin_room.R
15 | import kotlinx.android.synthetic.main.fragment_create_note.*
16 | import kotlinx.android.synthetic.main.layout_create_note_toolbar.*
17 | import kotlinx.android.synthetic.main.layout_note_toolbar.*
18 | import org.koin.android.viewmodel.ext.android.sharedViewModel
19 |
20 | /**
21 | * Class for creating new notes
22 | */
23 | class CreateNoteFragment : Fragment() {
24 |
25 | private val TAG: String = "AppDebug"
26 |
27 | // "SharedViewModel": reuse existing viewmodel from hosting activity
28 | val viewModel: NoteViewModel by sharedViewModel()
29 |
30 | lateinit var iMainActivity: IMainActivity
31 |
32 | private val note: Note by lazy {
33 | Note(
34 | title = getString(R.string.default_note_title)
35 | )
36 | }
37 |
38 | override fun onCreateView(
39 | inflater: LayoutInflater, container: ViewGroup?,
40 | savedInstanceState: Bundle?
41 | ): View? {
42 | return inflater.inflate(R.layout.fragment_create_note, container, false)
43 | }
44 |
45 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
46 | super.onViewCreated(view, savedInstanceState)
47 |
48 | create_note_toolbar_check.setOnClickListener {
49 | iMainActivity.hideSoftKeyboard()
50 | iMainActivity.onBackPressed()
51 | }
52 |
53 | subscribeObservers()
54 | }
55 |
56 | private fun subscribeObservers(){
57 | viewModel.executeSave.observe(viewLifecycleOwner, Observer { executeSave ->
58 | executeSave?.let{
59 | if(it){
60 | saveNewNote()
61 | viewModel.setExecuteSave(false) // reset
62 | }
63 | }
64 | })
65 | }
66 |
67 | fun saveNewNote() {
68 | create_note_title?.let { title ->
69 | note.title = title.text.toString()
70 | }
71 | note.content = create_note_content.text.toString()
72 |
73 | if(validateFields()){
74 | viewModel.insertNote(note)
75 | }
76 | }
77 |
78 | private fun validateFields(): Boolean {
79 | when{
80 | note.title.isBlank() -> {
81 | return false
82 | }
83 |
84 | note.title.equals(getString(R.string.default_note_title)) -> {
85 | return false
86 | }
87 | }
88 | return true
89 | }
90 |
91 | override fun onPause() {
92 | super.onPause()
93 | viewModel.setSingleNote(note)
94 | }
95 |
96 | override fun onAttach(context: Context) {
97 | super.onAttach(context)
98 | try{
99 | iMainActivity = context as IMainActivity
100 | }catch(e: ClassCastException){
101 | Log.e(TAG, "$context must implement IMainActivity" )
102 | }
103 | }
104 | }
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
--------------------------------------------------------------------------------
/app/src/main/java/com/codingwithmitch/mvvm_koin_room/ui/DetailNoteFragment.kt:
--------------------------------------------------------------------------------
1 | package com.codingwithmitch.mvvm_koin_room.ui
2 |
3 |
4 | import android.content.Context
5 | import android.os.Bundle
6 | import android.util.Log
7 | import android.view.*
8 | import androidx.fragment.app.Fragment
9 | import androidx.lifecycle.Observer
10 | import com.codingwithmitch.mvp_room_koin.models.Note
11 |
12 | import com.codingwithmitch.mvvm_koin_room.R
13 | import kotlinx.android.synthetic.main.fragment_detail_note.*
14 | import kotlinx.android.synthetic.main.layout_note_toolbar.*
15 | import org.koin.android.viewmodel.ext.android.sharedViewModel
16 |
17 | const val MODE_BUNDLE_KEY = "com.codingwithmitch.mvvm_koin_room.ui.mode"
18 | /**
19 | * class for viewing and updating notes
20 | */
21 | class DetailNoteFragment : Fragment(),
22 | View.OnClickListener,
23 | View.OnTouchListener,
24 | GestureDetector.OnGestureListener,
25 | GestureDetector.OnDoubleTapListener
26 | {
27 |
28 | private val TAG: String = "AppDebug"
29 |
30 | // "SharedViewModel": reuse existing viewmodel from hosting activity
31 | val viewModel: NoteViewModel by sharedViewModel()
32 |
33 | lateinit var iMainActivity: IMainActivity
34 |
35 | private lateinit var note: Note
36 |
37 | private val gestureDetector: GestureDetector by lazy {
38 | GestureDetector(this@DetailNoteFragment.context, this)
39 | }
40 |
41 | override fun onCreateView(
42 | inflater: LayoutInflater, container: ViewGroup?,
43 | savedInstanceState: Bundle?
44 | ): View? {
45 | return inflater.inflate(R.layout.fragment_detail_note, container, false)
46 | }
47 |
48 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
49 | super.onViewCreated(view, savedInstanceState)
50 | setListeners()
51 | subscribeObservers()
52 | }
53 |
54 | private fun setListeners(){
55 | detail_note_content.setOnTouchListener(this)
56 | note_toolbar_back_arrow.setOnClickListener(this)
57 | note_toolbar_check.setOnClickListener(this)
58 | detail_note_title.setOnClickListener(this)
59 | detail_note_edit_title.setOnClickListener(this)
60 | note_toolbar_back_arrow.setOnClickListener {
61 | iMainActivity.hideSoftKeyboard()
62 | iMainActivity.onBackPressed()
63 | }
64 | }
65 |
66 | private fun subscribeObservers(){
67 | viewModel.singleNote.observe(viewLifecycleOwner, Observer {
68 | note = it
69 | setNotePropertiesToView()
70 | })
71 |
72 | viewModel.executeSave.observe(viewLifecycleOwner, Observer { executeSave ->
73 | executeSave?.let{
74 | if(it){
75 | Log.d(TAG, "EXECUTING SAVE: ")
76 | updateNote()
77 | viewModel.setExecuteSave(false) // reset
78 | viewModel.setMode(EDIT_MODE_DISABLED) // reset)
79 | }
80 | }
81 | })
82 |
83 | viewModel.mode.observe(viewLifecycleOwner, Observer { mode ->
84 | when(mode){
85 |
86 | EDIT_MODE_DISABLED -> {
87 | disableEditMode()
88 | }
89 |
90 | EDIT_MODE_ENABLED -> {
91 | enableEditMode()
92 | }
93 | }
94 | })
95 | }
96 |
97 | fun setNotePropertiesToView(){
98 | detail_note_edit_title.setText(note.title)
99 | detail_note_title.text = note.title
100 | detail_note_content.setText(note.content) // setText() b/c custom view? Not sure but don't care.
101 | }
102 |
103 | fun setNoteProperties(){
104 | note.updated = System.currentTimeMillis()
105 | note.title = detail_note_edit_title.text.toString()
106 | note.content = detail_note_content.text.toString()
107 | }
108 |
109 | fun updateNote() {
110 | setNoteProperties()
111 | if(validateFields()){
112 | viewModel.updateNote(note)
113 | }
114 | }
115 |
116 | private fun validateFields(): Boolean {
117 | when{
118 | note.title.isBlank() -> {
119 | return false
120 | }
121 | }
122 | return true
123 | }
124 |
125 | override fun onPause() {
126 | super.onPause()
127 | // for restoring after process death or config change
128 | setNoteProperties()
129 | viewModel.setSingleNote(note)
130 | }
131 |
132 | override fun onAttach(context: Context) {
133 | super.onAttach(context)
134 | try{
135 | iMainActivity = context as IMainActivity
136 | }catch(e: ClassCastException){
137 | Log.e(TAG, "$context must implement IMainActivity" )
138 | }
139 | }
140 |
141 | override fun onClick(view: View?) {
142 | when(view?.id){
143 |
144 | R.id.note_toolbar_back_arrow -> {
145 | iMainActivity.hideSoftKeyboard()
146 | iMainActivity.onBackPressed()
147 | }
148 |
149 | R.id.note_toolbar_check -> {
150 | iMainActivity.hideSoftKeyboard()
151 | iMainActivity.onBackPressed()
152 | }
153 |
154 | R.id.detail_note_title ->{
155 | viewModel.setMode(EDIT_MODE_ENABLED)
156 | detail_note_edit_title.requestFocus()
157 | detail_note_edit_title.setSelection(detail_note_edit_title.length())
158 | }
159 | }
160 | }
161 |
162 | override fun onTouch(view: View?, motionEvent: MotionEvent?): Boolean {
163 | return gestureDetector.onTouchEvent(motionEvent)
164 | }
165 |
166 | override fun onShowPress(p0: MotionEvent?) {
167 | }
168 |
169 | override fun onSingleTapUp(p0: MotionEvent?): Boolean {
170 | return false
171 | }
172 |
173 | override fun onDown(p0: MotionEvent?): Boolean {
174 | return false
175 | }
176 |
177 | override fun onFling(p0: MotionEvent?, p1: MotionEvent?, p2: Float, p3: Float): Boolean {
178 | return false
179 | }
180 |
181 | override fun onScroll(p0: MotionEvent?, p1: MotionEvent?, p2: Float, p3: Float): Boolean {
182 | return false
183 | }
184 |
185 | override fun onLongPress(p0: MotionEvent?) {
186 |
187 | }
188 |
189 | override fun onDoubleTap(p0: MotionEvent?): Boolean {
190 | viewModel.setMode(EDIT_MODE_ENABLED)
191 | return false
192 | }
193 |
194 | override fun onDoubleTapEvent(p0: MotionEvent?): Boolean {
195 | return false
196 | }
197 |
198 | override fun onSingleTapConfirmed(p0: MotionEvent?): Boolean {
199 | return false
200 | }
201 |
202 |
203 | }
204 |
205 |
206 |
207 |
208 |
209 |
--------------------------------------------------------------------------------
/app/src/main/java/com/codingwithmitch/mvvm_koin_room/ui/IMainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.codingwithmitch.mvvm_koin_room.ui
2 |
3 | interface IMainActivity {
4 |
5 | fun onBackPressed()
6 |
7 | fun hideSoftKeyboard()
8 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/codingwithmitch/mvvm_koin_room/ui/INoteViewModel.kt:
--------------------------------------------------------------------------------
1 | package com.codingwithmitch.mvvm_koin_room.ui
2 |
3 | import com.codingwithmitch.mvp_room_koin.models.Note
4 |
5 | interface INoteViewModel {
6 |
7 | // NoteListFragment
8 | fun insertNote(note: Note)
9 |
10 | fun updateNote(note: Note)
11 |
12 | fun deleteNote(note: Note)
13 |
14 | fun deleteAllNotes()
15 |
16 | fun retrieveAllNotes()
17 |
18 | fun selectNote(note: Note)
19 |
20 | // NoteDetailFragment
21 |
22 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/codingwithmitch/mvvm_koin_room/ui/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.codingwithmitch.mvvm_koin_room.ui
2 |
3 | import android.content.Context
4 | import androidx.appcompat.app.AppCompatActivity
5 | import android.os.Bundle
6 | import android.view.inputmethod.InputMethodManager
7 | import com.codingwithmitch.mvp_room_koin.models.Note
8 | import com.codingwithmitch.mvp_room_koin.models.SINGLE_NOTE_BUNDLE_KEY
9 | import com.codingwithmitch.mvvm_koin_room.R
10 | import org.koin.android.viewmodel.ext.android.viewModel
11 |
12 | class MainActivity : AppCompatActivity(),
13 | IMainActivity
14 | {
15 |
16 | private val TAG: String = "AppDebug"
17 |
18 | // provide viewmodel instance using koin
19 | val viewModel: NoteViewModel by viewModel()
20 |
21 | override fun onCreate(savedInstanceState: Bundle?) {
22 | super.onCreate(savedInstanceState)
23 | setContentView(R.layout.activity_main)
24 |
25 | if(savedInstanceState != null){
26 | (savedInstanceState[SINGLE_NOTE_BUNDLE_KEY] as Note?)?.let {note ->
27 | viewModel.setSingleNote(note)
28 | }
29 | (savedInstanceState[MODE_BUNDLE_KEY] as Int?)?.let { mode ->
30 | viewModel.setMode(mode)
31 | }
32 | }
33 | }
34 |
35 | override fun onBackPressed() {
36 | viewModel.setExecuteSave(true) // save any notes being updated or created
37 | super.onBackPressed()
38 | }
39 |
40 | override fun onSaveInstanceState(outState: Bundle) {
41 | super.onSaveInstanceState(outState)
42 | outState.putParcelable(SINGLE_NOTE_BUNDLE_KEY, viewModel.singleNote.value)
43 | viewModel.mode.value?.let { mode ->
44 | outState.putInt(MODE_BUNDLE_KEY, mode)
45 | }
46 |
47 | }
48 |
49 | override fun hideSoftKeyboard() {
50 | if (currentFocus != null) {
51 | val inputMethodManager = getSystemService(
52 | Context.INPUT_METHOD_SERVICE) as InputMethodManager
53 | inputMethodManager
54 | .hideSoftInputFromWindow(currentFocus!!.windowToken, 0)
55 | }
56 | }
57 | }
58 |
59 |
60 |
61 |
62 |
63 |
64 |
--------------------------------------------------------------------------------
/app/src/main/java/com/codingwithmitch/mvvm_koin_room/ui/NoteListAdapter.kt:
--------------------------------------------------------------------------------
1 | package com.codingwithmitch.mvvm_koin_room.ui
2 |
3 | import android.annotation.SuppressLint
4 | import androidx.recyclerview.widget.RecyclerView
5 | import android.view.LayoutInflater
6 | import android.view.View
7 | import android.view.ViewGroup
8 | import androidx.recyclerview.widget.AsyncListDiffer
9 | import androidx.recyclerview.widget.DiffUtil
10 | import com.codingwithmitch.mvp_room_koin.models.Note
11 | import com.codingwithmitch.mvvm_koin_room.R
12 | import com.codingwithmitch.mvvm_koin_room.util.DateUtils
13 | import kotlinx.android.synthetic.main.layout_note_list_item.view.*
14 |
15 | class NoteListAdapter(private val interaction: Interaction? = null) :
16 | RecyclerView.Adapter() {
17 |
18 | val DIFF_CALLBACK = object : DiffUtil.ItemCallback() {
19 |
20 | override fun areItemsTheSame(oldItem: Note, newItem: Note): Boolean {
21 | return oldItem.id == newItem.id
22 | }
23 |
24 | @SuppressLint("DiffUtilEquals")
25 | override fun areContentsTheSame(oldItem: Note, newItem: Note): Boolean {
26 | return oldItem == newItem
27 | }
28 |
29 | }
30 | private val differ = AsyncListDiffer(this, DIFF_CALLBACK)
31 |
32 |
33 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
34 |
35 | return NoteViewHolder(
36 | LayoutInflater.from(parent.context).inflate(
37 | R.layout.layout_note_list_item,
38 | parent,
39 | false
40 | ),
41 | interaction
42 | )
43 | }
44 |
45 | override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
46 | when (holder) {
47 | is NoteViewHolder -> {
48 | holder.bind(differ.currentList.get(position))
49 | }
50 | }
51 | }
52 |
53 | override fun getItemCount(): Int {
54 | return differ.currentList.size
55 | }
56 |
57 | fun getNotes(): List{
58 | return differ.currentList
59 | }
60 |
61 | fun submitList(list: List) {
62 | differ.submitList(list)
63 | }
64 |
65 | class NoteViewHolder
66 | constructor(
67 | itemView: View,
68 | private val interaction: Interaction?
69 | ) : RecyclerView.ViewHolder(itemView) {
70 |
71 | fun bind(item: Note) = with(itemView) {
72 | itemView.setOnClickListener {
73 | interaction?.onItemSelected(adapterPosition, item)
74 | }
75 | itemView.note_title.text = item.title
76 | itemView.note_timestamp.text = DateUtils.convertLongToStringDate(item.updated)
77 | }
78 | }
79 |
80 | interface Interaction {
81 | fun onItemSelected(position: Int, item: Note)
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/app/src/main/java/com/codingwithmitch/mvvm_koin_room/ui/NoteListFragment.kt:
--------------------------------------------------------------------------------
1 | package com.codingwithmitch.mvvm_koin_room.ui
2 |
3 |
4 | import android.os.Bundle
5 | import android.util.Log
6 | import androidx.fragment.app.Fragment
7 | import android.view.LayoutInflater
8 | import android.view.View
9 | import android.view.ViewGroup
10 | import androidx.lifecycle.Observer
11 | import androidx.navigation.fragment.findNavController
12 | import androidx.recyclerview.widget.LinearLayoutManager
13 | import com.codingwithmitch.mvp_room_koin.models.Note
14 |
15 | import com.codingwithmitch.mvvm_koin_room.R
16 | import com.codingwithmitch.mvvm_koin_room.util.TopSpacingItemDecoration
17 | import kotlinx.android.synthetic.main.fragment_note_list.*
18 | import org.koin.android.viewmodel.ext.android.sharedViewModel
19 |
20 | /**
21 | * View a list of all notes
22 | */
23 | class NoteListFragment : Fragment(),
24 | NoteListAdapter.Interaction
25 | {
26 |
27 | private val TAG: String = "AppDebug"
28 |
29 | // "SharedViewModel": reuse existing viewmodel from hosting activity
30 | val viewModel: NoteViewModel by sharedViewModel()
31 |
32 | lateinit var listAdapter: NoteListAdapter
33 |
34 | override fun onCreateView(
35 | inflater: LayoutInflater, container: ViewGroup?,
36 | savedInstanceState: Bundle?
37 | ): View? {
38 | return inflater.inflate(R.layout.fragment_note_list, container, false)
39 | }
40 |
41 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
42 | super.onViewCreated(view, savedInstanceState)
43 |
44 | add_note_fab.setOnClickListener {
45 | findNavController().navigate(R.id.action_noteListFragment_to_createNoteFragment)
46 | }
47 |
48 | subscribeObservers()
49 |
50 | initRecyclerView()
51 | getAllNotes()
52 | }
53 |
54 | private fun initRecyclerView() {
55 | recyclerview.apply {
56 | listAdapter = NoteListAdapter(this@NoteListFragment)
57 | val layoutManager = LinearLayoutManager(this@NoteListFragment.context)
58 | recyclerview.addItemDecoration(TopSpacingItemDecoration(15))
59 | recyclerview.layoutManager = layoutManager
60 | recyclerview.adapter = listAdapter
61 | }
62 | }
63 |
64 | private fun subscribeObservers(){
65 | viewModel.allNotes.observe(viewLifecycleOwner, Observer { notesList ->
66 | notesList?.let{
67 | for(note in notesList){
68 | Log.d(TAG, "$note")
69 | }
70 | listAdapter.submitList(notesList)
71 | }
72 | })
73 | }
74 |
75 | private fun getAllNotes(){
76 | viewModel.retrieveAllNotes()
77 | }
78 |
79 | override fun onItemSelected(position: Int, item: Note) {
80 | viewModel.setSingleNote(item)
81 | findNavController().navigate(R.id.action_noteListFragment_to_detailNoteFragment)
82 | }
83 | }
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
--------------------------------------------------------------------------------
/app/src/main/java/com/codingwithmitch/mvvm_koin_room/ui/NoteViewModel.kt:
--------------------------------------------------------------------------------
1 | package com.codingwithmitch.mvvm_koin_room.ui
2 |
3 | import android.util.Log
4 | import androidx.lifecycle.LiveData
5 | import androidx.lifecycle.MutableLiveData
6 | import androidx.lifecycle.ViewModel
7 | import androidx.lifecycle.viewModelScope
8 | import com.codingwithmitch.mvp_room_koin.models.Note
9 | import com.codingwithmitch.mvvm_koin_room.repositories.NoteRepository
10 | import kotlinx.coroutines.cancel
11 | import kotlinx.coroutines.launch
12 |
13 | const val EDIT_MODE_ENABLED = 1
14 | const val EDIT_MODE_DISABLED = 2
15 |
16 | class NoteViewModel(
17 | private val noteRepository: NoteRepository
18 | ) : INoteViewModel, ViewModel(){
19 |
20 | private val TAG: String = "AppDebug"
21 |
22 | private val _allNotes: MutableLiveData> = MutableLiveData()
23 | private val _singleNote: MutableLiveData = MutableLiveData()
24 | private val _executeSave: MutableLiveData = MutableLiveData()
25 | private val _mode: MutableLiveData = MutableLiveData()
26 |
27 | val allNotes: LiveData> get() = _allNotes
28 | val singleNote: LiveData get() = _singleNote
29 | val executeSave: LiveData get() = _executeSave
30 | val mode: LiveData get() = _mode
31 |
32 | init {
33 | setMode(EDIT_MODE_DISABLED)
34 | }
35 |
36 | override fun insertNote(note: Note) {
37 | viewModelScope.launch {
38 | noteRepository.insertNote(note)
39 | }
40 | }
41 |
42 | override fun updateNote(note: Note) {
43 | viewModelScope.launch {
44 | noteRepository.updateNote(note)
45 | }
46 | }
47 |
48 | override fun deleteNote(note: Note) {
49 | viewModelScope.launch {
50 | noteRepository.deleteNote(note)
51 | }
52 | }
53 |
54 | override fun deleteAllNotes() {
55 | viewModelScope.launch {
56 | noteRepository.deleteAllNotes()
57 | }
58 | }
59 |
60 | override fun retrieveAllNotes() {
61 | viewModelScope.launch {
62 | _allNotes.value = noteRepository.getAllNotes()
63 | }
64 | }
65 |
66 | override fun selectNote(note: Note) {
67 | _singleNote.value = note
68 | }
69 |
70 | fun setSingleNote(note: Note){
71 | _singleNote.value = note
72 | }
73 |
74 | fun setExecuteSave(shouldExecuteNow: Boolean){
75 | _executeSave.value = shouldExecuteNow
76 | }
77 |
78 | fun setMode(mode: Int){
79 | _mode.value = mode
80 | }
81 |
82 | override fun onCleared() {
83 | super.onCleared()
84 | viewModelScope.cancel()
85 | }
86 | }
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
--------------------------------------------------------------------------------
/app/src/main/java/com/codingwithmitch/mvvm_koin_room/ui/ViewExtensions.kt:
--------------------------------------------------------------------------------
1 | package com.codingwithmitch.mvvm_koin_room.ui
2 |
3 | import android.view.View
4 | import android.widget.EditText
5 | import kotlinx.android.synthetic.main.fragment_detail_note.*
6 | import kotlinx.android.synthetic.main.layout_note_toolbar.*
7 |
8 | fun DetailNoteFragment.disableContentInteraction(){
9 | detail_note_content.setKeyListener(null)
10 | detail_note_content.setFocusable(false)
11 | detail_note_content.setFocusableInTouchMode(false)
12 | detail_note_content.setCursorVisible(false)
13 | detail_note_content.clearFocus()
14 | }
15 |
16 |
17 | fun DetailNoteFragment.enableContentInteraction(){
18 | detail_note_content.setKeyListener(EditText(this.context).keyListener)
19 | detail_note_content.setFocusable(true)
20 | detail_note_content.setFocusableInTouchMode(true)
21 | detail_note_content.setCursorVisible(true)
22 | detail_note_content.requestFocus()
23 | }
24 |
25 |
26 | fun DetailNoteFragment.enableEditMode() {
27 | back_arrow_container.setVisibility(View.GONE)
28 | check_container.setVisibility(View.VISIBLE)
29 | detail_note_title.setVisibility(View.GONE)
30 | detail_note_edit_title.setVisibility(View.VISIBLE)
31 | enableContentInteraction()
32 | }
33 |
34 | fun DetailNoteFragment.disableEditMode(){
35 | back_arrow_container.setVisibility(View.VISIBLE)
36 | check_container.setVisibility(View.GONE)
37 | detail_note_title.setVisibility(View.VISIBLE)
38 | detail_note_edit_title.setVisibility(View.GONE)
39 | disableContentInteraction()
40 | }
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
--------------------------------------------------------------------------------
/app/src/main/java/com/codingwithmitch/mvvm_koin_room/util/AbsentLiveData.kt:
--------------------------------------------------------------------------------
1 | package com.codingwithmitch.mvvm_koin_room.util
2 |
3 | import androidx.lifecycle.LiveData
4 |
5 | /**
6 | * A LiveData class that has `null` value.
7 | */
8 | class AbsentLiveData private constructor(): LiveData() {
9 |
10 | init {
11 | // use post instead of set since this can be created on any thread
12 | postValue(null)
13 | }
14 |
15 | companion object {
16 | fun create(): LiveData {
17 | return AbsentLiveData()
18 | }
19 | }
20 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/codingwithmitch/mvvm_koin_room/util/DateUtils.kt:
--------------------------------------------------------------------------------
1 | package com.codingwithmitch.mvvm_koin_room.util
2 |
3 | import java.text.SimpleDateFormat
4 | import java.util.*
5 |
6 | class DateUtils {
7 |
8 | companion object{
9 |
10 | private val TAG: String = "AppDebug"
11 |
12 | // dates from server look like this: "2019-07-23T03:28:01.406944Z"
13 | fun convertServerStringDateToLong(sd: String): Long{
14 | var stringDate = sd.removeRange(sd.indexOf("T") until sd.length)
15 | val sdf = SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH)
16 | try {
17 | val time = sdf.parse(stringDate).time
18 | return time
19 | } catch (e: Exception) {
20 | throw Exception(e)
21 | }
22 | }
23 |
24 | fun convertLongToStringDate(longDate: Long): String{
25 | val sdf = SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH)
26 | try {
27 | val date = sdf.format(Date(longDate))
28 | return date
29 | } catch (e: Exception) {
30 | throw Exception(e)
31 | }
32 | }
33 | }
34 |
35 |
36 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/codingwithmitch/mvvm_koin_room/util/TopSpacingItemDecoration.kt:
--------------------------------------------------------------------------------
1 | package com.codingwithmitch.mvvm_koin_room.util
2 |
3 | import androidx.recyclerview.widget.RecyclerView
4 | import android.graphics.Rect
5 | import android.view.View
6 |
7 |
8 | class TopSpacingItemDecoration(private val padding: Int) : RecyclerView.ItemDecoration() {
9 |
10 | override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
11 | super.getItemOffsets(outRect, view, parent, state)
12 | outRect.top = padding
13 | }
14 | }
--------------------------------------------------------------------------------
/app/src/main/res/anim/slide_in_left.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/anim/slide_in_right.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/anim/slide_out_left.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/anim/slide_out_right.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_add_white_24dp.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_arrow_back_black_24dp.xml:
--------------------------------------------------------------------------------
1 |
6 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_check_black_24dp.xml:
--------------------------------------------------------------------------------
1 |
6 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
9 |
10 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_create_note.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
14 |
15 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
36 |
37 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_detail_note.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
14 |
15 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
36 |
37 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_note_list.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
15 |
16 |
17 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/layout_create_note_toolbar.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
16 |
17 |
24 |
25 |
26 |
27 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/layout_note_list_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
16 |
17 |
27 |
28 |
32 |
33 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/layout_note_toolbar.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
14 |
15 |
23 |
24 |
25 |
26 |
27 |
33 |
34 |
41 |
42 |
43 |
44 |
54 |
55 |
67 |
68 |
69 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mitchtabian/MVVM-Koin-Repository-Pattern/b06099630058e4ffd0abfaa3faf0dfa1f3845db5/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mitchtabian/MVVM-Koin-Repository-Pattern/b06099630058e4ffd0abfaa3faf0dfa1f3845db5/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mitchtabian/MVVM-Koin-Repository-Pattern/b06099630058e4ffd0abfaa3faf0dfa1f3845db5/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mitchtabian/MVVM-Koin-Repository-Pattern/b06099630058e4ffd0abfaa3faf0dfa1f3845db5/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mitchtabian/MVVM-Koin-Repository-Pattern/b06099630058e4ffd0abfaa3faf0dfa1f3845db5/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mitchtabian/MVVM-Koin-Repository-Pattern/b06099630058e4ffd0abfaa3faf0dfa1f3845db5/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mitchtabian/MVVM-Koin-Repository-Pattern/b06099630058e4ffd0abfaa3faf0dfa1f3845db5/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mitchtabian/MVVM-Koin-Repository-Pattern/b06099630058e4ffd0abfaa3faf0dfa1f3845db5/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mitchtabian/MVVM-Koin-Repository-Pattern/b06099630058e4ffd0abfaa3faf0dfa1f3845db5/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mitchtabian/MVVM-Koin-Repository-Pattern/b06099630058e4ffd0abfaa3faf0dfa1f3845db5/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/navigation/notes_nav_graph.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
12 |
19 |
20 |
27 |
28 |
29 |
34 |
35 |
40 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #ffd24d
4 | #e6ac00
5 | #d9d9d9
6 |
7 | #ffffb3
8 | #ffdf80
9 | #ffd24d
10 | #f2f2f2
11 | #a2a2a2
12 | #000
13 | #fff
14 |
15 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimen.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 16dp
6 |
7 |
8 | .2
9 |
10 |
11 | 55dp
12 | 40dp
13 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | MVVM-Koin-Room
3 | Title
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/codingwithmitch/mvvm_koin_room/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package com.codingwithmitch.mvvm_koin_room
2 |
3 | import org.junit.Test
4 |
5 | import org.junit.Assert.*
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * See [testing documentation](http://d.android.com/tools/testing).
11 | */
12 | class ExampleUnitTest {
13 | @Test
14 | fun addition_isCorrect() {
15 | assertEquals(4, 2 + 2)
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | ext.kotlin_version = '1.3.61'
5 | repositories {
6 | google()
7 | jcenter()
8 |
9 | }
10 | dependencies {
11 | classpath 'com.android.tools.build:gradle:3.5.2'
12 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
13 | // NOTE: Do not place your application dependencies here; they belong
14 | // in the individual module build.gradle files
15 | }
16 | }
17 |
18 | allprojects {
19 | repositories {
20 | google()
21 | jcenter()
22 |
23 | }
24 | }
25 |
26 | task clean(type: Delete) {
27 | delete rootProject.buildDir
28 | }
29 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app's APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Automatically convert third-party libraries to use AndroidX
19 | android.enableJetifier=true
20 | # Kotlin code style for this project: "official" or "obsolete":
21 | kotlin.code.style=official
22 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mitchtabian/MVVM-Koin-Repository-Pattern/b06099630058e4ffd0abfaa3faf0dfa1f3845db5/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Tue Jan 07 13:24:34 PST 2020
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 | rootProject.name='MVVM-Koin-Room'
3 |
--------------------------------------------------------------------------------