Skip to content

Commit

Permalink
Merge pull request #14 from felipecastilhos/refact/concerns_division
Browse files Browse the repository at this point in the history
Melhorando a divisão de responsabilidades
  • Loading branch information
felipecastilhos committed Nov 21, 2021
2 parents 2b8b4ee + c5a7542 commit adfba29
Show file tree
Hide file tree
Showing 36 changed files with 407 additions and 167 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ Libraries Used
* [Espresso](https://developer.android.com/training/testing/espresso/) - UI test
* [Junit](https://junit.org/junit4/) - unit tests
* [Truth](https://github.com/google/truth) - Makes your test assertions and failure messages more readable
* [Mockk](https://mockk.io/) - Mocking library for kotlin
* [Detekt](https://github.com/detekt/detekt) - a static code analysis tool for the Kotlin programming language
* [KTLint](https://github.com/pinterest/ktlint) - Kotlin linter in spirit of feross/standard (JavaScript) and gofmt (Go).
* [Gradle Kotlin Plugin](https://kotlinlang.org/docs/gradle.html) - Gradle scripts in Kotlin
* [Timber](https://github.com/JakeWharton/timber) - Small log library based on Android SDK log
* [Timber](https://github.com/JakeWharton/timber) - Small log library based on Android SDK log
3 changes: 2 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ android {

dependencies {
// Common libraries
jetpackUtilLibraries()
jetpackCoreLibraries()
jetpackKotlinExtensionsLibraries()
jetpackAndroidLifecycleLibraries()

Expand All @@ -83,4 +83,5 @@ dependencies {
// Add Tests
unitTestsLibraries()
instrumentationTestsLibraries()
mockLibraries()
}
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
android:supportsRtl="true"
android:theme="@style/Theme.Pokedexandroid">
<activity
android:name=".MainActivity"
android:name=".features.home.HomeActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.Pokedexandroid.NoActionBar">
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.github.felipecastilhos.pokedexandroid

import android.app.Application
import com.github.felipecastilhos.pokedexandroid.logs.LogHandler
import com.github.felipecastilhos.pokedexandroid.commun.logs.LogHandler
import dagger.hilt.android.HiltAndroidApp

/**
* Create an application for the pokedex app to extends adding needed behaviours
*/
@HiltAndroidApp
class PokedexApplication : Application() {
override fun onCreate() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.felipecastilhos.pokedexandroid.coroutines
package com.github.felipecastilhos.pokedexandroid.commun.coroutines

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.felipecastilhos.pokedexandroid.coroutines
package com.github.felipecastilhos.pokedexandroid.commun.coroutines

import kotlinx.coroutines.CoroutineDispatcher

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.felipecastilhos.pokedexandroid.coroutines
package com.github.felipecastilhos.pokedexandroid.commun.coroutines

import kotlinx.coroutines.CoroutineScope
import kotlin.coroutines.CoroutineContext
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.felipecastilhos.pokedexandroid.logs
package com.github.felipecastilhos.pokedexandroid.commun.logs

import com.github.felipecastilhos.pokedexandroid.BuildConfig
import timber.log.Timber
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.github.felipecastilhos.pokedexandroid.network
package com.github.felipecastilhos.pokedexandroid.commun.network

import com.apollographql.apollo.ApolloClient
import com.github.felipecastilhos.pokedexandroid.Enviroment
import com.github.felipecastilhos.pokedexandroid.PokedexApolloClient
import com.github.felipecastilhos.pokedexandroid.datasource.remote.Enviroment
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import javax.inject.Singleton

/**
Expand All @@ -28,9 +26,7 @@ class NetworkModule {
*/
@Provides
@Singleton
fun provideOkHttpClient(): OkHttpClient = OkHttpClient.Builder()
.addInterceptor(PokedexApolloClient.buildLogInterceptor(HttpLoggingInterceptor.Level.BODY))
.build()
fun provideOkHttpClient(): OkHttpClient = PokedexOkHttpBuilder.buildOkHttp()

/**
* Provides an ApolloClient setup
Expand All @@ -40,8 +36,5 @@ class NetworkModule {
@Provides
@Singleton
fun provideApolloClient(okHttpClient: OkHttpClient, apiUrl: String): ApolloClient =
ApolloClient.builder()
.serverUrl(apiUrl)
.okHttpClient(okHttpClient)
.build()
PokedexApolloBuilder.buildApollo(okHttpClient = okHttpClient, apiUrl = apiUrl)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.github.felipecastilhos.pokedexandroid.commun.network

import com.apollographql.apollo.ApolloClient
import okhttp3.OkHttpClient

/**
* Application builder for [ApolloClient] the abstraction of GraphQl Client
*/
object PokedexApolloBuilder {
/**
* Builds an [ApolloClient]
* @param okHttpClient to be used on [ApolloClient] for making network requests
* @param apiUrl is the server url to be used on [ApolloClient]
*/
fun buildApollo(okHttpClient: OkHttpClient, apiUrl: String): ApolloClient =
ApolloClient.builder()
.serverUrl(apiUrl)
.okHttpClient(okHttpClient)
.build()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.felipecastilhos.pokedexandroid.commun.network

import com.github.felipecastilhos.pokedexandroid.commun.logs.LogHandler
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor

/**
*
*/
object PokedexOkHttpBuilder {
fun buildOkHttp(): OkHttpClient {
return OkHttpClient.Builder()
.addInterceptor(buildLogInterceptor(HttpLoggingInterceptor.Level.BODY))
.build()
}

private fun buildLogInterceptor(level: HttpLoggingInterceptor.Level): HttpLoggingInterceptor {
val logger = HttpLoggingInterceptor.Logger { message -> LogHandler.d(message) }
val logging = HttpLoggingInterceptor(logger)
logging.level = level
return logging
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.github.felipecastilhos.pokedexandroid.ui.theme
package com.github.felipecastilhos.pokedexandroid.commun.ui.theme

import androidx.compose.ui.graphics.Color

val Purple200 = Color(0xFFBB86FC)
val Purple500 = Color(0xFF6200EE)
val Purple700 = Color(0xFF3700B3)
val Teal200 = Color(0xFF03DAC5)
val Teal200 = Color(0xFF03DAC5)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.felipecastilhos.pokedexandroid.ui.theme
package com.github.felipecastilhos.pokedexandroid.commun.ui.theme

import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Shapes
Expand All @@ -8,4 +8,4 @@ val Shapes = Shapes(
small = RoundedCornerShape(4.dp),
medium = RoundedCornerShape(4.dp),
large = RoundedCornerShape(0.dp)
)
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.felipecastilhos.pokedexandroid.ui.theme
package com.github.felipecastilhos.pokedexandroid.commun.ui.theme

import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material.MaterialTheme
Expand Down Expand Up @@ -44,4 +44,4 @@ fun PokedexandroidTheme(
shapes = Shapes,
content = content
)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.felipecastilhos.pokedexandroid.ui.theme
package com.github.felipecastilhos.pokedexandroid.commun.ui.theme

import androidx.compose.material.Typography
import androidx.compose.ui.text.TextStyle
Expand All @@ -25,4 +25,4 @@ val Typography = Typography(
fontSize = 12.sp
)
*/
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.github.felipecastilhos.pokedexandroid.datasource

import com.github.felipecastilhos.pokedexandroid.datasource.remote.DataSourceException

/**
* Resource is an abstraction for data fetch
*/
sealed class Resource<out T> {
/**
* Resource data fetch was successful
* @param data was the result of data fetch
*/
data class Success<out T>(val data: T) : Resource<T>()

/**
* Resource data fetch didn't work
* @param exception was the error that occurred fetching data
*/
data class Error(val exception: DataSourceException) : Resource<Nothing>()

/**
* Resource is loading, none result was returned yet.
*/
object Loading : Resource<Nothing>()
}

/**
* Extension to handle resouce on success data fetch
*/
inline fun <T : Any> Resource<T>.onSuccess(action: (T) -> Unit): Resource<T> {
if (this is Resource.Success) action(data)
return this
}

/**
* Extension to handle resouce on error data fetch
*/
inline fun <T : Any> Resource<T>.onError(action: (DataSourceException) -> Unit): Resource<T> {
if (this is Resource.Error) action(exception)
return this
}

/**
* Extension to handle resouce when is loading
*/
inline fun <T : Any> Resource<T>.onLoading(action: () -> Unit): Resource<T> {
if (this is Resource.Loading) action()
return this
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.felipecastilhos.pokedexandroid.datasource.remote

import com.apollographql.apollo.api.Error

/**
* Data source exception
* @param messageResource message for the error
*/
sealed class DataSourceException(
val messageResource: Any?
) : RuntimeException() {
/**
* Unexpected error
* @param messageResource resource for the message
*/
class Unexpected(messageResource: Int) : DataSourceException(messageResource)

/**
* Server error
* @param error that occurred
*/
class Server(error: Error?) : DataSourceException(error)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.github.felipecastilhos.pokedexandroid.datasource.remote

/**
* Configurations of enviroments of the data sources
*/
sealed class Enviroment(val graphQlUrl: String) {
object Production : Enviroment("https://graphqlpokemon.favware.tech/")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.github.felipecastilhos.pokedexandroid.datasource.remote

interface RemoteDataSource
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.github.felipecastilhos.pokedexandroid.di

import com.github.felipecastilhos.pokedexandroid.commun.coroutines.AppDispatchersProvider
import com.github.felipecastilhos.pokedexandroid.commun.coroutines.DispatcherProvider
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent

/**
* [AppModule] provides communs components for the architecture over the application
*/
@Module
@InstallIn(SingletonComponent::class)
class AppModule {
@Provides
fun providesDispatcherProvider(): DispatcherProvider {
return AppDispatchersProvider
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.github.felipecastilhos.pokedexandroid.viewmodels
package com.github.felipecastilhos.pokedexandroid.domain.viewmodels

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.github.felipecastilhos.pokedexandroid.coroutines.DispatcherProvider
import com.github.felipecastilhos.pokedexandroid.coroutines.ScopedContextDispatcher
import com.github.felipecastilhos.pokedexandroid.commun.coroutines.DispatcherProvider
import com.github.felipecastilhos.pokedexandroid.commun.coroutines.ScopedContextDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.launch
Expand Down

0 comments on commit adfba29

Please sign in to comment.