-
Notifications
You must be signed in to change notification settings - Fork 2
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
16 changed files
with
968 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
16 changes: 15 additions & 1 deletion
16
frontend/app/src/main/java/com/example/haengsha/model/dataSource/AppContainer.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
17 changes: 17 additions & 0 deletions
17
frontend/app/src/main/java/com/example/haengsha/model/dataSource/EventDataRepository.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,17 @@ | ||
package com.example.haengsha.model.dataSource | ||
|
||
import com.example.haengsha.model.network.apiService.EventApiService | ||
import com.example.haengsha.model.network.apiService.EventResponse | ||
|
||
interface EventDataRepository { | ||
suspend fun getEventByDate(eventType: Int, date: String): List<EventResponse> | ||
} | ||
|
||
|
||
class NetworkEventDataRepository( | ||
private val eventApiService: EventApiService | ||
) : EventDataRepository { | ||
override suspend fun getEventByDate(eventType: Int, date: String): List<EventResponse> { | ||
return eventApiService.getEventByDate(eventType, date) | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
frontend/app/src/main/java/com/example/haengsha/model/network/apiService/EventApiService.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,76 @@ | ||
package com.example.haengsha.model.network.apiService | ||
|
||
import android.os.Build | ||
import androidx.annotation.RequiresApi | ||
import com.example.haengsha.ui.screens.home.EventCardData | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
import retrofit2.http.GET | ||
import retrofit2.http.Path | ||
import java.time.LocalDate | ||
import java.time.format.DateTimeFormatter | ||
|
||
@Serializable | ||
data class EventResponse( | ||
@SerialName("nickname") val organizer: String, | ||
@SerialName("title") val title: String, | ||
@SerialName("content") val content: String, | ||
@SerialName("place") val place: String, | ||
@SerialName("image") val image: String?, | ||
@SerialName("is_festival") val isFestival: Boolean, | ||
@SerialName("like_count") val likeCount: Int, | ||
@SerialName("favorite_count") val favoriteCount: Int, | ||
@SerialName("time") val time: String, | ||
@SerialName("event_durations") val eventDurations: List<EventDurationResponse> | ||
) | ||
|
||
@Serializable | ||
data class EventDurationResponse( | ||
@SerialName("event_day") val eventDay: String | ||
) | ||
|
||
@RequiresApi(Build.VERSION_CODES.O) | ||
fun EventResponse.toEventCardData(): EventCardData { | ||
var startDate = stringToDate(eventDurations[0].eventDay) | ||
var endDate = startDate | ||
if (eventDurations.size>1) { | ||
endDate = stringToDate(eventDurations[eventDurations.size-1].eventDay) | ||
} | ||
|
||
var eventType = "Festival" | ||
if (!isFestival){ | ||
eventType = "Academic" | ||
} | ||
return EventCardData( | ||
organizer = organizer, | ||
eventTitle = title, | ||
startDate = startDate, | ||
endDate = endDate, | ||
likes = likeCount, | ||
favorites = favoriteCount, | ||
eventType = eventType, | ||
place = place, | ||
time = time | ||
) | ||
} | ||
|
||
interface EventApiService { | ||
@GET("/api/post/festival/{eventType}/date/{date}/{date}") | ||
suspend fun getEventByDate( | ||
@Path("eventType") eventType: Int, // Replace with appropriate type | ||
@Path("date") date: String | ||
): List<EventResponse> // Change the return type to a list of EventResponse | ||
} | ||
|
||
@RequiresApi(Build.VERSION_CODES.O) | ||
fun stringToDate(dateString: String): LocalDate { | ||
var formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd") | ||
try { | ||
return LocalDate.parse(dateString, formatter) | ||
//val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss") | ||
//date = format.parse(dateString)!! | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
} | ||
return LocalDate.now() | ||
} |
75 changes: 75 additions & 0 deletions
75
frontend/app/src/main/java/com/example/haengsha/model/viewModel/event/EventViewModel.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,75 @@ | ||
package com.example.haengsha.model.viewModel.event | ||
|
||
import android.os.Build | ||
import androidx.annotation.RequiresApi | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.lifecycle.viewModelScope | ||
import androidx.lifecycle.viewmodel.initializer | ||
import androidx.lifecycle.viewmodel.viewModelFactory | ||
import com.example.haengsha.HaengshaApplication | ||
import com.example.haengsha.model.dataSource.EventDataRepository | ||
import com.example.haengsha.model.network.apiService.EventResponse | ||
import com.example.haengsha.model.network.apiService.toEventCardData | ||
import com.example.haengsha.ui.screens.home.EventCardData | ||
import com.example.haengsha.ui.screens.home.SharedViewModel | ||
import kotlinx.coroutines.launch | ||
import retrofit2.HttpException | ||
import java.io.Closeable | ||
import java.io.IOException | ||
import java.time.LocalDate | ||
|
||
@RequiresApi(Build.VERSION_CODES.O) | ||
open class EventViewModel( | ||
private val eventDataRepository: EventDataRepository, | ||
private val sharedViewModel: SharedViewModel | ||
) : ViewModel() { | ||
|
||
companion object { | ||
private lateinit var sharedViewModelInstance: SharedViewModel | ||
|
||
fun Factory(sharedViewModel: SharedViewModel): ViewModelProvider.Factory { | ||
sharedViewModelInstance = sharedViewModel | ||
return viewModelFactory { | ||
initializer { | ||
val application = | ||
this[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY] as HaengshaApplication | ||
val eventDataRepository = application.container.eventDataRepository | ||
EventViewModel(eventDataRepository, sharedViewModel) | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
@RequiresApi(Build.VERSION_CODES.O) | ||
fun getEventByDate(eventType: String, date: LocalDate) { | ||
viewModelScope.launch { | ||
try { | ||
var eventTypeConverted = if (eventType == "Festival") 1 else 0 | ||
|
||
val eventGetResponse: List<EventResponse> = | ||
eventDataRepository.getEventByDate(eventTypeConverted, date.toString()) | ||
|
||
val eventCardDataList: List<EventCardData> = | ||
eventGetResponse.map { it.toEventCardData() } | ||
|
||
if (eventType == "Festival") { | ||
sharedViewModel.updateFestivalItems(eventCardDataList) | ||
} else { | ||
sharedViewModel.updateAcademicItems(eventCardDataList) | ||
} | ||
|
||
sharedViewModel.updateSelectedDate(date) | ||
|
||
} catch (e: HttpException) { | ||
val errorMessage = e.response()?.errorBody()?.string() ?: "이벤트를 불러오지 못했습니다." | ||
} catch (e: IOException) { | ||
val errorMessage = "이벤트를 불러오지 못했습니다." | ||
} catch (e: Exception) { | ||
val errorMessage = "이벤트를 불러오지 못했습니다." | ||
e.printStackTrace() | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.