Skip to content

Commit

Permalink
Merge pull request #201 from TeamDMU/refactor/cafeteria
Browse files Browse the repository at this point in the history
Refactor : ์‹๋‹จ ํ™”๋ฉด QA ์ ์šฉ ๋ฐ ์ˆ˜์ •
  • Loading branch information
huiwoo-jo authored Mar 26, 2024
2 parents af8c2fc + 6976a2e commit 2ceecc6
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.dongyang.android.youdongknowme.ui.view.cafeteria

import android.annotation.SuppressLint
import android.view.MotionEvent
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.window.layout.WindowMetricsCalculator
import com.dongyang.android.youdongknowme.R
import com.dongyang.android.youdongknowme.databinding.FragmentCafeteriaBinding
Expand All @@ -16,6 +19,7 @@ import org.koin.androidx.viewmodel.ext.android.viewModel
import java.time.DayOfWeek
import java.time.LocalDate
import java.time.YearMonth
import java.time.temporal.TemporalAdjusters


class CafeteriaFragment : BaseFragment<FragmentCafeteriaBinding, CafeteriaViewModel>(),
Expand All @@ -24,27 +28,27 @@ class CafeteriaFragment : BaseFragment<FragmentCafeteriaBinding, CafeteriaViewMo
override val layoutResourceId: Int = R.layout.fragment_cafeteria
override val viewModel: CafeteriaViewModel by viewModel()

private lateinit var stuKoreanMenuAdapter: CafeteriaAdapter
private lateinit var stuAnotherMenuAdapter: CafeteriaAdapter
private lateinit var koreanMenuAdapter: CafeteriaAdapter
private lateinit var anotherMenuAdapter: CafeteriaAdapter

override fun initStartView() {
binding.vm = viewModel

stuKoreanMenuAdapter = CafeteriaAdapter()
stuAnotherMenuAdapter = CafeteriaAdapter()
koreanMenuAdapter = CafeteriaAdapter()
anotherMenuAdapter = CafeteriaAdapter()

binding.rvCafeteriaMenuList.apply {
val layoutManager = FlexboxLayoutManager(context)
layoutManager.flexDirection = FlexDirection.ROW
this.adapter = this@CafeteriaFragment.stuKoreanMenuAdapter
this.adapter = this@CafeteriaFragment.koreanMenuAdapter
this.layoutManager = layoutManager
this.setHasFixedSize(true)
}

binding.rvCafeteriaAnotherMenuList.apply {
val layoutManager = FlexboxLayoutManager(context)
layoutManager.flexDirection = FlexDirection.ROW
this.adapter = this@CafeteriaFragment.stuAnotherMenuAdapter
this.adapter = this@CafeteriaFragment.anotherMenuAdapter
this.layoutManager = layoutManager
this.setHasFixedSize(true)
}
Expand Down Expand Up @@ -79,25 +83,54 @@ class CafeteriaFragment : BaseFragment<FragmentCafeteriaBinding, CafeteriaViewMo
})

viewModel.menus.observe(viewLifecycleOwner) {
stuKoreanMenuAdapter.submitList(it)
koreanMenuAdapter.submitList(it)
// ์ผํ’ˆ ๋ฉ”๋‰ด : ์ผํ’ˆ ๋ฉ”๋‰ด๋Š” ๋ฆฌ์ŠคํŠธ๋กœ ์ œ์ž‘ํ•˜์—ฌ ๋“ฑ๋ก
stuAnotherMenuAdapter.submitList(listOf(getString(R.string.cafeteria_no_menu)))
anotherMenuAdapter.submitList(listOf(getString(R.string.cafeteria_no_menu)))
}

}

@SuppressLint("ClickableViewAccessibility")
override fun initAfterBinding() {
val nearestMonday = findNearestMonday(LocalDate.now())

binding.cvCafeteriaCalendar.setup(
YearMonth.now().minusMonths(2),
YearMonth.now().plusMonths(1),
DayOfWeek.values().random()
YearMonth.from(nearestMonday),
YearMonth.from(nearestMonday.plusDays(4)),
DayOfWeek.MONDAY
)

binding.cvCafeteriaCalendar.scrollToDate(LocalDate.now().minusDays(2))
binding.cvCafeteriaCalendar.scrollToDate(nearestMonday)

binding.cafeteriaErrorContainer.refresh.setOnClickListener {
viewModel.fetchCafeteria()
}

binding.cvCafeteriaCalendar.setOnTouchListener { _, event ->
when (event.actionMasked) {
MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE -> {
true
}

else -> false
}
}
}

private fun findNearestMonday(currentDate: LocalDate): LocalDate {
return when (currentDate.dayOfWeek) {
DayOfWeek.SATURDAY, DayOfWeek.SUNDAY -> {
currentDate.with(TemporalAdjusters.next(DayOfWeek.MONDAY))
}

DayOfWeek.MONDAY -> {
currentDate
}

else -> {
currentDate.with(TemporalAdjusters.previous(DayOfWeek.MONDAY))
}
}
}

override fun onPause() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class CafeteriaViewModel(
val menuList = result.data
_cafeteriaList.value = menuList
_selectedDate.value = LocalDate.now()
updateMenuList(selectedDate.toString())
selectedDate.value?.let { updateMenuList(it) }
_isError.postValue(false)
_isLoading.postValue(false)
}
Expand All @@ -64,8 +64,11 @@ class CafeteriaViewModel(
}
}

