Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #17 from johsoe/master
Browse files Browse the repository at this point in the history
Async/await interactors
  • Loading branch information
johsoe authored Jan 30, 2019
2 parents 436eb41 + e28c906 commit 1ada565
Show file tree
Hide file tree
Showing 27 changed files with 293 additions and 442 deletions.
11 changes: 4 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ android {
applicationId "dk.nodes.template"
minSdkVersion 19
targetSdkVersion 28
multiDexEnabled true
versionCode 2
versionName "0.0.2"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down Expand Up @@ -103,11 +104,10 @@ dependencies {
junit_version = '4.12',
espresso_core_version = '3.1.0',
mockito_version = '2.7.22',
nodes_nstack_kotlin_version = '2.1.4',
nodes_nstack_kotlin_version = '2.2.0',
nodes_arch_version = '2.1.9',
retrofit_version = "2.3.0",
okhttp_version = "3.9.0",
store_version = "3.0.0-beta",
rx_android_version = '2.1.0',
rx_java_version = "2.2.4",
rx_kolin_version = "2.2.0",
Expand All @@ -126,9 +126,10 @@ dependencies {

implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'com.google.android.material:material:1.1.0-alpha01'
implementation 'com.google.android.material:material:1.1.0-alpha02'
implementation 'androidx.cardview:cardview:1.0.0'
implementation "androidx.constraintlayout:constraintlayout:$constraint_layout_version"
implementation 'androidx.multidex:multidex:2.0.1'

implementation "com.google.dagger:dagger-android:$dagger_version"
implementation "com.google.dagger:dagger-android-support:$dagger_version"
Expand All @@ -152,9 +153,6 @@ dependencies {
implementation "io.reactivex.rxjava2:rxandroid:$rx_android_version"
implementation "io.reactivex.rxjava2:rxjava:$rx_java_version"
implementation "io.reactivex.rxjava2:rxkotlin:$rx_kolin_version"
implementation "com.nytimes.android:store-kotlin3:$store_version"
implementation "com.nytimes.android:middleware3:$store_version"
implementation "com.nytimes.android:filesystem3:$store_version"

// Testing
testImplementation "junit:junit:$junit_version"
Expand All @@ -179,5 +177,4 @@ dependencies {
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
implementation "androidx.core:core-ktx:$ktx_version"
implementation "nl.littlerobots.rxlint:rxlint:$rx_lint_version"
}
104 changes: 0 additions & 104 deletions app/src/androidTest/java/dk/nodes/template/SampleActivityTest.kt

This file was deleted.

7 changes: 3 additions & 4 deletions app/src/main/java/dk/nodes/template/App.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dk.nodes.template

import android.content.Context
import androidx.multidex.MultiDex
import dagger.android.AndroidInjector
import dagger.android.DaggerApplication
import dk.nodes.template.inititializers.AppInitializers
Expand All @@ -14,14 +16,11 @@ class App : DaggerApplication() {
initializers.init(this)
}

// uncomment me if multidex

/*
override fun attachBaseContext(base: Context) {
super.attachBaseContext(base)
MultiDex.install(this)
}
*/

override fun applicationInjector(): AndroidInjector<out DaggerApplication> {
return DaggerAppComponent.builder().create(this)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dk.nodes.template.domain.interactors


interface BaseAsyncInteractor<O> {
suspend fun run(): O
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dk.nodes.template.domain.interactors

import dk.nodes.template.domain.models.Post
import dk.nodes.template.domain.models.Result
import dk.nodes.template.domain.repositories.PostRepository
import dk.nodes.template.domain.repositories.RepositoryException
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import javax.inject.Inject

class PostsInteractor @Inject constructor(
private val postRepository: PostRepository
): BaseAsyncInteractor<Result<List<Post>>> {

override suspend fun run(): Result<List<Post>> {
return try {
val posts = postRepository.getPosts(true)
Result.Success(posts)
} catch (e: RepositoryException) {
Result.Error(e)
}
}
}
17 changes: 17 additions & 0 deletions app/src/main/java/dk/nodes/template/domain/models/Result.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dk.nodes.template.domain.models

/**
* From: https://github.com/nickbutcher/plaid/blob/master/core/src/main/java/io/plaidapp/core/data/Result.kt
*/
sealed class Result<out T : Any> {

data class Success<out T : Any>(val data: T) : Result<T>()
data class Error(val exception: Exception) : Result<Nothing>()

override fun toString(): String {
return when (this) {
is Success<*> -> "Success[data=$data]"
is Error -> "Error[exception=$exception]"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package dk.nodes.template.domain.repositories
import dk.nodes.template.domain.models.Post

interface PostRepository {
fun getPosts(cached: Boolean = false): List<Post>
suspend fun getPosts(cached: Boolean = false): List<Post>
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
package dk.nodes.template.injection.components

import dagger.Component
import dk.nodes.arch.domain.injection.scopes.AppScope
import dagger.android.AndroidInjector
import dagger.android.support.AndroidSupportInjectionModule
import dk.nodes.arch.domain.injection.scopes.AppScope
import dk.nodes.template.App
import dk.nodes.template.injection.modules.AppModule
import dk.nodes.template.injection.modules.ExecutorModule
import dk.nodes.template.injection.modules.InteractorModule
import dk.nodes.template.injection.modules.RestModule
import dk.nodes.template.injection.modules.RestRepositoryModule
import dk.nodes.template.injection.modules.StorageModule
import dk.nodes.template.injection.modules.ViewModelBuilder
import dk.nodes.template.injection.modules.*

@Component(
modules = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,9 @@ package dk.nodes.template.injection.modules
import dagger.Module
import dagger.Provides
import dk.nodes.arch.domain.executor.Executor
import dk.nodes.template.domain.interactors.GetPostsInteractor
import dk.nodes.template.domain.interactors.GetPostsInteractorImpl
import dk.nodes.template.domain.repositories.PostRepository

@Module
class InteractorModule {
@Provides
fun provideGetPostsInteractor(
executor: Executor,
postRepository: PostRepository
): GetPostsInteractor {
return GetPostsInteractorImpl(executor, postRepository)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import dagger.Provides
import dk.nodes.arch.domain.injection.scopes.AppScope
import dk.nodes.template.domain.repositories.PostRepository
import dk.nodes.template.network.rest.Api
import dk.nodes.template.network.rest.StorePostRepository
import dk.nodes.template.network.rest.RestPostRepository

@Module
class RestRepositoryModule {
@Provides
@AppScope
fun providePostRepository(api: Api, gson: Gson, context: Context): PostRepository {
return StorePostRepository(api, gson, context)
return RestPostRepository(api)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import dk.nodes.template.domain.models.Post
import dk.nodes.template.domain.repositories.PostRepository
import dk.nodes.template.domain.repositories.RepositoryException

class RestPostRepository(val api: Api) : PostRepository {
override fun getPosts(cached: Boolean): List<Post> {
class RestPostRepository(private val api: Api) : PostRepository {
@Throws(RepositoryException::class)
override suspend fun getPosts(cached: Boolean): List<Post> {
val response = api.getPosts().execute()
if (response.isSuccessful) {
return response.body()!!
return response.body() ?: throw(RepositoryException(response.code(), response.message()))
}
throw(RepositoryException(response.code(), response.message()))
}
Expand Down
Loading

0 comments on commit 1ada565

Please sign in to comment.