Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup/data module #21

Merged
merged 11 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions core/data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ plugins {
alias(libs.plugins.caloreeMultiplaform)
}

kotlin {
sourceSets {
commonMain.dependencies {
implementation(projects.core.network)
implementation(projects.core.local)

implementation(libs.koin.core)
implementation(libs.coroutines.core)
}
}
}

android {
namespace = "com.theophiluskibet.caloree.data"
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.theophilus.kibet.caloree.data.di

import com.theophilus.kibet.caloree.data.sources.CaloreeRepository
import com.theophiluskibet.caloree.local.di.localModule
import com.theophiluskibet.caloree.network.di.networkModule
import org.koin.core.module.dsl.singleOf
import org.koin.dsl.module

/**
* [dataModule] provide instances of dependencies needed by classes
* loads local and network module using [localModule] and [networkModule] respectively
*/
val dataModule =
module {
includes(listOf(localModule(), networkModule))

singleOf(::CaloreeRepositoryImpl) { bind<CaloreeRepository>() }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.theophilus.kibet.caloree.data.mappers

import com.theophiluskibet.caloree.local.entities.CaloreeEntity
import com.theophiluskibet.caloree.network.model.CalorieItemDto

/**
* Converts result from network to database model
*/
fun CalorieItemDto.toEntity() =
CaloreeEntity(
name = name,
calories = calories,
carbohydratesTotalGrams = carbohydrates_total_g,
cholesterolMilliGrams = cholesterol_mg,
fatSaturatedGrams = fat_saturated_g,
fatTotalGrams = fat_total_g,
fiberGrams = fiber_g,
potassiumMilliGrams = potassium_mg,
proteinGrams = protein_g,
servingSizeGrams = serving_size_g,
sodiumMilliGrams = sodium_mg,
sugarGrams = sugar_g,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.theophilus.kibet.caloree.data.mappers

import com.theophilus.kibet.caloree.data.model.Caloree
import com.theophiluskibet.caloree.local.entities.CaloreeEntity

/**
* Converts database model to ui model
*/
fun CaloreeEntity.toModel() =
Caloree(
name = name,
calories = calories,
carbohydratesTotalGrams = carbohydratesTotalGrams,
cholesterolMilliGrams = cholesterolMilliGrams,
fatSaturatedGrams = fatSaturatedGrams,
fatTotalGrams = fatTotalGrams,
fiberGrams = fiberGrams,
potassiumMilliGrams = potassiumMilliGrams,
proteinGrams = proteinGrams,
servingSizeGrams = servingSizeGrams,
sodiumMilliGrams = sodiumMilliGrams,
sugarGrams = sugarGrams,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.theophilus.kibet.caloree.data.model

/**
* Data representation used ui
*/
data class Caloree(
val name: String,
val calories: Double,
val carbohydratesTotalGrams: Double,
val cholesterolMilliGrams: Int,
val fatSaturatedGrams: Double,
val fatTotalGrams: Double,
val fiberGrams: Double,
val potassiumMilliGrams: Int,
val proteinGrams: Double,
val servingSizeGrams: Double,
val sodiumMilliGrams: Int,
val sugarGrams: Double,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.theophilus.kibet.caloree.data.model

/**
* Utility class to hold network responses i.e success and error states
*/
sealed class DataResult {
data class Success(val calories: List<Caloree>) : DataResult()

data class Error(val message: String) : DataResult()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.theophilus.kibet.caloree.data.sources

import com.theophilus.kibet.caloree.data.model.Caloree
import com.theophilus.kibet.caloree.data.model.DataResult
import kotlinx.coroutines.flow.Flow

interface CaloreeRepository {
suspend fun searchCalories(query: String): Flow<DataResult>

suspend fun getSavedCalories(): Flow<List<Caloree>>

suspend fun getCalorieDetails(food: String): Flow<Caloree>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.theophilus.kibet.caloree.data.sources

import com.theophilus.kibet.caloree.data.mappers.toEntity
import com.theophilus.kibet.caloree.data.mappers.toModel
import com.theophilus.kibet.caloree.data.model.Caloree
import com.theophilus.kibet.caloree.data.model.DataResult
import com.theophiluskibet.caloree.local.dao.CaloreeDao
import com.theophiluskibet.caloree.network.api.CaloreeApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf

class CaloreeRepositoryImpl(
private val caloreeApi: CaloreeApi,
private val caloreeDao: CaloreeDao,
) : CaloreeRepository {
/**
* Retreives a list of calories from db if matches the query else fetch from network
*
* 1. Splits the query into a list of food names, removing "and" and trims whitespace then
* retrieves data from database, this is because "and" isn't saved in db.
* 2. If data is not available in db, fetch from network taking the whole query. Unlike db, the
* api accepts "and" and will return a list of calories.
* @param query food to retrieve calories
* @sample
* ```
* val query = "food and banana"
* caloreeDao.getCaloriesByNames(food,banana) // from db
* caloreeApi.getCalories(food and banana) // from network
* ```
*/
override suspend fun searchCalories(query: String): Flow<DataResult> {
val listOfFoods = query.replace("and", "").trim().split(" ")
val calories = caloreeDao.getCaloriesByNames(listOfFoods)
return if (calories.size == listOfFoods.size) {
flowOf(DataResult.Success(calories = calories))
} else {
val response = caloreeApi.getCalories(query = query)
response.onSuccess { result ->
caloreeDao.saveCalories(result.calorieItemsDto.map { it.toEntity() })
flowOf(
DataResult.Success(
calories =
result.calorieItemsDto.map {
it.toEntity().toModel()
},
),
)
}
response.onFailure { error ->
flowOf(DataResult.Error(message = error.message))
}
}
}

/**
* retreives a list of calories saved in the db
*/
override suspend fun getSavedCalories(): Flow<List<Caloree>> {
return flowOf(caloreeDao.getCalories().map { it.toModel() })
}

/**
* retreives details of a single calorie saved in the db
* @param food name of the calorie
*/
override suspend fun getCalorieDetails(food: String): Flow<Caloree> {
return flowOf(caloreeDao.getCaloryDetails(name = food).toModel())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ interface CaloreeDao {
* retreives a single calorie matching provided name from the db
*/
@Query("SELECT * FROM caloree_table WHERE name=:name")
suspend fun getCalory(name: String): CaloreeEntity
suspend fun getCaloryDetails(name: String): CaloreeEntity

/**
* retreives a list of calories matching the provided names from the getCaloriesByNames
Expand Down
4 changes: 4 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ androidx-room = "2.7.0-alpha01"
androidx-test-junit = "1.1.5"
caloree = "0.1.0" # not a real version, just providing since using alias to access plugins requires a version
compose-plugin = "1.6.10"
coroutines = "1.9.0-RC"
junit = "4.13.2"
kotlin = "2.0.0"
ktlint = "12.1.1"
Expand Down Expand Up @@ -54,6 +55,9 @@ ktor-client-mock = { module = "io.ktor:ktor-client-mock", version.ref = "ktor" }
koin-core = { module = "io.insert-koin:koin-core", version.ref = "koin" }
koin-test = { module = "io.insert-koin:koin-test", version.ref = "koin" }

# coroutines
coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" }

# Dependencies of the included build-logic
android-gradlePlugin = { group = "com.android.tools.build", name = "gradle", version.ref = "agp" }
kotlin-gradlePlugin = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-plugin", version.ref = "kotlin" }
Expand Down