fun updateMenuList(selectedDate: String) {
fun updateMenuList(selectedDate: LocalDate) {
val cafeteriaList = _cafeteriaList.value ?: emptyList()
_menus.postValue(cafeteriaList.find { it.date == selectedDate }?.menus ?: emptyMenu)
_selectedDate.value = selectedDate
_menus.postValue(
cafeteriaList.find { it.date == selectedDate.toString() }?.menus ?: emptyMenu
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface CalendarInterface {
oldDate: LocalDate?,
selectedDate: LocalDate
) {
viewModel.updateMenuList(selectedDate.toString())
viewModel.updateMenuList(selectedDate)
calendarView.notifyDateChanged(selectedDate)
oldDate?.let { calendarView.notifyDateChanged(it) }
}
Expand Down
54 changes: 36 additions & 18 deletions app/src/main/res/layout/fragment_cafeteria.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
android:id="@+id/cv_cafeteria_calendar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:scrollbars="none"
app:cv_dayViewResource="@layout/item_calendar_day"
app:cv_hasBoundaries="false"
app:cv_inDateStyle="none"
Expand All @@ -51,7 +52,7 @@
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:paddingVertical="16dp"
android:paddingVertical="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/cv_cafeteria_calendar">
Expand All @@ -63,27 +64,27 @@

<TextView
android:id="@+id/tv_cafeteria_info_place"
style="@style/PretendardRegular14"
style="@style/PretendardMedium14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="4dp"
android:layout_marginStart="12dp"
android:text="@string/cafeteria_place"
android:textColor="@color/line" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginStart="16dp"
android:src="@drawable/ic_timer" />

<TextView
android:id="@+id/tv_cafeteria_info_time"
style="@style/PretendardRegular14"
style="@style/PretendardMedium14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="4dp"
android:layout_marginStart="12dp"
android:text="@string/cafeteria_time"
android:textColor="@color/line"
app:layout_constraintEnd_toEndOf="parent" />
Expand All @@ -107,6 +108,21 @@
android:layout_gravity="center" />
</LinearLayout>

<com.google.android.material.chip.Chip
android:id="@+id/chip_weekend_cafeteria"
style="@style/Colors_Widget.MaterialComponents.Chip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:backgroundTint="@color/blue100"
android:clickable="false"
android:text="@string/cafetria_weekend"
android:textAlignment="center"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/cafeteria_info_container" />

<com.google.android.material.card.MaterialCardView
android:id="@+id/mv_cafeteria_menu"
bind_is_error="@{vm.cafeteriaList == null}"
Expand All @@ -115,20 +131,21 @@
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="12dp"
android:outlineSpotShadowColor="@color/blue300"
app:cardCornerRadius="16dp"
app:cardElevation="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/cafeteria_info_container">
app:layout_constraintTop_toBottomOf="@id/cafeteria_info_container"
app:strokeColor="@color/blue300"
app:strokeWidth="2dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:orientation="vertical"
android:padding="8dp" >

<TextView
style="@style/PretendardBold14"
style="@style/PretendardBold20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
Expand All @@ -140,8 +157,8 @@
android:id="@+id/rv_cafeteria_menu_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginVertical="8dp"
android:layout_marginStart="8dp"
tools:listitem="@layout/item_cafeteria_menu" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
Expand All @@ -154,20 +171,21 @@
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="20dp"
android:outlineSpotShadowColor="@color/blue300"
app:cardCornerRadius="16dp"
app:cardElevation="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/mv_cafeteria_menu">
app:layout_constraintTop_toBottomOf="@id/mv_cafeteria_menu"
app:strokeColor="@color/blue300"
app:strokeWidth="2dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:orientation="vertical"
android:padding="8dp" >

<TextView
style="@style/PretendardBold14"
style="@style/PretendardBold20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
Expand All @@ -179,8 +197,8 @@
android:id="@+id/rv_cafeteria_another_menu_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginVertical="8dp"
android:layout_marginStart="8dp"
tools:listitem="@layout/item_cafeteria_menu" />

</LinearLayout>
Expand Down
11 changes: 5 additions & 6 deletions app/src/main/res/layout/item_calendar_day.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<androidx.constraintlayout.widget.ConstraintLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground"
tools:background="@color/white">

<LinearLayout
Expand All @@ -21,7 +20,7 @@

<TextView
android:id="@+id/item_calendar_month"
style="@style/PretendardRegular12"
style="@style/PretendardMedium14"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
Expand All @@ -34,14 +33,14 @@
android:layout_width="32dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="8dp"
android:layout_marginTop="12dp"
android:gravity="center"
app:cardElevation="0dp"
app:cardCornerRadius="10dp">

<TextView
android:id="@+id/item_calendar_date"
style="@style/PretendardRegular16"
style="@style/PretendardSemiBold16"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
Expand All @@ -52,11 +51,11 @@

<TextView
android:id="@+id/item_calendar_day"
style="@style/PretendardRegular12"
style="@style/PretendardMedium14"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="8dp"
android:layout_marginTop="12dp"
android:gravity="center"
android:textColor="@color/black"
tools:text="Mon" />
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</style>

<style name="Colors_Widget.MaterialComponents.Chip" parent="Widget.MaterialComponents.Chip.Choice">
<item name="android:textAppearance">@style/PretendardRegular14</item>
<item name="android:textAppearance">@style/PretendardMedium16</item>
<item name="android:textAlignment">center</item>
<item name="android:textColor">@color/black</item>
</style>
Expand Down

0 comments on commit 2ceecc6

Please sign in to comment.