mirror of
https://github.com/szkolny-eu/szkolny-android.git
synced 2024-11-24 10:54:36 -06:00
[APIv2] Add sparse array extensions - simplify getting data from database.
This commit is contained in:
parent
7a06593821
commit
f0bf6b8b81
@ -6,7 +6,10 @@ import android.content.Context
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.util.LongSparseArray
|
||||
import android.util.SparseArray
|
||||
import androidx.core.app.ActivityCompat
|
||||
import androidx.core.util.forEach
|
||||
import com.google.gson.JsonArray
|
||||
import com.google.gson.JsonElement
|
||||
import com.google.gson.JsonObject
|
||||
@ -135,4 +138,42 @@ const val HOUR = 60L*MINUTE
|
||||
const val DAY = 24L*HOUR
|
||||
const val WEEK = 7L*DAY
|
||||
const val MONTH = 30L*DAY
|
||||
const val YEAR = 365L*DAY
|
||||
const val YEAR = 365L*DAY
|
||||
|
||||
fun <T> LongSparseArray<T>.values(): List<T> {
|
||||
val result = mutableListOf<T>()
|
||||
forEach { _, value ->
|
||||
result += value
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fun <T> SparseArray<T>.values(): List<T> {
|
||||
val result = mutableListOf<T>()
|
||||
forEach { _, value ->
|
||||
result += value
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fun <T> List<T>.toSparseArray(destination: SparseArray<T>, key: (T) -> Int) {
|
||||
forEach {
|
||||
destination.put(key(it), it)
|
||||
}
|
||||
}
|
||||
fun <T> List<T>.toSparseArray(destination: LongSparseArray<T>, key: (T) -> Long) {
|
||||
forEach {
|
||||
destination.put(key(it), it)
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> List<T>.toSparseArray(key: (T) -> Int): SparseArray<T> {
|
||||
val result = SparseArray<T>()
|
||||
toSparseArray(result, key)
|
||||
return result
|
||||
}
|
||||
fun <T> List<T>.toSparseArray(key: (T) -> Long): LongSparseArray<T> {
|
||||
val result = LongSparseArray<T>()
|
||||
toSparseArray(result, key)
|
||||
return result
|
||||
}
|
@ -41,7 +41,8 @@ class LibrusApiSchools(override val data: DataLibrus,
|
||||
val range = rangeEl.asJsonObject
|
||||
val from = range.getString("From") ?: return@forEachIndexed
|
||||
val to = range.getString("To") ?: return@forEachIndexed
|
||||
data.lessonRanges.add(
|
||||
data.lessonRanges.put(
|
||||
index,
|
||||
LessonRange(
|
||||
profileId,
|
||||
index,
|
||||
|
@ -1,6 +1,7 @@
|
||||
package pl.szczodrzynski.edziennik.api.v2.models
|
||||
|
||||
import android.util.LongSparseArray
|
||||
import android.util.SparseArray
|
||||
import androidx.core.util.forEach
|
||||
import androidx.core.util.isNotEmpty
|
||||
import com.google.gson.JsonObject
|
||||
@ -27,7 +28,9 @@ import pl.szczodrzynski.edziennik.data.db.modules.profiles.Profile
|
||||
import pl.szczodrzynski.edziennik.data.db.modules.subjects.Subject
|
||||
import pl.szczodrzynski.edziennik.data.db.modules.teachers.Teacher
|
||||
import pl.szczodrzynski.edziennik.data.db.modules.teams.Team
|
||||
import pl.szczodrzynski.edziennik.toSparseArray
|
||||
import pl.szczodrzynski.edziennik.utils.models.Date
|
||||
import pl.szczodrzynski.edziennik.values
|
||||
import java.io.InterruptedIOException
|
||||
import java.net.SocketTimeoutException
|
||||
import java.net.UnknownHostException
|
||||
@ -37,6 +40,9 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore)
|
||||
|
||||
var fakeLogin = false
|
||||
|
||||
val profileId
|
||||
get() = profile?.id ?: -1
|
||||
|
||||
/**
|
||||
* A callback passed to all [Feature]s and [LoginMethod]s
|
||||
*/
|
||||
@ -90,8 +96,8 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore)
|
||||
|
||||
val teacherList = LongSparseArray<Teacher>()
|
||||
val subjectList = LongSparseArray<Subject>()
|
||||
var teamList = mutableListOf<Team>()
|
||||
var lessonRanges = mutableListOf<LessonRange>()
|
||||
var teamList = LongSparseArray<Team>()
|
||||
var lessonRanges = SparseArray<LessonRange>()
|
||||
|
||||
var lessonsToRemove: DataRemoveModel? = null
|
||||
val lessonList = mutableListOf<Lesson>()
|
||||
@ -128,18 +134,11 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore)
|
||||
clear()
|
||||
|
||||
if (profile != null) {
|
||||
db.endpointTimerDao().getAllNow(profile.id).forEach { endpointTimer ->
|
||||
endpointTimers.add(endpointTimer)
|
||||
}
|
||||
|
||||
lessonRanges = db.lessonRangeDao().getAllNow(profile.id).toMutableList()
|
||||
db.teacherDao().getAllNow(profile.id).forEach { teacher ->
|
||||
teacherList.put(teacher.id, teacher)
|
||||
}
|
||||
db.subjectDao().getAllNow(profile.id).forEach { subject ->
|
||||
subjectList.put(subject.id, subject)
|
||||
}
|
||||
teamList = db.teamDao().getAllNow(profile.id).toMutableList()
|
||||
endpointTimers = db.endpointTimerDao().getAllNow(profile.id).toMutableList()
|
||||
db.teacherDao().getAllNow(profileId).toSparseArray(teacherList) { it.id }
|
||||
db.subjectDao().getAllNow(profileId).toSparseArray(subjectList) { it.id }
|
||||
db.teamDao().getAllNow(profileId).toSparseArray(teamList) { it.id }
|
||||
db.lessonRangeDao().getAllNow(profileId).toSparseArray(lessonRanges) { it.lessonNumber }
|
||||
}
|
||||
|
||||
/*val teacher = teachers.byNameFirstLast("Jan Kowalski") ?: Teacher(1, 1, "", "").let {
|
||||
@ -152,11 +151,11 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore)
|
||||
loginMethods.clear()
|
||||
|
||||
endpointTimers.clear()
|
||||
|
||||
teacherList.clear()
|
||||
subjectList.clear()
|
||||
teamList.clear()
|
||||
lessonRanges.clear()
|
||||
|
||||
lessonList.clear()
|
||||
lessonChangeList.clear()
|
||||
gradeCategoryList.clear()
|
||||
@ -180,25 +179,15 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore)
|
||||
db.loginStoreDao().add(loginStore)
|
||||
|
||||
db.endpointTimerDao().addAll(endpointTimers)
|
||||
db.teacherDao().clear(profileId)
|
||||
db.teacherDao().addAll(teacherList.values())
|
||||
db.subjectDao().clear(profileId)
|
||||
db.subjectDao().addAll(subjectList.values())
|
||||
db.teamDao().clear(profileId)
|
||||
db.teamDao().addAll(teamList.values())
|
||||
db.lessonRangeDao().clear(profileId)
|
||||
db.lessonRangeDao().addAll(lessonRanges.values())
|
||||
|
||||
db.lessonRangeDao().addAll(lessonRanges)
|
||||
|
||||
if (teacherList.isNotEmpty()) {
|
||||
val tempList: ArrayList<Teacher> = ArrayList()
|
||||
teacherList.forEach { _, teacher ->
|
||||
tempList.add(teacher)
|
||||
}
|
||||
db.teacherDao().addAll(tempList)
|
||||
}
|
||||
if (subjectList.isNotEmpty()) {
|
||||
val tempList: ArrayList<Subject> = ArrayList()
|
||||
subjectList.forEach { _, subject ->
|
||||
tempList.add(subject)
|
||||
}
|
||||
db.subjectDao().addAll(tempList)
|
||||
}
|
||||
if (teamList.isNotEmpty())
|
||||
db.teamDao().addAll(teamList)
|
||||
if (lessonList.isNotEmpty()) {
|
||||
db.lessonDao().clear(profile.id)
|
||||
db.lessonDao().addAll(lessonList)
|
||||
|
Loading…
Reference in New Issue
Block a user