Skip to content

Commit

Permalink
feat: 여행 수정 view 연결
Browse files Browse the repository at this point in the history
  • Loading branch information
hxeyexn committed Jul 25, 2024
1 parent 4e36d02 commit a2c4410
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,57 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.viewModels
import com.google.android.material.datepicker.MaterialDatePicker
import com.woowacourse.staccato.R
import com.woowacourse.staccato.databinding.ActivityTravelUpdateBinding
import com.woowacourse.staccato.presentation.base.BindingActivity
import com.woowacourse.staccato.presentation.travelcreation.TravelCreationHandler
import com.woowacourse.staccato.presentation.travelupdate.viewmodel.TravelUpdateViewModel
import com.woowacourse.staccato.presentation.travelupdate.viewmodel.TravelUpdateViewModelFactory

class TravelUpdateActivity : BindingActivity<ActivityTravelUpdateBinding>() {
class TravelUpdateActivity : BindingActivity<com.woowacourse.staccato.databinding.ActivityTravelUpdateBinding>(), TravelCreationHandler {
override val layoutResourceId = R.layout.activity_travel_update
private val viewModel: TravelUpdateViewModel by viewModels { TravelUpdateViewModelFactory() }

override fun initStartView(savedInstanceState: Bundle?) {
binding.btnTravelUpdateDone.setOnClickListener {
val resultIntent = Intent()
setResult(Activity.RESULT_OK, resultIntent)
initBinding()
navigateToTravel()
}

override fun onPeriodSelectionClicked() {
val dateRangePicker =
MaterialDatePicker.Builder.dateRangePicker()
.setTheme(R.style.DatePickerStyle)
.setSelection(
androidx.core.util.Pair(
MaterialDatePicker.thisMonthInUtcMilliseconds(),
MaterialDatePicker.todayInUtcMilliseconds(),
),
).build()

dateRangePicker.show(supportFragmentManager, dateRangePicker.toString())

dateRangePicker.addOnPositiveButtonClickListener { selection ->
val startDate: Long = selection.first
val endDate: Long = selection.second
viewModel.setTravelPeriod(startDate, endDate)
}
}

override fun onSaveClicked() {
val resultIntent = Intent()
setResult(Activity.RESULT_OK, resultIntent)
finish()
}

private fun initBinding() {
binding.lifecycleOwner = this
binding.viewModel = viewModel
binding.handler = this
}

private fun navigateToTravel() {
binding.toolbarTravelUpdate.setNavigationOnClickListener {
finish()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.woowacourse.staccato.presentation.travelupdate.viewmodel

import androidx.databinding.ObservableField
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.woowacourse.staccato.presentation.travelcreation.DateConverter.convertLongToLocalDate
import java.time.LocalDate

class TravelUpdateViewModel : ViewModel() {
private val _imageUrl = MutableLiveData<String>()
val imageUrl: LiveData<String> get() = _imageUrl

val title = ObservableField<String>()
val description = ObservableField<String>()

private val _startDate = MutableLiveData<LocalDate>(null)
val startDate: LiveData<LocalDate> get() = _startDate

private val _endDate = MutableLiveData<LocalDate>(null)
val endDate: LiveData<LocalDate> get() = _endDate

fun setTravelPeriod(
startAt: Long,
endAt: Long,
) {
_startDate.value = convertLongToLocalDate(startAt)
_endDate.value = convertLongToLocalDate(endAt)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.woowacourse.staccato.presentation.travelupdate.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider

class TravelUpdateViewModelFactory : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(TravelUpdateViewModel::class.java)) {
return TravelUpdateViewModel() as T
}
throw IllegalArgumentException()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@

<data>

<variable
name="viewModel"
type="com.woowacourse.staccato.presentation.travelupdate.viewmodel.TravelUpdateViewModel" />

<variable
name="handler"
type="com.woowacourse.staccato.presentation.travelcreation.TravelCreationHandler" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
Expand Down Expand Up @@ -69,6 +76,7 @@
android:layout_marginTop="8dp"
android:hint="@string/travel_creation_title_hint"
android:maxLength="30"
android:text="@={viewModel.title}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_travel_update_title" />
Expand All @@ -91,6 +99,7 @@
android:hint="@string/travel_creation_description_hint"
android:maxLength="500"
android:minHeight="255dp"
android:text="@={viewModel.description}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_travel_update_description" />
Expand All @@ -113,24 +122,30 @@
android:background="@drawable/shape_all_gray1_8dp"
android:clickable="true"
android:focusable="true"
android:onClick="@{() -> handler.onPeriodSelectionClicked()}"
android:paddingHorizontal="16dp"
android:paddingVertical="12dp"
android:text="@string/travel_creation_period_hint"
android:textAppearance="@style/Typography.Body1"
android:textColor="@color/gray3"
app:endDate="@{viewModel.endDate}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_travel_update_period_title" />
app:layout_constraintTop_toBottomOf="@id/tv_travel_update_period_title"
app:startDate="@{viewModel.startDate}" />

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn_travel_update_save"
style="@style/ButtonStyle.Save.Active"
android:layout_marginHorizontal="16dp"
android:layout_marginVertical="24dp"
android:onClick="@{() -> handler.onSaveClicked()}"
app:endDate="@{viewModel.endDate}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_travel_update_period_description" />
app:layout_constraintTop_toBottomOf="@id/tv_travel_update_period_description"
app:startDate="@{viewModel.startDate}"
app:travelTitle="@{viewModel.title}" />

</androidx.constraintlayout.widget.ConstraintLayout>

Expand Down

0 comments on commit a2c4410

Please sign in to comment.