Skip to content

Commit

Permalink
1.0.3 (#15)
Browse files Browse the repository at this point in the history
## 更新如下
+ [fix
改为使用exposed持久化设置,后续可以用来缓存内容,切换保存路径后,首页会刷新](a566d11)
@1939323749
+
[数据保存方式改为key-value](07d78ff)
@RikaCelery
+
[全部数据的持久化保存](89fac86)
@RikaCelery
+
[下载提示替换为中文](e7b9c62)
@RikaCelery
+
[重定向输出到文件](e84291a)
@RikaCelery
  • Loading branch information
RikaCelery authored Jun 29, 2023
2 parents 73bd747 + c62444c commit 664c035
Show file tree
Hide file tree
Showing 11 changed files with 199 additions and 70 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@ bin/

### 默认在保存项目目录的课程(直接忽略中文字符命名文件) ###
*\u{4e00}-\u{9fa5}*

### 缓存数据库 ###
data.db
12 changes: 10 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ plugins {
group = "rikacelery.github.io"
//MAJOR.MINOR.BUILD
//255.255.65535
version = "1.0.21"
version = "1.0.3"

repositories {
google()
mavenCentral()
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
}
dependencies{

}

kotlin {
jvm {
Expand All @@ -24,7 +27,6 @@ kotlin {
sourceSets {

val jvmMain by getting {

dependencies {
implementation("org.jsoup:jsoup:1.16.1")
val ktorVersion = "2.3.0"
Expand All @@ -41,6 +43,11 @@ kotlin {
implementation(compose.desktop.currentOs)
implementation("org.jetbrains.kotlinx:kotlinx-coroutines:0.19.2")
implementation("com.typesafe:config:1.4.1")

implementation("org.jetbrains.exposed:exposed-core:0.36.2")
implementation("org.jetbrains.exposed:exposed-dao:0.36.2")
implementation("org.jetbrains.exposed:exposed-jdbc:0.36.2")
implementation("org.xerial:sqlite-jdbc:3.36.0.3")
}
}
val jvmTest by getting
Expand All @@ -51,6 +58,7 @@ compose.desktop {
application {
mainClass = "MainKt"
nativeDistributions {
modules("java.sql")//fix ClassNotFound: java.sql.Driver
windows {
perUserInstall = true
shortcut = true
Expand Down
11 changes: 7 additions & 4 deletions src/jvmMain/kotlin/Downloader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ suspend fun downloadVideo(folder: File, teacherFile: File, pcFile: File, resourc
if (States.tasks.get(resourceId + "_1")?.isActive == true)
return@supervisorScope
States.tasks[resourceId + "_1"] = launch {
States.progress[resourceId + "_1"] = 0f
runCatching {
downloadToFile(teacherFile, teacherUrl) { current, total, totalTime, _, startBytes ->
if (total != -1L) {
Expand All @@ -57,10 +58,10 @@ suspend fun downloadVideo(folder: File, teacherFile: File, pcFile: File, resourc
States.progressInfo.remove(resourceId + "_1")
}.onFailure {
if (it is CancellationException) {
States.progressInfo.put(resourceId + "_1", "Cancelled")
States.progressInfo.put(resourceId + "_1", "暂停中")

} else {
States.progressInfo.put(resourceId + "_1", "Failed")
States.progressInfo.put(resourceId + "_1", "下载失败")
it.printStackTrace()
}
}
Expand All @@ -69,6 +70,7 @@ suspend fun downloadVideo(folder: File, teacherFile: File, pcFile: File, resourc
if (States.tasks.get(resourceId + "_2")?.isActive == true)
return@supervisorScope
States.tasks[resourceId + "_2"] = launch {
States.progress[resourceId + "_2"] = 0f
runCatching {
downloadToFile(pcFile, pcUrl) { current, total, totalTime, _, startBytes ->
if (total != -1L) {
Expand All @@ -84,14 +86,15 @@ suspend fun downloadVideo(folder: File, teacherFile: File, pcFile: File, resourc
States.progressInfo.remove(resourceId + "_2")
}.onFailure {
if (it is CancellationException) {
States.progressInfo.put(resourceId + "_2", "Cancelled")
States.progressInfo.put(resourceId + "_2", "暂停中")

} else {
States.progressInfo.put(resourceId + "_2", "Failed")
States.progressInfo.put(resourceId + "_2", "下载失败")
it.printStackTrace()
}
}
}

}
}
//播放器
Expand Down
13 changes: 11 additions & 2 deletions src/jvmMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import io.ktor.client.*
Expand All @@ -11,6 +12,7 @@ import kotlinx.serialization.json.Json
import ui.MainPage
import utils.OkHttpUtil
import java.io.File
import java.io.PrintStream

val client = HttpClient(OkHttp) {
// install(Logging) {
Expand Down Expand Up @@ -62,9 +64,16 @@ fun logOut() {
}

fun main() {

val err = PrintStream(File("err.txt").outputStream())
val out = PrintStream(File("out.txt").outputStream())
System.setErr(err)
System.setErr(out)
States.loadAll()
application {
Window(onCloseRequest = ::exitApplication) {
Window(onCloseRequest = {
States.saveAll()
exitApplication()
}) {
MainPage()
}
}
Expand Down
43 changes: 37 additions & 6 deletions src/jvmMain/kotlin/States.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@

import androidx.compose.runtime.*
import kotlinx.coroutines.Job
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import utils.DB
import utils.conf.Conf
import utils.objectArray
import java.io.File
import java.util.concurrent.ConcurrentHashMap

object States {
var conf=utils.conf.Conf
var currentTerm: String = ""
var videos = mutableStateListOf<JsonObject>()
var syncState: SyncState by mutableStateOf(SyncState.OUT_DATE)
var pageState by mutableStateOf(PageState.INDEX)
var downloadFolder = File(conf.getConf("savepath")).canonicalFile
val progress = mutableStateMapOf<String,Float>()
val progressInfo = mutableStateMapOf<String,String>()
val downloadFolder: File
get() = File(Conf.savePath).canonicalFile


val progress = mutableStateMapOf<String, Float>()
val progressInfo = mutableStateMapOf<String, String>()

val tasks = ConcurrentHashMap<String,Job>()
val tasks = ConcurrentHashMap<String, Job>()

var currentJob: Job? = null

Expand All @@ -25,6 +34,28 @@ object States {
var lessons = mutableStateListOf<JsonObject>()
var termNow by mutableStateOf("---")
var terms = mutableStateListOf<JsonObject>()
var cookie by mutableStateOf("")
var cookie: String by mutableStateOf("")

fun saveAll(){
DB.setValue("currentTerm",currentTerm)
DB.setValue("videos_cache", Json.Default.encodeToString<List<JsonObject>>(videos))
DB.setValue("query_type", queryType.toString())
DB.setValue("query_videos", Json.Default.encodeToString(queryVideos))
DB.setValue("lesson_now", lessonNow)
DB.setValue("lessons", Json.Default.encodeToString<List<JsonObject>>(lessons))
DB.setValue("term_now", termNow)
DB.setValue("terms", Json.Default.encodeToString<List<JsonObject>>(terms))
DB.setValue("cookie_cache", cookie)
}
fun loadAll(){
currentTerm = DB.getValue("currentTerm")?:""
videos.addAll(DB.getValue("videos_cache")?.let { Json.parseToJsonElement(it).objectArray }?.toTypedArray()?: arrayOf())
queryType = DB.getValue("query_type")?.toInt()?:-1
queryVideos = DB.getValue("query_videos")?.let { Json.decodeFromString<List<Pair<String, String>>>(it) }?: listOf()
lessonNow = DB.getValue("lesson_now")?:"---"
lessons.addAll(DB.getValue("lessons")?.let { Json.parseToJsonElement(it).objectArray }?.toTypedArray()?: arrayOf())
termNow = DB.getValue("term_now")?:"---"
terms.addAll(DB.getValue("terms")?.let { Json.parseToJsonElement(it).objectArray }?.toTypedArray()?: arrayOf())
cookie = DB.getValue("cookie_cache")?:""
}
}
12 changes: 6 additions & 6 deletions src/jvmMain/kotlin/TopBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,9 @@ import androidx.compose.ui.draw.rotate
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.input.OffsetMapping
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.TransformedText
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.unit.dp
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.jsonArray
import utils.String

@Composable
Expand All @@ -46,7 +42,10 @@ fun TopBar(
) {
Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
TextField(
cookieString, setCookieString, Modifier.weight(1f, true).height(50.dp), visualTransformation = PasswordVisualTransformation()
cookieString,
setCookieString,
Modifier.weight(1f, true).height(50.dp),
visualTransformation = PasswordVisualTransformation()
)
Spacer(Modifier.width(2.dp))
IconButton(onClick = onSync) {
Expand All @@ -66,6 +65,7 @@ fun TopBar(
SyncState.SYNCED -> {
Icon(painterResource("done_outline.svg"), "refresh list")
}

SyncState.SYNCING -> {
Icon(painterResource("sync.svg"), "refresh list", Modifier.rotate(rotationState.value))
}
Expand All @@ -85,7 +85,7 @@ fun TopBar(
append(it.String("year"))
append(it.String("name"))
}
},setFilter2, Modifier)
}, setFilter2, Modifier)
Spacer(Modifier.width(2.dp))
OutlinedButton(
onFnClick, Modifier.size(50.dp), shape = CircleShape, contentPadding = PaddingValues(0.dp)
Expand Down
24 changes: 16 additions & 8 deletions src/jvmMain/kotlin/ui/MainPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Settings
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand Down Expand Up @@ -61,6 +62,7 @@ suspend fun updateVideoList() {
}?.getOrNull()?.runCatching {
States.videos.clear()
States.videos.addAll(map {

buildJsonObject {
put("lessonName", it.String("courseName"))
put("date", it.String("scheduleTimeStart").substringBefore(' '))
Expand Down Expand Up @@ -96,6 +98,7 @@ suspend fun updateVideoList() {
}

}

//贝塞尔曲线
fun calculateY(t: Float): Float {
val tSquared = t * t
Expand All @@ -106,6 +109,7 @@ fun calculateY(t: Float): Float {

return y.toFloat()
}

suspend fun updateTermVideoList(termId: String) {
States.syncState = SyncState.SYNCING
require(States.queryVideos != null)
Expand Down Expand Up @@ -253,7 +257,6 @@ fun MainPage() {
PageState.SETTINGS -> "Back"
},
onFnClick = {
logOut(States.pageState)
when (States.pageState) {
PageState.INDEX -> States.pageState = PageState.SETTINGS
PageState.SETTINGS -> States.pageState = PageState.INDEX
Expand All @@ -262,6 +265,7 @@ fun MainPage() {
syncState = States.syncState,
onSync = {
if (States.syncState != SyncState.SYNCING) {
DB.setValue("cookie_cache", States.cookie)
syncCourses(mainScope)
}
})
Expand Down Expand Up @@ -378,25 +382,24 @@ fun MainPage() {
}
}
Column(Modifier.fillMaxSize(), verticalArrangement = Arrangement.SpaceBetween) {
Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.Top) {
Box(Modifier.fillMaxWidth(), contentAlignment = Alignment.TopCenter ) {
States.progress[id + "_1"]?.let {

LinearProgressIndicator(
it,
Modifier.height(8.dp).weight(1f),
Modifier.height(8.dp).fillMaxWidth(1f),
color = Color.hsv(calculateY(it) * 120, 1f, 1f)
)
} ?: Spacer(Modifier.height(8.dp).weight(1f))
} ?: Spacer(Modifier.height(8.dp).fillMaxWidth(1f))
Text(States.progressInfo[id + "_1"] ?: "")
}
Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.Bottom) {
Box(Modifier.fillMaxWidth(), contentAlignment = Alignment.BottomCenter) {
States.progress[id + "_2"]?.let {
LinearProgressIndicator(
it,
Modifier.height(8.dp).weight(1f),
Modifier.height(8.dp).fillMaxWidth(1f),
color = Color.hsv(calculateY(it) * 120, 1f, 1f)
)
} ?: Spacer(Modifier.height(8.dp).weight(1f))
} ?: Spacer(Modifier.height(8.dp).fillMaxWidth(1f))
Text(States.progressInfo[id + "_2"] ?: "")
}
}
Expand All @@ -416,6 +419,11 @@ fun MainPage() {
}

}
LaunchedEffect(Unit){
if (States.cookie.isNotEmpty()&&States.videos.isEmpty()){
syncCourses(mainScope)
}
}
}
}

Expand Down
Loading

0 comments on commit 664c035

Please sign in to comment.