Created
January 21, 2021 00:58
-
-
Save Marcrobito/8fa01793ecec9dae0dd14afc3ab58f7e to your computer and use it in GitHub Desktop.
Comments
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
interface Api{ | |
suspend fun createComment(comment:Comment):RegularComment? | |
suspend fun readComments() | |
suspend fun readComment(id:String) | |
suspend fun updateComment(id:String, comment:RegularComment) | |
suspend fun deleteComment(id:String) | |
suspend fun createAnnotation(annotation:Annotation):Annotation? | |
suspend fun readAnnotations() | |
suspend fun readAnnotation(id:String) | |
suspend fun updateAnnotation(id:String, annotation: Annotation) | |
suspend fun deleteAnnotation(id:String) | |
} |
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
abstract class Comment(){ | |
abstract var id:String? | |
abstract val content:String | |
} | |
data class RegularComment( override var id: String? = null, | |
override val content:String, | |
val title:String): Comment() | |
data class Annotation( override var id: String?, | |
override val content:String): Comment() |
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
abstract class CommentsRepository(){ | |
abstract suspend fun createComment(comment: Comment):Response<Comment> | |
abstract suspend fun getComments():List<Comment> | |
abstract suspend fun getComment(id:String) | |
abstract suspend fun updateComment(id:String, comment: Comment) | |
abstract suspend fun deleteComment(id:String) | |
} |
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
class CommentsRepositoryImpl (private val api:Api): CommentsRepository() { | |
override suspend fun createComment(comment: Comment):Response<Comment> { | |
when(comment){ | |
is RegularComment -> api.createComment(comment) | |
is Annotation -> api.createAnnotation(comment) | |
else -> null | |
}?.let { | |
return Response.Success(it) | |
}.run{ | |
return Response.Fail(Exception()) | |
} | |
} | |
override suspend fun getComments(): List<Comment> { | |
TODO("Not yet implemented") | |
} | |
override suspend fun getComment(id: String) { | |
TODO("Not yet implemented") | |
} | |
override suspend fun updateComment(id: String, comment: Comment) { | |
TODO("Not yet implemented") | |
} | |
override suspend fun deleteComment(id: String) { | |
TODO("Not yet implemented") | |
} | |
} |
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
class CommentsViewModel(private val repository: CommentsRepository):ViewModel(){ | |
private val _comment = MutableLiveData<Response<RegularComment>>() | |
val comment: LiveData<Response<RegularComment>> get() = _comment | |
fun createComment(title:String, message:String){ | |
val comment = RegularComment( null, message, title) | |
viewModelScope.launch { | |
_comment.value = withContext(Dispatchers.IO){ | |
repository.createComment(comment) as Response<RegularComment> | |
} | |
} | |
} | |
} |
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
sealed class Response<out T> { | |
data class Success<out T>(val data: T) : Response<T>() { | |
override fun succeeded(): Boolean { | |
return true | |
} | |
} | |
data class Fail(val exception: Throwable): Response<Nothing>() | |
object NotInitialized : Response<Nothing>() | |
object Loading : Response<Nothing>() | |
open fun succeeded(): Boolean { | |
return false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment