This file contains hidden or 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
apply plugin: 'com.android.application' | |
apply plugin: 'kotlin-android' | |
apply plugin: 'kotlin-android-extensions' | |
apply plugin: 'kotlin-kapt' | |
android { | |
// android stuff here | |
// BuildConfig strings to communicate with MovieDB |
This file contains hidden or 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
/** | |
* Declare everything here. Go wild! | |
* You can learn how to do loads of modules, scopes and other nonsense later. | |
*/ | |
@Module | |
class AppModule { | |
// Some static strings declared here to give names to some variables below so they aren't orphans | |
companion object { | |
const val NAME_URL = "key.base.url" |
This file contains hidden or 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
/** | |
* The AppComponent is like a basket for your modules. | |
* Put all your modules here! | |
* Just throwing most of the stuff in AppModule for now to get things working | |
*/ | |
@Singleton | |
@Component( | |
modules = [ | |
AndroidSupportInjectionModule::class, | |
AppModule::class, |
This file contains hidden or 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
@Module | |
abstract class ActivityBuilderModule { | |
/** | |
* This is how you inject into activities/fragments/android specific stuff | |
* You gotta write "AndroidInjection.inject(this)" | |
* right before your onCreate in the activity to make this work! | |
*/ | |
@ContributesAndroidInjector | |
abstract fun mainActivity(): MainActivity |
This file contains hidden or 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
class ReviewApplication : DaggerApplication() { | |
override fun onCreate() { | |
super.onCreate() | |
// ignore this if you don't like the Timber logging library | |
if (BuildConfig.DEBUG) { | |
Timber.plant(Timber.DebugTree()) | |
Timber.d("Timber initialised") | |
} |
This file contains hidden or 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
/** | |
* Copy pasta how to ViewModelFactory | |
* Google says you cannot pass stuff in the ViewModel constructor like normal | |
* Just do this I guess, who am I to judge | |
*/ | |
class NowPlayingMoviesViewModelFactory( | |
private val placeholderApi: MovieApi, | |
private val apiKey: String, | |
private val scheduler: Scheduler | |
) : ViewModelProvider.Factory { |
This file contains hidden or 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
/** | |
* Instantiating this guy with the NowPlayingMoviesViewModelFactory | |
* The factory comes in ready with everything bundled from the AppModule so have at it | |
*/ | |
class NowPlayingMoviesViewModel( | |
private val placeholderApi: MovieApi, | |
private val apiKey: String, | |
private val scheduler: Scheduler | |
) : ViewModel() { |
This file contains hidden or 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
class MainActivity : AppCompatActivity() { | |
// field injection for primitive stuff needs @JvmField !! | |
// this is written as an example and not needed cause it's a frequent question in StackOverflow | |
@JvmField | |
@Inject | |
var debugFlag: Boolean = false | |
// this guy is injected with the providesViewModelFactory method that was declared in AppModule | |
@Inject |
This file contains hidden or 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
class MovieApi(retrofit: Retrofit) { | |
private val api: Api = retrofit.create(Api::class.java) | |
fun getNowPlayingMovies(apiKey: String) = api.getNowPlayingMovies(apiKey) | |
interface Api { | |
@GET("movie/now_playing") | |
fun getNowPlayingMovies(@Query("api_key") apiKey: String): Single<Movies> |
This file contains hidden or 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
/** | |
* The class representing the Json response. Use http://www.jsonschema2pojo.org/ to get this. | |
* Or you can add this plugin for AS here https://plugins.jetbrains.com/plugin/9960-json-to-kotlin-class-jsontokotlinclass- | |
* It will create your data class from JSON to kotlin. | |
* | |
* Just writing "Any" cause I'm a terrible human being and don't care about the JSON in this case | |
*/ | |
data class Movies( | |
@SerializedName("dates") | |
val dates: Any?, |