Skip to content

Commit

Permalink
Merge pull request #265 from TeamDMU/feat/update-days-food
Browse files Browse the repository at this point in the history
Feat : ์š”์ผ ๋ณ„ ์ผํ’ˆ ๋ฉ”๋‰ด ์—…๋ฐ์ดํŠธ
  • Loading branch information
m6z1 authored Sep 15, 2024
2 parents 57bcfba + f0430cd commit 38bcccb
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 27 deletions.
1 change: 0 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ android {
versionCode 11
versionName "2.0.1"

buildConfigField "String", "API_KEY", properties['API_KEY']
buildConfigField "String", "BASE_URL", properties['BASE_URL']
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,42 @@ import com.dongyang.android.youdongknowme.data.remote.service.CafeteriaService
import com.dongyang.android.youdongknowme.standard.network.ErrorResponseHandler
import com.dongyang.android.youdongknowme.standard.network.NetworkResult
import com.dongyang.android.youdongknowme.standard.network.RetrofitObject
import com.dongyang.android.youdongknowme.standard.util.Weekdays

class CafeteriaRepository(
private val errorResponseHandler: ErrorResponseHandler
) {
suspend fun fetchMenuList(): NetworkResult<List<Cafeteria>> {
return try {
val response = RetrofitObject.getNetwork().create(CafeteriaService::class.java).getMenuList()
val response =
RetrofitObject.getNetwork().create(CafeteriaService::class.java).getMenuList()
NetworkResult.Success(response)
} catch (exception: Exception) {
val error = errorResponseHandler.getError(exception)
NetworkResult.Error(error)
}
}

fun fetchDaysMenus(todayDay: Weekdays): List<DaysMenu> {
return DaysMenu.values().filter { menu ->
menu.operatingDays.contains(todayDay)
}
}

enum class DaysMenu(
val menuNameKr: String,
val price: Int,
val operatingDays: List<Weekdays>,
) {
PORKCUTLET("๋ˆ๊นŒ์Šค", 5_000, Weekdays.values().toList()),
CHEESE_PORKCUTLET("์น˜์ฆˆ๋ˆ๊นŒ์Šค", 5_500, Weekdays.values().toList()),
SWEET_POTATO_CHEESE_PORKCUTLET("๊ณ ๊ตฌ๋งˆ์น˜์ฆˆ๋ˆ๊นŒ์Šค", 6_000, Weekdays.values().toList()),
RAMEN("๋ผ๋ฉด", 3_500, Weekdays.values().toList()),
CHEESE_RAMEN("์น˜์ฆˆ๋ผ๋ฉด", 4_000, Weekdays.values().toList()),
SEAFOOD_RAMEN("ํ•ด๋ฌผ๋ผ๋ฉด", 4_500, Weekdays.values().toList()),
SPAM_KIMCHI_FRIED_RICE("์ŠคํŒธ๊น€์น˜๋ณถ์Œ๋ฐฅ", 4_900, listOf(Weekdays.MONDAY, Weekdays.TUESDAY)),
HOT_CHICKEN_MAYO_RICE("๋ถˆ๋‹ญ๋งˆ์š”๋ฎ๋ฐฅ", 4_500, listOf(Weekdays.WEDNESDAY, Weekdays.THURSDAY)),
CHICKEN_MAYO_RICE("์น˜ํ‚จ๋งˆ์š”๋ฎ๋ฐฅ", 4_500, listOf(Weekdays.WEDNESDAY, Weekdays.THURSDAY)),
OMELET_RICE("์˜ค๋ฏ€๋ผ์ด์Šค", 5_500, listOf(Weekdays.FRIDAY));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object RetrofitObject {
val request = chain.request().newBuilder()
val originalHttpUrl = chain.request().url

val url = originalHttpUrl.newBuilder().addQueryParameter("api_key", BuildConfig.API_KEY)
val url = originalHttpUrl.newBuilder()
.build()
request.url(url)
chain.proceed(request.build())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.dongyang.android.youdongknowme.standard.util

import java.time.DayOfWeek

enum class Weekdays {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY;

companion object {

fun from(dayOfWeek: DayOfWeek): Weekdays {
return when (dayOfWeek) {
DayOfWeek.MONDAY -> MONDAY
DayOfWeek.TUESDAY -> TUESDAY
DayOfWeek.WEDNESDAY -> WEDNESDAY
DayOfWeek.THURSDAY -> THURSDAY
DayOfWeek.FRIDAY -> FRIDAY
else -> throw IllegalArgumentException("์›”์š”์ผ - ๊ธˆ์š”์ผ์„ ๋ฒ—์–ด๋‚ฌ์Šต๋‹ˆ๋‹ค.")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.annotation.SuppressLint
import android.util.TypedValue
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 @@ -17,12 +16,11 @@ import com.kizitonwose.calendarview.model.CalendarDay
import com.kizitonwose.calendarview.ui.DayBinder
import com.kizitonwose.calendarview.utils.Size
import org.koin.androidx.viewmodel.ext.android.viewModel
import java.time.DayOfWeek
import java.time.DayOfWeek.*
import java.time.LocalDate
import java.time.YearMonth
import java.time.temporal.TemporalAdjusters


class CafeteriaFragment : BaseFragment<FragmentCafeteriaBinding, CafeteriaViewModel>(),
CalendarInterface {

Expand Down Expand Up @@ -60,9 +58,9 @@ class CafeteriaFragment : BaseFragment<FragmentCafeteriaBinding, CafeteriaViewMo
binding.cvCafeteriaCalendar.apply {
val dayWidth = wmc.bounds.width() / 5
val dayHeight: Int = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
124f,
resources.displayMetrics
TypedValue.COMPLEX_UNIT_DIP,
124f,
resources.displayMetrics
).toInt()

daySize = Size(dayWidth, dayHeight)
Expand All @@ -74,10 +72,11 @@ class CafeteriaFragment : BaseFragment<FragmentCafeteriaBinding, CafeteriaViewMo

override fun bind(container: CafeteriaContainer, day: CalendarDay) = container.bind(day)
}

viewModel.updateDaysMenu(findNearestMonday(LocalDate.now()))
}

override fun initDataBinding() {

viewModel.isLoading.observe(viewLifecycleOwner) {
if (it) showLoading()
else dismissLoading()
Expand All @@ -87,10 +86,12 @@ class CafeteriaFragment : BaseFragment<FragmentCafeteriaBinding, CafeteriaViewMo
showToast(getString(resId))
})

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

viewModel.daysMenus.observe(viewLifecycleOwner) {
anotherMenuAdapter.submitList(it)
}
}

Expand All @@ -101,13 +102,14 @@ class CafeteriaFragment : BaseFragment<FragmentCafeteriaBinding, CafeteriaViewMo
binding.cvCafeteriaCalendar.setup(
YearMonth.from(nearestMonday),
YearMonth.from(nearestMonday.plusDays(4)),
DayOfWeek.MONDAY
MONDAY
)

binding.cvCafeteriaCalendar.scrollToDate(nearestMonday)

binding.cafeteriaErrorContainer.refresh.setOnClickListener {
viewModel.fetchCafeteria()
viewModel.updateDaysMenu(findNearestMonday(LocalDate.now()))
}

binding.cvCafeteriaCalendar.setOnTouchListener { _, event ->
Expand All @@ -123,27 +125,27 @@ class CafeteriaFragment : BaseFragment<FragmentCafeteriaBinding, CafeteriaViewMo

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

DayOfWeek.MONDAY -> {
MONDAY -> {
currentDate
}

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

override fun onPause() {
super.onPause()
notifyDateChanged(
viewModel,
binding.cvCafeteriaCalendar,
viewModel.selectedDate.value,
LocalDate.now()
viewModel = viewModel,
calendarView = binding.cvCafeteriaCalendar,
oldDate = viewModel.selectedDate.value,
selectedDate = findNearestMonday(LocalDate.now())
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import com.dongyang.android.youdongknowme.data.remote.entity.Cafeteria
import com.dongyang.android.youdongknowme.data.repository.CafeteriaRepository
import com.dongyang.android.youdongknowme.standard.base.BaseViewModel
import com.dongyang.android.youdongknowme.standard.network.NetworkResult
import com.dongyang.android.youdongknowme.standard.util.Weekdays
import com.dongyang.android.youdongknowme.ui.view.util.Event
import com.dongyang.android.youdongknowme.ui.view.util.ResourceProvider
import kotlinx.coroutines.launch
import java.text.DecimalFormat
import java.time.LocalDate

class CafeteriaViewModel(
Expand All @@ -33,8 +35,11 @@ class CafeteriaViewModel(
private val _cafeteriaList: MutableLiveData<List<Cafeteria>> = MutableLiveData()
val cafeteriaList: LiveData<List<Cafeteria>> = _cafeteriaList

private val _menus: MutableLiveData<List<String>> = MutableLiveData()
val menus: LiveData<List<String>> = _menus
private val _koreaMenus: MutableLiveData<List<String>> = MutableLiveData()
val koreaMenus: LiveData<List<String>> = _koreaMenus

private val _daysMenus: MutableLiveData<List<String>> = MutableLiveData()
val daysMenus: LiveData<List<String>> = _daysMenus

private val emptyMenu = listOf(resourceProvider.getString(R.string.cafeteria_no_menu))

Expand Down Expand Up @@ -68,12 +73,27 @@ class CafeteriaViewModel(
val cafeteriaList = _cafeteriaList.value ?: emptyList()
_selectedDate.value = selectedDate
val selectedMenu = cafeteriaList.find { it.date == selectedDate.toString() }?.menus
_menus.postValue(
_koreaMenus.postValue(
if (selectedMenu.isNullOrEmpty()) {
emptyMenu
} else {
selectedMenu
}
)
}

fun updateDaysMenu(selectedDate: LocalDate) {
viewModelScope.launch {
val dateToWeekday: Weekdays = Weekdays.from(selectedDate.dayOfWeek)
runCatching {
cafeteriaRepository.fetchDaysMenus(dateToWeekday)
}.onSuccess { daysMenus ->
val formatter = DecimalFormat("#,###")
val formattedMenuWithPrice = daysMenus.map { "${it.menuNameKr} ${formatter.format(it.price)}์›" }
_daysMenus.value = formattedMenuWithPrice
}.onFailure {
_isError.value = true
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface CalendarInterface {
selectedDate: LocalDate
) {
viewModel.updateMenuList(selectedDate)
viewModel.updateDaysMenu(selectedDate)
calendarView.notifyDateChanged(selectedDate)
oldDate?.let { calendarView.notifyDateChanged(it) }
}
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,11 @@
<string name="cafeteria_student">ํ•™์ƒ์‹๋‹น</string>
<string name="cafeteria_menu">๋ฉ”๋‰ด</string>
<string name="cafeteria_lunch">์ ์‹ฌ</string>
<string name="cafeteria_time">11:30 - 14:00, 16:30 - 18:00</string>
<string name="cafeteria_time">11:00 - 14:00, 16:30 - 18:00</string>
<string name="cafeteria_place">8ํ˜ธ๊ด€ 3์ธต</string>
<string name="cafeteria_no_menu">&#128517; ๋“ฑ๋ก๋œ ๋ฉ”๋‰ด๊ฐ€ ์—†์–ด์š”.</string>
<string name="cafeteria_korean">๐Ÿš ํ•œ์‹</string>
<string name="cafeteria_another">๐Ÿ› ์ผํ’ˆ</string>
<string name="cafeteria_another_list">๋ผ๋ฉด 3,500์› / ์น˜์ฆˆ๋ผ๋ฉด 4,000์› / ํ•ด๋ฌผ๋ผ๋ฉด 4,500์› / ๋ˆ๊นŒ์Šค 5,500์› / ์น˜์ฆˆ๋ˆ๊นŒ์Šค 5,500์› / ๊ณ ๊ตฌ๋งˆ์น˜์ฆˆ๋ˆ๊นŒ์Šค 6,000์› / ์ŠคํŒธ๊น€์น˜๋ณถ์Œ๋ฐฅ 4,900์› / ์น˜ํ‚จ๋งˆ์š”๋ฎ๋ฐฅ 4,500์› / ๋ถˆ๋‹ญ๋งˆ์š”๋ฎ๋ฐฅ 4,500์› / ์˜ค๋ฏ€๋ผ์ด์Šค 5,500์›</string>

<!-- license -->
<string name="license_title">์˜คํ”ˆ์†Œ์Šค ๋ผ์ด์„ผ์Šค</string>
Expand Down

0 comments on commit 38bcccb

Please sign in to comment.