Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

프로필 이미지 PATCH, 알림 GET api 연결 #91

Merged
merged 13 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package com.project.meongcare.home.model.entities
data class GetUserProfileResponse(
val email: String,
val imageUrl: String,
val pushAgreement: Boolean,
)
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,11 @@ interface ProfileApi {
@Query("pushAgreement") pushAgreement: Boolean,
@Header("AccessToken") accessToken: String,
): Response<Int>

@Multipart
@PATCH("member/profile")
suspend fun patchProfileImage(
@Header("AccessToken") accessToken: String,
@Part file: MultipartBody.Part,
): Response<Int>
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ interface ProfileRepository {
pushAgreement: Boolean,
accessToken: String,
): Int?

suspend fun patchProfileImage(
accessToken: String,
file: MultipartBody.Part,
): Int?
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,23 @@ class ProfileRepositoryImpl
null
}
}

override suspend fun patchProfileImage(
accessToken: String,
file: MultipartBody.Part,
): Int? {
return try {
val response = profileRetrofitClient.profileApi.patchProfileImage(accessToken, file)
if (response.code() == 200) {
Log.d("ProfileRepo-PatchProfile", "통신 성공")
response.code()
} else {
Log.d("ProfileRepo-PatchProfile", "통실 실패 : ${response.code()}")
response.code()
}
} catch (e: Exception) {
e.printStackTrace()
null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.project.meongcare.databinding.FragmentProfileBinding
import com.project.meongcare.info.viewmodel.ProfileViewModel
import com.project.meongcare.login.model.data.local.UserPreferences
import com.project.meongcare.onboarding.model.data.local.PhotoMenuListener
import com.project.meongcare.onboarding.view.createMultipartBody
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import javax.inject.Inject
Expand All @@ -33,6 +34,7 @@ class ProfileFragment : Fragment(), PhotoMenuListener {
private lateinit var mainActivity: MainActivity

private val profileViewModel: ProfileViewModel by viewModels()
private lateinit var profileUri: Uri

@Inject
lateinit var userPreferences: UserPreferences
Expand Down Expand Up @@ -86,7 +88,25 @@ class ProfileFragment : Fragment(), PhotoMenuListener {
}
}

val accessToken = ""
profileViewModel.patchProfileResponse.observe(viewLifecycleOwner) { response ->
if (response == 200) {
binding.run {
Glide.with(this@ProfileFragment)
.load(profileUri)
.into(imageviewProfileImage)
}
makeSnackBar(binding.root, requireContext(), "프로필 사진 변경이 완료되었습니다.")
} else {
binding.run {
Glide.with(this@ProfileFragment)
.load(R.drawable.profile_default_image)
.into(imageviewProfileImage)
}
makeSnackBar(binding.root, requireContext(), "프로필 사진 변경에 실패하였습니다.")
}
}

val accessToken = "Bearer eyJhbGciOiJIUzI1NiJ9.eyJpZCI6MywiZXhwIjoxNzA0NzY4MzU0fQ.cN4yZ3Ou9YcUHusdd8Z_IsmA7KF-gzZ3kVc5fljELTM"
profileViewModel.getUserProfile(accessToken)
profileViewModel.getDogList(accessToken)

Expand All @@ -108,7 +128,9 @@ class ProfileFragment : Fragment(), PhotoMenuListener {
}

buttonProfileSetting.setOnClickListener {
findNavController().navigate(R.id.action_profileFragment_to_settingFragment)
val bundle = Bundle()
bundle.putBoolean("pushAgreement", profileViewModel.userProfile.value?.pushAgreement!!)
findNavController().navigate(R.id.action_profileFragment_to_settingFragment, bundle)
}

buttonProfileLogout.setOnClickListener {
Expand All @@ -135,13 +157,10 @@ class ProfileFragment : Fragment(), PhotoMenuListener {
}

override fun onUriPassed(uri: Uri) {
// uri로 회원 정보 patch api 연결한 후 통신 정상이면 이미지 뷰에 표시되도록 로직 변경
binding.run {
Glide.with(this@ProfileFragment)
.load(uri)
.error(R.drawable.profile_default_image)
.into(imageviewProfileImage)
}
val accessToken = "Bearer eyJhbGciOiJIUzI1NiJ9.eyJpZCI6MywiZXhwIjoxNzA0NzY4MzU0fQ.cN4yZ3Ou9YcUHusdd8Z_IsmA7KF-gzZ3kVc5fljELTM"
profileUri = uri
val multipartBody = createMultipartBody(requireContext(), uri)
profileViewModel.patchProfileImage(accessToken, multipartBody)
}

private fun kakaoLogout() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.project.meongcare.info.view

import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
Expand Down Expand Up @@ -45,14 +46,19 @@ class SettingFragment : Fragment() {
settingViewModel.patchPushResponse.observe(viewLifecycleOwner) { response ->
if (response == 200) {
when (binding.switchSettingNotification.isChecked) {
true -> makeSnackBar("알림 수신 처리되었습니다.")
false -> makeSnackBar("알림 거부 처리되었습니다.")
true -> makeSnackBar(binding.root, requireContext(), "알림 수신 처리되었습니다.")
false -> makeSnackBar(binding.root, requireContext(), "알림 거부 처리되었습니다.")
}
}
}

val accessToken = ""
val pushAgreement = arguments?.getBoolean("pushAgreement")!!
binding.run {
switchSettingNotification.run {
isChecked = pushAgreement
}

imagebuttonSettingBack.setOnClickListener {
findNavController().popBackStack()
}
Expand Down Expand Up @@ -87,29 +93,33 @@ class SettingFragment : Fragment() {

return binding.root
}
}

fun makeSnackBar(message: String) {
val snackBar = Snackbar.make(binding.root, message, Snackbar.LENGTH_SHORT)
val snackBarLayout = snackBar.view as Snackbar.SnackbarLayout

val imageView = ImageView(context)
imageView.setImageResource(R.drawable.all_snack_bar_complete)
val imageViewStartPadding = resources.getDimensionPixelSize(R.dimen.snackbar_image_start_padding)
imageView.setPadding(imageViewStartPadding, 0, 0, 0)

val params =
LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.MATCH_PARENT,
)
params.gravity = Gravity.CENTER_VERTICAL or Gravity.START
snackBarLayout.addView(imageView, 0, params)

val textView = snackBarLayout.findViewById<TextView>(com.google.android.material.R.id.snackbar_text)

val textViewStartPadding = resources.getDimensionPixelSize(R.dimen.snackbar_text_start_padding)
textView.setPadding(textViewStartPadding, 0, 0, 0)

snackBar.show()
}
fun makeSnackBar(
view: View,
context: Context,
message: String,
) {
val snackBar = Snackbar.make(view, message, Snackbar.LENGTH_SHORT)
val snackBarLayout = snackBar.view as Snackbar.SnackbarLayout

val imageView = ImageView(context)
imageView.setImageResource(R.drawable.all_snack_bar_complete)
val imageViewStartPadding = context.resources.getDimensionPixelSize(R.dimen.snackbar_image_start_padding)
imageView.setPadding(imageViewStartPadding, 0, 0, 0)

val params =
LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.MATCH_PARENT,
)
params.gravity = Gravity.CENTER_VERTICAL or Gravity.START
snackBarLayout.addView(imageView, 0, params)

val textView = snackBarLayout.findViewById<TextView>(com.google.android.material.R.id.snackbar_text)

val textViewStartPadding = context.resources.getDimensionPixelSize(R.dimen.snackbar_text_start_padding)
textView.setPadding(textViewStartPadding, 0, 0, 0)

snackBar.show()
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.project.meongcare.info.view

import android.app.Activity
import android.content.ContentResolver
import android.content.Context
import android.content.Intent
import android.net.Uri
Expand All @@ -14,6 +15,7 @@ import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.FileProvider
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.project.meongcare.MainActivity
import com.project.meongcare.R
import com.project.meongcare.databinding.FragmentPhotoSelectBottomSheetBinding
import com.project.meongcare.onboarding.model.data.local.PhotoMenuListener
import java.io.File
Expand All @@ -36,12 +38,17 @@ class UserProfileSelectBottomSheetFragment : BottomSheetDialogFragment() {
mainActivity = activity as MainActivity

binding.run {
divider2.visibility = View.VISIBLE
textviewSelectDefault.visibility = View.VISIBLE
textviewSelectCamera.setOnClickListener {
executeCamera(mainActivity)
}
textviewSelectAlbum.setOnClickListener {
executeAlbum()
}
textviewSelectDefault.setOnClickListener {
setDefaultImage()
}
}

return binding.root
Expand Down Expand Up @@ -106,6 +113,20 @@ class UserProfileSelectBottomSheetFragment : BottomSheetDialogFragment() {
albumLauncher.launch(intent)
}

private fun setDefaultImage() {
val defaultImageUri =
requireContext().resources.let { resources ->
Uri.Builder()
.scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
.authority(resources.getResourcePackageName(R.drawable.profile_default_image))
.appendPath(resources.getResourceTypeName(R.drawable.profile_default_image))
.appendPath(resources.getResourceEntryName(R.drawable.profile_default_image))
.build()
}
sendUri(defaultImageUri)
dismiss()
}

private fun sendUri(uri: Uri) {
photoMenuListener?.onUriPassed(uri)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ class ProfileViewModel
val patchPushResponse
get() = _patchPushResponse

private val _patchProfileResponse = MutableLiveData<Int>()
val patchProfileResponse
get() = _patchProfileResponse

private val _dogProfile = MutableLiveData<Uri>()
val dogProfile
get() = _dogProfile
Expand Down Expand Up @@ -127,4 +131,13 @@ class ProfileViewModel
_patchPushResponse.value = profileRepository.patchPushAgreement(pushAgreement, accessToken)
}
}

fun patchProfileImage(
accessToken: String,
file: MultipartBody.Part,
) {
viewModelScope.launch {
_patchProfileResponse.value = profileRepository.patchProfileImage(accessToken, file)
}
}
}
32 changes: 29 additions & 3 deletions app/src/main/res/layout/fragment_photo_select_bottom_sheet.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".onboarding.view.PhotoSelectBottomSheetFragment" >
android:paddingBottom="30dp"
tools:context=".onboarding.view.PhotoSelectBottomSheetFragment">

<TextView
android:id="@+id/textView25"
Expand Down Expand Up @@ -50,12 +51,37 @@
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="30dp"
android:letterSpacing="-0.02"
android:text="앨범 선택"
android:textAppearance="@style/Typography.Body1.Medium"
android:textColor="@color/gray5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/divider" />

<com.google.android.material.divider.MaterialDivider
android:id="@+id/divider2"
android:layout_width="312dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="24dp"
android:visibility="gone"
app:dividerColor="@color/gray2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textview_select_album" />

<TextView
android:id="@+id/textview_select_default"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginTop="16dp"
android:letterSpacing="-0.02"
android:text="기본 이미지로 변경"
android:textAppearance="@style/Typography.Body1.Medium"
android:textColor="@color/gray5"
android:visibility="gone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/divider2" />
</androidx.constraintlayout.widget.ConstraintLayout>