-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit dba6abd
Showing
49 changed files
with
922 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea | ||
.DS_Store | ||
/build | ||
/captures | ||
.externalNativeBuild | ||
.project | ||
app/.classpath | ||
app/libs/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Koin-Example | ||
|
||
An example of Dependency Injection with Koin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
apply plugin: 'com.android.application' | ||
|
||
apply plugin: 'kotlin-android' | ||
|
||
apply plugin: 'kotlin-android-extensions' | ||
|
||
android { | ||
compileSdkVersion 28 | ||
defaultConfig { | ||
applicationId "com.maruiz.glideexample" | ||
minSdkVersion 21 | ||
targetSdkVersion 28 | ||
versionCode 1 | ||
versionName "1.0" | ||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
} | ||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
|
||
packagingOptions { | ||
pickFirst 'META-INF/kotlinx-io.kotlin_module' | ||
pickFirst 'META-INF/atomicfu.kotlin_module' | ||
pickFirst 'META-INF/kotlinx-coroutines-io.kotlin_module' | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation fileTree(dir: 'libs', include: ['*.jar']) | ||
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" | ||
implementation 'androidx.appcompat:appcompat:1.1.0-rc01' | ||
implementation 'androidx.core:core-ktx:1.2.0-alpha03' | ||
implementation 'androidx.constraintlayout:constraintlayout:1.1.3' | ||
implementation 'com.google.code.gson:gson:2.8.5' | ||
implementation 'androidx.recyclerview:recyclerview:1.0.0' | ||
implementation 'androidx.cardview:cardview:1.0.0' | ||
|
||
implementation 'com.github.bumptech.glide:glide:4.9.0' | ||
|
||
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.0' | ||
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.2.0' | ||
|
||
implementation 'com.squareup.retrofit2:retrofit:2.6.1' | ||
implementation 'com.squareup.retrofit2:converter-gson:2.5.0' | ||
|
||
implementation 'io.arrow-kt:arrow-core:0.8.2' | ||
|
||
def koinVersion = '2.0.1' | ||
|
||
//Koin dependencies | ||
implementation "org.koin:koin-android:$koinVersion" | ||
implementation "org.koin:koin-android-viewmodel:$koinVersion" | ||
implementation "org.koin:koin-android-scope:$koinVersion" | ||
|
||
testImplementation 'junit:junit:4.12' | ||
androidTestImplementation 'androidx.test:runner:1.3.0-alpha02' | ||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0-alpha02' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
24 changes: 24 additions & 0 deletions
24
app/src/androidTest/java/com/maruiz/koinexample/ExampleInstrumentedTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.maruiz.koinexample | ||
|
||
import androidx.test.InstrumentationRegistry | ||
import androidx.test.runner.AndroidJUnit4 | ||
|
||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
import org.junit.Assert.* | ||
|
||
/** | ||
* Instrumented test, which will execute on an Android device. | ||
* | ||
* See [testing documentation](http://d.android.com/tools/testing). | ||
*/ | ||
@RunWith(AndroidJUnit4::class) | ||
class ExampleInstrumentedTest { | ||
@Test | ||
fun useAppContext() { | ||
// Context of the app under test. | ||
val appContext = InstrumentationRegistry.getTargetContext() | ||
assertEquals("com.maruiz.koinexample", appContext.packageName) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.maruiz.koinexample"> | ||
|
||
<uses-permission android:name="android.permission.INTERNET"/> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme" | ||
android:name=".presentation.application.KoinExampleApp"> | ||
<activity android:name="com.maruiz.koinexample.presentation.activity.MainActivity"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN"/> | ||
|
||
<category android:name="android.intent.category.LAUNCHER"/> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
|
||
</manifest> |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/com/maruiz/koinexample/data/api/BookApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.maruiz.koinexample.data.api | ||
|
||
import com.maruiz.koinexample.data.model.BookModel | ||
import retrofit2.Call | ||
import retrofit2.http.GET | ||
|
||
interface BookApi { | ||
@GET("BookList.json") | ||
fun getBooks(): Call<List<BookModel>> | ||
} |
8 changes: 8 additions & 0 deletions
8
app/src/main/java/com/maruiz/koinexample/data/error/Failure.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.maruiz.koinexample.data.error | ||
|
||
sealed class Failure { | ||
object ServerError : Failure() | ||
|
||
/** * Extend this class for feature specific failures.*/ | ||
abstract class FeatureFailure : Failure() | ||
} |
17 changes: 17 additions & 0 deletions
17
app/src/main/java/com/maruiz/koinexample/data/extensions/Call.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.maruiz.koinexample.data.extensions | ||
|
||
import android.util.Log | ||
import arrow.core.Either | ||
import arrow.core.Left | ||
import arrow.core.Right | ||
import arrow.core.Try | ||
import com.maruiz.koinexample.data.error.Failure | ||
import retrofit2.Call | ||
|
||
fun <T> Call<T>.makeRequest(): Either<Failure, T?> = | ||
Try { | ||
this.execute().let { | ||
if (it.isSuccessful) Right(it.body()) | ||
else Left(Failure.ServerError) | ||
} | ||
}.fold({ Left(Failure.ServerError) }, { it }) |
7 changes: 7 additions & 0 deletions
7
app/src/main/java/com/maruiz/koinexample/data/model/BookModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.maruiz.koinexample.data.model | ||
|
||
import com.google.gson.annotations.SerializedName | ||
|
||
data class BookModel(val bookId: Int = 0, val title: String = "", val author: String = "", | ||
@SerializedName("short_synopsis") val shortSynopsis: String, val synopsis: String = "", | ||
val image: String = "") |
13 changes: 13 additions & 0 deletions
13
app/src/main/java/com/maruiz/koinexample/domain/usecase/GetBooks.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.maruiz.koinexample.domain.usecase | ||
|
||
import arrow.core.Either | ||
import arrow.core.None | ||
import com.maruiz.koinexample.data.api.BookApi | ||
import com.maruiz.koinexample.data.error.Failure | ||
import com.maruiz.koinexample.data.extensions.makeRequest | ||
import com.maruiz.koinexample.data.model.BookModel | ||
|
||
class GetBooks(private val bookApi: BookApi) : UseCase<List<BookModel>, None>() { | ||
override suspend fun run(params: None): Either<Failure, List<BookModel>> = | ||
bookApi.getBooks().makeRequest().map { it ?: emptyList() } | ||
} |
20 changes: 20 additions & 0 deletions
20
app/src/main/java/com/maruiz/koinexample/domain/usecase/UseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.maruiz.koinexample.domain.usecase | ||
|
||
import arrow.core.Either | ||
import com.maruiz.koinexample.data.error.Failure | ||
import kotlinx.coroutines.* | ||
|
||
abstract class UseCase<out Type, in Params> where Type : Any { | ||
|
||
private val mainJob = Job() | ||
private val uiScope = CoroutineScope(Dispatchers.Main + mainJob) | ||
|
||
abstract suspend fun run(params: Params): Either<Failure, Type> | ||
|
||
operator fun invoke(params: Params, onResult: (Either<Failure, Type>) -> Unit = {}) = | ||
uiScope.launch { onResult(withContext(Dispatchers.IO) { run(params) }) } | ||
|
||
fun cancel() { | ||
mainJob.cancel() | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
app/src/main/java/com/maruiz/koinexample/presentation/activity/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.maruiz.koinexample.presentation.activity | ||
|
||
import android.os.Bundle | ||
import android.widget.Toast | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.lifecycle.Observer | ||
import androidx.recyclerview.widget.GridLayoutManager | ||
import com.maruiz.koinexample.R | ||
import com.maruiz.koinexample.presentation.adapter.BooksAdapter | ||
import com.maruiz.koinexample.presentation.viewmodel.BooksViewModel | ||
import kotlinx.android.synthetic.main.activity_main.* | ||
import org.koin.android.ext.android.inject | ||
|
||
class MainActivity : AppCompatActivity() { | ||
|
||
private val booksViewModel: BooksViewModel by inject() | ||
private val adapter: BooksAdapter by inject() | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_main) | ||
|
||
setupRecycler() | ||
|
||
booksViewModel.observeBooks().observe(this, Observer { | ||
it?.let { | ||
adapter.renderables = it | ||
} | ||
}) | ||
booksViewModel.observeFailure().observe(this, Observer { | ||
Toast.makeText(this, R.string.error, Toast.LENGTH_LONG).show() | ||
}) | ||
booksViewModel.getBooks() | ||
} | ||
|
||
private fun setupRecycler() { | ||
recyclerView.layoutManager = GridLayoutManager(this, 3) | ||
recyclerView.adapter = adapter | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
app/src/main/java/com/maruiz/koinexample/presentation/adapter/BooksAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.maruiz.koinexample.presentation.adapter | ||
|
||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.maruiz.koinexample.R | ||
import com.maruiz.koinexample.presentation.extensions.inflate | ||
import com.maruiz.koinexample.presentation.extensions.loadImage | ||
import com.maruiz.koinexample.presentation.presentationmodel.BookPresentationModel | ||
import kotlinx.android.synthetic.main.row_book.view.* | ||
import kotlin.properties.Delegates | ||
|
||
class BooksAdapter : RecyclerView.Adapter<BooksAdapter.ViewHolder>() { | ||
var renderables: List<BookPresentationModel> by Delegates.observable(emptyList()) { _, _, _ -> | ||
notifyDataSetChanged() | ||
} | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder = | ||
ViewHolder(parent.inflate(R.layout.row_book)) | ||
|
||
override fun getItemCount(): Int = renderables.size | ||
|
||
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | ||
holder.bind(renderables[position]) | ||
} | ||
|
||
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { | ||
fun bind(model: BookPresentationModel) { | ||
itemView.image.loadImage(model.image) | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
app/src/main/java/com/maruiz/koinexample/presentation/application/KoinExampleApp.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.maruiz.koinexample.presentation.application | ||
|
||
import android.app.Application | ||
import com.maruiz.koinexample.presentation.di.appModule | ||
import org.koin.android.ext.koin.androidContext | ||
import org.koin.core.context.startKoin | ||
|
||
class KoinExampleApp : Application() { | ||
override fun onCreate() { | ||
super.onCreate() | ||
|
||
startKoin { | ||
androidContext(this@KoinExampleApp) | ||
// modules | ||
modules(appModule) | ||
|
||
fileProperties() | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
app/src/main/java/com/maruiz/koinexample/presentation/di/modules.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.maruiz.koinexample.presentation.di | ||
|
||
import com.maruiz.koinexample.data.api.BookApi | ||
import com.maruiz.koinexample.domain.usecase.GetBooks | ||
import com.maruiz.koinexample.presentation.adapter.BooksAdapter | ||
import com.maruiz.koinexample.presentation.viewmodel.BooksViewModel | ||
import org.koin.android.viewmodel.dsl.viewModel | ||
import org.koin.dsl.module | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.gson.GsonConverterFactory | ||
|
||
val appModule = module { | ||
single { | ||
Retrofit.Builder() | ||
.baseUrl(getProperty<String>("base_url")) | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.build() | ||
} | ||
single { get<Retrofit>().create(BookApi::class.java) } | ||
single { GetBooks(get()) } | ||
factory { BooksAdapter() } | ||
viewModel { BooksViewModel(get()) } | ||
} |
37 changes: 37 additions & 0 deletions
37
app/src/main/java/com/maruiz/koinexample/presentation/extensions/View.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.maruiz.koinexample.presentation.extensions | ||
|
||
import android.graphics.drawable.Drawable | ||
import android.util.Log | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.ImageView | ||
import androidx.annotation.LayoutRes | ||
import com.bumptech.glide.Glide | ||
import com.bumptech.glide.request.target.CustomTarget | ||
import com.bumptech.glide.request.transition.Transition | ||
import kotlin.time.ExperimentalTime | ||
import kotlin.time.MonoClock | ||
|
||
fun ViewGroup.inflate(@LayoutRes layoutRes: Int): View = | ||
LayoutInflater.from(context).inflate(layoutRes, this, false) | ||
|
||
@UseExperimental(ExperimentalTime::class) | ||
fun ImageView.loadImage(url: String) { | ||
val TAG = "ImageLoading" | ||
totalMark | ||
val mark = MonoClock.markNow() | ||
Glide.with(this).load(url).into(object : CustomTarget<Drawable>() { | ||
override fun onLoadCleared(placeholder: Drawable?) { | ||
} | ||
|
||
override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) { | ||
this@loadImage.setImageDrawable(resource) | ||
Log.d(TAG, "Elapsed time: ${mark.elapsedNow()} $url") | ||
Log.d(TAG, "Total time: ${totalMark.elapsedNow()}, $url") | ||
} | ||
}) | ||
} | ||
|
||
@UseExperimental(ExperimentalTime::class) | ||
private val totalMark by lazy { MonoClock.markNow() } |
Oops, something went wrong.