se permite eliminar foto de perfil, y varios fixes

This commit is contained in:
erik-everardo 2024-04-22 22:42:54 -06:00
parent 837b38ef97
commit 8a345fc59b
8 changed files with 62 additions and 17 deletions

View File

@ -18,4 +18,7 @@ interface ProfileApi {
@POST("EditProfile/UpdateProfile") @POST("EditProfile/UpdateProfile")
fun updateProfile(@Body updateProfileDto: UpdateProfileDto): Call<UserProfileDto> fun updateProfile(@Body updateProfileDto: UpdateProfileDto): Call<UserProfileDto>
@POST("EditProfile/RemoveProfileImage")
fun removeCurrentProfilePicture(): Call<Void>
} }

View File

@ -67,4 +67,21 @@ class ProfileRepositoryImpl @Inject constructor(
emit(Resource.Error(Resource.Error.ErrorType.NetworkError)) emit(Resource.Error(Resource.Error.ErrorType.NetworkError))
} }
} }
override fun removeProfileImage(): Flow<Resource<Boolean>> = flow {
emit(Resource.Loading())
try {
val response = profileApi.removeCurrentProfilePicture().awaitResponse()
if(response.isSuccessful) {
emit(Resource.Success(true))
} else {
emit(Resource.Error(Resource.Error.mapErrorCode(response.code())))
}
} catch (e: Exception) {
Log.e("ProfileRepositoryImpl", e.message.toString())
emit(Resource.Error(Resource.Error.ErrorType.NetworkError))
}
}
} }

View File

@ -12,4 +12,6 @@ interface ProfileRepository {
fun setProfileImage(image: Image): Flow<Resource<Boolean>> fun setProfileImage(image: Image): Flow<Resource<Boolean>>
fun updateProfile(newDisplayName: String, newDescription: String): Flow<Resource<Boolean>> fun updateProfile(newDisplayName: String, newDescription: String): Flow<Resource<Boolean>>
fun removeProfileImage(): Flow<Resource<Boolean>>
} }

View File

@ -0,0 +1,9 @@
package com.isolaatti.profile.domain.use_case
import com.isolaatti.profile.domain.ProfileRepository
import javax.inject.Inject
class RemoveProfilePicture @Inject constructor(private val profileRepository: ProfileRepository) {
operator fun invoke() = profileRepository.removeProfileImage()
}

View File

@ -11,6 +11,7 @@ import com.isolaatti.profile.domain.entity.UserProfile
import com.isolaatti.profile.domain.use_case.FollowUser import com.isolaatti.profile.domain.use_case.FollowUser
import com.isolaatti.profile.domain.use_case.GetProfile import com.isolaatti.profile.domain.use_case.GetProfile
import com.isolaatti.profile.domain.use_case.GetProfilePosts import com.isolaatti.profile.domain.use_case.GetProfilePosts
import com.isolaatti.profile.domain.use_case.RemoveProfilePicture
import com.isolaatti.profile.domain.use_case.SetProfileImage import com.isolaatti.profile.domain.use_case.SetProfileImage
import com.isolaatti.utils.Resource import com.isolaatti.utils.Resource
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
@ -26,7 +27,8 @@ class ProfileViewModel @Inject constructor(
private val getProfileUseCase: GetProfile, private val getProfileUseCase: GetProfile,
private val getProfilePostsUseCase: GetProfilePosts, private val getProfilePostsUseCase: GetProfilePosts,
private val setProfileImageUC: SetProfileImage, private val setProfileImageUC: SetProfileImage,
private val followUserUC: FollowUser private val followUserUC: FollowUser,
private val removeProfilePictureUC: RemoveProfilePicture
) : PostListingViewModelBase() { ) : PostListingViewModelBase() {
private val _profile = MutableLiveData<UserProfile>() private val _profile = MutableLiveData<UserProfile>()
val profile: LiveData<UserProfile> get() = _profile val profile: LiveData<UserProfile> get() = _profile
@ -143,4 +145,18 @@ class ProfileViewModel @Inject constructor(
fun setProfile(profile: UserProfile) { fun setProfile(profile: UserProfile) {
_profile.value = profile _profile.value = profile
} }
fun removeCurrentProfileImage() {
viewModelScope.launch {
removeProfilePictureUC().onEach {
when(it) {
is Resource.Error -> {}
is Resource.Loading -> {}
is Resource.Success -> {
_profile.postValue(_profile.value?.copy(profileImageId = null))
}
}
}.flowOn(Dispatchers.IO).launchIn(this)
}
}
} }

View File

@ -292,18 +292,25 @@ class ProfileMainFragment : Fragment() {
.setTitle(R.string.remove_photo) .setTitle(R.string.remove_photo)
.setNegativeButton(R.string.cancel, null) .setNegativeButton(R.string.cancel, null)
.setPositiveButton(R.string.yes_continue) { _, _ -> .setPositiveButton(R.string.yes_continue) { _, _ ->
// TODO remove image here viewModel.removeCurrentProfileImage()
} }
.show() .show()
} }
private lateinit var postListingRecyclerViewAdapterWiring: PostListingRecyclerViewAdapterWiring private lateinit var postListingRecyclerViewAdapterWiring: PostListingRecyclerViewAdapterWiring
// counter variable to prevent calling appBarLayout.totalScrollRange many times
private var i = 0
private fun setupCollapsingBar() { private fun setupCollapsingBar() {
viewBinding.topAppBarLayout.addOnOffsetChangedListener { appBarLayout, verticalOffset -> viewBinding.topAppBarLayout.addOnOffsetChangedListener { appBarLayout, verticalOffset ->
if (scrollRange == -1) scrollRange = appBarLayout.totalScrollRange // prevent calling appBarLayout.totalScrollRange many times
if (i == 30) {
scrollRange = appBarLayout.totalScrollRange
i = 0
} else {
i++
}
if (scrollRange + verticalOffset == 0) { if (scrollRange + verticalOffset == 0) {
viewBinding.collapsingToolbarLayout.title = viewModel.profile.value?.name viewBinding.collapsingToolbarLayout.title = viewModel.profile.value?.name
isShow = true isShow = true

View File

@ -106,20 +106,11 @@
android:maxLines="4" android:maxLines="4"
android:textAlignment="center" android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/description_options_button" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/play_button_container" app:layout_constraintStart_toEndOf="@id/play_button_container"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
tools:text="Hi there, I am software developer!" /> tools:text="Hi there, I am software developer!" />
<com.google.android.material.button.MaterialButton
android:id="@+id/description_options_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
style="?attr/materialIconButtonStyle"
app:icon="@drawable/baseline_more_horiz_24"/>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView> </com.google.android.material.card.MaterialCardView>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Mark as read" /> <!-- <item android:title="Mark as read" />-->
<item android:title="Remove all" /> <!-- <item android:title="Remove all" />-->
</menu> </menu>