This repository has been archived by the owner on Mar 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2caec46
commit f1f4465
Showing
31 changed files
with
275 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 0 additions & 29 deletions
29
app/src/main/java/com/example/kotlincats/data/CatMapper.kt
This file was deleted.
Oops, something went wrong.
36 changes: 0 additions & 36 deletions
36
app/src/main/java/com/example/kotlincats/data/CatRepository.kt
This file was deleted.
Oops, something went wrong.
27 changes: 27 additions & 0 deletions
27
app/src/main/java/com/example/kotlincats/data/CatRepositoryImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.example.kotlincats.data | ||
|
||
import com.example.kotlincats.data.api.ApiDataSource | ||
import com.example.kotlincats.data.database.CatDao | ||
import com.example.kotlincats.data.mappers.CatMapper | ||
import com.example.kotlincats.domain.models.Cat | ||
import com.example.kotlincats.domain.repositories.CatRepository | ||
import javax.inject.Inject | ||
|
||
class CatRepositoryImpl @Inject constructor( | ||
private val catDao: CatDao, | ||
private val apiDataSource: ApiDataSource, | ||
private val catMapper: CatMapper | ||
): CatRepository { | ||
|
||
override suspend fun getCats(quantity: Int): List<Cat> { | ||
return apiDataSource.getCats(quantity).map { catMapper.toCat(it) } | ||
} | ||
|
||
override suspend fun deleteCat(cat: Cat) = catDao.delete( | ||
catMapper.toCatDatabaseModel(cat) | ||
) | ||
|
||
override suspend fun saveCats(cats: List<Cat>) = catDao.insert( | ||
cats.map { catMapper.toCatDatabaseModel(it) } | ||
) | ||
} |
20 changes: 3 additions & 17 deletions
20
app/src/main/java/com/example/kotlincats/data/api/ApiDataSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,7 @@ | ||
package com.example.kotlincats.data.api | ||
|
||
import com.example.kotlincats.data.CatMapper | ||
import com.example.kotlincats.data.api.catApi.CatApi | ||
import com.example.kotlincats.data.api.hipsterIpsumApi.HipsterIpsumApi | ||
import com.example.kotlincats.domain.model.Cat | ||
import javax.inject.Inject | ||
import com.example.kotlincats.data.models.CatApiModel | ||
|
||
class ApiDataSource @Inject constructor( | ||
private val catApi: CatApi, | ||
private val hipsterIpsumApi: HipsterIpsumApi, | ||
private val catMapper: CatMapper | ||
) { | ||
|
||
suspend fun getCats(quantity: Int): List<Cat> { | ||
val catsFromApi = catApi.getCats(quantity) | ||
val text = hipsterIpsumApi.getParagraphs(quantity); | ||
|
||
return catMapper.mapCatsWithText(catsFromApi, text) | ||
} | ||
interface ApiDataSource { | ||
suspend fun getCats(quantity: Int): List<CatApiModel> | ||
} |
21 changes: 21 additions & 0 deletions
21
app/src/main/java/com/example/kotlincats/data/api/ApiDataSourceImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.example.kotlincats.data.api | ||
|
||
import com.example.kotlincats.data.api.catApi.CatApi | ||
import com.example.kotlincats.data.api.hipsterIpsumApi.HipsterIpsumApi | ||
import com.example.kotlincats.data.models.CatApiModel | ||
import com.example.kotlincats.util.ArrayUtil | ||
import javax.inject.Inject | ||
|
||
class ApiDataSourceImpl @Inject constructor( | ||
private val catApi: CatApi, | ||
private val hipsterIpsumApi: HipsterIpsumApi, | ||
private val arrayUtil: ArrayUtil | ||
): ApiDataSource { | ||
|
||
override suspend fun getCats(quantity: Int): List<CatApiModel> { | ||
val catsFromApi = catApi.getCats(quantity) | ||
val text = hipsterIpsumApi.getParagraphs(quantity); | ||
|
||
return arrayUtil.transformIntoCatDataModel(catsFromApi, text) | ||
} | ||
} |
10 changes: 5 additions & 5 deletions
10
app/src/main/java/com/example/kotlincats/data/database/CatDao.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,23 @@ | ||
package com.example.kotlincats.data.database | ||
|
||
import androidx.room.* | ||
import com.example.kotlincats.domain.model.Cat | ||
import com.example.kotlincats.data.models.CatDatabaseModel | ||
|
||
@Dao | ||
interface CatDao { | ||
|
||
@Query("SELECT * from cat_table ORDER BY photoUrl ASC") | ||
suspend fun getCats(): List<Cat> | ||
suspend fun getCats(): List<CatDatabaseModel> | ||
|
||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
suspend fun insert(cat: Cat) | ||
suspend fun insert(cat: CatDatabaseModel) | ||
|
||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
suspend fun insert(cats: List<Cat>) | ||
suspend fun insert(cats: List<CatDatabaseModel>) | ||
|
||
@Query("DELETE FROM cat_table") | ||
suspend fun deleteAllCats() | ||
|
||
@Delete | ||
suspend fun delete(cat: Cat) | ||
suspend fun delete(cat: CatDatabaseModel) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
app/src/main/java/com/example/kotlincats/data/database/DatabaseDataSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.example.kotlincats.data.database | ||
|
||
interface DatabaseDataSource { | ||
|
||
suspend fun getCats() | ||
} |
10 changes: 10 additions & 0 deletions
10
app/src/main/java/com/example/kotlincats/data/database/DatabaseDataSourceImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.example.kotlincats.data.database | ||
|
||
import javax.inject.Inject | ||
|
||
class DatabaseDataSourceImpl @Inject constructor(private val catDao: CatDao): DatabaseDataSource { | ||
|
||
override suspend fun getCats() { | ||
catDao.getCats() | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
app/src/main/java/com/example/kotlincats/data/mappers/CatMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.example.kotlincats.data.mappers | ||
|
||
import com.example.kotlincats.data.models.CatApiModel | ||
import com.example.kotlincats.data.models.CatDatabaseModel | ||
import com.example.kotlincats.domain.models.Cat | ||
import javax.inject.Inject | ||
|
||
class CatMapper @Inject constructor() { | ||
|
||
fun toCat(catApiModel: CatApiModel): Cat { | ||
return Cat( | ||
catApiModel.hashCode(), | ||
catApiModel.name, | ||
catApiModel.imageUrl, | ||
catApiModel.info | ||
) | ||
} | ||
|
||
fun toCatDatabaseModel(cat: Cat): CatDatabaseModel { | ||
return CatDatabaseModel( | ||
cat.id, | ||
cat.name, | ||
cat.photoUrl, | ||
cat.info | ||
) | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
app/src/main/java/com/example/kotlincats/data/models/CatApiModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.example.kotlincats.data.models | ||
|
||
data class CatApiModel( | ||
val name: String, | ||
val imageUrl: String, | ||
val info: String | ||
) |
9 changes: 3 additions & 6 deletions
9
...om/example/kotlincats/domain/model/Cat.kt → ...otlincats/data/models/CatDatabaseModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,13 @@ | ||
package com.example.kotlincats.domain.model | ||
package com.example.kotlincats.data.models | ||
|
||
import android.os.Parcelable | ||
import androidx.room.ColumnInfo | ||
import androidx.room.Entity | ||
import androidx.room.PrimaryKey | ||
import kotlinx.android.parcel.Parcelize | ||
|
||
@Parcelize | ||
@Entity(tableName = "cat_table") | ||
class Cat( | ||
class CatDatabaseModel( | ||
@PrimaryKey val id: Int, | ||
@ColumnInfo(name = "name") val name: String, | ||
@ColumnInfo(name = "photoUrl") val photoUrl: String, | ||
@ColumnInfo(name = "info") val info: String | ||
) : Parcelable | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
app/src/main/java/com/example/kotlincats/di/RepositoryModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.example.kotlincats.di | ||
|
||
import com.example.kotlincats.data.CatRepositoryImpl | ||
import com.example.kotlincats.data.api.ApiDataSource | ||
import com.example.kotlincats.data.api.ApiDataSourceImpl | ||
import com.example.kotlincats.domain.repositories.CatRepository | ||
import dagger.Binds | ||
import dagger.Module | ||
|
||
@Module | ||
abstract class RepositoryModule { | ||
|
||
@Binds | ||
abstract fun bindCatRepository(catRepository: CatRepositoryImpl): CatRepository | ||
|
||
@Binds | ||
abstract fun bindApiDataSource(apiDataSourceImpl: ApiDataSourceImpl): ApiDataSource | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
app/src/main/java/com/example/kotlincats/domain/models/Cat.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.example.kotlincats.domain.models | ||
|
||
import android.os.Parcelable | ||
import kotlinx.android.parcel.Parcelize | ||
|
||
@Parcelize | ||
class Cat( | ||
val id: Int, | ||
val name: String, | ||
val photoUrl: String, | ||
val info: String | ||
) : Parcelable |
12 changes: 12 additions & 0 deletions
12
app/src/main/java/com/example/kotlincats/domain/repositories/CatRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.example.kotlincats.domain.repositories | ||
|
||
import com.example.kotlincats.domain.models.Cat | ||
|
||
interface CatRepository { | ||
|
||
suspend fun getCats(quantity: Int): List<Cat> | ||
|
||
suspend fun deleteCat(cat: Cat) | ||
|
||
suspend fun saveCats(cats: List<Cat>) | ||
} |
12 changes: 12 additions & 0 deletions
12
app/src/main/java/com/example/kotlincats/domain/usecases/DeleteCatUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.example.kotlincats.domain.usecases | ||
|
||
import com.example.kotlincats.domain.models.Cat | ||
import com.example.kotlincats.domain.repositories.CatRepository | ||
import javax.inject.Inject | ||
|
||
class DeleteCatUseCase @Inject constructor(private val catRepository: CatRepository) { | ||
|
||
suspend fun execute(cat: Cat) { | ||
catRepository.deleteCat(cat) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
app/src/main/java/com/example/kotlincats/domain/usecases/GetCatsUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.example.kotlincats.domain.usecases | ||
|
||
import com.example.kotlincats.domain.models.Cat | ||
import com.example.kotlincats.domain.repositories.CatRepository | ||
import javax.inject.Inject | ||
|
||
class GetCatsUseCase @Inject constructor(private val catRepository: CatRepository) { | ||
|
||
suspend fun execute(quantity: Int): List<Cat> { | ||
return catRepository.getCats(quantity) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
app/src/main/java/com/example/kotlincats/domain/usecases/SaveCatsUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.example.kotlincats.domain.usecases | ||
|
||
import com.example.kotlincats.domain.models.Cat | ||
import com.example.kotlincats.domain.repositories.CatRepository | ||
import javax.inject.Inject | ||
|
||
class SaveCatsUseCase @Inject constructor(private val catRepository: CatRepository) { | ||
|
||
suspend fun execute(cats: List<Cat>) { | ||
catRepository.saveCats(cats) | ||
} | ||
} |
Oops, something went wrong.