-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
130 additions
and
12 deletions.
There are no files selected for viewing
78 changes: 67 additions & 11 deletions
78
app/src/main/java/com/teampophory/pophory/feature/home/photo/AddPhotoActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,87 @@ | ||
package com.teampophory.pophory.feature.home.photo | ||
|
||
import android.os.Bundle | ||
import androidx.activity.viewModels | ||
import androidx.lifecycle.flowWithLifecycle | ||
import androidx.lifecycle.lifecycleScope | ||
import com.google.android.material.datepicker.CalendarConstraints | ||
import com.google.android.material.datepicker.DateValidatorPointBackward | ||
import com.google.android.material.datepicker.MaterialDatePicker | ||
import com.teampophory.pophory.R | ||
import com.teampophory.pophory.common.activity.BindingActivity | ||
import com.teampophory.pophory.common.time.systemNow | ||
import com.teampophory.pophory.databinding.ActivityAddPhotoBinding | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.onEach | ||
import kotlinx.datetime.Instant | ||
import java.text.SimpleDateFormat | ||
import java.util.Date | ||
import java.util.Locale | ||
import java.util.TimeZone | ||
|
||
@AndroidEntryPoint | ||
class AddPhotoActivity : BindingActivity<ActivityAddPhotoBinding>(R.layout.activity_add_photo) { | ||
private val viewModel: AddPhotoViewModel by viewModels() | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
initView() | ||
subscribeEvent() | ||
collectState() | ||
} | ||
|
||
private fun initView() { | ||
binding.layoutDate.setOnClickListener { | ||
MaterialDatePicker.Builder | ||
.datePicker() | ||
.setTheme(R.style.ThemeOverlay_Pophory_MaterialCalendar) | ||
.setInputMode(MaterialDatePicker.INPUT_MODE_CALENDAR) | ||
.setCalendarConstraints( | ||
CalendarConstraints.Builder() | ||
.setEnd(Instant.systemNow().toEpochMilliseconds()) | ||
.build() | ||
) | ||
.build() | ||
.show(supportFragmentManager, "datePicker") | ||
viewModel.onCreatedAtPressed() | ||
} | ||
binding.layoutStudio.setOnClickListener { | ||
viewModel.onStudioPressed() | ||
} | ||
} | ||
|
||
private fun subscribeEvent() { | ||
viewModel.event | ||
.flowWithLifecycle(lifecycle) | ||
.onEach { | ||
when (it) { | ||
AddPhotoEvent.DATE -> { | ||
val currentCreatedAt = viewModel.createdAt.value | ||
val picker = MaterialDatePicker.Builder | ||
.datePicker() | ||
.setTheme(R.style.ThemeOverlay_Pophory_MaterialCalendar) | ||
.setInputMode(MaterialDatePicker.INPUT_MODE_CALENDAR) | ||
.setCalendarConstraints( | ||
CalendarConstraints.Builder() | ||
.setValidator( | ||
DateValidatorPointBackward.now() | ||
) | ||
.setEnd(Instant.systemNow().toEpochMilliseconds()).build() | ||
) | ||
.setSelection( | ||
currentCreatedAt + TimeZone.getDefault().getOffset(currentCreatedAt) | ||
) | ||
.build() | ||
picker.show(supportFragmentManager, "datePicker") | ||
picker.addOnPositiveButtonClickListener(viewModel::onUpdateCreateAt) | ||
} | ||
|
||
AddPhotoEvent.STUDIO -> { | ||
// TODO BottomSheet 회의 | ||
} | ||
} | ||
}.launchIn(lifecycleScope) | ||
} | ||
|
||
private fun collectState() { | ||
viewModel.createdAt | ||
.flowWithLifecycle(lifecycle) | ||
.onEach { | ||
binding.txtDate.text = | ||
SimpleDateFormat( | ||
"yyyy.MM.dd(EEEEE)", | ||
Locale.getDefault() | ||
).format(Date(it)) | ||
}.launchIn(lifecycleScope) | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
app/src/main/java/com/teampophory/pophory/feature/home/photo/AddPhotoViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.teampophory.pophory.feature.home.photo | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.teampophory.pophory.common.time.systemNow | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asSharedFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.launch | ||
import kotlinx.datetime.Instant | ||
import javax.inject.Inject | ||
|
||
enum class AddPhotoEvent { | ||
DATE, | ||
STUDIO; | ||
} | ||
|
||
@HiltViewModel | ||
class AddPhotoViewModel @Inject constructor() : ViewModel() { | ||
private val _createdAt = MutableStateFlow(Instant.systemNow().toEpochMilliseconds()) | ||
val createdAt = _createdAt.asStateFlow() | ||
private var studio: String? = null | ||
private val _event = MutableSharedFlow<AddPhotoEvent>() | ||
val event = _event.asSharedFlow() | ||
|
||
fun onCreatedAtPressed() { | ||
viewModelScope.launch { | ||
_event.emit(AddPhotoEvent.DATE) | ||
} | ||
} | ||
|
||
fun onUpdateCreateAt(new: Long) { | ||
_createdAt.value = new | ||
} | ||
|
||
fun onStudioPressed() { | ||
viewModelScope.launch { | ||
_event.emit(AddPhotoEvent.STUDIO) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters