Last active
May 8, 2020 10:55
-
-
Save duyp/fe25a734cd3ee98ccb58ba39f7d4b502 to your computer and use it in GitHub Desktop.
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
// domain | |
sealed class ErrorEntity { | |
sealed class ApiError: ErrorEntity() { | |
// ..... | |
} | |
sealed class FileError: ErrorEntity() { | |
object NotFound: FileError() | |
object ReadError: FileError() | |
} | |
} | |
// domain | |
interface FileRepository { | |
fun loadFile(fileName: String): Result<String> | |
} | |
// data layer | |
class AndroidFileRepository : FileRepository { | |
override fun loadFile(fileName: String): Result<String> { | |
return try { | |
val fileContent = File(fileName).inputStream().readBytes().toString(Charsets.UTF_8) | |
Result.Success(fileContent) | |
} catch (e: FileNotFoundException) { | |
Result.Error(ErrorEntity.FileError.NotFound) | |
} catch (e: IOException) { | |
Result.Error(ErrorEntity.FileError.ReadError) | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment