Skip to content

Commit 9e8f548

Browse files
feat: kodein example added
1 parent 15452b8 commit 9e8f548

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1019
-0
lines changed

Kodein/.gitignore

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures
9+
.externalNativeBuild
10+
/gradle.properties

Kodein/.idea/gradle.xml

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Kodein/.idea/misc.xml

+33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Kodein/.idea/modules.xml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Kodein/.idea/runConfigurations.xml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Kodein/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Kodein
2+
Kodein is a painless dependency injector which can be easily used as replaceble for Dagger2 in small projects. In this project the singleton instance of Retrofit from InitApp file is provided as singelton all over app and injected in MainActivity by delegate properties.
3+
4+

Kodein/app/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

Kodein/app/build.gradle

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
apply plugin: 'com.android.application'
2+
3+
apply plugin: 'kotlin-android'
4+
5+
apply plugin: 'kotlin-android-extensions'
6+
7+
android {
8+
compileSdkVersion 26
9+
defaultConfig {
10+
applicationId "com.developers.kodein"
11+
minSdkVersion 19
12+
targetSdkVersion 26
13+
versionCode 1
14+
versionName "1.0"
15+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
16+
}
17+
buildTypes {
18+
release {
19+
minifyEnabled false
20+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
21+
}
22+
}
23+
buildTypes.each {
24+
it.buildConfigField('String', "MOVIE_KEY", API_KEY)
25+
}
26+
}
27+
28+
dependencies {
29+
implementation fileTree(dir: 'libs', include: ['*.jar'])
30+
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
31+
implementation 'com.android.support:appcompat-v7:26.1.0'
32+
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
33+
testImplementation 'junit:junit:4.12'
34+
androidTestImplementation 'com.android.support.test:runner:1.0.1'
35+
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
36+
implementation "com.squareup.retrofit2:retrofit:2.3.0"
37+
implementation "com.squareup.retrofit2:converter-gson:2.3.0"
38+
implementation 'com.android.support:recyclerview-v7:26.1.0'
39+
implementation 'com.android.support:cardview-v7:26.1.0'
40+
implementation 'com.github.salomonbrys.kodein:kodein:4.1.0'
41+
implementation 'com.github.salomonbrys.kodein:kodein-android:4.1.0'
42+
implementation "io.reactivex.rxjava2:rxandroid:2.0.1"
43+
implementation 'com.squareup.picasso:picasso:2.5.2'
44+
implementation "com.squareup.retrofit2:adapter-rxjava2:2.3.0"
45+
}

Kodein/app/proguard-rules.pro

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.developers.kodein
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.developers.kodein", appContext.packageName)
23+
}
24+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.developers.kodein">
4+
5+
<uses-permission android:name="android.permission.INTERNET"/>
6+
7+
<application
8+
android:name=".InitApp"
9+
android:allowBackup="true"
10+
android:icon="@mipmap/ic_launcher"
11+
android:label="@string/app_name"
12+
android:roundIcon="@mipmap/ic_launcher_round"
13+
android:supportsRtl="true"
14+
android:theme="@style/AppTheme">
15+
<activity android:name=".MainActivity">
16+
<intent-filter>
17+
<action android:name="android.intent.action.MAIN" />
18+
19+
<category android:name="android.intent.category.LAUNCHER" />
20+
</intent-filter>
21+
</activity>
22+
</application>
23+
24+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.developers.kodein
2+
3+
import com.developers.kodein.model.MovieResult
4+
import io.reactivex.Observable
5+
import retrofit2.http.GET
6+
import retrofit2.http.Query
7+
8+
/**
9+
* Created by Amanjeet Singh on 24/1/18.
10+
*/
11+
interface ApiInterface{
12+
13+
@GET("popular")
14+
fun getMovies(@Query("api_key") key: String,
15+
@Query("page") page: Int): Observable<MovieResult>
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.developers.kodein
2+
3+
import android.app.Application
4+
import com.github.salomonbrys.kodein.*
5+
import retrofit2.Retrofit
6+
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
7+
import retrofit2.converter.gson.GsonConverterFactory
8+
9+
/**
10+
* Created by Amanjeet Singh on 24/1/18.
11+
*/
12+
class InitApp : Application(), KodeinAware {
13+
14+
override val kodein: Kodein = Kodein {
15+
bind<Retrofit>() with singleton {
16+
Retrofit.Builder()
17+
.addConverterFactory(GsonConverterFactory.create())
18+
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
19+
.baseUrl("https://api.themoviedb.org/3/movie/")
20+
.build()
21+
}
22+
}
23+
24+
override fun onCreate() {
25+
super.onCreate()
26+
27+
}
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.developers.kodein
2+
3+
import android.os.Bundle
4+
import android.support.v7.widget.GridLayoutManager
5+
import com.developers.kodein.adapter.MovieAdapter
6+
import com.github.salomonbrys.kodein.android.KodeinAppCompatActivity
7+
import com.github.salomonbrys.kodein.instance
8+
import io.reactivex.android.schedulers.AndroidSchedulers
9+
import io.reactivex.schedulers.Schedulers
10+
import retrofit2.Retrofit
11+
import kotlinx.android.synthetic.main.activity_main.*
12+
13+
class MainActivity : KodeinAppCompatActivity() {
14+
15+
private val retrofit: Retrofit by injector.instance()
16+
private lateinit var apiInterface: ApiInterface
17+
18+
19+
override fun onCreate(savedInstanceState: Bundle?) {
20+
super.onCreate(savedInstanceState)
21+
setContentView(R.layout.activity_main)
22+
initializeInjector()
23+
apiInterface = retrofit.create(ApiInterface::class.java)
24+
apiInterface.getMovies(BuildConfig.MOVIE_KEY, 1)
25+
.subscribeOn(Schedulers.io())
26+
.observeOn(AndroidSchedulers.mainThread())
27+
.subscribe({ movieList ->
28+
val adapter = MovieAdapter(applicationContext, movieList.results)
29+
movie_recycler_view.adapter = adapter
30+
movie_recycler_view.layoutManager = GridLayoutManager(applicationContext
31+
, 2)
32+
}, { e -> e.printStackTrace() })
33+
}
34+
35+
override fun onDestroy() {
36+
super.onDestroy()
37+
destroyInjector()
38+
}
39+
}
40+
41+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.developers.kodein.adapter
2+
3+
import android.content.Context
4+
import android.net.Uri
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.developers.kodein.R
10+
import com.developers.kodein.model.Result
11+
import com.squareup.picasso.Callback
12+
import com.squareup.picasso.Picasso
13+
import kotlinx.android.synthetic.main.list_row.view.*
14+
15+
/**
16+
* Created by Amanjeet Singh on 25/1/18.
17+
*/
18+
class MovieAdapter(val context: Context, val resultList: List<Result>?) : RecyclerView.Adapter<MovieAdapter.MyViewHolder>() {
19+
20+
override fun onBindViewHolder(holder: MyViewHolder?, position: Int) {
21+
holder?.bindItems(resultList?.get(position))
22+
}
23+
24+
override fun getItemCount(): Int {
25+
return resultList?.size!!
26+
}
27+
28+
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): MyViewHolder {
29+
val view = LayoutInflater.from(context).inflate(R.layout.list_row,
30+
parent, false)
31+
return MyViewHolder(view)
32+
}
33+
34+
35+
class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
36+
37+
fun bindItems(result: Result?) {
38+
itemView.movie_title.text = result?.title
39+
val posterUri = Uri.parse("http://image.tmdb.org/t/p/w185/").buildUpon()
40+
.appendEncodedPath(result?.posterPath)
41+
.build()
42+
itemView.progress_bar.visibility = View.VISIBLE
43+
Picasso.with(itemView.context).load(posterUri.toString())
44+
.into(itemView.image_view_movie, object : Callback {
45+
46+
override fun onError() {
47+
//Show Error here
48+
}
49+
50+
override fun onSuccess() {
51+
itemView.progress_bar.visibility = View.GONE
52+
}
53+
54+
})
55+
}
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.developers.kodein.model
2+
3+
/**
4+
* Created by Amanjeet Singh on 25/1/18.
5+
*/
6+
import com.google.gson.annotations.Expose
7+
import com.google.gson.annotations.SerializedName
8+
9+
data class MovieResult(@SerializedName("page")
10+
@Expose
11+
var page: Int? = null,
12+
@SerializedName("total_results")
13+
@Expose
14+
var totalResults: Int? = null,
15+
@SerializedName("total_pages")
16+
@Expose
17+
var totalPages: Int? = null,
18+
@SerializedName("results")
19+
@Expose
20+
var results: List<Result>? = null)

0 commit comments

Comments
 (0)