Skip to content
This repository has been archived by the owner on Mar 12, 2023. It is now read-only.

Commit

Permalink
- architecture was updated;
Browse files Browse the repository at this point in the history
  • Loading branch information
ayerakhavets committed May 19, 2020
1 parent 2caec46 commit f1f4465
Show file tree
Hide file tree
Showing 31 changed files with 275 additions and 148 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ dependencies {
implementation 'io.coil-kt:coil:0.8.0'

// fragment
implementation 'androidx.fragment:fragment-ktx:1.2.3'
implementation 'androidx.fragment:fragment-ktx:1.2.4'

// Room components
implementation "androidx.room:room-runtime:$rootProject.roomVersion"
Expand Down
29 changes: 0 additions & 29 deletions app/src/main/java/com/example/kotlincats/data/CatMapper.kt

This file was deleted.

36 changes: 0 additions & 36 deletions app/src/main/java/com/example/kotlincats/data/CatRepository.kt

This file was deleted.

27 changes: 27 additions & 0 deletions app/src/main/java/com/example/kotlincats/data/CatRepositoryImpl.kt
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 app/src/main/java/com/example/kotlincats/data/api/ApiDataSource.kt
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>
}
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 app/src/main/java/com/example/kotlincats/data/database/CatDao.kt
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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package com.example.kotlincats.data.database

import androidx.room.Database
import androidx.room.RoomDatabase
import com.example.kotlincats.domain.model.Cat
import com.example.kotlincats.data.models.CatDatabaseModel

@Database(entities = [Cat::class], version = 1, exportSchema = false)
@Database(entities = [CatDatabaseModel::class], version = 1, exportSchema = false)
abstract class CatDatabase : RoomDatabase() {

abstract fun userDao(): CatDao
Expand Down
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()
}
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 app/src/main/java/com/example/kotlincats/data/mappers/CatMapper.kt
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
)
}
}
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
)
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
)
5 changes: 3 additions & 2 deletions app/src/main/java/com/example/kotlincats/di/AppComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.example.kotlincats.di

import android.content.Context
import com.example.kotlincats.di.viewModel.ViewModelModule
import com.example.kotlincats.presentation.list.CatListFragment
import com.example.kotlincats.presentation.catList.CatListFragment
import dagger.BindsInstance
import dagger.Component
import javax.inject.Singleton
Expand All @@ -12,7 +12,8 @@ import javax.inject.Singleton
modules = [
NetworkModule::class,
DatabaseModule::class,
ViewModelModule::class
ViewModelModule::class,
RepositoryModule::class
]
)
interface AppComponent {
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/com/example/kotlincats/di/RepositoryModule.kt
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.example.kotlincats.di.viewModel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.example.kotlincats.presentation.list.CatListViewModel
import com.example.kotlincats.presentation.catList.CatListViewModel
import dagger.Binds
import dagger.Module
import dagger.multibindings.IntoMap
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/com/example/kotlincats/domain/models/Cat.kt
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
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>)
}
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)
}
}
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)
}
}
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)
}
}
Loading

0 comments on commit f1f4465

Please sign in to comment.