Skip to content

Commit

Permalink
update home ui and logic
Browse files Browse the repository at this point in the history
  • Loading branch information
yuri-0415 committed Oct 29, 2023
1 parent 7b6c420 commit f15ece1
Show file tree
Hide file tree
Showing 16 changed files with 968 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions frontend/.idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion frontend/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ dependencies {
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3")
implementation("com.google.android.material:material:1.11.0-beta01")

implementation("androidx.navigation:navigation-compose:2.7.4")
implementation("androidx.compose.material3:material3-android:1.2.0-alpha09")
implementation("androidx.navigation:navigation-runtime-ktx:2.7.4")
implementation("androidx.navigation:navigation-fragment-ktx:2.7.4")
implementation("androidx.navigation:navigation-ui-ktx:2.7.4")
Expand All @@ -79,6 +79,14 @@ dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1")

implementation("com.github.GrenderG:Toasty:1.5.2")
// The view calendar library
implementation("com.kizitonwose.calendar:view:2.4.0")
// The compose calendar library
implementation("com.kizitonwose.calendar:compose:2.4.0")
implementation("com.google.accompanist:accompanist-pager:0.12.0")
implementation("androidx.tv:tv-material:1.0.0-alpha10")
implementation("androidx.compose.runtime:runtime-livedata:1.5.4")


testImplementation("junit:junit:4.13.2")
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.1")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.example.haengsha

import android.os.Build
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.annotation.RequiresApi
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
Expand All @@ -11,6 +13,7 @@ import com.example.haengsha.ui.HaengshaApp
import com.example.haengsha.ui.theme.HaengshaTheme

class MainActivity : ComponentActivity() {
@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
package com.example.haengsha.model.dataSource

import com.example.haengsha.model.network.apiService.EventApiService
import com.example.haengsha.model.network.apiService.LoginApiService
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import java.util.concurrent.TimeUnit

interface AppContainer {
val loginDataRepository: LoginDataRepository
val eventDataRepository: EventDataRepository
}

class HaengshaAppContainer : AppContainer {
private val baseUrl = "http://ec2-43-201-28-141.ap-northeast-2.compute.amazonaws.com:8080/"
private val baseUrl = "http://ec2-52-79-228-36.ap-northeast-2.compute.amazonaws.com:8080/"
// Create an OkHttpClient with a default timeout
private val okHttpClient = OkHttpClient.Builder()
.readTimeout(30, TimeUnit.SECONDS) // Set the read timeout to 30 seconds
.connectTimeout(30, TimeUnit.SECONDS) // Set the connect timeout to 30 seconds
.build()

private val retrofit = Retrofit.Builder()
.addConverterFactory(Json.asConverterFactory("application/json".toMediaType()))
.baseUrl(baseUrl)
.client(okHttpClient)
.build()

private val retrofitEventService: EventApiService by lazy { retrofit.create(EventApiService::class.java)}
override val eventDataRepository: EventDataRepository by lazy{ NetworkEventDataRepository(retrofitEventService)}

private val retrofitLoginService: LoginApiService by lazy {
retrofit.create(LoginApiService::class.java)
}
Expand Down
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)
}
}
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()
}
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()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.haengsha.ui

import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
Expand All @@ -17,6 +19,7 @@ import com.example.haengsha.ui.screens.home.Home
import com.example.haengsha.ui.screens.login.Login
import com.example.haengsha.ui.screens.setting.Setting

@RequiresApi(Build.VERSION_CODES.O)
@Composable
fun HaengshaApp() {
val userViewModel: UserViewModel = viewModel()
Expand All @@ -36,7 +39,6 @@ fun HaengshaApp() {
}
composable(MainRoute.Home.route) {
Home(
userUiState = userUiState,
mainNavController = mainNavController
)
}
Expand Down
Loading

0 comments on commit f15ece1

Please sign in to comment.