Skip to content

Commit

Permalink
refactor: datastore and network module
Browse files Browse the repository at this point in the history
  • Loading branch information
I-Info committed Nov 3, 2023
1 parent c511df9 commit 1825b6f
Show file tree
Hide file tree
Showing 42 changed files with 303 additions and 244 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -37,7 +37,7 @@ import javax.inject.Inject

@HiltViewModel
class CourseCalendarViewModel @Inject constructor(
campusInfoRepository: CampusInfoRepository,
campusRepository: CampusRepository,
weJhUserRepository: WeJhUserRepository,
private val courseRepository: CourseRepository,
) : ViewModel() {
Expand All @@ -53,7 +53,7 @@ class CourseCalendarViewModel @Inject constructor(
)

@OptIn(ExperimentalCoroutinesApi::class)
private val localTermDayState = campusInfoRepository.infoStream
private val localTermDayState = campusRepository.infoStream
.mapLatest {
it?.toTermDayState()
}
Expand Down
18 changes: 9 additions & 9 deletions app/src/main/kotlin/com/zjutjh/ijh/ui/viewmodel/HomeViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Unit> = flow {
Expand Down Expand Up @@ -66,7 +66,7 @@ class HomeViewModel @Inject constructor(
private val termLocalRefreshChannel: MutableStateFlow<Unit> = MutableStateFlow(Unit)

@OptIn(ExperimentalCoroutinesApi::class)
val termDayState: StateFlow<LoadResult<TermDayState?>> = campusInfoRepository.infoStream
val termDayState: StateFlow<LoadResult<TermDayState?>> = campusRepository.infoStream
.combine(termLocalRefreshChannel) { t1, _ -> t1 }
.mapLatest {
it?.toTermDayState()
Expand All @@ -93,15 +93,15 @@ class HomeViewModel @Inject constructor(
started = SharingStarted.WhileSubscribed(5_000)
)

val cardBalanceState: StateFlow<LoadResult<String?>> = cardInfoRepository.balanceStream
val cardBalanceState: StateFlow<LoadResult<String?>> = cardRepository.balanceStream
.distinctUntilChanged()
.asLoadResultStateFlow(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5_000),
)

val cardBalanceLastSyncState: StateFlow<LoadResult<Duration?>> =
cardInfoRepository.lastSyncTimeStream
cardRepository.lastSyncTimeStream
.distinctUntilChanged()
.combine(timerFlow) { t1, _ -> t1 }
.map {
Expand Down Expand Up @@ -178,7 +178,7 @@ class HomeViewModel @Inject constructor(
}

private suspend fun refreshTerm(): Pair<Int, Term>? {
runCatching { campusInfoRepository.sync() }
runCatching { campusRepository.sync() }
.fold({
Log.i("Home", "Sync WeJhInfo succeed.")
return it
Expand Down Expand Up @@ -208,7 +208,7 @@ class HomeViewModel @Inject constructor(

private suspend fun refreshCard() {
runCatching {
cardInfoRepository.sync()
cardRepository.sync()
}.fold({
Log.i("Home", "Sync Card succeed.")
}) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class ScheduleWidget : GlanceAppWidget() {
val entryPoint =
EntryPointAccessors.fromApplication<ScheduleWidgetReceiver.Repositories>(context)

val dayStateFlow = entryPoint.campusInfoRepository.infoStream
val dayStateFlow = entryPoint.campusRepository.infoStream
.map { it?.toTermDayState() }
val coursesFlow = dayStateFlow.flatMapLatest {
it ?: return@flatMapLatest flowOf(null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CampusInfo?>

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions core/data/src/main/kotlin/com/zjutjh/ijh/data/di/DataModule.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<CampusInfo?> =
local.data.map { it.campusOrNull?.asExternalModel() }

override suspend fun sync(): Pair<Int, Term> {
val weJhInfo = network.getInfo().asExternalModel()
local.setCampus(weJhInfo)
return Pair(weJhInfo.year, weJhInfo.term)
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<String?> = local.data.map {
it.cardOrNull?.balance
}

override val lastSyncTimeStream: Flow<ZonedDateTime?> = local.data.map {
it.cardOrNull?.lastSyncTime?.toZonedDateTime()
it.cardOrNull?.syncTime?.toZonedDateTime()
}

override suspend fun sync() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {

Expand All @@ -44,15 +44,15 @@ class CourseRepositoryImpl @Inject constructor(

override val lastSyncTimeStream: Flow<ZonedDateTime?> =
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) }

Expand Down
Loading

0 comments on commit 1825b6f

Please sign in to comment.