Skip to content

Commit

Permalink
사진찍은 날짜 관련 QA
Browse files Browse the repository at this point in the history
  • Loading branch information
l2hyunwoo committed Jul 6, 2023
1 parent 0f3c29b commit cefa77c
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 12 deletions.
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)
}
}
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)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.teampophory.pophory.common.time

import android.content.Context
import android.text.format.DateUtils
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import kotlinx.datetime.LocalDate
Expand All @@ -8,4 +10,21 @@ import kotlinx.datetime.toLocalDateTime

fun Instant.Companion.systemNow(): Instant = Clock.System.now()

fun Instant.toDefaultLocalDate(): LocalDate = toLocalDateTime(TimeZone.currentSystemDefault()).date
fun Instant.toDefaultLocalDate(): LocalDate = toLocalDateTime(TimeZone.currentSystemDefault()).date

fun Long.formatDate(context: Context): String = DateUtils.formatDateTime(
context,
this,
DateUtils.FORMAT_SHOW_YEAR or DateUtils.FORMAT_SHOW_DATE
)

fun Instant.formatDate(context: Context): String = toEpochMilliseconds().formatDate(context)

fun Long.formatNumericDate(context: Context): String = DateUtils.formatDateTime(
context,
this,
DateUtils.FORMAT_SHOW_YEAR or DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_NUMERIC_DATE
)

fun Instant.formatNumericDate(context: Context): String =
toEpochMilliseconds().formatNumericDate(context)

0 comments on commit cefa77c

Please sign in to comment.