diff --git a/app/src/main/kotlin/com/zjutjh/ijh/ui/viewmodel/CourseCalendarViewModel.kt b/app/src/main/kotlin/com/zjutjh/ijh/ui/viewmodel/CourseCalendarViewModel.kt index 2f414a0..57f5743 100644 --- a/app/src/main/kotlin/com/zjutjh/ijh/ui/viewmodel/CourseCalendarViewModel.kt +++ b/app/src/main/kotlin/com/zjutjh/ijh/ui/viewmodel/CourseCalendarViewModel.kt @@ -3,7 +3,7 @@ package com.zjutjh.ijh.ui.viewmodel import android.util.Log import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope -import com.zjutjh.ijh.data.CampusInfoRepository +import com.zjutjh.ijh.data.CampusRepository import com.zjutjh.ijh.data.CourseRepository import com.zjutjh.ijh.data.WeJhUserRepository import com.zjutjh.ijh.model.Course @@ -37,7 +37,7 @@ import javax.inject.Inject @HiltViewModel class CourseCalendarViewModel @Inject constructor( - campusInfoRepository: CampusInfoRepository, + campusRepository: CampusRepository, weJhUserRepository: WeJhUserRepository, private val courseRepository: CourseRepository, ) : ViewModel() { @@ -53,7 +53,7 @@ class CourseCalendarViewModel @Inject constructor( ) @OptIn(ExperimentalCoroutinesApi::class) - private val localTermDayState = campusInfoRepository.infoStream + private val localTermDayState = campusRepository.infoStream .mapLatest { it?.toTermDayState() } diff --git a/app/src/main/kotlin/com/zjutjh/ijh/ui/viewmodel/HomeViewModel.kt b/app/src/main/kotlin/com/zjutjh/ijh/ui/viewmodel/HomeViewModel.kt index 7876db3..73a1aa4 100644 --- a/app/src/main/kotlin/com/zjutjh/ijh/ui/viewmodel/HomeViewModel.kt +++ b/app/src/main/kotlin/com/zjutjh/ijh/ui/viewmodel/HomeViewModel.kt @@ -3,8 +3,8 @@ package com.zjutjh.ijh.ui.viewmodel import android.util.Log import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope -import com.zjutjh.ijh.data.CampusInfoRepository -import com.zjutjh.ijh.data.CardInfoRepository +import com.zjutjh.ijh.data.CampusRepository +import com.zjutjh.ijh.data.CardRepository import com.zjutjh.ijh.data.CourseRepository import com.zjutjh.ijh.data.WeJhUserRepository import com.zjutjh.ijh.model.Course @@ -27,8 +27,8 @@ import kotlin.time.toKotlinDuration class HomeViewModel @Inject constructor( weJhUserRepository: WeJhUserRepository, private val courseRepository: CourseRepository, - private val campusInfoRepository: CampusInfoRepository, - private val cardInfoRepository: CardInfoRepository, + private val campusRepository: CampusRepository, + private val cardRepository: CardRepository, ) : ViewModel() { private val timerFlow: Flow = flow { @@ -66,7 +66,7 @@ class HomeViewModel @Inject constructor( private val termLocalRefreshChannel: MutableStateFlow = MutableStateFlow(Unit) @OptIn(ExperimentalCoroutinesApi::class) - val termDayState: StateFlow> = campusInfoRepository.infoStream + val termDayState: StateFlow> = campusRepository.infoStream .combine(termLocalRefreshChannel) { t1, _ -> t1 } .mapLatest { it?.toTermDayState() @@ -93,7 +93,7 @@ class HomeViewModel @Inject constructor( started = SharingStarted.WhileSubscribed(5_000) ) - val cardBalanceState: StateFlow> = cardInfoRepository.balanceStream + val cardBalanceState: StateFlow> = cardRepository.balanceStream .distinctUntilChanged() .asLoadResultStateFlow( scope = viewModelScope, @@ -101,7 +101,7 @@ class HomeViewModel @Inject constructor( ) val cardBalanceLastSyncState: StateFlow> = - cardInfoRepository.lastSyncTimeStream + cardRepository.lastSyncTimeStream .distinctUntilChanged() .combine(timerFlow) { t1, _ -> t1 } .map { @@ -178,7 +178,7 @@ class HomeViewModel @Inject constructor( } private suspend fun refreshTerm(): Pair? { - runCatching { campusInfoRepository.sync() } + runCatching { campusRepository.sync() } .fold({ Log.i("Home", "Sync WeJhInfo succeed.") return it @@ -208,7 +208,7 @@ class HomeViewModel @Inject constructor( private suspend fun refreshCard() { runCatching { - cardInfoRepository.sync() + cardRepository.sync() }.fold({ Log.i("Home", "Sync Card succeed.") }) { diff --git a/app/src/main/kotlin/com/zjutjh/ijh/widget/ScheduleWidget.kt b/app/src/main/kotlin/com/zjutjh/ijh/widget/ScheduleWidget.kt index 9986be3..7c0cf3d 100644 --- a/app/src/main/kotlin/com/zjutjh/ijh/widget/ScheduleWidget.kt +++ b/app/src/main/kotlin/com/zjutjh/ijh/widget/ScheduleWidget.kt @@ -58,7 +58,7 @@ class ScheduleWidget : GlanceAppWidget() { val entryPoint = EntryPointAccessors.fromApplication(context) - val dayStateFlow = entryPoint.campusInfoRepository.infoStream + val dayStateFlow = entryPoint.campusRepository.infoStream .map { it?.toTermDayState() } val coursesFlow = dayStateFlow.flatMapLatest { it ?: return@flatMapLatest flowOf(null) diff --git a/app/src/main/kotlin/com/zjutjh/ijh/widget/ScheduleWidgetReceiver.kt b/app/src/main/kotlin/com/zjutjh/ijh/widget/ScheduleWidgetReceiver.kt index 0eb485b..fc87c2a 100644 --- a/app/src/main/kotlin/com/zjutjh/ijh/widget/ScheduleWidgetReceiver.kt +++ b/app/src/main/kotlin/com/zjutjh/ijh/widget/ScheduleWidgetReceiver.kt @@ -7,7 +7,7 @@ import androidx.work.Constraints import androidx.work.ExistingPeriodicWorkPolicy import androidx.work.PeriodicWorkRequestBuilder import androidx.work.WorkManager -import com.zjutjh.ijh.data.CampusInfoRepository +import com.zjutjh.ijh.data.CampusRepository import com.zjutjh.ijh.data.CourseRepository import com.zjutjh.ijh.work.ScheduleWidgetUpdater import com.zjutjh.ijh.work.enqueueWidgetRefresh @@ -25,7 +25,7 @@ class ScheduleWidgetReceiver : GlanceAppWidgetReceiver() { @InstallIn(SingletonComponent::class) interface Repositories { val courseRepository: CourseRepository - val campusInfoRepository: CampusInfoRepository + val campusRepository: CampusRepository } override val glanceAppWidget = ScheduleWidget() diff --git a/app/src/main/kotlin/com/zjutjh/ijh/work/ScheduleWidgetUpdater.kt b/app/src/main/kotlin/com/zjutjh/ijh/work/ScheduleWidgetUpdater.kt index d461f97..e48a2fb 100644 --- a/app/src/main/kotlin/com/zjutjh/ijh/work/ScheduleWidgetUpdater.kt +++ b/app/src/main/kotlin/com/zjutjh/ijh/work/ScheduleWidgetUpdater.kt @@ -50,7 +50,7 @@ class ScheduleWidgetUpdater( override suspend fun doWork(): Result { val result = try { - val info = entryPoint.campusInfoRepository.sync() + val info = entryPoint.campusRepository.sync() entryPoint.courseRepository.sync(info.first, info.second) Log.i("ScheduleWidget", "Synced.") diff --git a/core/data/src/main/kotlin/com/zjutjh/ijh/data/CampusInfoRepository.kt b/core/data/src/main/kotlin/com/zjutjh/ijh/data/CampusRepository.kt similarity index 90% rename from core/data/src/main/kotlin/com/zjutjh/ijh/data/CampusInfoRepository.kt rename to core/data/src/main/kotlin/com/zjutjh/ijh/data/CampusRepository.kt index b0b052f..b4d29d3 100644 --- a/core/data/src/main/kotlin/com/zjutjh/ijh/data/CampusInfoRepository.kt +++ b/core/data/src/main/kotlin/com/zjutjh/ijh/data/CampusRepository.kt @@ -4,7 +4,7 @@ import com.zjutjh.ijh.model.CampusInfo import com.zjutjh.ijh.model.Term import kotlinx.coroutines.flow.Flow -interface CampusInfoRepository { +interface CampusRepository { val infoStream: Flow /** diff --git a/core/data/src/main/kotlin/com/zjutjh/ijh/data/CardInfoRepository.kt b/core/data/src/main/kotlin/com/zjutjh/ijh/data/CardRepository.kt similarity index 92% rename from core/data/src/main/kotlin/com/zjutjh/ijh/data/CardInfoRepository.kt rename to core/data/src/main/kotlin/com/zjutjh/ijh/data/CardRepository.kt index 2323420..59073d7 100644 --- a/core/data/src/main/kotlin/com/zjutjh/ijh/data/CardInfoRepository.kt +++ b/core/data/src/main/kotlin/com/zjutjh/ijh/data/CardRepository.kt @@ -5,7 +5,7 @@ import kotlinx.coroutines.flow.Flow import java.time.ZonedDateTime import java.util.Date -interface CardInfoRepository { +interface CardRepository { /** * balance (Unit: CNY) in string diff --git a/core/data/src/main/kotlin/com/zjutjh/ijh/data/converter/Course.kt b/core/data/src/main/kotlin/com/zjutjh/ijh/data/converter/Course.kt index 827041b..4bc4c8a 100644 --- a/core/data/src/main/kotlin/com/zjutjh/ijh/data/converter/Course.kt +++ b/core/data/src/main/kotlin/com/zjutjh/ijh/data/converter/Course.kt @@ -3,11 +3,11 @@ package com.zjutjh.ijh.data.converter import com.zjutjh.ijh.database.model.CourseEntity import com.zjutjh.ijh.exception.CourseParseException import com.zjutjh.ijh.model.Term -import com.zjutjh.ijh.network.model.ZfClassTable +import com.zjutjh.ijh.network.model.NetworkClassTable import com.zjutjh.ijh.network.model.parseWeekString import java.time.DayOfWeek -fun ZfClassTable.LessonsTable.asLocalModel(year: Int, term: Term): CourseEntity { +fun NetworkClassTable.LessonsTable.asLocalModel(year: Int, term: Term): CourseEntity { val section = sections.split('-') if (section.size != 2) { throw CourseParseException("Invalid class section format.") diff --git a/core/data/src/main/kotlin/com/zjutjh/ijh/data/converter/WeJhUser.kt b/core/data/src/main/kotlin/com/zjutjh/ijh/data/converter/WeJhUser.kt index 8df35aa..797f888 100644 --- a/core/data/src/main/kotlin/com/zjutjh/ijh/data/converter/WeJhUser.kt +++ b/core/data/src/main/kotlin/com/zjutjh/ijh/data/converter/WeJhUser.kt @@ -1,13 +1,14 @@ package com.zjutjh.ijh.data.converter -import com.zjutjh.ijh.datastore.model.WeJhPreference -import com.zjutjh.ijh.datastore.model.WeJhPreferenceKt +import com.zjutjh.ijh.datastore.model.LocalWeJhUser +import com.zjutjh.ijh.datastore.model.LocalWeJhUserKt +import com.zjutjh.ijh.datastore.model.localWeJhUser import com.zjutjh.ijh.network.model.NetworkWeJhUser import java.time.ZonedDateTime import java.time.format.DateTimeFormatter -fun NetworkWeJhUser.asLocalModel(): WeJhPreference.User = - WeJhPreferenceKt.user { +fun NetworkWeJhUser.asLocalModel(): LocalWeJhUser = + localWeJhUser { uid = this@asLocalModel.id username = this@asLocalModel.username studentId = this@asLocalModel.studentId @@ -20,8 +21,8 @@ fun NetworkWeJhUser.asLocalModel(): WeJhPreference.User = bind = this@asLocalModel.bind.asLocalModel() } -fun NetworkWeJhUser.Bind.asLocalModel(): WeJhPreference.User.Bind = - WeJhPreferenceKt.UserKt.bind { +fun NetworkWeJhUser.Bind.asLocalModel(): LocalWeJhUser.Bind = + LocalWeJhUserKt.bind { lib = this@asLocalModel.lib yxy = this@asLocalModel.yxy zf = this@asLocalModel.zf diff --git a/core/data/src/main/kotlin/com/zjutjh/ijh/data/di/DataModule.kt b/core/data/src/main/kotlin/com/zjutjh/ijh/data/di/DataModule.kt index 46b9ec4..62e1cd3 100644 --- a/core/data/src/main/kotlin/com/zjutjh/ijh/data/di/DataModule.kt +++ b/core/data/src/main/kotlin/com/zjutjh/ijh/data/di/DataModule.kt @@ -1,11 +1,11 @@ package com.zjutjh.ijh.data.di -import com.zjutjh.ijh.data.CampusInfoRepository -import com.zjutjh.ijh.data.CardInfoRepository +import com.zjutjh.ijh.data.CampusRepository +import com.zjutjh.ijh.data.CardRepository import com.zjutjh.ijh.data.CourseRepository import com.zjutjh.ijh.data.WeJhUserRepository -import com.zjutjh.ijh.data.impl.CampusInfoRepositoryImpl -import com.zjutjh.ijh.data.impl.CardInfoRepositoryImpl +import com.zjutjh.ijh.data.impl.CampusRepositoryImpl +import com.zjutjh.ijh.data.impl.CardRepositoryImpl import com.zjutjh.ijh.data.impl.CourseRepositoryImpl import com.zjutjh.ijh.data.impl.WeJhUserRepositoryImpl import dagger.Binds @@ -24,8 +24,8 @@ interface DataModule { fun bindWeJhUserRepository(impl: WeJhUserRepositoryImpl): WeJhUserRepository @Binds - fun bindWeJhInfoRepository(impl: CampusInfoRepositoryImpl): CampusInfoRepository + fun bindWeJhInfoRepository(impl: CampusRepositoryImpl): CampusRepository @Binds - fun bindCardInfoRepository(impl: CardInfoRepositoryImpl): CardInfoRepository + fun bindCardInfoRepository(impl: CardRepositoryImpl): CardRepository } \ No newline at end of file diff --git a/core/data/src/main/kotlin/com/zjutjh/ijh/data/impl/CampusInfoRepositoryImpl.kt b/core/data/src/main/kotlin/com/zjutjh/ijh/data/impl/CampusInfoRepositoryImpl.kt deleted file mode 100644 index 76631ed..0000000 --- a/core/data/src/main/kotlin/com/zjutjh/ijh/data/impl/CampusInfoRepositoryImpl.kt +++ /dev/null @@ -1,27 +0,0 @@ -package com.zjutjh.ijh.data.impl - -import com.zjutjh.ijh.data.CampusInfoRepository -import com.zjutjh.ijh.datastore.WeJhPreferenceDataSource -import com.zjutjh.ijh.datastore.converter.asExternalModel -import com.zjutjh.ijh.datastore.model.infoOrNull -import com.zjutjh.ijh.model.CampusInfo -import com.zjutjh.ijh.model.Term -import com.zjutjh.ijh.network.CampusInfoNetworkDataSource -import com.zjutjh.ijh.network.model.asExternalModel -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.map -import javax.inject.Inject - -class CampusInfoRepositoryImpl @Inject constructor( - private val network: CampusInfoNetworkDataSource, - private val local: WeJhPreferenceDataSource -) : CampusInfoRepository { - - override val infoStream: Flow = local.data.map { it.infoOrNull?.asExternalModel() } - - override suspend fun sync(): Pair { - val weJhInfo = network.getInfo().asExternalModel() - local.setInfo(weJhInfo) - return Pair(weJhInfo.year, weJhInfo.term) - } -} \ No newline at end of file diff --git a/core/data/src/main/kotlin/com/zjutjh/ijh/data/impl/CampusRepositoryImpl.kt b/core/data/src/main/kotlin/com/zjutjh/ijh/data/impl/CampusRepositoryImpl.kt new file mode 100644 index 0000000..b9a98a9 --- /dev/null +++ b/core/data/src/main/kotlin/com/zjutjh/ijh/data/impl/CampusRepositoryImpl.kt @@ -0,0 +1,28 @@ +package com.zjutjh.ijh.data.impl + +import com.zjutjh.ijh.data.CampusRepository +import com.zjutjh.ijh.datastore.IJhPreferenceDataSource +import com.zjutjh.ijh.datastore.converter.asExternalModel +import com.zjutjh.ijh.datastore.model.campusOrNull +import com.zjutjh.ijh.model.CampusInfo +import com.zjutjh.ijh.model.Term +import com.zjutjh.ijh.network.CampusNetworkDataSource +import com.zjutjh.ijh.network.model.asExternalModel +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.map +import javax.inject.Inject + +class CampusRepositoryImpl @Inject constructor( + private val network: CampusNetworkDataSource, + private val local: IJhPreferenceDataSource +) : CampusRepository { + + override val infoStream: Flow = + local.data.map { it.campusOrNull?.asExternalModel() } + + override suspend fun sync(): Pair { + val weJhInfo = network.getInfo().asExternalModel() + local.setCampus(weJhInfo) + return Pair(weJhInfo.year, weJhInfo.term) + } +} \ No newline at end of file diff --git a/core/data/src/main/kotlin/com/zjutjh/ijh/data/impl/CardInfoRepositoryImpl.kt b/core/data/src/main/kotlin/com/zjutjh/ijh/data/impl/CardRepositoryImpl.kt similarity index 70% rename from core/data/src/main/kotlin/com/zjutjh/ijh/data/impl/CardInfoRepositoryImpl.kt rename to core/data/src/main/kotlin/com/zjutjh/ijh/data/impl/CardRepositoryImpl.kt index 492e618..c18db0c 100644 --- a/core/data/src/main/kotlin/com/zjutjh/ijh/data/impl/CardInfoRepositoryImpl.kt +++ b/core/data/src/main/kotlin/com/zjutjh/ijh/data/impl/CardRepositoryImpl.kt @@ -1,11 +1,11 @@ package com.zjutjh.ijh.data.impl -import com.zjutjh.ijh.data.CardInfoRepository -import com.zjutjh.ijh.datastore.WeJhPreferenceDataSource +import com.zjutjh.ijh.data.CardRepository +import com.zjutjh.ijh.datastore.IJhPreferenceDataSource import com.zjutjh.ijh.datastore.converter.toZonedDateTime import com.zjutjh.ijh.datastore.model.cardOrNull import com.zjutjh.ijh.model.CardRecord -import com.zjutjh.ijh.network.CardInfoDataSource +import com.zjutjh.ijh.network.CardNetworkDataSource import com.zjutjh.ijh.network.model.NetworkCardRecord import com.zjutjh.ijh.network.model.asExternalModel import kotlinx.coroutines.flow.Flow @@ -14,17 +14,17 @@ import java.time.ZonedDateTime import java.util.Date import javax.inject.Inject -class CardInfoRepositoryImpl @Inject constructor( - private val network: CardInfoDataSource, - private val local: WeJhPreferenceDataSource -) : CardInfoRepository { +class CardRepositoryImpl @Inject constructor( + private val network: CardNetworkDataSource, + private val local: IJhPreferenceDataSource +) : CardRepository { override val balanceStream: Flow = local.data.map { it.cardOrNull?.balance } override val lastSyncTimeStream: Flow = local.data.map { - it.cardOrNull?.lastSyncTime?.toZonedDateTime() + it.cardOrNull?.syncTime?.toZonedDateTime() } override suspend fun sync() { diff --git a/core/data/src/main/kotlin/com/zjutjh/ijh/data/impl/CourseRepositoryImpl.kt b/core/data/src/main/kotlin/com/zjutjh/ijh/data/impl/CourseRepositoryImpl.kt index 53d6523..0ca1f21 100644 --- a/core/data/src/main/kotlin/com/zjutjh/ijh/data/impl/CourseRepositoryImpl.kt +++ b/core/data/src/main/kotlin/com/zjutjh/ijh/data/impl/CourseRepositoryImpl.kt @@ -5,7 +5,7 @@ import com.zjutjh.ijh.data.converter.asLocalModel import com.zjutjh.ijh.data.converter.equalsIgnoreId import com.zjutjh.ijh.database.dao.CourseDao import com.zjutjh.ijh.database.model.CourseEntity -import com.zjutjh.ijh.datastore.WeJhPreferenceDataSource +import com.zjutjh.ijh.datastore.IJhPreferenceDataSource import com.zjutjh.ijh.datastore.converter.toZonedDateTime import com.zjutjh.ijh.model.Course import com.zjutjh.ijh.model.Term @@ -24,7 +24,7 @@ import javax.inject.Singleton @Singleton class CourseRepositoryImpl @Inject constructor( private val networkDataSource: CourseNetworkDataSource, - private val localPreference: WeJhPreferenceDataSource, + private val localPreference: IJhPreferenceDataSource, private val dao: CourseDao, ) : CourseRepository { @@ -44,15 +44,15 @@ class CourseRepositoryImpl @Inject constructor( override val lastSyncTimeStream: Flow = localPreference.data.map { - if (it.hasCoursesLastSyncTime()) - it.coursesLastSyncTime.toZonedDateTime() + if (it.hasCoursesSyncTime()) + it.coursesSyncTime.toZonedDateTime() else null } override suspend fun sync(year: Int, term: Term) { val old = dao.getCourses(year, term).first() - val classTable = networkDataSource.getZfClassTable(year.toString(), term.value).lessonsTable + val classTable = networkDataSource.getClassTable(year.toString(), term.value).lessonsTable if (!classTable.isNullOrEmpty()) { val new = classTable.map { it.asLocalModel(year, term) } diff --git a/core/data/src/main/kotlin/com/zjutjh/ijh/data/impl/WeJhUserRepositoryImpl.kt b/core/data/src/main/kotlin/com/zjutjh/ijh/data/impl/WeJhUserRepositoryImpl.kt index 7634852..3a037a5 100644 --- a/core/data/src/main/kotlin/com/zjutjh/ijh/data/impl/WeJhUserRepositoryImpl.kt +++ b/core/data/src/main/kotlin/com/zjutjh/ijh/data/impl/WeJhUserRepositoryImpl.kt @@ -2,10 +2,10 @@ package com.zjutjh.ijh.data.impl import com.zjutjh.ijh.data.WeJhUserRepository import com.zjutjh.ijh.data.converter.asLocalModel -import com.zjutjh.ijh.datastore.WeJhPreferenceDataSource +import com.zjutjh.ijh.datastore.IJhPreferenceDataSource import com.zjutjh.ijh.datastore.converter.asExternalModel -import com.zjutjh.ijh.datastore.model.sessionOrNull -import com.zjutjh.ijh.datastore.model.userOrNull +import com.zjutjh.ijh.datastore.model.weJhSessionOrNull +import com.zjutjh.ijh.datastore.model.weJhUserOrNull import com.zjutjh.ijh.model.Session import com.zjutjh.ijh.model.WeJhUser import com.zjutjh.ijh.network.WeJhUserNetworkDataSource @@ -15,36 +15,36 @@ import javax.inject.Inject class WeJhUserRepositoryImpl @Inject constructor( private val network: WeJhUserNetworkDataSource, - private val local: WeJhPreferenceDataSource, + private val local: IJhPreferenceDataSource, ) : WeJhUserRepository { override val userStream: Flow = local.data.map { - it.userOrNull?.asExternalModel() + it.weJhUserOrNull?.asExternalModel() } override val sessionStream: Flow = local.data.map { - it.sessionOrNull?.asExternalModel() + it.weJhSessionOrNull?.asExternalModel() } override suspend fun login(username: String, password: String) { val user = network.login(username, password) - local.setUser(user.asLocalModel()) + local.setWeJhUser(user.asLocalModel()) } override suspend fun renewSession() { val user = network.loginBySession() - local.setUser(user.asLocalModel()) + local.setWeJhUser(user.asLocalModel()) } override suspend fun logout() { local.deleteSession() - local.deleteUser() + local.deleteWeJhUser() } override suspend fun sync() { val user = network.getUserInfo() - local.setUser(user.asLocalModel()) + local.setWeJhUser(user.asLocalModel()) } } \ No newline at end of file diff --git a/core/datastore/src/main/kotlin/com/zjutjh/ijh/datastore/WeJhPreferenceDataSource.kt b/core/datastore/src/main/kotlin/com/zjutjh/ijh/datastore/IJhPreferenceDataSource.kt similarity index 56% rename from core/datastore/src/main/kotlin/com/zjutjh/ijh/datastore/WeJhPreferenceDataSource.kt rename to core/datastore/src/main/kotlin/com/zjutjh/ijh/datastore/IJhPreferenceDataSource.kt index 9db4051..c76a33e 100644 --- a/core/datastore/src/main/kotlin/com/zjutjh/ijh/datastore/WeJhPreferenceDataSource.kt +++ b/core/datastore/src/main/kotlin/com/zjutjh/ijh/datastore/IJhPreferenceDataSource.kt @@ -2,9 +2,10 @@ package com.zjutjh.ijh.datastore import androidx.datastore.core.DataStore import com.zjutjh.ijh.datastore.converter.asLocalModel +import com.zjutjh.ijh.datastore.model.IJhPreference +import com.zjutjh.ijh.datastore.model.IJhPreferenceKt import com.zjutjh.ijh.datastore.model.LocalSession -import com.zjutjh.ijh.datastore.model.WeJhPreference -import com.zjutjh.ijh.datastore.model.WeJhPreferenceKt +import com.zjutjh.ijh.datastore.model.LocalWeJhUser import com.zjutjh.ijh.datastore.model.copy import com.zjutjh.ijh.model.CampusInfo import com.zjutjh.ijh.model.WeJhUser @@ -12,34 +13,34 @@ import kotlinx.coroutines.flow.Flow import java.time.ZonedDateTime import javax.inject.Inject -class WeJhPreferenceDataSource @Inject constructor(private val dataStore: DataStore) { +class IJhPreferenceDataSource @Inject constructor(private val dataStore: DataStore) { - val data: Flow = dataStore.data + val data: Flow = dataStore.data - suspend fun setUser(user: WeJhUser) { + suspend fun setWeJhUser(user: WeJhUser) { dataStore.updateData { it.copy { - this.user = user.asLocalModel() + this.weJhUser = user.asLocalModel() } } } - suspend fun setUser(user: WeJhPreference.User) { + suspend fun setWeJhUser(user: LocalWeJhUser) { dataStore.updateData { - it.copy { this.user = user } + it.copy { this.weJhUser = user } } } - suspend fun updateUser(transform: suspend (WeJhPreference.User) -> WeJhPreference.User) = + suspend fun updateWeJhUser(transform: suspend (LocalWeJhUser) -> LocalWeJhUser) = dataStore.updateData { - it.copy { this.user = transform(it.user) } + it.copy { this.weJhUser = transform(it.weJhUser) } } - suspend fun deleteUser() { + suspend fun deleteWeJhUser() { dataStore.updateData { it.copy { - clearUser() + clearWeJhUser() } } } @@ -47,7 +48,7 @@ class WeJhPreferenceDataSource @Inject constructor(private val dataStore: DataSt suspend fun setCoursesLastSyncTime(time: ZonedDateTime) { dataStore.updateData { it.copy { - coursesLastSyncTime = time.toEpochSecond() + coursesSyncTime = time.toEpochSecond() } } } @@ -55,42 +56,42 @@ class WeJhPreferenceDataSource @Inject constructor(private val dataStore: DataSt suspend fun deleteCoursesLastSyncTime() = dataStore.updateData { it.copy { - clearCoursesLastSyncTime() + clearCoursesSyncTime() } } - private suspend fun setInfo(info: WeJhPreference.Info) = + private suspend fun setCampus(info: IJhPreference.Campus) = dataStore.updateData { it.copy { - this.info = info + this.campus = info } } - suspend fun setInfo(info: CampusInfo) = - setInfo(info.asLocalModel()) + suspend fun setCampus(info: CampusInfo) = + setCampus(info.asLocalModel()) - suspend fun deleteInfo() = + suspend fun deleteCampus() = dataStore.updateData { it.copy { - clearInfo() + clearCampus() } } suspend fun setSession(session: LocalSession) = dataStore.updateData { it.copy { - this.session = session + this.weJhSession = session } } suspend fun deleteSession() = dataStore.updateData { it.copy { - clearSession() + clearWeJhSession() } } - private suspend fun setCard(card: WeJhPreference.Card) = + private suspend fun setCard(card: IJhPreference.Card) = dataStore.updateData { it.copy { this.card = card @@ -98,9 +99,9 @@ class WeJhPreferenceDataSource @Inject constructor(private val dataStore: DataSt } suspend fun setCard(balance: String, syncTime: ZonedDateTime) = - setCard(WeJhPreferenceKt.card { + setCard(IJhPreferenceKt.card { this.balance = balance - lastSyncTime = syncTime.toEpochSecond() + this.syncTime = syncTime.toEpochSecond() }) suspend fun deleteCard() = diff --git a/core/datastore/src/main/kotlin/com/zjutjh/ijh/datastore/WeJhPreferenceSerializer.kt b/core/datastore/src/main/kotlin/com/zjutjh/ijh/datastore/WeJhPreferenceSerializer.kt index a925c6b..1f8376d 100644 --- a/core/datastore/src/main/kotlin/com/zjutjh/ijh/datastore/WeJhPreferenceSerializer.kt +++ b/core/datastore/src/main/kotlin/com/zjutjh/ijh/datastore/WeJhPreferenceSerializer.kt @@ -3,22 +3,22 @@ package com.zjutjh.ijh.datastore import androidx.datastore.core.CorruptionException import androidx.datastore.core.Serializer import com.google.protobuf.InvalidProtocolBufferException -import com.zjutjh.ijh.datastore.model.WeJhPreference +import com.zjutjh.ijh.datastore.model.IJhPreference import java.io.InputStream import java.io.OutputStream import javax.inject.Inject -class WeJhPreferenceSerializer @Inject constructor() : Serializer { +class WeJhPreferenceSerializer @Inject constructor() : Serializer { - override val defaultValue: WeJhPreference = WeJhPreference.getDefaultInstance() + override val defaultValue: IJhPreference = IJhPreference.getDefaultInstance() - override suspend fun readFrom(input: InputStream): WeJhPreference = + override suspend fun readFrom(input: InputStream): IJhPreference = try { - WeJhPreference.parseFrom(input) + IJhPreference.parseFrom(input) } catch (exception: InvalidProtocolBufferException) { throw CorruptionException("Cannot read proto.", exception) } - override suspend fun writeTo(t: WeJhPreference, output: OutputStream) = + override suspend fun writeTo(t: IJhPreference, output: OutputStream) = t.writeTo(output) } \ No newline at end of file diff --git a/core/datastore/src/main/kotlin/com/zjutjh/ijh/datastore/converter/WeJhInfo.kt b/core/datastore/src/main/kotlin/com/zjutjh/ijh/datastore/converter/WeJhInfo.kt index 6b50854..d3b3fa2 100644 --- a/core/datastore/src/main/kotlin/com/zjutjh/ijh/datastore/converter/WeJhInfo.kt +++ b/core/datastore/src/main/kotlin/com/zjutjh/ijh/datastore/converter/WeJhInfo.kt @@ -1,25 +1,25 @@ package com.zjutjh.ijh.datastore.converter -import com.zjutjh.ijh.datastore.model.WeJhPreference -import com.zjutjh.ijh.datastore.model.WeJhPreferenceKt +import com.zjutjh.ijh.datastore.model.IJhPreference +import com.zjutjh.ijh.datastore.model.IJhPreferenceKt import com.zjutjh.ijh.model.CampusInfo import com.zjutjh.ijh.model.toTerm import java.time.LocalDate -fun WeJhPreference.Info.asExternalModel(): CampusInfo = +fun IJhPreference.Campus.asExternalModel(): CampusInfo = CampusInfo( term = term.toTerm(), year = year, termStartDate = LocalDate.ofEpochDay(termStartDate), - lastSyncTime = lastSyncTime.toZonedDateTime(), + lastSyncTime = syncTime.toZonedDateTime(), schoolBusUrl = schoolBusUrl, ) -fun CampusInfo.asLocalModel(): WeJhPreference.Info = - WeJhPreferenceKt.info { +fun CampusInfo.asLocalModel(): IJhPreference.Campus = + IJhPreferenceKt.campus { term = this@asLocalModel.term.ordinal year = this@asLocalModel.year termStartDate = this@asLocalModel.termStartDate.toEpochDay() - lastSyncTime = this@asLocalModel.lastSyncTime.toEpochSecond() + syncTime = this@asLocalModel.lastSyncTime.toEpochSecond() schoolBusUrl = this@asLocalModel.schoolBusUrl } diff --git a/core/datastore/src/main/kotlin/com/zjutjh/ijh/datastore/converter/WeJhUser.kt b/core/datastore/src/main/kotlin/com/zjutjh/ijh/datastore/converter/WeJhUser.kt index 0027771..fc03211 100644 --- a/core/datastore/src/main/kotlin/com/zjutjh/ijh/datastore/converter/WeJhUser.kt +++ b/core/datastore/src/main/kotlin/com/zjutjh/ijh/datastore/converter/WeJhUser.kt @@ -1,13 +1,14 @@ package com.zjutjh.ijh.datastore.converter -import com.zjutjh.ijh.datastore.model.WeJhPreference -import com.zjutjh.ijh.datastore.model.WeJhPreferenceKt +import com.zjutjh.ijh.datastore.model.LocalWeJhUser +import com.zjutjh.ijh.datastore.model.LocalWeJhUserKt +import com.zjutjh.ijh.datastore.model.localWeJhUser import com.zjutjh.ijh.model.WeJhUser import java.time.Instant import java.time.ZoneOffset import java.time.ZonedDateTime -fun WeJhPreference.User.asExternalModel() = WeJhUser( +fun LocalWeJhUser.asExternalModel() = WeJhUser( uid = uid, username = username, studentId = studentId, @@ -19,14 +20,14 @@ fun WeJhPreference.User.asExternalModel() = WeJhUser( bind = bind.asExternalModel(), ) -fun WeJhPreference.User.Bind.asExternalModel() = WeJhUser.Bind( +fun LocalWeJhUser.Bind.asExternalModel() = WeJhUser.Bind( lib = lib, yxy = yxy, zf = zf, ) -fun WeJhUser.asLocalModel(): WeJhPreference.User = - WeJhPreferenceKt.user { +fun WeJhUser.asLocalModel(): LocalWeJhUser = + localWeJhUser { uid = this@asLocalModel.uid username = this@asLocalModel.username studentId = this@asLocalModel.studentId @@ -36,8 +37,8 @@ fun WeJhUser.asLocalModel(): WeJhPreference.User = bind = this@asLocalModel.bind.asLocalModel() } -fun WeJhUser.Bind.asLocalModel(): WeJhPreference.User.Bind = - WeJhPreferenceKt.UserKt.bind { +fun WeJhUser.Bind.asLocalModel(): LocalWeJhUser.Bind = + LocalWeJhUserKt.bind { lib = this@asLocalModel.lib yxy = this@asLocalModel.yxy zf = this@asLocalModel.zf diff --git a/core/datastore/src/main/kotlin/com/zjutjh/ijh/datastore/di/DataStoreModule.kt b/core/datastore/src/main/kotlin/com/zjutjh/ijh/datastore/di/DataStoreModule.kt index 8842a92..db95063 100644 --- a/core/datastore/src/main/kotlin/com/zjutjh/ijh/datastore/di/DataStoreModule.kt +++ b/core/datastore/src/main/kotlin/com/zjutjh/ijh/datastore/di/DataStoreModule.kt @@ -5,7 +5,7 @@ import androidx.datastore.core.DataStore import androidx.datastore.core.DataStoreFactory import androidx.datastore.dataStoreFile import com.zjutjh.ijh.datastore.WeJhPreferenceSerializer -import com.zjutjh.ijh.datastore.model.WeJhPreference +import com.zjutjh.ijh.datastore.model.IJhPreference import dagger.Module import dagger.Provides import dagger.hilt.InstallIn @@ -16,17 +16,17 @@ import javax.inject.Singleton @Module @InstallIn(SingletonComponent::class) object DataStoreModule { - private const val WE_JH_FILENAME = "we_jh.pb" + private const val IJH_FILENAME = "ijh.pb" @Provides @Singleton - fun provideWeJhDataStore( + fun provideIJhDataStore( @ApplicationContext context: Context, serializer: WeJhPreferenceSerializer - ): DataStore = + ): DataStore = DataStoreFactory.create( serializer, ) { - context.dataStoreFile(WE_JH_FILENAME) + context.dataStoreFile(IJH_FILENAME) } } \ No newline at end of file diff --git a/core/datastore/src/main/proto/com/zjutjh/ijh/datastore/i_jh_preference.proto b/core/datastore/src/main/proto/com/zjutjh/ijh/datastore/i_jh_preference.proto new file mode 100644 index 0000000..b2c0bc3 --- /dev/null +++ b/core/datastore/src/main/proto/com/zjutjh/ijh/datastore/i_jh_preference.proto @@ -0,0 +1,32 @@ +syntax = "proto3"; + +import "com/zjutjh/ijh/datastore/local_session.proto"; +import "com/zjutjh/ijh/datastore/local_we_jh_user.proto"; + +option java_package = "com.zjutjh.ijh.datastore.model"; +option java_multiple_files = true; + +message IJhPreference { + optional Campus campus = 1; + + optional LocalWeJhUser we_jh_user = 2; + optional LocalSession we_jh_session = 3; + + optional uint64 courses_sync_time = 4; + + optional Card card = 5; + + // Campus info + message Campus { + uint32 year = 1; + uint32 term = 2; + uint64 term_start_date = 3; + uint64 sync_time = 4; + string school_bus_url = 5; + } + + message Card { + string balance = 1; + uint64 sync_time = 2; + } +} \ No newline at end of file diff --git a/core/datastore/src/main/proto/com/zjutjh/ijh/datastore/local_we_jh_user.proto b/core/datastore/src/main/proto/com/zjutjh/ijh/datastore/local_we_jh_user.proto new file mode 100644 index 0000000..3754758 --- /dev/null +++ b/core/datastore/src/main/proto/com/zjutjh/ijh/datastore/local_we_jh_user.proto @@ -0,0 +1,20 @@ +syntax = "proto3"; + +option java_package = "com.zjutjh.ijh.datastore.model"; +option java_multiple_files = true; + +message LocalWeJhUser { + string username = 1; + int64 uid = 2; + string student_id = 3; + uint64 create_time = 4; + string phone_number = 5; + uint32 user_type = 6; + Bind bind = 7; + + message Bind { + bool zf = 1; + bool lib = 2; + bool yxy = 3; + } +} \ No newline at end of file diff --git a/core/datastore/src/main/proto/com/zjutjh/ijh/datastore/we_jh_preference.proto b/core/datastore/src/main/proto/com/zjutjh/ijh/datastore/we_jh_preference.proto deleted file mode 100644 index cbead93..0000000 --- a/core/datastore/src/main/proto/com/zjutjh/ijh/datastore/we_jh_preference.proto +++ /dev/null @@ -1,46 +0,0 @@ -syntax = "proto3"; - -import "com/zjutjh/ijh/datastore/local_session.proto"; - -option java_package = "com.zjutjh.ijh.datastore.model"; -option java_multiple_files = true; - -message WeJhPreference { - optional Info info = 1; - optional User user = 2; - optional LocalSession session = 3; - - optional uint64 courses_last_sync_time = 4; - - optional Card card = 5; - - // Campus info - message Info { - uint32 year = 1; - uint32 term = 2; - uint64 term_start_date = 3; - uint64 last_sync_time = 4; - string school_bus_url = 5; - } - - message Card { - string balance = 1; - uint64 last_sync_time = 2; - } - - message User { - string username = 1; - int64 uid = 2; - string student_id = 3; - uint64 create_time = 4; - string phone_number = 5; - uint32 user_type = 6; - Bind bind = 7; - - message Bind { - bool zf = 1; - bool lib = 2; - bool yxy = 3; - } - } -} \ No newline at end of file diff --git a/core/network/src/main/kotlin/com/zjutjh/ijh/network/CampusInfoNetworkDataSource.kt b/core/network/src/main/kotlin/com/zjutjh/ijh/network/CampusInfoNetworkDataSource.kt deleted file mode 100644 index 3e469d2..0000000 --- a/core/network/src/main/kotlin/com/zjutjh/ijh/network/CampusInfoNetworkDataSource.kt +++ /dev/null @@ -1,12 +0,0 @@ -package com.zjutjh.ijh.network - -import com.zjutjh.ijh.network.model.WeJhCampusInfo -import com.zjutjh.ijh.network.service.WeJhBasicService -import javax.inject.Inject - -/** - * Get campus info from network - */ -class CampusInfoNetworkDataSource @Inject constructor(private val weJhBasicService: WeJhBasicService) { - suspend fun getInfo(): WeJhCampusInfo = weJhBasicService.getInfo() -} \ No newline at end of file diff --git a/core/network/src/main/kotlin/com/zjutjh/ijh/network/CampusNetworkDataSource.kt b/core/network/src/main/kotlin/com/zjutjh/ijh/network/CampusNetworkDataSource.kt new file mode 100644 index 0000000..4b9862b --- /dev/null +++ b/core/network/src/main/kotlin/com/zjutjh/ijh/network/CampusNetworkDataSource.kt @@ -0,0 +1,12 @@ +package com.zjutjh.ijh.network + +import com.zjutjh.ijh.network.model.NetworkCampusInfo +import com.zjutjh.ijh.network.service.WeJhBasicService +import javax.inject.Inject + +/** + * Get campus info from network + */ +class CampusNetworkDataSource @Inject constructor(private val weJhBasicService: WeJhBasicService) { + suspend fun getInfo(): NetworkCampusInfo = weJhBasicService.getInfo() +} \ No newline at end of file diff --git a/core/network/src/main/kotlin/com/zjutjh/ijh/network/CardInfoDataSource.kt b/core/network/src/main/kotlin/com/zjutjh/ijh/network/CardNetworkDataSource.kt similarity index 86% rename from core/network/src/main/kotlin/com/zjutjh/ijh/network/CardInfoDataSource.kt rename to core/network/src/main/kotlin/com/zjutjh/ijh/network/CardNetworkDataSource.kt index 50b6958..809b811 100644 --- a/core/network/src/main/kotlin/com/zjutjh/ijh/network/CardInfoDataSource.kt +++ b/core/network/src/main/kotlin/com/zjutjh/ijh/network/CardNetworkDataSource.kt @@ -6,7 +6,7 @@ import java.time.format.DateTimeFormatter import java.util.Date import javax.inject.Inject -class CardInfoDataSource @Inject constructor(private val cardService: WeJhCardService) { +class CardNetworkDataSource @Inject constructor(private val cardService: WeJhCardService) { suspend fun getBalance(): String = cardService.getBalance() diff --git a/core/network/src/main/kotlin/com/zjutjh/ijh/network/CourseNetworkDataSource.kt b/core/network/src/main/kotlin/com/zjutjh/ijh/network/CourseNetworkDataSource.kt index 779e4c6..07cb4d8 100644 --- a/core/network/src/main/kotlin/com/zjutjh/ijh/network/CourseNetworkDataSource.kt +++ b/core/network/src/main/kotlin/com/zjutjh/ijh/network/CourseNetworkDataSource.kt @@ -1,13 +1,13 @@ package com.zjutjh.ijh.network -import com.zjutjh.ijh.network.model.ZfClassTable +import com.zjutjh.ijh.network.model.NetworkClassTable import com.zjutjh.ijh.network.service.WeJhZfService -import com.zjutjh.ijh.network.service.request.GetClassTableBody import javax.inject.Inject class CourseNetworkDataSource @Inject constructor(private val service: WeJhZfService) { - suspend fun getZfClassTable(year: String, term: String): ZfClassTable = service.getClassTable( - GetClassTableBody(year, term) - ) + suspend fun getClassTable(year: String, term: String): NetworkClassTable = + service.getClassTable( + WeJhZfService.GetClassTableBody(year, term) + ) } \ No newline at end of file diff --git a/core/network/src/main/kotlin/com/zjutjh/ijh/network/ElectricityNetworkDataSource.kt b/core/network/src/main/kotlin/com/zjutjh/ijh/network/ElectricityNetworkDataSource.kt new file mode 100644 index 0000000..5261cd0 --- /dev/null +++ b/core/network/src/main/kotlin/com/zjutjh/ijh/network/ElectricityNetworkDataSource.kt @@ -0,0 +1,11 @@ +package com.zjutjh.ijh.network + +import com.zjutjh.ijh.network.model.NetworkElectricityBalance +import com.zjutjh.ijh.network.service.WeJhElectricityService +import javax.inject.Inject + +class ElectricityNetworkDataSource @Inject constructor(private val weJhElectricityService: WeJhElectricityService) { + + suspend fun getBalance(): NetworkElectricityBalance = weJhElectricityService.getBalance() + +} \ No newline at end of file diff --git a/core/network/src/main/kotlin/com/zjutjh/ijh/network/WeJhUserNetworkDataSource.kt b/core/network/src/main/kotlin/com/zjutjh/ijh/network/WeJhUserNetworkDataSource.kt index 6014550..c236da1 100644 --- a/core/network/src/main/kotlin/com/zjutjh/ijh/network/WeJhUserNetworkDataSource.kt +++ b/core/network/src/main/kotlin/com/zjutjh/ijh/network/WeJhUserNetworkDataSource.kt @@ -2,7 +2,6 @@ package com.zjutjh.ijh.network import com.zjutjh.ijh.network.model.NetworkWeJhUser import com.zjutjh.ijh.network.service.WeJhUserService -import com.zjutjh.ijh.network.service.request.LoginBody import javax.inject.Inject class WeJhUserNetworkDataSource @Inject constructor( @@ -10,7 +9,7 @@ class WeJhUserNetworkDataSource @Inject constructor( ) { suspend fun login(username: String, password: String): NetworkWeJhUser = - weJhUserService.login(LoginBody(username, password)).user + weJhUserService.login(WeJhUserService.LoginBody(username, password)).user suspend fun loginBySession(): NetworkWeJhUser = weJhUserService.loginBySession().user diff --git a/core/network/src/main/kotlin/com/zjutjh/ijh/network/cookie/WeJhCookieJar.kt b/core/network/src/main/kotlin/com/zjutjh/ijh/network/cookie/WeJhCookieJar.kt index 1c1bc17..b9e7724 100644 --- a/core/network/src/main/kotlin/com/zjutjh/ijh/network/cookie/WeJhCookieJar.kt +++ b/core/network/src/main/kotlin/com/zjutjh/ijh/network/cookie/WeJhCookieJar.kt @@ -1,9 +1,9 @@ package com.zjutjh.ijh.network.cookie import android.util.Log -import com.zjutjh.ijh.datastore.WeJhPreferenceDataSource +import com.zjutjh.ijh.datastore.IJhPreferenceDataSource import com.zjutjh.ijh.datastore.model.localSession -import com.zjutjh.ijh.datastore.model.sessionOrNull +import com.zjutjh.ijh.datastore.model.weJhSessionOrNull import com.zjutjh.ijh.network.BuildConfig import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -21,14 +21,19 @@ import java.io.IOException import javax.inject.Inject import javax.inject.Singleton +/** + * To simplify the cookie/session management, we use a cookie jar bind to the [IJhPreferenceDataSource]. + * Although theoretically no other data sources are allowed in the network module. + * Therefore, we strictly enforce that only [CookieJar] can access local data sources. + */ @Singleton -class WeJhCookieJar @Inject constructor(private val local: WeJhPreferenceDataSource) : +class WeJhCookieJar @Inject constructor(private val local: IJhPreferenceDataSource) : CookieJar { private val scope = CoroutineScope(Dispatchers.Default + SupervisorJob()) private val tokenFlow: StateFlow = local.data - .map { it.sessionOrNull?.token ?: String() } + .map { it.weJhSessionOrNull?.token ?: String() } .stateIn( scope = scope, started = SharingStarted.Eagerly, diff --git a/core/network/src/main/kotlin/com/zjutjh/ijh/network/model/WeJhCampusInfo.kt b/core/network/src/main/kotlin/com/zjutjh/ijh/network/model/NetworkCampusInfo.kt similarity index 89% rename from core/network/src/main/kotlin/com/zjutjh/ijh/network/model/WeJhCampusInfo.kt rename to core/network/src/main/kotlin/com/zjutjh/ijh/network/model/NetworkCampusInfo.kt index 198e7e8..b3f7c23 100644 --- a/core/network/src/main/kotlin/com/zjutjh/ijh/network/model/WeJhCampusInfo.kt +++ b/core/network/src/main/kotlin/com/zjutjh/ijh/network/model/NetworkCampusInfo.kt @@ -11,7 +11,7 @@ import java.time.ZonedDateTime * WeJH campus info */ @JsonClass(generateAdapter = true) -data class WeJhCampusInfo( +data class NetworkCampusInfo( @Json(name = "is_begin") val isBegin: Boolean, val term: String, @@ -22,7 +22,7 @@ data class WeJhCampusInfo( val schoolBusUrl: String, ) -fun WeJhCampusInfo.asExternalModel(): CampusInfo = +fun NetworkCampusInfo.asExternalModel(): CampusInfo = CampusInfo( term = term.toTerm(), year = termYear.toInt(), diff --git a/core/network/src/main/kotlin/com/zjutjh/ijh/network/model/ZfClassTable.kt b/core/network/src/main/kotlin/com/zjutjh/ijh/network/model/NetworkClassTable.kt similarity index 95% rename from core/network/src/main/kotlin/com/zjutjh/ijh/network/model/ZfClassTable.kt rename to core/network/src/main/kotlin/com/zjutjh/ijh/network/model/NetworkClassTable.kt index 9887700..83096c8 100644 --- a/core/network/src/main/kotlin/com/zjutjh/ijh/network/model/ZfClassTable.kt +++ b/core/network/src/main/kotlin/com/zjutjh/ijh/network/model/NetworkClassTable.kt @@ -11,7 +11,7 @@ import java.time.DayOfWeek * **Note:** Due to Zf class table is network-only model, so omit the `Network` prefix. */ @JsonClass(generateAdapter = true) -data class ZfClassTable( +data class NetworkClassTable( val info: Info, val lessonsTable: List?, val practiceLessons: List?, @@ -55,7 +55,7 @@ data class ZfClassTable( /** * @throws CourseParseException */ -fun ZfClassTable.LessonsTable.parseWeekString(): CourseWeek { +fun NetworkClassTable.LessonsTable.parseWeekString(): CourseWeek { val singles: ArrayList = ArrayList() val ranges: ArrayList = ArrayList() try { @@ -102,7 +102,7 @@ fun ZfClassTable.LessonsTable.parseWeekString(): CourseWeek { /** * @throws CourseParseException */ -fun ZfClassTable.LessonsTable.asExternalModel(): Course { +fun NetworkClassTable.LessonsTable.asExternalModel(): Course { val section = sections.split('-') if (section.size != 2) { throw CourseParseException("Invalid class section format.") diff --git a/core/network/src/main/kotlin/com/zjutjh/ijh/network/model/NetworkElectricityBalance.kt b/core/network/src/main/kotlin/com/zjutjh/ijh/network/model/NetworkElectricityBalance.kt new file mode 100644 index 0000000..8d710c4 --- /dev/null +++ b/core/network/src/main/kotlin/com/zjutjh/ijh/network/model/NetworkElectricityBalance.kt @@ -0,0 +1,22 @@ +package com.zjutjh.ijh.network.model + +import com.squareup.moshi.JsonClass + +@JsonClass(generateAdapter = true) +data class NetworkElectricityBalance( + val areaID: String, + val buildingCode: String, + val displayRoomName: String, + val floorCode: String, + val mdName: String, + val mdType: String, + val roomCode: String, + val roomStatus: String, + val schoolCode: String, + val soc: Double, + val socAmount: Double, + val subsidy: Long, + val subsidyAmount: Long, + val surplus: Double, + val surplusAmount: Double +) \ No newline at end of file diff --git a/core/network/src/main/kotlin/com/zjutjh/ijh/network/service/WeJhBasicService.kt b/core/network/src/main/kotlin/com/zjutjh/ijh/network/service/WeJhBasicService.kt index d456c63..cfab5f2 100644 --- a/core/network/src/main/kotlin/com/zjutjh/ijh/network/service/WeJhBasicService.kt +++ b/core/network/src/main/kotlin/com/zjutjh/ijh/network/service/WeJhBasicService.kt @@ -1,6 +1,6 @@ package com.zjutjh.ijh.network.service -import com.zjutjh.ijh.network.model.WeJhCampusInfo +import com.zjutjh.ijh.network.model.NetworkCampusInfo import retrofit2.http.POST /** @@ -8,5 +8,5 @@ import retrofit2.http.POST */ interface WeJhBasicService { @POST("info") - suspend fun getInfo(): WeJhCampusInfo + suspend fun getInfo(): NetworkCampusInfo } \ No newline at end of file diff --git a/core/network/src/main/kotlin/com/zjutjh/ijh/network/service/WeJhElectricityService.kt b/core/network/src/main/kotlin/com/zjutjh/ijh/network/service/WeJhElectricityService.kt new file mode 100644 index 0000000..6e2698a --- /dev/null +++ b/core/network/src/main/kotlin/com/zjutjh/ijh/network/service/WeJhElectricityService.kt @@ -0,0 +1,10 @@ +package com.zjutjh.ijh.network.service + +import com.zjutjh.ijh.network.model.NetworkElectricityBalance +import retrofit2.http.GET + +interface WeJhElectricityService { + + @GET("balance") + suspend fun getBalance(): NetworkElectricityBalance +} \ No newline at end of file diff --git a/core/network/src/main/kotlin/com/zjutjh/ijh/network/service/WeJhUserService.kt b/core/network/src/main/kotlin/com/zjutjh/ijh/network/service/WeJhUserService.kt index 7470abb..52821bf 100644 --- a/core/network/src/main/kotlin/com/zjutjh/ijh/network/service/WeJhUserService.kt +++ b/core/network/src/main/kotlin/com/zjutjh/ijh/network/service/WeJhUserService.kt @@ -1,7 +1,7 @@ package com.zjutjh.ijh.network.service -import com.zjutjh.ijh.network.service.request.LoginBody -import com.zjutjh.ijh.network.service.response.WeJhUserResult +import com.squareup.moshi.JsonClass +import com.zjutjh.ijh.network.model.NetworkWeJhUser import retrofit2.http.Body import retrofit2.http.POST @@ -15,4 +15,15 @@ interface WeJhUserService { @POST("login/session") suspend fun loginBySession(): WeJhUserResult + + @JsonClass(generateAdapter = true) + data class LoginBody( + val username: String, + val password: String, + ) + + @JsonClass(generateAdapter = true) + data class WeJhUserResult( + val user: NetworkWeJhUser, + ) } \ No newline at end of file diff --git a/core/network/src/main/kotlin/com/zjutjh/ijh/network/service/WeJhZfService.kt b/core/network/src/main/kotlin/com/zjutjh/ijh/network/service/WeJhZfService.kt index 4b6f96e..063f003 100644 --- a/core/network/src/main/kotlin/com/zjutjh/ijh/network/service/WeJhZfService.kt +++ b/core/network/src/main/kotlin/com/zjutjh/ijh/network/service/WeJhZfService.kt @@ -1,7 +1,7 @@ package com.zjutjh.ijh.network.service -import com.zjutjh.ijh.network.model.ZfClassTable -import com.zjutjh.ijh.network.service.request.GetClassTableBody +import com.squareup.moshi.JsonClass +import com.zjutjh.ijh.network.model.NetworkClassTable import retrofit2.http.Body import retrofit2.http.POST @@ -11,5 +11,11 @@ import retrofit2.http.POST interface WeJhZfService { @POST("classtable") - suspend fun getClassTable(@Body body: GetClassTableBody): ZfClassTable + suspend fun getClassTable(@Body body: GetClassTableBody): NetworkClassTable + + @JsonClass(generateAdapter = true) + data class GetClassTableBody( + val year: String, + val term: String, + ) } \ No newline at end of file diff --git a/core/network/src/main/kotlin/com/zjutjh/ijh/network/service/di/ServiceModule.kt b/core/network/src/main/kotlin/com/zjutjh/ijh/network/service/di/ServiceModule.kt index e1169d3..04cc16f 100644 --- a/core/network/src/main/kotlin/com/zjutjh/ijh/network/service/di/ServiceModule.kt +++ b/core/network/src/main/kotlin/com/zjutjh/ijh/network/service/di/ServiceModule.kt @@ -5,6 +5,7 @@ import com.zjutjh.ijh.network.di.DefaultOkHttpClient import com.zjutjh.ijh.network.di.WeJhAuthorizedOkHttpClient import com.zjutjh.ijh.network.service.WeJhBasicService import com.zjutjh.ijh.network.service.WeJhCardService +import com.zjutjh.ijh.network.service.WeJhElectricityService import com.zjutjh.ijh.network.service.WeJhUserService import com.zjutjh.ijh.network.service.WeJhZfService import dagger.Module @@ -68,4 +69,15 @@ object ServiceModule { .client(client) .build() .create() + + @Provides + @Singleton + fun provideWeJhElectricityService( + @WeJhAuthorizedOkHttpClient client: OkHttpClient + ): WeJhElectricityService = + retrofitCommonBuilder() + .baseUrl(BuildConfig.WE_JH_API_BASE_URL + "func/electricity/") + .client(client) + .build() + .create() } \ No newline at end of file diff --git a/core/network/src/main/kotlin/com/zjutjh/ijh/network/service/request/LoginBody.kt b/core/network/src/main/kotlin/com/zjutjh/ijh/network/service/request/LoginBody.kt deleted file mode 100644 index ab95171..0000000 --- a/core/network/src/main/kotlin/com/zjutjh/ijh/network/service/request/LoginBody.kt +++ /dev/null @@ -1,9 +0,0 @@ -package com.zjutjh.ijh.network.service.request - -import com.squareup.moshi.JsonClass - -@JsonClass(generateAdapter = true) -data class LoginBody( - val username: String, - val password: String, -) \ No newline at end of file diff --git a/core/network/src/main/kotlin/com/zjutjh/ijh/network/service/request/WeJhZfServiceRequests.kt b/core/network/src/main/kotlin/com/zjutjh/ijh/network/service/request/WeJhZfServiceRequests.kt deleted file mode 100644 index bf5f836..0000000 --- a/core/network/src/main/kotlin/com/zjutjh/ijh/network/service/request/WeJhZfServiceRequests.kt +++ /dev/null @@ -1,9 +0,0 @@ -package com.zjutjh.ijh.network.service.request - -import com.squareup.moshi.JsonClass - -@JsonClass(generateAdapter = true) -data class GetClassTableBody( - val year: String, - val term: String, -) \ No newline at end of file diff --git a/core/network/src/main/kotlin/com/zjutjh/ijh/network/service/response/WeJhUserResult.kt b/core/network/src/main/kotlin/com/zjutjh/ijh/network/service/response/WeJhUserResult.kt deleted file mode 100644 index 8647053..0000000 --- a/core/network/src/main/kotlin/com/zjutjh/ijh/network/service/response/WeJhUserResult.kt +++ /dev/null @@ -1,9 +0,0 @@ -package com.zjutjh.ijh.network.service.response - -import com.squareup.moshi.JsonClass -import com.zjutjh.ijh.network.model.NetworkWeJhUser - -@JsonClass(generateAdapter = true) -data class WeJhUserResult( - val user: NetworkWeJhUser, -) \ No newline at end of file diff --git a/core/network/src/test/kotlin/com/zjutjh/ijh/network/model/ZfClassTableUnitTest.kt b/core/network/src/test/kotlin/com/zjutjh/ijh/network/model/NetworkClassTableUnitTest.kt similarity index 96% rename from core/network/src/test/kotlin/com/zjutjh/ijh/network/model/ZfClassTableUnitTest.kt rename to core/network/src/test/kotlin/com/zjutjh/ijh/network/model/NetworkClassTableUnitTest.kt index 36204bc..5e322e7 100644 --- a/core/network/src/test/kotlin/com/zjutjh/ijh/network/model/ZfClassTableUnitTest.kt +++ b/core/network/src/test/kotlin/com/zjutjh/ijh/network/model/NetworkClassTableUnitTest.kt @@ -3,8 +3,8 @@ package com.zjutjh.ijh.network.model import org.junit.Assert import org.junit.Test -class ZfClassTableUnitTest { - private fun table(week: String) = ZfClassTable.LessonsTable( +class NetworkClassTableUnitTest { + private fun table(week: String) = NetworkClassTable.LessonsTable( "紫金港", "计算机科学与技术(计算机科学与技术)", "2019211300",