Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package com.onmi.data.datasource

import com.onmi.domain.model.school.SchoolType

interface TimeTableDataSource {

suspend fun getTimeTable(
schoolCode: String,
schoolType: String,
schoolType: SchoolType,
educationCode: String,
grade: Int,
`class`: Int,
department: String,
department: String?,
date: String,
): List<String>
): List<String>?
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.onmi.data.dto.timetable.response.GetMiddleSchoolTimeTableResponse
import com.onmi.data.dto.timetable.response.GetSpecialSchoolTimeTableResponse
import com.onmi.data.datasource.TimeTableDataSource
import com.onmi.data.utils.bodyOrThrow
import com.onmi.domain.model.school.SchoolType
import io.ktor.client.HttpClient
import io.ktor.client.request.get
import io.ktor.client.request.parameter
Expand All @@ -18,58 +19,33 @@ class TimeTableDataSourceImpl @Inject constructor(

override suspend fun getTimeTable(
schoolCode: String,
schoolType: String,
schoolType: SchoolType,
educationCode: String,
grade: Int,
`class`: Int,
department: String,
department: String?,
date: String,
): List<String> {
return fetchTimeTable(
schoolType, educationCode, schoolCode, grade, `class`, date, department
) ?: fetchTimeTable(
schoolType, educationCode, schoolCode, grade, `class`, date
) ?: emptyList()
}


private suspend fun fetchTimeTable(
schoolType: String,
educationCode: String,
schoolCode: String,
grade: Int,
`class`: Int,
date: String,
department: String? = null,
): List<String>? {
val response = httpClient.get {
url("/hub/${schoolType}Timetable")
url("/hub/${schoolType.key}Timetable")
parameter("ATPT_OFCDC_SC_CODE", educationCode)
parameter("SD_SCHUL_CODE", schoolCode)
parameter("DDDEP_NM", department)
if (department != null) parameter("DDDEP_NM", department)
parameter("GRADE", grade)
parameter("CLASS_NM", `class`)
parameter("ALL_TI_YMD", date)
}

return when (schoolType) {
"els" -> response.bodyOrThrow<GetElementarySchoolTimTableResponse>().timetable?.getOrNull(1)?.row
?.distinctBy { it.period }
?.map { it.subject }
SchoolType.Elementary -> response.bodyOrThrow<GetElementarySchoolTimTableResponse>().timetable

"mis" -> response.bodyOrThrow<GetMiddleSchoolTimeTableResponse>().timetable?.getOrNull(1)?.row
?.distinctBy { it.period }
?.map { it.subject }
SchoolType.Middle -> response.bodyOrThrow<GetMiddleSchoolTimeTableResponse>().timetable

"his" -> response.bodyOrThrow<GetHighSchoolTimeTableResponse>().timetable?.getOrNull(1)?.row
?.distinctBy { it.period }
?.map { it.subject }
SchoolType.High -> response.bodyOrThrow<GetHighSchoolTimeTableResponse>().timetable

"sps" -> response.bodyOrThrow<GetSpecialSchoolTimeTableResponse>().timetable?.getOrNull(1)?.row
?.distinctBy { it.period }
?.map { it.subject }

else -> emptyList()
}
SchoolType.Special -> response.bodyOrThrow<GetSpecialSchoolTimeTableResponse>().timetable
}?.getOrNull(1)?.row
?.distinctBy { it.period }
?.map { it.subject }
}
}
4 changes: 2 additions & 2 deletions data/src/main/java/com/onmi/data/network/KtorClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import io.ktor.client.HttpClient
import io.ktor.client.engine.cio.CIO
import io.ktor.client.engine.android.Android
import io.ktor.client.plugins.DefaultRequest
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
import io.ktor.client.plugins.logging.LogLevel
Expand All @@ -27,7 +27,7 @@ object KtorClient {
@Singleton
@Provides
fun provideKtorHttpClient(): HttpClient {
val client = HttpClient(CIO) {
val client = HttpClient(Android) {
install(Logging) {
level = LogLevel.ALL
logger = object : Logger {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.onmi.data.repository

import com.onmi.data.datasource.TimeTableDataSource
import com.onmi.domain.model.school.SchoolType
import com.onmi.domain.repository.TimeTableRepository
import javax.inject.Inject

Expand All @@ -10,13 +11,13 @@ class TimeTableRepositoryImpl @Inject constructor(

override suspend fun getTimeTable(
schoolCode: String,
schoolType: String,
schoolType: SchoolType,
educationCode: String,
grade: Int,
`class`: Int,
department: String,
department: String?,
date: String,
): List<String> {
): List<String>? {
return service.getTimeTable(
schoolCode = schoolCode,
schoolType = schoolType,
Expand Down
20 changes: 20 additions & 0 deletions domain/src/main/java/com/onmi/domain/model/school/SchoolType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.onmi.domain.model.school

enum class SchoolType(val key: String) {
Elementary("els"),
Middle("mis"),
High("his"),
Special("sps");

companion object {
fun convertSchoolTypeToKey(type: String): SchoolType {
return when (type) {
"μ΄ˆλ“±ν•™κ΅" -> Elementary
"쀑학ꡐ" -> Middle
"고등학ꡐ" -> High
"νŠΉμˆ˜ν•™κ΅" -> Special
else -> throw RuntimeException("μ•Œ 수 μ—†λŠ” 학ꡐ νƒ€μž…μž…λ‹ˆλ‹€.")
Comment thread
khs3994 marked this conversation as resolved.
Outdated
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package com.onmi.domain.repository

import com.onmi.domain.model.school.SchoolType

interface TimeTableRepository {

suspend fun getTimeTable(
schoolCode: String,
schoolType: String,
schoolType: SchoolType,
educationCode: String,
grade: Int,
`class`: Int,
department: String,
department: String?,
date: String,
): List<String>
): List<String>?
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.onmi.domain.usecase.timetable

import com.onmi.domain.exception.NeisException
import com.onmi.domain.exception.NeisResult
import com.onmi.domain.model.school.SchoolType
import com.onmi.domain.model.user.UserInfoModel
import com.onmi.domain.repository.TimeTableRepository
import com.onmi.domain.usecase.user.GetUserInfoFlowUseCase
import com.onmi.domain.util.DateUtils
Expand Down Expand Up @@ -42,16 +44,27 @@ class GetTimeTableUseCase @Inject constructor(
) {
suspend operator fun invoke(targetDate: String) = runCatching {
val userInfo = getUserInfoFlowUseCase().first()

repository.getTimeTable(
schoolCode = userInfo.schoolCode,
schoolType = convertSchoolTypeToKey(userInfo.schoolType),
educationCode = userInfo.educationCode,
grade = userInfo.grade,
`class` = userInfo.classroom,
department = userInfo.department,
date = targetDate
)
val department = userInfo.department.takeIf { it.isNotEmpty() }
Comment thread
khs3994 marked this conversation as resolved.
Outdated
val schoolType = SchoolType.convertSchoolTypeToKey(userInfo.schoolType)

try {
request(
userInfo = userInfo,
schoolType = schoolType,
targetDate = targetDate,
department = department
)
} catch (e: NeisException) {
// ν•™κ³Ό 정보가 있고, DATA_NOT_FOUND μ—λŸ¬μΈ κ²½μš°μ—λ§Œ ν•™κ³Ό 정보 μ œμ™Έν•˜κ³  μž¬μ‹œλ„
if (department != null && e.result == NeisResult.DATA_NOT_FOUND) {
request(
userInfo = userInfo,
schoolType = schoolType,
targetDate = targetDate,
department = null
)
} else throw e
}
}.fold(
onSuccess = { result ->
TimeTableState.Success(result)
Expand All @@ -77,13 +90,20 @@ class GetTimeTableUseCase @Inject constructor(
}
)

private fun convertSchoolTypeToKey(type: String): String {
return when (type) {
"μ΄ˆλ“±ν•™κ΅" -> "els"
"쀑학ꡐ" -> "mis"
"고등학ꡐ" -> "his"
"νŠΉμˆ˜ν•™κ΅" -> "sps"
else -> throw RuntimeException("μ•Œ 수 μ—†λŠ” 학ꡐ νƒ€μž…μž…λ‹ˆλ‹€.")
}
private suspend fun request(
userInfo: UserInfoModel,
schoolType: SchoolType,
targetDate: String,
department: String?,
): List<String> {
return repository.getTimeTable(
schoolCode = userInfo.schoolCode,
schoolType = schoolType,
educationCode = userInfo.educationCode,
grade = userInfo.grade,
`class` = userInfo.classroom,
department = department,
date = targetDate
) ?: throw NeisException(NeisResult.UNKNOWN_ERROR)
Comment thread
khs3994 marked this conversation as resolved.
Outdated
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package khs.onmi.main.viewmodel

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.onmi.domain.usecase.common.CalculateTargetDateUseCase
Expand Down Expand Up @@ -67,6 +68,8 @@ class MainViewModel @Inject constructor(
viewModelScope.launch {
val response = getTimeTableUseCase(targetDate = targetDate)

Log.d("logtag", response.toString())

Comment thread
khs3994 marked this conversation as resolved.
Outdated
reduce {
state.copy(timeTableState = response)
}
Expand Down
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ orbit-compose = { group = "org.orbit-mvi", name = "orbit-compose", version.ref =

# ktor
ktor-client-core = { group = "io.ktor", name = "ktor-client-core", version.ref = "ktor" }
ktor-client-cio = { group = "io.ktor", name = "ktor-client-cio", version.ref = "ktor" }
ktor-client-android = { group = "io.ktor", name = "ktor-client-android", version.ref = "ktor" }
ktor-client-logging-jvm = { group = "io.ktor", name = "ktor-client-logging-jvm", version.ref = "ktor" }
ktor-client-serialization = { group = "io.ktor", name = "ktor-serialization-kotlinx-json", version.ref = "ktor" }
ktor-client-content-negotiation = { group = "io.ktor", name = "ktor-client-content-negotiation", version.ref = "ktor" }
Expand Down Expand Up @@ -109,7 +109,7 @@ orbit = [
]
ktor = [
"ktor-client-core",
"ktor-client-cio",
"ktor-client-android",
"ktor-client-logging-jvm",
"ktor-client-serialization",
"ktor-client-content-negotiation"
Expand Down