This file contains 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
interface MainContract { | |
interface View { | |
fun showClickText() | |
} | |
interface UserAction { | |
fun createTopic(view: View) | |
} | |
} |
This file contains 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(), View { | |
@Inject lateinit var userAction: MainContract.UserAction | |
override fun onCreate(savedInstanceState: Bundle?) { | |
AndroidInjection.inject(this) | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
setSupportActionBar(toolbar) |
This file contains 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 MyApp : Application(), HasActivityInjector { | |
@Inject lateinit var dispatchingActivityInjector: DispatchingAndroidInjector<Any> | |
override fun androidInjector() = dispatchingActivityInjector | |
override fun onCreate() { | |
super.onCreate() | |
DaggerAppComponent.builder().application(this).build().inject(this) | |
} | |
} |
This file contains 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
inline fun <reified T : Activity> createFakeMainActivityInjector(crossinline block : T.() -> Unit) | |
: DispatchingAndroidInjector<Any> { | |
val injector = AndroidInjector<Activity> { instance -> | |
if (instance is T) { | |
instance.block() | |
} | |
} | |
val factory = Factory<Activity> { injector } | |
val map = mapOf(Pair<Class <*>, Provider<Factory<*>>>(T::class.java, Provider { factory })) | |
return DispatchingAndroidInjector_Factory.newInstance(map, emptyMap()) |
This file contains 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
@RunWith(AndroidJUnit4::class) | |
class MainViewTests { | |
val mockUserAction = mock(MainContract.UserAction::class.java) | |
@get:Rule val activityTestRule = object : ActivityTestRule<MainActivity>(MainActivity::class.java, true, true) { | |
override fun beforeActivityLaunched() { | |
super.beforeActivityLaunched() | |
val myApp = InstrumentationRegistry.getTargetContext().applicationContext as MyApp | |
myApp.dispatchingActivityInjector = createFakeMainActivityInjector { | |
userAction = mockUserAction |
This file contains 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
inline fun <reified T : Activity> createFakeActivityInjector(crossinline block : T.() -> Unit) | |
: DispatchingAndroidInjector<Any> { | |
val injector = AndroidInjector<Activity> { instance -> | |
if (instance is T) { | |
instance.block() | |
} | |
} | |
val factory = Factory<Activity> { injector } | |
val map = mapOf(Pair<Class <*>, Provider<Factory<*>>>(T::class.java, Provider { factory })) | |
return DispatchingAndroidInjector_Factory.newInstance(map, emptyMap()) |
This file contains 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
interface Storage { | |
fun getUrlForKey(key: String) : String | |
} | |
interface ImageFetcher { | |
fun getImageFromUrl(url: String) : Bitmap | |
} |
This file contains 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
interface UseCase <in Request, out Response> { | |
fun handleRequest(request: Request) : Response | |
} | |
interface UseCaseResponseHandler <in Response> { | |
fun handleResponse(response: Response) | |
} | |
class UseCaseRunner <Request, Response> { | |
This file contains 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
data class LoadImageUseCaseRequest ( | |
val key: String, | |
val storage: Storage, | |
val imageFetcher: ImageFetcher | |
) | |
data class LoadImageUseCaseResponse (val bitmap: Bitmap) | |
class LoadImageUseCase : UseCase<LoadImageUseCaseRequest, LoadImageUseCaseResponse> { | |
override fun handleRequest(request: LoadImageUseCaseRequest) : LoadImageUseCaseResponse { |
This file contains 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
suspend fun fetchImage(key: String, storage: Storage, imageFetcher: ImageFetcher) : Bitmap { | |
val url = async {storage.getUrlForKey(key)}.await() | |
val image = async { imageFetcher.getImageFromUrl(url) } | |
println("this might be run after the async have started") | |
return image.await() | |
} |
OlderNewer