Add: data and domain layers

This commit is contained in:
Carlos Martinez
2021-06-17 11:27:28 -04:00
parent 205de0ef8d
commit 3848ac0519
64 changed files with 211 additions and 648 deletions

1
shortform/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/build

16
shortform/build.gradle Normal file
View File

@@ -0,0 +1,16 @@
apply from: '../versions.gradle'
apply from: '../base.gradle'
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-parcelize'
android {
defaultConfig {
buildConfigField "String", "DB_NAME", '"acronyms.db"'
}
}
dependencies {
implementation project(':core')
}

21
shortform/proguard-rules.pro vendored Normal file
View File

@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

View File

@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="dev.carlos.shortform" />

View File

@@ -0,0 +1,16 @@
package dev.carlos.shortform.data
import dev.carlos.shortform.data.cloud.AcronymsRemoteSource
import dev.carlos.shortform.data.models.ShortformModel
import dev.carlos.shortform.data.models.toShortformModel
import dev.carlos.shortform.domain.AcronymsRepository
import io.reactivex.Single
class AcronymsDataRepository(
private val remoteDatasource: AcronymsRemoteSource
) : AcronymsRepository {
override fun getAcronymDefinition(acronym: String): Single<ShortformModel> {
return remoteDatasource.getAcronymDefinition(acronym).map { it.single()?.toShortformModel() }
}
}

View File

@@ -0,0 +1,8 @@
package dev.carlos.shortform.data.cloud
import dev.carlos.shortform.data.cloud.model.ShortformRemote
import io.reactivex.Single
interface AcronymsRemoteSource {
fun getAcronymDefinition(acronym: String): Single<List<ShortformRemote>>
}

View File

@@ -0,0 +1,18 @@
package dev.carlos.shortform.data.cloud.model
import android.os.Parcelable
import com.google.gson.annotations.SerializedName
import kotlinx.parcelize.Parcelize
@Parcelize
data class ShortformRemote(
@SerializedName("sf") val value: String,
@SerializedName("lfs") val results: List<LongformRemote>
) : Parcelable {
@Parcelize
data class LongformRemote(
@SerializedName("lf") val value: String,
@SerializedName("freq") val corpusFrequency: Int,
@SerializedName("since") val since: Int
) : Parcelable
}

View File

@@ -0,0 +1,11 @@
package dev.carlos.shortform.data.cloud.retrofit
import dev.carlos.shortform.data.cloud.AcronymsRemoteSource
import dev.carlos.shortform.data.cloud.model.ShortformRemote
import io.reactivex.Single
class AcronymsRemoteDatasource(private val acronymsService: AcronymsService) : AcronymsRemoteSource {
override fun getAcronymDefinition(acronym: String): Single<List<ShortformRemote>> {
return acronymsService.getAcronymDefinition(acronym)
}
}

View File

@@ -0,0 +1,11 @@
package dev.carlos.shortform.data.cloud.retrofit
import dev.carlos.shortform.data.cloud.model.ShortformRemote
import io.reactivex.Single
import retrofit2.http.GET
import retrofit2.http.Query
interface AcronymsService {
@GET("/software/acromine/dictionary.py")
fun getAcronymDefinition(@Query("sf") acronym: String): Single<List<ShortformRemote>>
}

View File

@@ -0,0 +1,7 @@
package dev.carlos.shortform.data.models
import dev.carlos.shortform.data.cloud.model.ShortformRemote
fun ShortformRemote.toShortformModel() = ShortformModel(this.value, this.results.map {
ShortformModel.LongformModel(it.value, it.corpusFrequency, it.since)
})

View File

@@ -0,0 +1,12 @@
package dev.carlos.shortform.data.models
data class ShortformModel(
val value: String,
val results: List<LongformModel>
) {
data class LongformModel(
val value: String,
val corpusFrequency: Int,
val since: Int
)
}

View File

@@ -0,0 +1,25 @@
package dev.carlos.shortform.di
import dev.carlos.core.domain.network.RemoteClient
import dev.carlos.core.scheduler.Scheduler
import dev.carlos.core.scheduler.SchedulerProvider
import dev.carlos.shortform.data.AcronymsDataRepository
import dev.carlos.shortform.data.cloud.AcronymsRemoteSource
import dev.carlos.shortform.data.cloud.retrofit.AcronymsRemoteDatasource
import dev.carlos.shortform.data.cloud.retrofit.AcronymsService
import dev.carlos.shortform.domain.AcronymsRepository
import dev.carlos.shortform.domain.GetAcronymDefinition
import org.koin.dsl.module
val acronymsModule = module {
single<Scheduler> { SchedulerProvider() }
single {
get<RemoteClient>().getClient(AcronymsService::class.java)
}
factory<AcronymsRemoteSource> { AcronymsRemoteDatasource(get()) }
factory<AcronymsRepository> { AcronymsDataRepository(get()) }
factory { GetAcronymDefinition(get(), get()) }
}

View File

@@ -0,0 +1,9 @@
package dev.carlos.shortform.domain
import dev.carlos.shortform.data.models.ShortformModel
import io.reactivex.Flowable
import io.reactivex.Single
interface AcronymsRepository {
fun getAcronymDefinition(acronym: String): Single<ShortformModel>
}

View File

@@ -0,0 +1,15 @@
package dev.carlos.shortform.domain
import dev.carlos.shortform.data.models.ShortformModel
import dev.carlos.core.extensions.runOnIo
import dev.carlos.core.scheduler.Scheduler
import io.reactivex.Single
class GetAcronymDefinition(
private val acronymsRepository: AcronymsRepository,
private val scheduler: Scheduler
) {
fun getAcronymDefinition(acronym: String): Single<ShortformModel> {
return acronymsRepository.getAcronymDefinition(acronym).runOnIo(scheduler)
}
}

View File

@@ -0,0 +1,19 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Acronyms"
parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_200</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/black</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_200</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor"
tools:targetApi="l">?attr/colorPrimaryVariant
</item>
<!-- Customize your theme here. -->
</style>
</resources>

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
</resources>

View File

@@ -0,0 +1,3 @@
<resources>
<string name="app_name">acronyms</string>
</resources>

View File

@@ -0,0 +1,19 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Acronyms"
parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor"
tools:targetApi="l">?attr/colorPrimaryVariant
</item>
<!-- Customize your theme here. -->
</style>
</resources>