uso clase Post de dominio en contratos y otros lados donde aun usaba el DTO

This commit is contained in:
erik-everardo 2023-12-26 22:02:35 -06:00
parent 02122e249a
commit 4aa61cfb38
6 changed files with 87 additions and 17 deletions

View File

@ -1,7 +1,10 @@
package com.isolaatti.posting.posts.domain.entity
import android.os.Parcel
import android.os.Parcelable
import com.isolaatti.common.Ownable
import com.isolaatti.posting.posts.data.remote.FeedDto
import java.io.Serializable
data class Post(
val id: Long,
@ -16,7 +19,23 @@ data class Post(
val userName: String,
val squadName: String?,
var liked: Boolean
) : Ownable {
) : Ownable, Parcelable {
constructor(parcel: Parcel) : this(
parcel.readLong(),
parcel.readString()!!,
parcel.readInt(),
parcel.readInt(),
parcel.readString()!!,
parcel.readString(),
parcel.readString(),
parcel.readInt(),
parcel.readInt(),
parcel.readString()!!,
parcel.readString(),
parcel.readByte() != 0.toByte()
) {
}
companion object {
fun fromFeedDto(feedDto: FeedDto): MutableList<Post> {
return feedDto.data.map {
@ -53,5 +72,35 @@ data class Post(
liked = postDto.liked
)
}
@JvmField
val CREATOR = object: Parcelable.Creator<Post> {
override fun createFromParcel(parcel: Parcel): Post {
return Post(parcel)
}
override fun newArray(size: Int): Array<Post?> {
return arrayOfNulls(size)
}
}
}
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeLong(id)
parcel.writeString(textContent)
parcel.writeInt(userId)
parcel.writeInt(privacy)
parcel.writeString(date)
parcel.writeString(audioId)
parcel.writeString(squadId)
parcel.writeInt(numberOfLikes)
parcel.writeInt(numberOfComments)
parcel.writeString(userName)
parcel.writeString(squadName)
parcel.writeByte(if (liked) 1 else 0)
}
override fun describeContents(): Int {
return 0
}
}

View File

@ -5,13 +5,14 @@ import android.content.Context
import android.content.Intent
import androidx.activity.result.contract.ActivityResultContract
import com.isolaatti.posting.posts.data.remote.FeedDto
import com.isolaatti.posting.posts.domain.entity.Post
import com.isolaatti.posting.posts.ui.CreatePostActivity
class CreatePostContract : ActivityResultContract<Unit, FeedDto.PostDto?>() {
class CreatePostContract : ActivityResultContract<Unit, Post?>() {
override fun createIntent(context: Context, input: Unit): Intent {
return Intent(context, CreatePostActivity::class.java)
}
override fun parseResult(resultCode: Int, intent: Intent?): FeedDto.PostDto? {
override fun parseResult(resultCode: Int, intent: Intent?): Post? {
return if(resultCode == RESULT_OK) {
intent?.extras?.getParcelable(CreatePostActivity.EXTRA_KEY_POST_POSTED)
} else {

View File

@ -7,6 +7,7 @@ import com.isolaatti.posting.posts.data.remote.CreatePostDto
import com.isolaatti.posting.posts.data.remote.EditPostDto
import com.isolaatti.posting.posts.data.remote.EditPostDto.Companion.PRIVACY_ISOLAATTI
import com.isolaatti.posting.posts.data.remote.FeedDto
import com.isolaatti.posting.posts.domain.entity.Post
import com.isolaatti.posting.posts.domain.use_case.EditPost
import com.isolaatti.posting.posts.domain.use_case.LoadSinglePost
import com.isolaatti.posting.posts.domain.use_case.MakePost
@ -22,7 +23,7 @@ import javax.inject.Inject
@HiltViewModel
class CreatePostViewModel @Inject constructor(private val makePost: MakePost, private val editPost: EditPost, private val loadPost: LoadSinglePost) : ViewModel() {
val validation: MutableLiveData<Boolean> = MutableLiveData(false)
val posted: MutableLiveData<FeedDto.PostDto?> = MutableLiveData()
val posted: MutableLiveData<Post?> = MutableLiveData()
val error: MutableLiveData<Resource.Error.ErrorType?> = MutableLiveData()
val loading: MutableLiveData<Boolean> = MutableLiveData(false)
val postToEdit: MutableLiveData<EditPostDto> = MutableLiveData()
@ -33,7 +34,7 @@ class CreatePostViewModel @Inject constructor(private val makePost: MakePost, pr
when(it) {
is Resource.Success -> {
loading.postValue(false)
posted.postValue(it.data)
posted.postValue(Post.fromPostDto(it.data!!))
}
is Resource.Error -> {
loading.postValue(false)
@ -53,7 +54,7 @@ class CreatePostViewModel @Inject constructor(private val makePost: MakePost, pr
when(it) {
is Resource.Success -> {
loading.postValue(false)
posted.postValue(it.data)
posted.postValue(Post.fromPostDto(it.data!!))
}
is Resource.Error -> {
loading.postValue(false)

View File

@ -5,9 +5,10 @@ import android.content.Context
import android.content.Intent
import androidx.activity.result.contract.ActivityResultContract
import com.isolaatti.posting.posts.data.remote.FeedDto
import com.isolaatti.posting.posts.domain.entity.Post
import com.isolaatti.posting.posts.ui.CreatePostActivity
class EditPostContract : ActivityResultContract<Long, FeedDto.PostDto?>() {
class EditPostContract : ActivityResultContract<Long, Post?>() {
override fun createIntent(context: Context, input: Long): Intent {
return Intent(context, CreatePostActivity::class.java).apply {
putExtra(CreatePostActivity.EXTRA_KEY_MODE, CreatePostActivity.EXTRA_MODE_EDIT)
@ -15,7 +16,7 @@ class EditPostContract : ActivityResultContract<Long, FeedDto.PostDto?>() {
}
}
override fun parseResult(resultCode: Int, intent: Intent?): FeedDto.PostDto? {
override fun parseResult(resultCode: Int, intent: Intent?): Post? {
return if(resultCode == Activity.RESULT_OK) {
intent?.extras?.getParcelable(CreatePostActivity.EXTRA_KEY_POST_POSTED)
} else {

View File

@ -18,6 +18,11 @@ import kotlinx.coroutines.launch
import javax.inject.Inject
abstract class PostListingViewModelBase : ViewModel() {
companion object {
const val TAG = "PostListingViewModelBase"
}
@Inject
lateinit var likesRepository: LikesRepository
@Inject
@ -46,8 +51,12 @@ abstract class PostListingViewModelBase : ViewModel() {
likesRepository.likePost(postId).onEach {like ->
when(like) {
is Resource.Error -> {}
is Resource.Loading -> {}
is Resource.Error -> {
Log.e(TAG, "Error likePost($postId) ${like.errorType} ${like.message}")
}
is Resource.Loading -> {
Log.i(TAG, "Loading likePost($postId)")
}
is Resource.Success -> {
val likedPost = posts.value?.first?.find { post -> post.id == like.data?.postId }
val index = posts.value?.first?.indexOf(likedPost)
@ -74,8 +83,12 @@ abstract class PostListingViewModelBase : ViewModel() {
likesRepository.unLikePost(postId).onEach {like ->
when(like) {
is Resource.Error -> TODO()
is Resource.Loading -> TODO()
is Resource.Error -> {
Log.e(TAG, "Error unLikePost($postId) ${like.errorType} ${like.message}")
}
is Resource.Loading -> {
Log.i(TAG, "Loading unLikePost($postId)")
}
is Resource.Success -> {
val likedPost = posts.value?.first?.find { post -> post.id == like.data?.postId }
val index = posts.value?.first?.indexOf(likedPost)
@ -115,7 +128,11 @@ abstract class PostListingViewModelBase : ViewModel() {
}
}
fun onPostUpdate(post: FeedDto.PostDto) {
fun onPostUpdate(post: Post) {
}
fun onPostAddedAtTheBeginning(post: Post) {
}
}

View File

@ -74,11 +74,12 @@ class ProfileMainFragment : Fragment() {
private var isShow = false
private val createDiscussion = registerForActivityResult(CreatePostContract()) {
if(it != null) {
Toast.makeText(requireContext(), R.string.posted_successfully, Toast.LENGTH_SHORT).show()
}
if(it == null)
return@registerForActivityResult
// TODO add post to recycler view
Toast.makeText(requireContext(), R.string.posted_successfully, Toast.LENGTH_SHORT).show()
viewModel.onPostAddedAtTheBeginning(it)
}
private val editDiscussion = registerForActivityResult(EditPostContract()) {