Skip to content

Instantly share code, notes, and snippets.

View sanogueralorenzo's full-sized avatar
👋
Hey there

mario sanogueralorenzo

👋
Hey there
View GitHub Profile
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]
version: 2
jobs:
build:
working_directory: ~/code
docker:
- image: circleci/android:api-25-alpha
environment:
JVM_OPTS: -Xmx3200m
steps:
- checkout
# 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
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) })
interface PostRepository {
fun get(refresh: Boolean): Single<List<Post>>
}
@Module
abstract class RepositoryModule {
@Binds
abstract fun bindPostRepository(repository: PostRepositoryImpl): PostRepository
}
@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) }
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)
class PostRepositoryImplTest {
private lateinit var repository: PostRepositoryImpl
private val mockCacheDataSource: PostCacheDataSource = mock()
private val mockRemoteDataSource: PostRemoteDataSource = mock()
private val postId = post.id
private val cacheItem = post.copy(title = "cache")
fun <T> MutableLiveData<Resource<T>>.setSuccess(data: T? = null) =
postValue(Resource(ResourceState.SUCCESS, data))
fun <T> MutableLiveData<Resource<T>>.setLoading() =
postValue(Resource(ResourceState.LOADING, value?.data))
fun <T> MutableLiveData<Resource<T>>.setError(message: String? = null) =
postValue(Resource(ResourceState.ERROR, value?.data, message))