Created
April 10, 2020 22:29
-
-
Save CristianMG/2c228e40bcf2166ce9daee6e70d5dc3f to your computer and use it in GitHub Desktop.
FarmRepository
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
/* | |
* | |
* * Copyright 2020 Cristian Menárguez González | |
* * | |
* * Licensed under the Apache License, Version 2.0 (the "License"); | |
* * you may not use this file except in compliance with the License. | |
* * You may obtain a copy of the License at | |
* * | |
* * http://www.apache.org/licenses/LICENSE-2.0 | |
* * | |
* * Unless required by applicable law or agreed to in writing, software | |
* * distributed under the License is distributed on an "AS IS" BASIS, | |
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* * See the License for the specific language governing permissions and | |
* * limitations under the License. | |
* | |
* | |
*/ | |
package com.cristianmg.repositories | |
import androidx.paging.DataSource | |
import com.cristianmg.api.FarmService | |
import com.cristianmg.common_objects.exception.Failure | |
import com.cristianmg.common_objects.functional.Either | |
import com.cristianmg.database.dao.FarmDao | |
import com.cristianmg.api.functional.NetworkHandler | |
import com.cristianmg.model.Farm | |
import com.cristianmg.repositories.mapper.FarmMapper | |
import javax.inject.Inject | |
interface FarmRepository { | |
fun farms(): DataSource.Factory<Int, Farm> | |
fun getAll(): Either<Failure, List<Farm>> | |
fun save(it: List<Farm>): Either<Failure, Unit> | |
fun removeAll(): Either<Failure, Unit> | |
class Disk @Inject constructor( | |
private val cache: FarmDao, | |
private val mapper: FarmMapper | |
) : FarmRepository { | |
override fun removeAll(): Either<Failure, Unit> = | |
Either.wrapFunction { | |
cache.delete() | |
} | |
override fun save(it: List<Farm>) = | |
Either.wrapFunction { | |
cache.insert(it.map { mapper.mapToEntity(it) }) | |
} | |
override fun getAll(): Either<Failure, List<Farm>> = | |
Either.wrapFunction { | |
mapper.mapListToModel(cache.get()) | |
} | |
override fun farms(): DataSource.Factory<Int, Farm> = | |
cache.getAll().map { mapper.mapToModel(it) } | |
} | |
class Network @Inject constructor( | |
private val service: FarmService, | |
private val networkHandler: NetworkHandler, | |
private val mapper: FarmMapper | |
) : FarmRepository { | |
override fun getAll(): Either<Failure, List<Farm>> { | |
return when (networkHandler.isConnected) { | |
true -> Either.wrapFunction { mapper.mapListToModel(service.farms()) } | |
else -> Either.Left(Failure.NetworkConnection) | |
} | |
} | |
/** | |
* Unused functions | |
*/ | |
override fun removeAll() = | |
throw IllegalAccessException("Network can't provide a data-source") | |
override fun save(it: List<Farm>) = | |
throw IllegalAccessException("Network can't provide a data-source") | |
override fun farms(): DataSource.Factory<Int, Farm> = | |
throw IllegalAccessException("Network can't provide a data-source") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment