Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Commit

Permalink
Abstract out part of the method
Browse files Browse the repository at this point in the history
  • Loading branch information
qhy040404 committed Apr 27, 2023
1 parent b9cb2a3 commit ed8eb24
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 102 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.qhy040404.libraryonetap.ui.tools

import androidx.annotation.StringRes
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.qhy040404.libraryonetap.R
import com.qhy040404.libraryonetap.constant.Constants
import com.qhy040404.libraryonetap.constant.GlobalValues
import com.qhy040404.libraryonetap.constant.URLManager
import com.qhy040404.libraryonetap.recyclerview.SimplePageActivity
import com.qhy040404.libraryonetap.utils.web.Requests

abstract class BaseEduActivity : SimplePageActivity() {
protected var currentVisible = true

fun showInitFailedAlertDialog(@StringRes title: Int) {
MaterialAlertDialogBuilder(this)
.setTitle(title)
.setMessage(
when (GlobalValues.netPrompt) {
Constants.NET_DISCONNECTED -> R.string.glb_net_disconnected
Constants.NET_ERROR -> R.string.glb_net_error
Constants.NET_TIMEOUT -> R.string.glb_net_timeout
else -> R.string.glb_fail_to_login_three_times
}
)
.setPositiveButton(R.string.glb_ok) { _, _ ->
finish()
}
.setCancelable(false)
.create().also {
if (this.currentVisible) {
it.show()
}
}
}

fun initMinor() {
if (GlobalValues.majorStuId == 0 || GlobalValues.minorStuId == 0) {
val initUrl = Requests.get(URLManager.EDU_GRADE_INIT_URL, null, true)
val initData = Requests.get(URLManager.EDU_GRADE_INIT_URL)

if (initUrl.contains("semester-index")) {
GlobalValues.majorStuId = initUrl.substringAfter("/").toInt()
GlobalValues.minorStuId = -1
} else {
val initList = initData.split("onclick=\"myFunction(this)\" value=\"")
if (initList.size == 3) {
val aStuId = initList[1].substringBefore("\"").toInt()
val bStuId = initList[2].substringBefore("\"").toInt()
GlobalValues.majorStuId = aStuId.coerceAtMost(bStuId)
GlobalValues.minorStuId = aStuId.coerceAtLeast(bStuId)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import com.qhy040404.datetime.Datetime
import com.qhy040404.datetime.Datetime.Companion.toDatetime
import com.qhy040404.libraryonetap.LibraryOneTapApp
import com.qhy040404.libraryonetap.R
import com.qhy040404.libraryonetap.constant.Constants
import com.qhy040404.libraryonetap.constant.GlobalValues
import com.qhy040404.libraryonetap.constant.URLManager
import com.qhy040404.libraryonetap.data.tools.Exam
import com.qhy040404.libraryonetap.recyclerview.SimplePageActivity
import com.qhy040404.libraryonetap.recyclerview.simplepage.Card
import com.qhy040404.libraryonetap.recyclerview.simplepage.Category
import com.qhy040404.libraryonetap.recyclerview.simplepage.Clickable
Expand All @@ -24,8 +22,7 @@ import com.qhy040404.libraryonetap.utils.web.Requests
import kotlinx.coroutines.delay
import org.json.JSONArray

class ExamsActivity : SimplePageActivity() {
private var currentVisible = true
class ExamsActivity : BaseEduActivity() {
private val exams = mutableListOf<Exam>()
private val now = Datetime.now()

Expand Down Expand Up @@ -132,44 +129,10 @@ class ExamsActivity : SimplePageActivity() {

if (!Requests.initEdu()) {
runOnUiThread {
MaterialAlertDialogBuilder(this@ExamsActivity)
.setTitle(R.string.exams_title)
.setMessage(
when (GlobalValues.netPrompt) {
Constants.NET_DISCONNECTED -> R.string.glb_net_disconnected
Constants.NET_ERROR -> R.string.glb_net_error
Constants.NET_TIMEOUT -> R.string.glb_net_timeout
else -> R.string.glb_fail_to_login_three_times
}
)
.setPositiveButton(R.string.glb_ok) { _, _ ->
finish()
}
.setCancelable(false)
.create().also {
if (this@ExamsActivity.currentVisible) {
it.show()
}
}
showInitFailedAlertDialog(R.string.exams_title)
}
} else {
if (GlobalValues.majorStuId == 0 || GlobalValues.minorStuId == 0) {
val initUrl = Requests.get(URLManager.EDU_GRADE_INIT_URL, null, true)
val initData = Requests.get(URLManager.EDU_GRADE_INIT_URL)

if (initUrl.contains("semester-index")) {
GlobalValues.majorStuId = initUrl.substringAfter("/").toInt()
GlobalValues.minorStuId = -1
} else {
val initList = initData.split("onclick=\"myFunction(this)\" value=\"")
if (initList.size == 3) {
val aStuId = initList[1].substringBefore("\"").toInt()
val bStuId = initList[2].substringBefore("\"").toInt()
GlobalValues.majorStuId = aStuId.coerceAtMost(bStuId)
GlobalValues.minorStuId = aStuId.coerceAtLeast(bStuId)
}
}
}
initMinor()

var examsMinorData: String? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ import com.absinthe.libraries.utils.extensions.getColorByAttr
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.qhy040404.libraryonetap.LibraryOneTapApp
import com.qhy040404.libraryonetap.R
import com.qhy040404.libraryonetap.constant.Constants
import com.qhy040404.libraryonetap.constant.GlobalValues
import com.qhy040404.libraryonetap.constant.URLManager
import com.qhy040404.libraryonetap.data.tools.Grade
import com.qhy040404.libraryonetap.data.tools.Semester
import com.qhy040404.libraryonetap.recyclerview.SimplePageActivity
import com.qhy040404.libraryonetap.recyclerview.simplepage.Card
import com.qhy040404.libraryonetap.recyclerview.simplepage.Category
import com.qhy040404.libraryonetap.recyclerview.simplepage.Clickable
Expand All @@ -33,8 +31,7 @@ import kotlinx.coroutines.launch
import me.saket.cascade.CascadePopupMenu
import org.json.JSONObject

class GradesActivity : SimplePageActivity(), MenuProvider {
private var currentVisible = true
class GradesActivity : BaseEduActivity(), MenuProvider {
private val semesters = mutableListOf<Semester>()
private var menu: Menu? = null
private var idPopup: CascadePopupMenu? = null
Expand Down Expand Up @@ -79,44 +76,10 @@ class GradesActivity : SimplePageActivity(), MenuProvider {

if (!Requests.initEdu()) {
runOnUiThread {
MaterialAlertDialogBuilder(this@GradesActivity)
.setTitle(R.string.exams_title)
.setMessage(
when (GlobalValues.netPrompt) {
Constants.NET_DISCONNECTED -> R.string.glb_net_disconnected
Constants.NET_ERROR -> R.string.glb_net_error
Constants.NET_TIMEOUT -> R.string.glb_net_timeout
else -> R.string.glb_fail_to_login_three_times
}
)
.setPositiveButton(R.string.glb_ok) { _, _ ->
finish()
}
.setCancelable(false)
.create().also {
if (this@GradesActivity.currentVisible) {
it.show()
}
}
showInitFailedAlertDialog(R.string.grade_title)
}
} else {
if (GlobalValues.majorStuId == 0 || GlobalValues.minorStuId == 0) {
val initUrl = Requests.get(URLManager.EDU_GRADE_INIT_URL, null, true)
val initData = Requests.get(URLManager.EDU_GRADE_INIT_URL)

if (initUrl.contains("semester-index")) {
GlobalValues.majorStuId = initUrl.substringAfter("/").toInt()
GlobalValues.minorStuId = -1
} else {
val initList = initData.split("onclick=\"myFunction(this)\" value=\"")
if (initList.size == 3) {
val aStuId = initList[1].substringBefore("\"").toInt()
val bStuId = initList[2].substringBefore("\"").toInt()
GlobalValues.majorStuId = aStuId.coerceAtMost(bStuId)
GlobalValues.minorStuId = aStuId.coerceAtLeast(bStuId)
}
}
}
initMinor()
if (GlobalValues.minorStuId > 0) {
runOnUiThread {
menu?.findItem(R.id.grade_id)?.isVisible = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.qhy040404.libraryonetap.constant.Constants
import com.qhy040404.libraryonetap.constant.GlobalValues
import com.qhy040404.libraryonetap.constant.URLManager
import com.qhy040404.libraryonetap.data.tools.Lesson
import com.qhy040404.libraryonetap.recyclerview.SimplePageActivity
import com.qhy040404.libraryonetap.recyclerview.simplepage.Card
import com.qhy040404.libraryonetap.recyclerview.simplepage.Category
import com.qhy040404.libraryonetap.recyclerview.simplepage.Clickable
Expand All @@ -21,8 +20,7 @@ import com.qhy040404.libraryonetap.utils.extensions.StringExtension.substringBet
import com.qhy040404.libraryonetap.utils.web.Requests
import org.json.JSONObject

class LessonsActivity : SimplePageActivity() {
private var currentVisible = true
class LessonsActivity : BaseEduActivity() {
private var courseTableAvailable = true
private var semester: String = Constants.STRING_NULL
private val lessons = mutableListOf<Lesson>()
Expand Down Expand Up @@ -65,25 +63,7 @@ class LessonsActivity : SimplePageActivity() {

if (!Requests.initEdu()) {
runOnUiThread {
MaterialAlertDialogBuilder(this@LessonsActivity)
.setTitle(R.string.exams_title)
.setMessage(
when (GlobalValues.netPrompt) {
Constants.NET_DISCONNECTED -> R.string.glb_net_disconnected
Constants.NET_ERROR -> R.string.glb_net_error
Constants.NET_TIMEOUT -> R.string.glb_net_timeout
else -> R.string.glb_fail_to_login_three_times
}
)
.setPositiveButton(R.string.glb_ok) { _, _ ->
finish()
}
.setCancelable(false)
.create().also {
if (this@LessonsActivity.currentVisible) {
it.show()
}
}
showInitFailedAlertDialog(R.string.lessons_title)
}
} else {
val source = Requests.get(URLManager.EDU_COURSE_TABLE_URL)
Expand Down

0 comments on commit ed8eb24

Please sign in to comment.