Skip to content

Commit

Permalink
[fix] Calendar 랜더링 순서 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
reezung committed Feb 26, 2024
1 parent fa79fdb commit c0bbe8b
Show file tree
Hide file tree
Showing 17 changed files with 206 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package com.example.sungdongwalk.activities

import android.Manifest
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
Expand All @@ -26,14 +28,14 @@ class MainActivity : ComponentActivity() {
private val LOCATION_REQUEST_CODE = 10001
private lateinit var fusedLocationProviderClient: FusedLocationProviderClient
private lateinit var locationManager: LocationManager
@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
locationSource = FusedLocationSource(this, LOCATION_REQUEST_CODE)
locationManager = LocationManager(fusedLocationProviderClient)


lifecycleScope.launch {
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED){
launch {
Expand All @@ -54,24 +56,6 @@ class MainActivity : ComponentActivity() {
setContent {
SungdongWalkTheme {
Nav()
// ModalDdg(
// modalText = "필수 권한을 허용해주세요",
// confirmText = "허용하기",
// cancelText = "나가기",
// confirmOnClick = {
// ActivityCompat.requestPermissions(this,
// arrayOf(
// Manifest.permission.ACCESS_FINE_LOCATION,
// Manifest.permission.ACCESS_COARSE_LOCATION,
// Manifest.permission.INTERNET,
// Manifest.permission.READ_EXTERNAL_STORAGE
// ),
// LOCATION_REQUEST_CODE
// )
// },
// cancelOnClick = {finishAffinity()},
// resourceId = R.drawable.ddg_with_map
// )
}
}
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/example/sungdongwalk/api/Dto.kt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class Dto {
val placeId: Int,
val placeName: String,
val name: String,
val placeImage: String,
val endDate: String,
val url: String
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.example.sungdongwalk.api.retrofit

import com.example.sungdongwalk.api.Dto.*
import com.example.sungdongwalk.api.utils.API.EVENTS
import com.example.sungdongwalk.api.utils.API.EVENTS_CALENDER
import com.example.sungdongwalk.api.utils.API.EVENTS_CALENDAR
import com.example.sungdongwalk.api.utils.API.MISSIONS
import com.example.sungdongwalk.api.utils.API.MISSIONS_ID
import com.example.sungdongwalk.api.utils.API.PLACES
Expand Down Expand Up @@ -110,8 +110,8 @@ interface IRetrofit {
@Query("category") category: String? = null,
@Query("placeId") placeId: Int? = null,
): Response<EventListResponseDTO>
@GET(EVENTS_CALENDER)
suspend fun getEventsCalender(
@GET(EVENTS_CALENDAR)
suspend fun getEventsCalendar(
@Query("startDate") startDate: String,
@Query("endDate") endDate: String,
): Response<CalenderResponseDTO>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.sungdongwalk.api.retrofit

import com.example.sungdongwalk.api.utils.User.TOKEN
import com.example.sungdongwalk.viewmodels.LoginViewModel
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Response
Expand Down Expand Up @@ -37,7 +37,7 @@ object RetrofitClient {
@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain) : Response = with(chain) {
val newRequest = request().newBuilder()
.addHeader("Authorization", "Bearer ${TOKEN}")
.addHeader("Authorization", "Bearer ${LoginViewModel.instance.token.value}")
.build()
proceed(newRequest)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package com.example.sungdongwalk.api.retrofit


import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi
import com.example.sungdongwalk.api.Dto
import com.example.sungdongwalk.api.utils.API.BASE_URL
import com.example.sungdongwalk.viewmodels.EventViewModel
import com.example.sungdongwalk.viewmodels.LoginViewModel
import com.example.sungdongwalk.viewmodels.PlaceViewModel
import com.example.sungdongwalk.viewmodels.WalkViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import java.time.LocalDate
import java.time.format.DateTimeFormatter

class RetrofitManager {

Expand All @@ -22,6 +26,31 @@ class RetrofitManager {
// 레트로핏 인터페이스 가져오기
private val iRetrofit : IRetrofit? = RetrofitClient.getClient(BASE_URL)?.create(IRetrofit::class.java)

fun postUserLogin(
userName: String,
password: String,
) = CoroutineScope(IO).launch {
try{
val request = CoroutineScope(IO).async { iRetrofit?.postUserLogin(
Dto.TokenRequestDTO(
userName,
password
)
) }
val response = request.await()
when(response?.code()){
200 -> {
val tokenResponse = response.body() ?: Dto.TokenResponseDTO(0,"")
LoginViewModel.instance.updateUserLogin(tokenResponse)
}
else ->{
Log.d(TAG, "Login Error code: ${response?.code()}")
}
}
}catch (e: Exception){
Log.d(TAG, e.toString())
}
}
fun getPlaces(
xCoordinate: String,
yCoordinate: String
Expand Down Expand Up @@ -147,9 +176,12 @@ class RetrofitManager {
val response = request.await()
when(response?.code()){
200 -> {
val events = response.body()?.events ?: listOf()
val events = response.body()?.events!!
setEvents(events)
}
404 ->{
setEvents(listOf())
}
else ->{
Log.d(TAG, "Error Code: ${response?.code()}")
}
Expand All @@ -158,20 +190,31 @@ class RetrofitManager {
Log.d(TAG, e.toString())
}
}
fun getEventCalender(
startDate: String,
endDate: String,
@RequiresApi(Build.VERSION_CODES.O)
fun getEventCalendar(
newLocalDate: LocalDate,
setEvents: (List<List<Dto.SimpleEventVo>>)->Unit,
setLocalDate: ((LocalDate) -> Unit)?
) = CoroutineScope(IO).launch {
val startDate = "${newLocalDate.format(DateTimeFormatter.ofPattern("yyyy-MM"))}-01"
val endDate = "${newLocalDate.format(DateTimeFormatter.ofPattern("yyyy-MM"))}-${newLocalDate.lengthOfMonth()}"
try{
val request = CoroutineScope(IO).async { iRetrofit?.getEventsCalender(startDate, endDate) }
val request = CoroutineScope(IO).async { iRetrofit?.getEventsCalendar(startDate, endDate) }
val response = request.await()
when(response?.code()){
200 -> {
val newEvents = response.body()?.data?.map{it.events} ?: listOf()
EventViewModel.instance.updateEvents(newEvents)
val newEvents = response.body()?.data?.map{it.events} as ArrayList<List<Dto.SimpleEventVo>>
if(newEvents.size < 31){
for(i in 1..31-newEvents.size){
newEvents.add(listOf())
}
}
setEvents(newEvents)
if(setLocalDate != null)
setLocalDate(newLocalDate)
}
else -> {

Log.d(TAG, "Calendar Error code: ${response?.code()}")
}
}
}catch (e: Exception){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,39 @@ import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.example.sungdongwalk.api.Dto
import com.example.sungdongwalk.ui.theme.SDwhite
import java.time.LocalDate

@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@OptIn(ExperimentalLayoutApi::class, ExperimentalMaterial3Api::class)
@RequiresApi(Build.VERSION_CODES.O)
@Composable
fun Calendar(
localDate: LocalDate,
setLocalDate: (LocalDate) -> Unit,
setIsShowModal: (Boolean) -> Unit,
setSelectedDay: (Int) -> Unit
setIsShowWebsite: (Boolean) -> Unit
){
val (localDate, setLocalDate) = rememberSaveable{
mutableStateOf(LocalDate.now())
}
val (events, setEvents) = rememberSaveable{
mutableStateOf<List<List<Dto.SimpleEventVo>>>((1..31).map{ listOf() })
}

Column(
modifier = Modifier
.fillMaxWidth()
.background(SDwhite)
.padding(start = 22.dp, end = 22.dp, bottom = 12.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
CalendarHeader(localDate, setLocalDate)
CalendarMonthItem(localDate, setIsShowModal, setSelectedDay)
CalendarHeader(localDate, setLocalDate, setEvents)
CalendarMonthItem(localDate, setIsShowWebsite, events)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,32 @@ import com.example.sungdongwalk.ui.theme.Gray700
import com.example.sungdongwalk.ui.theme.SDblue
import com.example.sungdongwalk.ui.theme.SDwhite
import com.example.sungdongwalk.ui.theme.Typography
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

enum class DayEventType{
NO_EVENT, HAS_EVENT, FIN_EVENT
}
@RequiresApi(Build.VERSION_CODES.O)
@Composable
fun CalendarDay(
localDate: LocalDate,
day: Int,
events: List<Dto.SimpleEventVo>,
events: List<List<Dto.SimpleEventVo>>,
setIsShow: (Boolean) -> Unit,
setSelectedDay: (Int) -> Unit
){
val dayEventType =
if(day==0 || events.isEmpty()) DayEventType.NO_EVENT
else if(day < localDate.dayOfMonth) DayEventType.FIN_EVENT
else DayEventType.HAS_EVENT
val currDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm"))
val dayEventType = if (day==0 || events[day-1].isEmpty()) DayEventType.NO_EVENT
else if(events[day - 1].maxOfOrNull { it.endDate }!! >= currDate) DayEventType.HAS_EVENT
else DayEventType.FIN_EVENT
Column(
modifier = Modifier
.padding(10.dp)
.background(SDwhite)
.width(20.dp)
.wrapContentHeight()
.clickable {
if(dayEventType != DayEventType.NO_EVENT){
if (dayEventType != DayEventType.NO_EVENT) {
setSelectedDay(day)
setIsShow(true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.example.sungdongwalk.R
import com.example.sungdongwalk.api.Dto
import com.example.sungdongwalk.api.retrofit.RetrofitManager
import com.example.sungdongwalk.ui.theme.Gray500
import com.example.sungdongwalk.ui.theme.Typography
import java.time.LocalDate
Expand All @@ -23,8 +25,13 @@ import java.time.LocalDate
@Composable
fun CalendarHeader(
localDate: LocalDate,
setLocalDate: (LocalDate) -> Unit
setLocalDate: (LocalDate) -> Unit,
setEvents: (List<List<Dto.SimpleEventVo>>) -> Unit
){
RetrofitManager.instance.getEventCalendar(
localDate,
setEvents,
null)
Row(
modifier = Modifier.padding(vertical = 10.dp),
horizontalArrangement = Arrangement.Center,
Expand All @@ -34,7 +41,13 @@ fun CalendarHeader(
painter = painterResource(id = R.drawable.ic_prev),
contentDescription = null,
tint = Gray500,
modifier = Modifier.clickable { setLocalDate(localDate.minusMonths(1) )}
modifier = Modifier.clickable {
val newLocalDate = localDate.minusMonths(1)
RetrofitManager.instance.getEventCalendar(
newLocalDate,
setEvents,
setLocalDate)
}
)
Text(
text = "${localDate.monthValue}",
Expand All @@ -46,7 +59,13 @@ fun CalendarHeader(
painter = painterResource(id = R.drawable.ic_front),
contentDescription = null,
tint = Gray500,
modifier = Modifier.clickable {setLocalDate(localDate.plusMonths(1))}
modifier = Modifier.clickable {
val newLocalDate = localDate.plusMonths(1)
RetrofitManager.instance.getEventCalendar(
newLocalDate,
setEvents,
setLocalDate)
}
)
}
}
Loading

0 comments on commit c0bbe8b

Please sign in to comment.