Skip to content

Commit

Permalink
Organize interfaces and naming a bit better.
Browse files Browse the repository at this point in the history
  • Loading branch information
pardom committed May 4, 2016
1 parent 7a8148b commit 50430db
Show file tree
Hide file tree
Showing 24 changed files with 203 additions and 100 deletions.
1 change: 1 addition & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ dependencies {
compile javaxInject
compile kotlinStdLib
compile kotterKnife
compile mortar
compile okHttpLoggingInterceptor
compile paperParcel
compile phrase
Expand Down
12 changes: 12 additions & 0 deletions android/src/main/kotlin/clean/news/CleanNewsApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,40 @@ package clean.news

import android.app.Application
import android.content.Context
import clean.news.flow.service.DaggerService
import clean.news.inject.component.ApplicationComponent
import clean.news.inject.component.DaggerApplicationComponent
import clean.news.inject.module.ApplicationModule
import clean.news.inject.module.DataModule
import clean.news.inject.module.PresentationModule
import mortar.MortarScope

class CleanNewsApplication : Application() {
private lateinit var applicationComponent: ApplicationComponent
private lateinit var rootScope: MortarScope

override fun onCreate() {
super.onCreate()

applicationComponent = DaggerApplicationComponent.builder()
.applicationModule(ApplicationModule(this))
.presentationModule(PresentationModule())
.dataModule(DataModule())
.build()

rootScope = MortarScope.buildRootScope()
.withService(DaggerService.NAME, applicationComponent)
.build("ROOT")
}

fun component(): ApplicationComponent {
return applicationComponent
}

fun scope(): MortarScope {
return rootScope
}

companion object {
fun get(context: Context) = context.applicationContext as CleanNewsApplication
}
Expand Down
23 changes: 0 additions & 23 deletions android/src/main/kotlin/clean/news/flow/ComponentService.kt

This file was deleted.

7 changes: 0 additions & 7 deletions android/src/main/kotlin/clean/news/flow/WithActivity.kt

This file was deleted.

5 changes: 0 additions & 5 deletions android/src/main/kotlin/clean/news/flow/WithComponent.kt

This file was deleted.

5 changes: 0 additions & 5 deletions android/src/main/kotlin/clean/news/flow/WithLayout.kt

This file was deleted.

8 changes: 0 additions & 8 deletions android/src/main/kotlin/clean/news/flow/WithTransition.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package clean.news.flow
package clean.news.flow.keychanger

import android.content.Context
import flow.Direction
import flow.KeyChanger
import flow.TraversalCallback
import kotlin.reflect.KClass

class CompositeDispatcher : KeyChanger() {
class CompositeKeyChanger : KeyChanger() {

private val dispatchers = mutableMapOf<KClass<out Any>, KeyChanger>()

fun addDispatcher(cls: KClass<out Any>, dispatcher: KeyChanger) {
Expand All @@ -28,4 +29,5 @@ class CompositeDispatcher : KeyChanger() {
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package clean.news.flow
package clean.news.flow.keychanger

import android.app.Activity
import android.content.Context
import android.transition.AutoTransition
import android.transition.Scene
import android.transition.Transition
import android.transition.TransitionManager
import android.view.LayoutInflater
import android.view.ViewGroup
import clean.news.R
import clean.news.R.id
import flow.Direction
import flow.KeyChanger
import flow.State
import flow.TraversalCallback

class SceneDispatcher(private val activity: Activity) : KeyChanger() {
class SceneKeyChanger(private val activity: Activity) : KeyChanger() {

override fun changeKey(
outgoingState: flow.State?,
incomingState: flow.State,
outgoingState: State?,
incomingState: State,
direction: Direction,
incomingContexts: MutableMap<Any, Context>,
callback: TraversalCallback) {
Expand All @@ -24,7 +27,7 @@ class SceneDispatcher(private val activity: Activity) : KeyChanger() {
val destination = incomingState.getKey<WithLayout>()
val layout = destination.getLayoutResId()
val context = incomingContexts[destination]
val frame = activity.findViewById(R.id.app_container) as ViewGroup
val frame = activity.findViewById(id.app_container) as ViewGroup
val incomingView = LayoutInflater.from(context).inflate(layout, frame, false)

outgoingState?.save(frame.getChildAt(0))
Expand All @@ -48,4 +51,13 @@ class SceneDispatcher(private val activity: Activity) : KeyChanger() {

callback.onTraversalCompleted()
}

interface WithLayout {
fun getLayoutResId(): Int
}

interface WithTransition {
fun createTransition(fromKey: Any?, toKey: Any, direction: Direction): Transition
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package clean.news.flow
package clean.news.flow.parceler

import android.os.Parcelable
import flow.KeyParceler
Expand Down
32 changes: 32 additions & 0 deletions android/src/main/kotlin/clean/news/flow/service/DaggerService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package clean.news.flow.service

import android.content.Context
import flow.Flow
import flow.Services.Binder
import flow.ServicesFactory

class DaggerService(private val rootComponent: Any) : ServicesFactory() {

override fun bindServices(services: Binder) {
val key = services.getKey<Any>()
if (key !is WithComponent) {
return
}

val parent = services.getService<Any>(NAME) ?: rootComponent
val component = key.createComponent(parent)

services.bind(NAME, component)
}

interface WithComponent {
fun createComponent(parent: Any): Any
}

companion object {
const val NAME = "DaggerService"

fun <T> get(context: Context) = Flow.getService<T>(NAME, context)
}

}
40 changes: 40 additions & 0 deletions android/src/main/kotlin/clean/news/flow/service/MortarService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package clean.news.flow.service

import android.content.Context
import flow.Flow
import flow.Services
import flow.Services.Binder
import flow.ServicesFactory
import mortar.MortarScope

class MortarService(private val rootScope: MortarScope) : ServicesFactory() {

override fun bindServices(services: Binder) {
val key = services.getKey<Any>()
if (key !is WithScope) {
return
}

val scopeName = key.javaClass.name
val parentScope = services.getService<MortarScope>(NAME) ?: rootScope
val childScope = parentScope.findChild(scopeName) ?: key.createScope(parentScope).build(scopeName)

services.bind(NAME, childScope)
}

override fun tearDownServices(services: Services) {
services.getService<MortarScope>(NAME)?.destroy()
super.tearDownServices(services)
}

interface WithScope {
fun createScope(parentScope: MortarScope): MortarScope.Builder
}

companion object {
const val NAME = "MortarService"

fun <T> get(context: Context) = Flow.getService<T>(NAME, context)
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package clean.news.navigation

import clean.news.flow.WithActivity
import android.content.Intent

class AppNavigationService : FlowNavigationService() {

override fun goTo(newTop: Any) {
navigate(newTop) { super.goTo(newTop) }
}
Expand All @@ -21,4 +22,9 @@ class AppNavigationService : FlowNavigationService() {
else -> default()
}
}

interface WithActivity {
fun createIntent(): Intent
}

}
26 changes: 17 additions & 9 deletions android/src/main/kotlin/clean/news/ui/BaseActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,33 @@ import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import clean.news.CleanNewsApplication
import clean.news.R
import clean.news.flow.*
import clean.news.inject.component.ApplicationComponent
import clean.news.flow.keychanger.CompositeKeyChanger
import clean.news.flow.keychanger.SceneKeyChanger
import clean.news.flow.keychanger.SceneKeyChanger.WithLayout
import clean.news.flow.parceler.PaperKeyParceler
import clean.news.flow.service.DaggerService
import clean.news.flow.service.MortarService
import clean.news.navigation.FlowNavigationService
import flow.Flow
import flow.KeyDispatcher

abstract class BaseActivity : AppCompatActivity() {
abstract fun getDefaultKey(): Any

private lateinit var applicationComponent: ApplicationComponent
abstract fun getDefaultKey(): Any

override fun attachBaseContext(newBase: Context) {
applicationComponent = CleanNewsApplication.get(newBase).component()
val applicationComponent = CleanNewsApplication.get(newBase).component()
val scope = CleanNewsApplication.get(newBase).scope()

val dispatcher = CompositeDispatcher()
dispatcher.addDispatcher(WithLayout::class, SceneDispatcher(this))
val keyChanger = CompositeKeyChanger()
keyChanger.addDispatcher(WithLayout::class, SceneKeyChanger(this))

val context = Flow.configure(newBase, this)
.addServicesFactory(ComponentService(applicationComponent))
.dispatcher(KeyDispatcher.configure(this, dispatcher).build())
.keyParceler(PaperKeyParceler())
.dispatcher(KeyDispatcher.configure(this, keyChanger).build())
.defaultKey(getDefaultKey())
.addServicesFactory(DaggerService(applicationComponent))
.addServicesFactory(MortarService(scope))
.install()

super.attachBaseContext(context)
Expand All @@ -39,11 +44,13 @@ abstract class BaseActivity : AppCompatActivity() {

override fun onResume() {
super.onResume()
val applicationComponent = CleanNewsApplication.get(this).component()
val navigationService = applicationComponent.navigationService() as FlowNavigationService
navigationService.setContext(this)
}

override fun onPause() {
val applicationComponent = CleanNewsApplication.get(this).component()
val navigationService = applicationComponent.navigationService() as FlowNavigationService
navigationService.removeContext()
super.onPause()
Expand All @@ -54,4 +61,5 @@ abstract class BaseActivity : AppCompatActivity() {
super.onBackPressed()
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package clean.news.ui.item.detail
import android.transition.ChangeBounds
import clean.news.R
import clean.news.core.entity.Item
import clean.news.flow.WithComponent
import clean.news.flow.WithLayout
import clean.news.flow.WithTransition
import clean.news.flow.keychanger.SceneKeyChanger.WithLayout
import clean.news.flow.keychanger.SceneKeyChanger.WithTransition
import clean.news.flow.service.DaggerService.WithComponent
import clean.news.presentation.inject.ClassScope
import clean.news.presentation.model.item.ItemDetailViewModel
import clean.news.presentation.navigation.NavigationFactory.ItemDetailKey
Expand All @@ -28,7 +28,7 @@ class ItemDetailScreen(val item: Item) : ClassKey(),
ItemDetailKey,
WithLayout,
WithTransition,
WithComponent<MainComponent>,
WithComponent,
PaperParcelable {

override fun getParentKey() = MainScreen()
Expand All @@ -37,7 +37,12 @@ class ItemDetailScreen(val item: Item) : ClassKey(),

override fun createTransition(fromKey: Any?, toKey: Any, direction: Direction) = ChangeBounds().setDuration(200)

override fun createComponent(parent: MainComponent) = parent.plus(ItemDetailModule(item))
override fun createComponent(parent: Any): Any {
if (parent !is MainComponent) {
throw IllegalArgumentException()
}
return parent.plus(ItemDetailModule(item))
}

@ClassScope(ItemDetailViewModel::class)
@Subcomponent(modules = arrayOf(ItemDetailModule::class))
Expand All @@ -52,4 +57,5 @@ class ItemDetailScreen(val item: Item) : ClassKey(),
@Provides
fun detailItem(): Item = item
}

}
Loading

0 comments on commit 50430db

Please sign in to comment.