Skip to content

Instantly share code, notes, and snippets.

View CostaFot's full-sized avatar
🍦

Costa Fotiadis CostaFot

🍦
View GitHub Profile
@CostaFot
CostaFot / build.gradle
Last active March 23, 2019 03:27
Build file
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
/**
* 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"
/**
* 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,
@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
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")
}
/**
* 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 {
/**
* 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() {
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
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>
/**
* 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?,