├── .gitignore ├── LICENSE ├── README.md ├── api ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── silverlotus │ │ └── api │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── silverlotus │ │ │ └── api │ │ │ ├── Api.kt │ │ │ ├── model │ │ │ ├── MangaListModel.kt │ │ │ └── MangaModel.kt │ │ │ └── subapi │ │ │ └── MangaEdenApi.kt │ └── res │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── silverlotus │ └── api │ └── ExampleUnitTest.kt ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── silverlotus │ │ └── kmvvm │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── silverlotus │ │ │ └── kmvvm │ │ │ ├── adapter │ │ │ └── MangaAdapter.kt │ │ │ ├── base │ │ │ ├── App.kt │ │ │ ├── BaseActivity.kt │ │ │ ├── BaseFragment.kt │ │ │ ├── BaseRepository.kt │ │ │ └── BaseViewModel.kt │ │ │ ├── repository │ │ │ └── MangaRepository.kt │ │ │ ├── room │ │ │ ├── AppDatabase.kt │ │ │ ├── dao │ │ │ │ └── MangaDao.kt │ │ │ └── entity │ │ │ │ └── MangaEntity.kt │ │ │ └── ui │ │ │ └── main │ │ │ ├── MainActivity.kt │ │ │ ├── MainFragment.kt │ │ │ ├── MainView.kt │ │ │ └── MainViewModel.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ ├── activity_main.xml │ │ ├── fragment_main.xml │ │ └── item_manga.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 │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── silverlotus │ └── kmvvm │ └── ExampleUnitTest.kt ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | # Intellij 36 | *.iml 37 | /.idea 38 | 39 | # Keystore files 40 | *.jks 41 | 42 | # External native build folder generated in Android Studio 2.2 and later 43 | .externalNativeBuild 44 | 45 | # Google Services (e.g. APIs or Firebase) 46 | google-services.json 47 | 48 | # Freeline 49 | freeline.py 50 | freeline/ 51 | freeline_project_description.json 52 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Gian Patrick Quintana 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Android-Kotlin-MVVM-Template 2 | 3 | Short Intro: 4 | 5 | Anyways so this is my MVVM template. I use these stuff: 6 | * Kotlin (Because Kotlin is life) 7 | * [Kodein-DI](https://github.com/Kodein-Framework/Kodein-DI) (For dependency injection) 8 | * Examples of Kodein Usages 9 | * [Fuel](https://github.com/kittinunf/Fuel) (Http Client) 10 | * Android Architecture LiveData 11 | * Android Architecture Lifecycle 12 | * Android Architecture Room 13 | * Android Architecture Paging 14 | * [Klaxon](https://github.com/cbeust/klaxon) (JSON Parser) 15 | * [Logger](https://github.com/orhanobut/logger) (Logger) 16 | 17 | Future Updates: 18 | A much more in depth example of this template. For now a more simple example which you can expand easily. -------------------------------------------------------------------------------- /api/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /api/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | apply plugin: 'kotlin-android' 4 | 5 | android { 6 | compileSdkVersion 27 7 | 8 | defaultConfig { 9 | minSdkVersion 19 10 | targetSdkVersion 27 11 | versionCode 1 12 | versionName "1.0" 13 | 14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 15 | } 16 | 17 | buildTypes { 18 | 19 | release { 20 | minifyEnabled false 21 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | 25 | testOptions { 26 | unitTests{ 27 | includeAndroidResources = true 28 | } 29 | } 30 | } 31 | 32 | dependencies { 33 | implementation fileTree(dir: 'libs', include: ['*.jar']) 34 | 35 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" 36 | implementation "com.android.support:appcompat-v7:$support_library_version" 37 | 38 | implementation "com.github.kittinunf.fuel:fuel:$fuel_version" 39 | implementation "com.github.kittinunf.fuel:fuel-android:$fuel_version" 40 | implementation "com.github.kittinunf.fuel:fuel-livedata:$fuel_version" 41 | 42 | implementation 'com.beust:klaxon:2.1.1' 43 | implementation 'com.orhanobut:logger:2.1.1' 44 | 45 | testImplementation 'org.robolectric:robolectric:3.8' 46 | testImplementation 'junit:junit:4.12' 47 | androidTestImplementation 'com.android.support.test:runner:1.0.1' 48 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' 49 | } 50 | repositories { 51 | mavenCentral() 52 | } 53 | -------------------------------------------------------------------------------- /api/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 | -------------------------------------------------------------------------------- /api/src/androidTest/java/com/silverlotus/api/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.silverlotus.api 2 | 3 | import android.content.Context 4 | import android.support.test.InstrumentationRegistry 5 | import android.support.test.runner.AndroidJUnit4 6 | 7 | import org.junit.Test 8 | import org.junit.runner.RunWith 9 | 10 | import org.junit.Assert.* 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see [Testing documentation](http://d.android.com/tools/testing) 16 | */ 17 | @RunWith(AndroidJUnit4::class) 18 | class ExampleInstrumentedTest { 19 | @Test 20 | @Throws(Exception::class) 21 | fun useAppContext() { 22 | // Context of the app under test. 23 | val appContext = InstrumentationRegistry.getTargetContext() 24 | 25 | assertEquals("com.silverlotus.api.test", appContext.packageName) 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /api/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /api/src/main/java/com/silverlotus/api/Api.kt: -------------------------------------------------------------------------------- 1 | package com.silverlotus.api 2 | 3 | import com.github.kittinunf.fuel.Fuel 4 | import com.github.kittinunf.fuel.core.Request 5 | import com.github.kittinunf.fuel.livedata.liveDataObject 6 | import com.silverlotus.api.model.MangaListModel 7 | import com.silverlotus.api.subapi.MangaEdenApi 8 | 9 | /** 10 | * Created by Gian Patrick Quintana on 2/1/2018. 11 | */ 12 | 13 | class Api { 14 | 15 | private fun request(api: Fuel.RequestConvertible): Request { 16 | 17 | return Fuel.request(api) 18 | .timeout(30000) 19 | } 20 | 21 | fun getMangaList() = request(MangaEdenApi.MangaList()).liveDataObject(MangaListModel.Deserializer()) 22 | } -------------------------------------------------------------------------------- /api/src/main/java/com/silverlotus/api/model/MangaListModel.kt: -------------------------------------------------------------------------------- 1 | package com.silverlotus.api.model 2 | 3 | import com.beust.klaxon.Json 4 | import com.beust.klaxon.Klaxon 5 | import com.github.kittinunf.fuel.core.ResponseDeserializable 6 | 7 | /** 8 | * Created by Gian Patrick Quintana on 2/19/2018. 9 | */ 10 | data class MangaListModel( 11 | @Json(name = "manga") val listOfMangaData: List = listOf() 12 | ) { 13 | class Deserializer : ResponseDeserializable { 14 | override fun deserialize(content: String) = Klaxon().parse(content) 15 | } 16 | } -------------------------------------------------------------------------------- /api/src/main/java/com/silverlotus/api/model/MangaModel.kt: -------------------------------------------------------------------------------- 1 | package com.silverlotus.api.model 2 | 3 | import com.beust.klaxon.Json 4 | 5 | /** 6 | * Created by Gian Patrick Quintana on 2/21/2018. 7 | */ 8 | data class MangaModel( 9 | @Json(name = "i") val id: String = "", 10 | @Json(name = "t") var title: String = "" 11 | ) 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /api/src/main/java/com/silverlotus/api/subapi/MangaEdenApi.kt: -------------------------------------------------------------------------------- 1 | package com.silverlotus.api.subapi 2 | 3 | import com.github.kittinunf.fuel.core.Method 4 | import com.github.kittinunf.fuel.util.FuelRouting 5 | 6 | /** 7 | * Created by Gian Patrick Quintana on 2/19/2018. 8 | */ 9 | internal sealed class MangaEdenApi : FuelRouting { 10 | 11 | class MangaList : MangaEdenApi() 12 | 13 | override val basePath: String = "https://www.mangaeden.com/" 14 | 15 | override val headers: Map? = null 16 | 17 | override val method: Method 18 | get() { 19 | when (this) { 20 | is MangaList -> return Method.GET 21 | } 22 | } 23 | 24 | override val params: List>? 25 | get() { 26 | when (this) { 27 | is MangaList -> return null 28 | } 29 | } 30 | override val path: String 31 | get() { 32 | when (this) { 33 | is MangaList -> return "api/list/0/" 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /api/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | api 3 | 4 | -------------------------------------------------------------------------------- /api/src/test/java/com/silverlotus/api/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package com.silverlotus.api 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 | @Throws(Exception::class) 15 | fun addition_isCorrect() { 16 | assertEquals(4, (2 + 2).toLong()) 17 | } 18 | } -------------------------------------------------------------------------------- /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 27 11 | 12 | defaultConfig { 13 | applicationId "com.silverlotus.kmvvm" 14 | minSdkVersion 19 15 | targetSdkVersion 27 16 | versionCode 1 17 | versionName "1.0" 18 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 19 | } 20 | 21 | compileOptions { 22 | sourceCompatibility JavaVersion.VERSION_1_8 23 | targetCompatibility JavaVersion.VERSION_1_8 24 | } 25 | 26 | buildTypes { 27 | 28 | release { 29 | minifyEnabled false 30 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 31 | } 32 | } 33 | 34 | testOptions { 35 | unitTests{ 36 | includeAndroidResources = true 37 | } 38 | } 39 | } 40 | 41 | dependencies { 42 | implementation fileTree(include: ['*.jar'], dir: 'libs') 43 | 44 | implementation project(':api') 45 | 46 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" 47 | implementation 'androidx.core:core-ktx:0.2' 48 | 49 | implementation "com.android.support:appcompat-v7:$support_library_version" 50 | implementation "com.android.support:design:$support_library_version" 51 | implementation "com.android.support:recyclerview-v7:$support_library_version" 52 | implementation "com.android.support:cardview-v7:$support_library_version" 53 | implementation "com.android.support:customtabs:$support_library_version" 54 | implementation 'com.android.support.constraint:constraint-layout:1.1.0-beta6' 55 | 56 | implementation "org.kodein.di:kodein-di-generic-jvm:$kodein_version" 57 | implementation "org.kodein.di:kodein-di-framework-android:$kodein_version" 58 | 59 | implementation "android.arch.lifecycle:extensions:$architecture_version" 60 | implementation "android.arch.lifecycle:common-java8:$architecture_version" 61 | 62 | implementation 'android.arch.persistence.room:runtime:1.1.0-beta1' 63 | kapt "android.arch.persistence.room:compiler:1.1.0-beta1" 64 | 65 | implementation "android.arch.paging:runtime:1.0.0-alpha7" 66 | 67 | implementation "com.github.kittinunf.fuel:fuel:$fuel_version" 68 | 69 | implementation 'com.orhanobut:logger:2.1.1' 70 | 71 | testImplementation 'org.robolectric:robolectric:3.8' 72 | testImplementation 'junit:junit:4.12' 73 | androidTestImplementation 'com.android.support.test:runner:1.0.1' 74 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' 75 | } 76 | -------------------------------------------------------------------------------- /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 | -keepattributes Signature 23 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/silverlotus/kmvvm/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.silverlotus.kmvvm 2 | 3 | import android.support.test.InstrumentationRegistry 4 | import android.support.test.runner.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.getTargetContext() 22 | assertEquals("com.silverlotus.kmvp", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 14 | 15 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /app/src/main/java/com/silverlotus/kmvvm/adapter/MangaAdapter.kt: -------------------------------------------------------------------------------- 1 | package com.silverlotus.kmvvm.adapter 2 | 3 | import android.support.v7.recyclerview.extensions.ListAdapter 4 | import android.support.v7.util.DiffUtil 5 | import android.support.v7.widget.RecyclerView 6 | import android.view.LayoutInflater 7 | import android.view.View 8 | import android.view.ViewGroup 9 | import com.silverlotus.kmvvm.R 10 | import com.silverlotus.kmvvm.room.entity.MangaEntity 11 | import kotlinx.android.synthetic.main.item_manga.view.* 12 | 13 | /** 14 | * Created by Gian Patrick Quintana on 2/24/2018. 15 | */ 16 | class MangaAdapter : ListAdapter(MangaItemCallback()) { 17 | 18 | private class MangaItemCallback : DiffUtil.ItemCallback() { 19 | 20 | override fun areItemsTheSame(oldItem: MangaEntity?, newItem: MangaEntity?): Boolean = oldItem?.id == newItem?.id 21 | 22 | override fun areContentsTheSame(oldItem: MangaEntity?, newItem: MangaEntity?): Boolean = oldItem?.title == newItem?.title 23 | } 24 | 25 | fun isLastItemVisible(): Boolean { 26 | 27 | getItem(itemCount - 1) 28 | 29 | return false 30 | } 31 | 32 | override fun onBindViewHolder(holder: MangaViewHolder, position: Int) { 33 | val manga = getItem(position) 34 | if (manga != null) 35 | holder.bind(manga) 36 | } 37 | 38 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MangaViewHolder = 39 | MangaViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_manga, parent, false)) 40 | 41 | inner class MangaViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { 42 | 43 | fun bind(manga: MangaEntity?) = with(itemView) { 44 | textTitle.text = manga?.title 45 | } 46 | } 47 | 48 | } -------------------------------------------------------------------------------- /app/src/main/java/com/silverlotus/kmvvm/base/App.kt: -------------------------------------------------------------------------------- 1 | package com.silverlotus.kmvvm.base 2 | 3 | import android.app.Application 4 | import android.arch.persistence.room.Room 5 | import android.content.Context 6 | import android.content.SharedPreferences 7 | import com.orhanobut.logger.AndroidLogAdapter 8 | import com.orhanobut.logger.Logger 9 | import com.silverlotus.api.Api 10 | import com.silverlotus.kmvvm.repository.MangaRepository 11 | import com.silverlotus.kmvvm.room.AppDatabase 12 | import com.silverlotus.kmvvm.room.dao.MangaDao 13 | import org.kodein.di.Kodein 14 | import org.kodein.di.KodeinAware 15 | import org.kodein.di.generic.bind 16 | import org.kodein.di.generic.instance 17 | import org.kodein.di.generic.singleton 18 | 19 | /** 20 | * Created by Gian Patrick Quintana on 1/22/2018. 21 | */ 22 | class App : Application(), KodeinAware { 23 | 24 | override val kodein = Kodein.lazy { 25 | 26 | val database: AppDatabase = Room.databaseBuilder(applicationContext, AppDatabase::class.java, "app.db") 27 | .allowMainThreadQueries() 28 | .build() 29 | 30 | bind() with singleton { database.mangaListDao() } 31 | bind() with singleton { MangaRepository(applicationContext) } 32 | bind() with singleton { Api() } 33 | bind() with instance(getSharedPreferences("PREFERENCES", Context.MODE_PRIVATE)) 34 | } 35 | 36 | // override val kodein by Kodein.lazy { 37 | // 38 | // val database: AppDatabase = Room.databaseBuilder(applicationContext, AppDatabase::class.java, "app.db") 39 | // .allowMainThreadQueries() 40 | // .build() 41 | // 42 | // bind() with singleton { database.mangaListDao() } 43 | // bind() with singleton { MangaRepository().init(appKodein()) } 44 | // bind() with singleton { Api() } 45 | // } 46 | 47 | override fun onCreate() { 48 | super.onCreate() 49 | 50 | Logger.addLogAdapter(AndroidLogAdapter()) 51 | } 52 | } -------------------------------------------------------------------------------- /app/src/main/java/com/silverlotus/kmvvm/base/BaseActivity.kt: -------------------------------------------------------------------------------- 1 | package com.silverlotus.kmvvm.base 2 | 3 | import android.annotation.SuppressLint 4 | import android.os.Bundle 5 | import android.support.v7.app.AppCompatActivity 6 | import org.kodein.di.Kodein 7 | import org.kodein.di.KodeinAware 8 | import org.kodein.di.KodeinTrigger 9 | import org.kodein.di.android.closestKodein 10 | 11 | /** 12 | * Created by Gian Patrick Quintana on 1/22/2018. 13 | */ 14 | @SuppressLint("Registered") 15 | abstract class BaseActivity : AppCompatActivity(), KodeinAware { 16 | 17 | override val kodein: Kodein by closestKodein() 18 | override val kodeinTrigger = KodeinTrigger() 19 | 20 | override fun onCreate(savedInstanceState: Bundle?) { 21 | super.onCreate(savedInstanceState) 22 | kodeinTrigger.trigger() 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /app/src/main/java/com/silverlotus/kmvvm/base/BaseFragment.kt: -------------------------------------------------------------------------------- 1 | package com.silverlotus.kmvvm.base 2 | 3 | import android.os.Bundle 4 | import android.support.v4.app.Fragment 5 | import org.kodein.di.Kodein 6 | import org.kodein.di.KodeinAware 7 | import org.kodein.di.KodeinTrigger 8 | import org.kodein.di.android.closestKodein 9 | 10 | /** 11 | * Created by Gian Patrick Quintana on 1/22/2018. 12 | */ 13 | abstract class BaseFragment : Fragment(), KodeinAware { 14 | 15 | override val kodein: Kodein by closestKodein() 16 | override val kodeinTrigger = KodeinTrigger() 17 | 18 | override fun onCreate(savedInstanceState: Bundle?) { 19 | super.onCreate(savedInstanceState) 20 | kodeinTrigger.trigger() 21 | } 22 | } -------------------------------------------------------------------------------- /app/src/main/java/com/silverlotus/kmvvm/base/BaseRepository.kt: -------------------------------------------------------------------------------- 1 | package com.silverlotus.kmvvm.base 2 | 3 | import android.content.Context 4 | import org.kodein.di.Kodein 5 | import org.kodein.di.KodeinAware 6 | import org.kodein.di.KodeinTrigger 7 | import org.kodein.di.android.closestKodein 8 | 9 | /** 10 | * Created by Gian Patrick Quintana on 2/23/2018. 11 | */ 12 | abstract class BaseRepository(context: Context) : KodeinAware { 13 | 14 | override val kodein: Kodein by closestKodein(context) 15 | override val kodeinTrigger: KodeinTrigger = KodeinTrigger() 16 | 17 | @Suppress("UNCHECKED_CAST") 18 | fun init(): T { 19 | 20 | kodeinTrigger.trigger() 21 | return this as T 22 | } 23 | 24 | } -------------------------------------------------------------------------------- /app/src/main/java/com/silverlotus/kmvvm/base/BaseViewModel.kt: -------------------------------------------------------------------------------- 1 | package com.silverlotus.kmvvm.base 2 | 3 | import android.arch.lifecycle.ViewModel 4 | import org.kodein.di.Kodein 5 | import org.kodein.di.KodeinAware 6 | import org.kodein.di.KodeinTrigger 7 | 8 | /** 9 | * Created by Gian Patrick Quintana on 2/23/2018. 10 | */ 11 | abstract class BaseViewModel : ViewModel(), KodeinAware { 12 | 13 | override lateinit var kodein: Kodein 14 | override val kodeinTrigger: KodeinTrigger = KodeinTrigger() 15 | 16 | @Suppress("UNCHECKED_CAST") 17 | fun init(kodein: Kodein): T { 18 | 19 | this.kodein = kodein 20 | kodeinTrigger.trigger() 21 | return this as T 22 | } 23 | 24 | } -------------------------------------------------------------------------------- /app/src/main/java/com/silverlotus/kmvvm/repository/MangaRepository.kt: -------------------------------------------------------------------------------- 1 | package com.silverlotus.kmvvm.repository 2 | 3 | import android.arch.paging.LivePagedListBuilder 4 | import android.arch.paging.PagedList 5 | import android.content.Context 6 | import com.silverlotus.api.Api 7 | import com.silverlotus.kmvvm.base.BaseRepository 8 | import com.silverlotus.kmvvm.room.dao.MangaDao 9 | import com.silverlotus.kmvvm.room.entity.MangaEntity 10 | import org.kodein.di.generic.instance 11 | 12 | /** 13 | * Created by Gian Patrick Quintana on 2/22/2018. 14 | */ 15 | 16 | class MangaRepository(private val context: Context) : BaseRepository(context = context) { 17 | 18 | private val mangaDao by instance() 19 | private val api by instance() 20 | 21 | val pagingConfig = PagedList.Config.Builder() 22 | .setPageSize(20) 23 | .build() 24 | 25 | fun getMangaList(max: Int) = LivePagedListBuilder(mangaDao.getMangaList(max), pagingConfig).build() 26 | 27 | fun fetchMangaList() { 28 | api.getMangaList().observeForever({ result -> 29 | if (result != null) { 30 | val (data, error) = result 31 | if (data != null) 32 | mangaDao.updateMangaList(MangaEntity.fromMangaList(data)) 33 | } 34 | }) 35 | } 36 | } -------------------------------------------------------------------------------- /app/src/main/java/com/silverlotus/kmvvm/room/AppDatabase.kt: -------------------------------------------------------------------------------- 1 | package com.silverlotus.kmvvm.room 2 | 3 | import android.arch.persistence.room.Database 4 | import android.arch.persistence.room.RoomDatabase 5 | import com.silverlotus.kmvvm.room.dao.MangaDao 6 | import com.silverlotus.kmvvm.room.entity.MangaEntity 7 | 8 | /** 9 | * Created by Gian Patrick Quintana on 2/21/2018. 10 | */ 11 | @Database(entities = [(MangaEntity::class)], version = 1, exportSchema = false) 12 | abstract class AppDatabase : RoomDatabase() { 13 | 14 | abstract fun mangaListDao(): MangaDao 15 | } -------------------------------------------------------------------------------- /app/src/main/java/com/silverlotus/kmvvm/room/dao/MangaDao.kt: -------------------------------------------------------------------------------- 1 | package com.silverlotus.kmvvm.room.dao 2 | 3 | import android.arch.paging.DataSource 4 | import android.arch.persistence.room.Dao 5 | import android.arch.persistence.room.Insert 6 | import android.arch.persistence.room.OnConflictStrategy.REPLACE 7 | import android.arch.persistence.room.Query 8 | import android.arch.persistence.room.Update 9 | import com.silverlotus.kmvvm.room.entity.MangaEntity 10 | 11 | /** 12 | * Created by Gian Patrick Quintana on 2/21/2018. 13 | */ 14 | @Dao 15 | interface MangaDao { 16 | 17 | @Query("SELECT * FROM manga_list LIMIT :max") 18 | fun getMangaList(max: Int): DataSource.Factory 19 | 20 | @Insert(onConflict = REPLACE) 21 | fun insertManga(mangaEntity: MangaEntity) 22 | 23 | @Insert(onConflict = REPLACE) 24 | fun updateMangaList(list: List) 25 | 26 | @Update(onConflict = REPLACE) 27 | fun updateManga(mangaEntity: MangaEntity) 28 | } -------------------------------------------------------------------------------- /app/src/main/java/com/silverlotus/kmvvm/room/entity/MangaEntity.kt: -------------------------------------------------------------------------------- 1 | package com.silverlotus.kmvvm.room.entity 2 | 3 | import android.arch.persistence.room.ColumnInfo 4 | import android.arch.persistence.room.Entity 5 | import android.arch.persistence.room.PrimaryKey 6 | import com.silverlotus.api.model.MangaListModel 7 | import com.silverlotus.api.model.MangaModel 8 | 9 | /** 10 | * Created by Gian Patrick Quintana on 2/21/2018. 11 | */ 12 | @Entity(tableName = "manga_list") 13 | class MangaEntity { 14 | 15 | @PrimaryKey 16 | @ColumnInfo(name = "id") 17 | var id: String = "" 18 | 19 | @ColumnInfo(name = "title") 20 | var title: String = "" 21 | 22 | fun fromMangaData(manga: MangaModel): MangaEntity { 23 | id = manga.id 24 | title = manga.title 25 | return this 26 | } 27 | 28 | companion object { 29 | 30 | fun fromMangaList(mangaListModel: MangaListModel): List = 31 | mangaListModel.listOfMangaData.map { data -> MangaEntity().fromMangaData(data) } 32 | 33 | } 34 | } -------------------------------------------------------------------------------- /app/src/main/java/com/silverlotus/kmvvm/ui/main/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.silverlotus.kmvvm.ui.main 2 | 3 | import android.os.Bundle 4 | import com.silverlotus.kmvvm.R 5 | import com.silverlotus.kmvvm.base.BaseActivity 6 | 7 | /** 8 | * Created by Gian Patrick Quintana on 1/22/2018. 9 | */ 10 | class MainActivity : BaseActivity() { 11 | 12 | override fun onCreate(savedInstanceState: Bundle?) { 13 | super.onCreate(savedInstanceState) 14 | setContentView(R.layout.activity_main) 15 | 16 | supportFragmentManager.beginTransaction() 17 | .add(R.id.fragmentContainer, MainFragment()) 18 | .commit() 19 | 20 | } 21 | } -------------------------------------------------------------------------------- /app/src/main/java/com/silverlotus/kmvvm/ui/main/MainFragment.kt: -------------------------------------------------------------------------------- 1 | package com.silverlotus.kmvvm.ui.main 2 | 3 | import android.arch.lifecycle.Observer 4 | import android.arch.lifecycle.ViewModelProviders 5 | import android.os.Bundle 6 | import android.support.v7.widget.LinearLayoutManager 7 | import android.support.v7.widget.RecyclerView 8 | import android.view.LayoutInflater 9 | import android.view.View 10 | import android.view.ViewGroup 11 | import com.orhanobut.logger.Logger 12 | import com.silverlotus.kmvvm.R 13 | import com.silverlotus.kmvvm.adapter.MangaAdapter 14 | import com.silverlotus.kmvvm.base.BaseFragment 15 | import kotlinx.android.synthetic.main.fragment_main.* 16 | 17 | /** 18 | * Created by Gian Patrick Quintana on 1/22/2018. 19 | */ 20 | class MainFragment : BaseFragment(), MainView { 21 | 22 | private lateinit var model: MainViewModel 23 | private val adapter = MangaAdapter() 24 | 25 | override fun onCreate(savedInstanceState: Bundle?) { 26 | super.onCreate(savedInstanceState) 27 | model = ViewModelProviders.of(this).get(MainViewModel::class.java).init(kodein) 28 | } 29 | 30 | override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? = 31 | inflater.inflate(R.layout.fragment_main, container, false) 32 | 33 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 34 | super.onViewCreated(view, savedInstanceState) 35 | 36 | recyclerMangaTitles.adapter = adapter 37 | val layoutManager = LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false) 38 | recyclerMangaTitles.layoutManager = layoutManager 39 | recyclerMangaTitles.addOnScrollListener(object : RecyclerView.OnScrollListener() { 40 | 41 | override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) { 42 | super.onScrolled(recyclerView, dx, dy) 43 | Logger.d(layoutManager.findLastCompletelyVisibleItemPosition()) 44 | } 45 | }) 46 | 47 | buttonGetMangas.setOnClickListener { 48 | showProgressBar() 49 | 50 | if (model.isFirstLoad) { 51 | 52 | model.getMangaList(20).observe(this, Observer { list -> 53 | 54 | adapter.submitList(list) 55 | hideProgressBar() 56 | }) 57 | 58 | model.fetchMangaList() 59 | model.isFirstLoad = false 60 | } else 61 | model.getMangaList(20) 62 | 63 | } 64 | } 65 | 66 | override fun showProgressBar() { 67 | 68 | progressBar.visibility = View.VISIBLE 69 | } 70 | 71 | override fun hideProgressBar() { 72 | 73 | progressBar.visibility = View.GONE 74 | } 75 | } -------------------------------------------------------------------------------- /app/src/main/java/com/silverlotus/kmvvm/ui/main/MainView.kt: -------------------------------------------------------------------------------- 1 | package com.silverlotus.kmvvm.ui.main 2 | 3 | /** 4 | * Created by Gian Patrick Quintana on 1/22/2018. 5 | */ 6 | interface MainView { 7 | 8 | fun showProgressBar() 9 | fun hideProgressBar() 10 | 11 | } -------------------------------------------------------------------------------- /app/src/main/java/com/silverlotus/kmvvm/ui/main/MainViewModel.kt: -------------------------------------------------------------------------------- 1 | package com.silverlotus.kmvvm.ui.main 2 | 3 | import android.arch.lifecycle.LiveData 4 | import android.arch.lifecycle.MutableLiveData 5 | import android.arch.paging.PagedList 6 | import com.silverlotus.kmvvm.base.BaseViewModel 7 | import com.silverlotus.kmvvm.repository.MangaRepository 8 | import com.silverlotus.kmvvm.room.entity.MangaEntity 9 | import org.kodein.di.generic.instance 10 | 11 | /** 12 | * Created by Gian Patrick Quintana on 1/22/2018. 13 | */ 14 | class MainViewModel : BaseViewModel() { 15 | 16 | private val mangaRepository by instance() 17 | private var liveDaoMangaEntity: LiveData> = MutableLiveData() 18 | 19 | var isFirstLoad = true 20 | 21 | fun getMangaList(max: Int): LiveData> { 22 | 23 | liveDaoMangaEntity = mangaRepository.getMangaList(max) 24 | 25 | return liveDaoMangaEntity 26 | } 27 | 28 | fun fetchMangaList() = mangaRepository.fetchMangaList() 29 | } -------------------------------------------------------------------------------- /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_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 | 2 | 5 | 6 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 18 | 19 | 27 | 28 |