Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Koin #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
76 changes: 76 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "com.android.support:appcompat-v7:$android_support_version"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
Expand Down Expand Up @@ -63,10 +63,8 @@ dependencies {
implementation "com.squareup.retrofit2:adapter-rxjava2:$retrofit_version"
implementation "com.squareup.retrofit2:converter-moshi:$retrofit_version"

// Dagger 2
implementation "com.google.dagger:dagger:$dagger2_version"
kapt "com.google.dagger:dagger-compiler:$dagger2_version"
compileOnly "org.glassfish:javax.annotation:3.1.1"
// Koin
implementation "org.koin:koin-android-architecture:$koin_version"

//Rx
implementation "io.reactivex.rxjava2:rxjava:2.1.15"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<uses-permission android:name="android.permission.INTERNET"/>

<application
android:name=".PostsApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/kotlin/net/gahfy/mvvmposts/PostsApplication.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package net.gahfy.mvvmposts

import android.app.Application
import net.gahfy.mvvmposts.utils.postsModule
import org.koin.android.ext.android.startKoin

class PostsApplication : Application() {

override fun onCreate() {
super.onCreate()

startKoin(this, listOf(postsModule(this)))
}
}
24 changes: 1 addition & 23 deletions app/src/main/kotlin/net/gahfy/mvvmposts/base/BaseViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
package net.gahfy.mvvmposts.base

import android.arch.lifecycle.ViewModel
import net.gahfy.mvvmposts.injection.component.DaggerViewModelInjector
import net.gahfy.mvvmposts.injection.component.ViewModelInjector
import net.gahfy.mvvmposts.injection.module.NetworkModule
import net.gahfy.mvvmposts.ui.post.PostListViewModel

abstract class BaseViewModel:ViewModel(){
private val injector: ViewModelInjector = DaggerViewModelInjector
.builder()
.networkModule(NetworkModule)
.build()

init {
inject()
}

/**
* Injects the required dependencies
*/
private fun inject() {
when (this) {
is PostListViewModel -> injector.inject(this)
}
}
}
abstract class BaseViewModel:ViewModel()
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ package net.gahfy.mvvmposts.injection

import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import android.arch.persistence.room.Room
import android.support.v7.app.AppCompatActivity
import net.gahfy.mvvmposts.model.database.AppDatabase
import net.gahfy.mvvmposts.model.PostDao
import net.gahfy.mvvmposts.network.PostApi
import net.gahfy.mvvmposts.ui.post.PostListViewModel

class ViewModelFactory(private val activity: AppCompatActivity): ViewModelProvider.Factory{
class ViewModelFactory(private val postDao: PostDao, private val postApi: PostApi): ViewModelProvider.Factory{

override fun <T : ViewModel?> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(PostListViewModel::class.java)) {
val db = Room.databaseBuilder(activity.applicationContext, AppDatabase::class.java, "posts").build()
@Suppress("UNCHECKED_CAST")
return PostListViewModel(db.postDao()) as T
return PostListViewModel(postDao, postApi) as T
}
throw IllegalArgumentException("Unknown ViewModel class")

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.gahfy.mvvmposts.ui.post

import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.databinding.DataBindingUtil
import android.os.Bundle
import android.support.annotation.StringRes
Expand All @@ -10,11 +9,11 @@ import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import net.gahfy.mvvmposts.R
import net.gahfy.mvvmposts.databinding.ActivityPostListBinding
import net.gahfy.mvvmposts.injection.ViewModelFactory
import org.koin.android.architecture.ext.viewModel

class PostListActivity: AppCompatActivity() {
private lateinit var binding: ActivityPostListBinding
private lateinit var viewModel: PostListViewModel
private val viewModel by viewModel<PostListViewModel>()
private var errorSnackbar: Snackbar? = null

override fun onCreate(savedInstanceState: Bundle?){
Expand All @@ -23,7 +22,6 @@ class PostListActivity: AppCompatActivity() {
binding = DataBindingUtil.setContentView(this, R.layout.activity_post_list)
binding.postList.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)

viewModel = ViewModelProviders.of(this, ViewModelFactory(this)).get(PostListViewModel::class.java)
viewModel.errorMessage.observe(this, Observer {
errorMessage -> if(errorMessage != null) showError(errorMessage) else hideError()
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ import net.gahfy.mvvmposts.base.BaseViewModel
import net.gahfy.mvvmposts.model.Post
import net.gahfy.mvvmposts.model.PostDao
import net.gahfy.mvvmposts.network.PostApi
import javax.inject.Inject

class PostListViewModel(private val postDao: PostDao):BaseViewModel(){
@Inject
lateinit var postApi: PostApi
class PostListViewModel(private val postDao: PostDao, private val postApi: PostApi):BaseViewModel(){
val postListAdapter: PostListAdapter = PostListAdapter()

val loadingVisibility: MutableLiveData<Int> = MutableLiveData()
Expand Down
31 changes: 31 additions & 0 deletions app/src/main/kotlin/net/gahfy/mvvmposts/utils/Modules.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.gahfy.mvvmposts.utils

import android.arch.persistence.room.Room
import android.content.Context
import io.reactivex.schedulers.Schedulers
import net.gahfy.mvvmposts.model.database.AppDatabase
import net.gahfy.mvvmposts.network.PostApi
import net.gahfy.mvvmposts.ui.post.PostListViewModel
import org.koin.android.architecture.ext.viewModel
import org.koin.dsl.module.Module
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.moshi.MoshiConverterFactory

fun postsModule(applicationContext: Context): Module = org.koin.dsl.module.applicationContext {
viewModel { PostListViewModel(get("dao"), get("network")) }
bean("dao") {
Room.databaseBuilder(applicationContext, AppDatabase::class.java, "posts").build().postDao()
}
bean("network") {
(get("retrofit") as Retrofit).create(PostApi::class.java) as PostApi
}
bean("retrofit") {
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(MoshiConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
.build()
}

}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ buildscript {
ext.kotlin_version = '1.2.30'
ext.lifecycle_version = '1.1.1'
ext.retrofit_version = '2.4.0'
ext.dagger2_version = '2.16'
ext.android_support_version = '28.0.0-alpha3'
ext.room_version = '1.1.1'
ext.koin_version = '0.9.3'

repositories {
google()
Expand Down