Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save angusholder/df712b4e73e28492144933d11785bc12 to your computer and use it in GitHub Desktop.
Save angusholder/df712b4e73e28492144933d11785bc12 to your computer and use it in GitHub Desktop.
@file:SuppressLint("ValidController")
package co.mopo.android.ui
import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import co.mopo.android.data.disk.AppDatabase
import com.bluelinelabs.conductor.Controller
import com.bluelinelabs.conductor.RouterTransaction
import dagger.Binds
import dagger.MapKey
import dagger.Subcomponent
import dagger.multibindings.IntoMap
import org.threeten.bp.Clock
import javax.inject.Inject
import javax.inject.Provider
import kotlin.reflect.KClass
class HomeController @Inject constructor(
private val database: AppDatabase,
private val clock: Clock,
bundle: Bundle
) : Controller(bundle) {
private val userId = args.getString("userId")!!
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup): View {
database.agentModel()
clock.instant()
println(userId)
TODO("not implemented")
}
}
fun ControllerFactory.home(userId: String) = create<HomeController> {
putString("userId", userId)
}
class LoginController @Inject constructor(
private val controllerFactory: ControllerFactory,
bundle: Bundle
) : Controller(bundle) {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup): View {
router.pushController(RouterTransaction.with(controllerFactory.home("01234")))
TODO("not implemented")
}
}
//
//
//
interface ControllerModule {
@Binds
@IntoMap
@ControllerKey(HomeController::class)
fun bindHome(homeController: HomeController): Controller
@Binds
@IntoMap
@ControllerKey(LoginController::class)
fun bindLogin(loginController: LoginController): Controller
}
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@MapKey
annotation class ControllerKey(val value: KClass<out Controller>)
@Subcomponent(modules = [ControllerModule::class])
interface ControllerComponent {
val map: Map<Class<out Controller>, Provider<out Controller>>
@Subcomponent.Factory
interface Factory {
fun createComponent(bundle: Bundle): ControllerComponent
}
}
fun <T : Controller> ControllerFactory.create(clazz: Class<T>, args: Bundle): T {
val factory: Provider<out Controller> = this.createComponent(args).map[clazz]
?: throw IllegalArgumentException("There is no registered Controller ${clazz.name}")
return clazz.cast(factory.get())!!
}
inline fun <reified T : Controller> ControllerFactory.create(body: Bundle.() -> Unit): T {
val args = Bundle().apply(body)
return create(T::class.java, args)
}
typealias ControllerFactory = ControllerComponent.Factory
interface ControllerRestorer {
fun restoreController(clazz: Class<out Controller>, bundle: Bundle): Controller
}
class ControllerRestorerImpl(private val controllerComponentFactory: ControllerFactory) : ControllerRestorer {
override fun restoreController(clazz: Class<out Controller>, bundle: Bundle): Controller {
return controllerComponentFactory.create(clazz, bundle)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment