Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/FOR-GRAD/For-Grad-android i…
Browse files Browse the repository at this point in the history
…nto 21-졸업-요건-ui
  • Loading branch information
dkyuuum committed Feb 18, 2024
2 parents feb79fa + b047dbc commit 2d85431
Show file tree
Hide file tree
Showing 24 changed files with 290 additions and 215 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class GradDateFragment : Fragment() {
val date = originalFormat.parse(dateString)
val formattedDate = targetFormat.format(date)
viewModel.updateDateInfo(formattedDate)
Toast.makeText(context, "저장되었습니다.", Toast.LENGTH_LONG).show()
//Toast.makeText(context, "저장되었습니다.", Toast.LENGTH_LONG).show()
navigate(R.id.action_fragment_date_to_fragment_home)
viewModel.onEditButtonClick()
} catch (e: ParseException) {
Expand Down Expand Up @@ -113,6 +113,11 @@ class GradDateFragment : Fragment() {
binding.tvGradDateDday.text = "D-$dday"
}

//api에서 메모 hint로 가져오기
viewModel._dateResponse2.observe(viewLifecycleOwner) { response ->
binding.tvGradDateMemo.hint = response!!.result.message
}

//수정하기 및 버튼 visible 조건
viewModel.isEditMode.observe(viewLifecycleOwner, Observer { isEditMode ->
if (isEditMode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import umc.com.mobile.project.data.model.home.UpdateGradDateRequest
import umc.com.mobile.project.data.model.home.UpdateGradDateResponse
import umc.com.mobile.project.data.network.ApiClient
import umc.com.mobile.project.data.network.api.HomeApi
import java.text.SimpleDateFormat
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit
import java.util.Locale

class GradDateViewModel : ViewModel() {
val _selectedDate: MutableLiveData<String> = MutableLiveData()
Expand Down Expand Up @@ -55,10 +57,6 @@ class GradDateViewModel : ViewModel() {
val isEditMode: LiveData<Boolean>
get() = _isEditMode

init {
cheeringMessage.value = ""
}

fun init() {
_isEditMode.value = false
}
Expand Down Expand Up @@ -86,27 +84,24 @@ class GradDateViewModel : ViewModel() {
_isEditMode.value = !_isEditMode.value!!
}

//전의 메모랑 같으면 저장 안됨
private var previousMessage: String? = null
private var previousDDay: Int? = null

//버튼 활성화 조건
val isButtonEnabled = MediatorLiveData<Boolean>().apply {
//이전 메모랑 내용 같으면 저장 안됨
var previousMessage: String? = null
var previousDDay: String? = null

//cheeringMessage 값이 변경될 때마다 실행
val cheeringMessageObserver = Observer<String> { currentMessage ->
val dDayValue = _dday.value
value =
previousMessage != currentMessage && currentMessage.isNotEmpty() && dDayValue != null && dDayValue != 0
previousMessage != currentMessage && currentMessage.isNotEmpty()
previousMessage = currentMessage
}
//_dday 값이 변경될 때마다 실행
val dDayObserver = Observer<Int> { currentDDay ->
val messageValue = cheeringMessage.value
value =
previousDDay != currentDDay.toString() && currentDDay != null && currentDDay != 0 && messageValue != null && messageValue.isNotEmpty()
previousDDay != currentDDay.toString() && currentDDay != null && currentDDay != 0
previousDDay = currentDDay.toString()
}
addSource(cheeringMessage, cheeringMessageObserver)
Expand All @@ -124,8 +119,12 @@ class GradDateViewModel : ViewModel() {
if (dateInfoResponse != null) {
dateResponse.postValue(dateInfoResponse)//home에 있어서 놔둠
_dateResponse2.postValue(dateInfoResponse)
if (_dateResponse2.value != null && _dateResponse2.value!!.result != null) {
_selectedDate.value = _dateResponse2.value!!.result.gradDate
val originalFormat = SimpleDateFormat("yyyy-MM-dd", Locale.KOREA)
val targetFormat = SimpleDateFormat("yyyy-M월-d", Locale.KOREA)
if (dateInfoResponse.result.dday != null) {
val date = originalFormat.parse(dateInfoResponse.result.gradDate)
val formattedDate = targetFormat.format(date)
_selectedDateRequest.value = formattedDate
}
Log.d("gradDate", "${response.body()}")
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class CareerEditActivityFragment : Fragment() {
if (isDeleted) {
//삭제 작업이 완료되면 목록 업데이트
navigate(R.id.action_fragment_edit_activity_to_fragment_career_activity)
Toast.makeText(mContext, "삭제되었습니다.", Toast.LENGTH_SHORT).show()
}
})
}
Expand All @@ -84,6 +85,7 @@ class CareerEditActivityFragment : Fragment() {
if (isUpdated) {
//수정 작업이 완료되면 목록 업데이트
navigate(R.id.action_fragment_edit_activity_to_fragment_career_activity)
Toast.makeText(mContext, "수정되었습니다.", Toast.LENGTH_SHORT).show()
}
})
}
Expand All @@ -101,7 +103,7 @@ class CareerEditActivityFragment : Fragment() {
}
})
//버튼 활성화
viewModel.isFilledAllOptions.observe(viewLifecycleOwner) { isEnabled ->
viewModel.isFilledAnyOptions.observe(viewLifecycleOwner) { isEnabled ->
binding?.btnCareerEdit?.isEnabled = isEnabled
binding?.btnCareerEdit?.backgroundTintList =
ContextCompat.getColorStateList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.text.Editable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
Expand Down Expand Up @@ -64,6 +65,7 @@ class CareerEditCertificateFragment : Fragment() {
if (isDeleted) {
//삭제 작업이 완료되면 목록 업데이트
navigate(R.id.action_fragment_edit_certificate_to_fragment_career_certificate)
Toast.makeText(mContext, "삭제되었습니다.", Toast.LENGTH_SHORT).show()
}
})
}
Expand All @@ -74,6 +76,7 @@ class CareerEditCertificateFragment : Fragment() {
if (isUpdated) {
//수정 작업이 완료되면 목록 업데이트
navigate(R.id.action_fragment_edit_certificate_to_fragment_career_certificate)
Toast.makeText(mContext, "수정되었습니다.", Toast.LENGTH_SHORT).show()
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.text.Editable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
Expand Down Expand Up @@ -72,6 +73,7 @@ class CareerEditContestFragment : Fragment() {
if (isDeleted) {
//삭제 작업이 완료되면 목록 업데이트
navigate(R.id.action_fragment_edit_contest_to_fragment_career_contest)
Toast.makeText(mContext, "삭제되었습니다.", Toast.LENGTH_SHORT).show()
}
})
}
Expand All @@ -81,6 +83,7 @@ class CareerEditContestFragment : Fragment() {
if (isUpdated) {
//수정 작업이 완료되면 목록 업데이트
navigate(R.id.action_fragment_edit_contest_to_fragment_career_contest)
Toast.makeText(mContext, "수정되었습니다.", Toast.LENGTH_SHORT).show()
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.text.Editable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
Expand Down Expand Up @@ -63,6 +64,7 @@ class CareerEditVolunteerFragment : Fragment() {
if (isDeleted) {
//삭제 작업이 완료되면 목록 업데이트
navigate(R.id.action_fragment_edit_volunteer_to_fragment_career_volunteer)
Toast.makeText(mContext, "삭제되었습니다.", Toast.LENGTH_SHORT).show()
}
})
}
Expand All @@ -72,6 +74,7 @@ class CareerEditVolunteerFragment : Fragment() {
if (isUpdated) {
//수정 작업이 완료되면 목록 업데이트
navigate(R.id.action_fragment_edit_volunteer_to_fragment_career_volunteer)
Toast.makeText(mContext, "수정되었습니다.", Toast.LENGTH_SHORT).show()
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,42 +28,52 @@ class CareerAddActivityViewModel : ViewModel() {
val endDate: MutableLiveData<String> = MutableLiveData()
val fileAddedEvent: MutableLiveData<Boolean> = MutableLiveData()
val imageList: MutableList<MultipartBody.Part> = mutableListOf()
val totalFileSize = MutableLiveData<Long>(0)

init {
title.value = ""
startDate.value = ""
endDate.value = ""
imageList.clear()
totalFileSize.value = 0
}

fun init() {
title.value = ""
startDate.value = ""
endDate.value = ""
imageList.clear()
totalFileSize.value = 0
}

/* 버튼 활성화 기능 */
val isFilledAllOptions: LiveData<Boolean> = MediatorLiveData<Boolean>().apply {
addSource(title) { value = areBothFieldsFilled() }
addSource(startDate) { value = areBothFieldsFilled() }
addSource(endDate) { value = areBothFieldsFilled() }
addSource(totalFileSize) { value = areBothFieldsFilled() }
}

private fun areBothFieldsFilled(): Boolean {
return !(title.value.isNullOrEmpty() || title.value!!.contains(" ") || title.value!!.length > 20) && isDateValid(
startDate.value
) && isDateValid(endDate.value)
) && isDateValid(endDate.value) && isFileSizeValid()
}

private fun isDateValid(date: String?): Boolean {
return !date.isNullOrBlank() && date.length == 8
}

//파일 사이즈가 30MB를 넘지 않는지 체크
private fun isFileSizeValid(): Boolean {
return totalFileSize.value ?: 0 <= 30 * 1024 * 1024
}

fun addImageFile(file: File) {
val requestFile = RequestBody.create("image/*".toMediaTypeOrNull(), file)
val body = MultipartBody.Part.createFormData("image", file.name, requestFile)
imageList.add(body)
calculateTotalFileSize() //파일 사이즈 계산
fileAddedEvent.value = true
}

Expand Down Expand Up @@ -93,9 +103,20 @@ class CareerAddActivityViewModel : ViewModel() {
val filePart = MultipartBody.Part.createFormData("image", fileName, requestFile)

imageList.add(filePart)
calculateTotalFileSize() //파일 사이즈 계산
fileAddedEvent.value = true
}

//파일 사이즈 측정
fun calculateTotalFileSize() {
var totalSize: Long = 0
for (filePart in imageList) {
val file = filePart.body
totalSize += file?.contentLength() ?: 0
}
totalFileSize.value = totalSize
}

//API에 전송할 데이터를 포함하는 RequestDto 생성 함수
fun createRequestDto(): ActivityDto? {
val startDateString = startDate.value
Expand Down Expand Up @@ -135,18 +156,6 @@ class CareerAddActivityViewModel : ViewModel() {
val requestDtoPart: RequestBody =
requestDtoJson.toRequestBody("application/json".toMediaTypeOrNull())

//파일 사이즈 측정
fun calculateTotalFileSize(): Long {
var totalSize: Long = 0
for (filePart in imageList) {
val file = filePart.body
totalSize += file?.contentLength() ?: 0
}
return totalSize
}

val totalFileSize = calculateTotalFileSize()
Log.d("File Size", "Total File Size: $totalFileSize bytes")
careerApiService.addCareer(imageList, requestDtoPart)
.enqueue(object : Callback<AddCareerResponse> {
override fun onResponse(
Expand All @@ -158,7 +167,7 @@ class CareerAddActivityViewModel : ViewModel() {
if (addCareerResponse != null) {
_addedCareerInfo.postValue(addCareerResponse)
Log.d("addCareer:Extras 성공", "${response.body()}")
Log.d("addSuccessFileName", imageList.toString())
Log.d("totalFileSize", totalFileSize.value.toString())
} else {
_error.postValue("서버 응답이 올바르지 않습니다.")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ class CareerAddCertificateViewModel : ViewModel() {
/* 버튼 활성화 기능 */
val isFilledAllOptions: LiveData<Boolean> = MediatorLiveData<Boolean>().apply {
addSource(title) { value = areBothFieldsFilled() }
addSource(selectedCertificateType) { value = areBothFieldsFilled() }
addSource(startDate) { value = areBothFieldsFilled() }
addSource(endDate) { value = areBothFieldsFilled() }
}

private fun areBothFieldsFilled(): Boolean {
return !(title.value.isNullOrEmpty() || title.value!!.contains(" ") || title.value!!.length > 20) && isDateValid(
startDate.value
) && isDateValid(endDate.value)
return !(title.value.isNullOrEmpty() || title.value!!.contains(" ") || title.value!!.length > 20) && !selectedCertificateType.value.isNullOrEmpty()
&& isDateValid(startDate.value) && isDateValid(endDate.value)
}

private fun isDateValid(date: String?): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ class CareerAddContestViewModel : ViewModel() {
/* 버튼 활성화 기능 */
val isFilledAllOptions: LiveData<Boolean> = MediatorLiveData<Boolean>().apply {
addSource(title) { value = areBothFieldsFilled() }
addSource(selectedAward) { value = areBothFieldsFilled() }
addSource(startDate) { value = areBothFieldsFilled() }
addSource(endDate) { value = areBothFieldsFilled() }
}

private fun areBothFieldsFilled(): Boolean {
return !(title.value.isNullOrEmpty() || title.value!!.contains(" ") || title.value!!.length > 20) && isDateValid(
startDate.value
) && isDateValid(endDate.value)
return !(title.value.isNullOrEmpty() || title.value!!.contains(" ") || title.value!!.length > 20) && !selectedAward.value.isNullOrEmpty()
&& isDateValid(startDate.value) && isDateValid(endDate.value)
}

private fun isDateValid(date: String?): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ class CareerAddVolunteerViewModel : ViewModel() {
}

private fun areBothFieldsFilled(): Boolean {
return !(title.value.isNullOrEmpty() || title.value!!.contains(" ") || title.value!!.length > 20) && isDateValid(
startDate.value
) && isDateValid(endDate.value)
return !(title.value.isNullOrEmpty() || title.value!!.contains(" ") || title.value!!.length > 20) && !hour.value.isNullOrBlank()
&& isDateValid(startDate.value) && isDateValid(endDate.value)
}

private fun isDateValid(date: String?): Boolean {
Expand Down
Loading

0 comments on commit 2d85431

Please sign in to comment.