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 UsersPostsUseCaseTest { | |
private lateinit var usersPostsUseCase: UsersPostsUseCase | |
private val mockUserRepository: UserRepository = mock() | |
private val mockPostRepository: PostRepository = mock() | |
private val userList = listOf(user) | |
private val postList = listOf(post) |
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
@Singleton | |
class PostRepositoryImpl @Inject constructor( | |
private val cacheDataSource: PostCacheDataSource, | |
private val remoteDataSource: PostRemoteDataSource | |
) : PostRepository { | |
override fun get(refresh: Boolean): Single<List<Post>> = | |
when (refresh) { | |
true -> remoteDataSource.get() | |
.flatMap { cacheDataSource.set(it) } |
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
@Module | |
abstract class RepositoryModule { | |
@Binds | |
abstract fun bindPostRepository(repository: PostRepositoryImpl): PostRepository | |
} |
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 PostRepository { | |
fun get(refresh: Boolean): Single<List<Post>> | |
} |
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 CombinedUserPost(val user: User, val post: Post) | |
class UsersPostsUseCase @Inject constructor( | |
private val userRepository: UserRepository, | |
private val postRepository: PostRepository | |
) { | |
fun get(refresh: Boolean): Single<List<CombinedUserPost>> = | |
Single.zip(userRepository.get(refresh), postRepository.get(refresh), | |
BiFunction { userList, postList -> map(userList, postList) }) |
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
# Android CircleCI 2.0 configuration file | |
# | |
# Check https://circleci.com/docs/2.0/language-android/ for more details | |
# | |
version: 2 | |
jobs: | |
build: | |
working_directory: ~/Android-Kotlin-Clean-Architecture | |
docker: | |
- image: circleci/android:api-25-alpha |
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
version: 2 | |
jobs: | |
build: | |
working_directory: ~/code | |
docker: | |
- image: circleci/android:api-25-alpha | |
environment: | |
JVM_OPTS: -Xmx3200m | |
steps: | |
- checkout |
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 PostListActivity : AppCompatActivity() { | |
@Inject | |
lateinit var viewModelFactory: ViewModelProvider.Factory | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_post_list) | |
getAppInjector().inject(this) | |
val vm = ViewModelProviders.of(this, viewModelFactory)[PostListViewModel::class.java] |
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 PostListViewModel @Inject constructor(private val usersPostsUseCase: UsersPostsUseCase) : | |
ViewModel() { | |
val posts = MutableLiveData<Resource<List<PostItem>>>() | |
private val compositeDisposable = CompositeDisposable() | |
fun get(refresh: Boolean = false) = | |
compositeDisposable.add(usersPostsUseCase.get(refresh) | |
.doOnSubscribe { posts.setLoading() } | |
.subscribeOn(Schedulers.io()) |
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
@Singleton | |
class ViewModelFactory @Inject constructor(private val viewModels: MutableMap<Class<out ViewModel>, Provider<ViewModel>>) : ViewModelProvider.Factory { | |
override fun <T : ViewModel> create(modelClass: Class<T>): T = viewModels[modelClass]?.get() as T | |
} | |
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER) | |
@kotlin.annotation.Retention(AnnotationRetention.RUNTIME) | |
@MapKey | |
internal annotation class ViewModelKey(val value: KClass<out ViewModel>) |