From cc6635a852ad15f5ca75c69b764c0a820d5e558e Mon Sep 17 00:00:00 2001 From: itning Date: Sun, 10 Feb 2019 12:38:13 +0800 Subject: [PATCH] fix bug --- app/build.gradle | 4 +- .../ui/activity/CustomActivity.kt | 38 +++++++++---------- .../ui/activity/LoginActivity.kt | 22 +++++------ .../ui/activity/ShareActivity.kt | 22 +++++------ .../yunshuclassschedule/util/DateUtils.kt | 14 ++++--- 5 files changed, 48 insertions(+), 52 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index c59c814..787205d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -11,8 +11,8 @@ android { applicationId "top.itning.yunshuclassschedule" minSdkVersion 21 targetSdkVersion 28 - versionCode 46 - versionName "2.4.1-BETA" + versionCode 47 + versionName "2.4.2-BETA" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" javaCompileOptions { annotationProcessorOptions { diff --git a/app/src/main/java/top/itning/yunshuclassschedule/ui/activity/CustomActivity.kt b/app/src/main/java/top/itning/yunshuclassschedule/ui/activity/CustomActivity.kt index b5ccdaa..5d84ed0 100644 --- a/app/src/main/java/top/itning/yunshuclassschedule/ui/activity/CustomActivity.kt +++ b/app/src/main/java/top/itning/yunshuclassschedule/ui/activity/CustomActivity.kt @@ -38,7 +38,7 @@ class CustomActivity : BaseActivity(), TimePickerDialog.OnTimeSetListener { private lateinit var msg: String private var classSchedule = 5 - private var timeMap: TreeMap = TreeMap() + private var timeMap: TreeMap = TreeMap() override fun onCreate(savedInstanceState: Bundle?) { ThemeChangeUtil.changeTheme(this) @@ -59,17 +59,17 @@ class CustomActivity : BaseActivity(), TimePickerDialog.OnTimeSetListener { App.sharedPreferences.edit().putString("3", "12:55-14:25").commit() App.sharedPreferences.edit().putString("4", "14:40-16:10").commit() App.sharedPreferences.edit().putString("5", "17:30-20:00").commit() - App.sharedPreferences.edit().putString("6", "12:00-12:01").commit() - App.sharedPreferences.edit().putString("7", "12:00-12:01").commit() - App.sharedPreferences.edit().putString("8", "12:00-12:01").commit() - App.sharedPreferences.edit().putString("9", "12:00-12:01").commit() - App.sharedPreferences.edit().putString("10", "12:00-12:01").commit() - App.sharedPreferences.edit().putString("11", "12:00-12:01").commit() - App.sharedPreferences.edit().putString("12", "12:00-12:01").commit() + App.sharedPreferences.edit().putString("6", "20:05-20:08").commit() + App.sharedPreferences.edit().putString("7", "20:10-20:15").commit() + App.sharedPreferences.edit().putString("8", "20:18-20:20").commit() + App.sharedPreferences.edit().putString("9", "20:30-20:35").commit() + App.sharedPreferences.edit().putString("10", "20:40-20:45").commit() + App.sharedPreferences.edit().putString("11", "20:50-21:01").commit() + App.sharedPreferences.edit().putString("12", "21:10-21:15").commit() } timeMap.clear() for (i in 1..classSchedule) { - timeMap[i.toString()] = App.sharedPreferences.getString(i.toString(), "12:00-12:01")!! + timeMap[i] = App.sharedPreferences.getString(i.toString(), "12:00-12:01")!! } } @@ -79,7 +79,7 @@ class CustomActivity : BaseActivity(), TimePickerDialog.OnTimeSetListener { private fun setText(key: String = "") { if (key == "") { for (i in 1..classSchedule) { - val a = timeMap[i.toString()]!!.split("-".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() + val a = timeMap[i]!!.split("-".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() getOneClass(i.toString(), a[0], a[1]) } } else { @@ -87,8 +87,8 @@ class CustomActivity : BaseActivity(), TimePickerDialog.OnTimeSetListener { val down = ll.findViewWithTag("$key-x") val upView: AppCompatTextView = up.getChildAt(1) as AppCompatTextView val downView: AppCompatTextView = down.getChildAt(1) as AppCompatTextView - upView.text = timeMap[key]!!.split("-".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[0] - downView.text = timeMap[key]!!.split("-".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[1] + upView.text = timeMap[key.toInt()]!!.split("-".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[0] + downView.text = timeMap[key.toInt()]!!.split("-".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[1] } } @@ -125,8 +125,8 @@ class CustomActivity : BaseActivity(), TimePickerDialog.OnTimeSetListener { } private fun getOneClass(classText: CharSequence, upTime: CharSequence, downTime: CharSequence) { - val up = getOneRowRelativeLayout("第${classText}节上课", upTime, View.OnClickListener { onViewClicked("$classText-s", it) }) - val down = getOneRowRelativeLayout("第${classText}节下课", downTime, View.OnClickListener { onViewClicked("$classText-x", it) }) + val up = getOneRowRelativeLayout("第${classText}节上课", upTime, View.OnClickListener { onViewClicked("$classText-s") }) + val down = getOneRowRelativeLayout("第${classText}节下课", downTime, View.OnClickListener { onViewClicked("$classText-x") }) up.tag = "$classText-s" down.tag = "$classText-x" ll.addView(up) @@ -209,7 +209,7 @@ class CustomActivity : BaseActivity(), TimePickerDialog.OnTimeSetListener { private fun updateSharedPreferences() { val edit = App.sharedPreferences.edit() for ((key, value) in timeMap) { - edit.putString(key, value) + edit.putString(key.toString(), value) } edit.apply() DateUtils.refreshTimeList() @@ -220,7 +220,7 @@ class CustomActivity : BaseActivity(), TimePickerDialog.OnTimeSetListener { } - private fun onViewClicked(id: String, view: View) { + private fun onViewClicked(id: String) { this.msg = id showTimePickerDialog() } @@ -236,7 +236,7 @@ class CustomActivity : BaseActivity(), TimePickerDialog.OnTimeSetListener { } else { stringBuilder.append("下课") } - val timePickerDialog = getTimePickerDialog(type[1], timeMap[type[0]]!!) + val timePickerDialog = getTimePickerDialog(type[1], timeMap[type[0].toInt()]!!) timePickerDialog.title = stringBuilder.toString() timePickerDialog.show(fragmentManager, "TimePickerDialog") } @@ -285,7 +285,7 @@ class CustomActivity : BaseActivity(), TimePickerDialog.OnTimeSetListener { m = "0$m" } val time = "$h:$m" - val s = timeMap[typeInfo[0]] ?: return + val s = timeMap[typeInfo[0].toInt()] ?: return if ("" != s) { val insertStr: String = if (CLASS_UP == typeInfo[1]) { time + "-" + s.split("-".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[1] @@ -293,7 +293,7 @@ class CustomActivity : BaseActivity(), TimePickerDialog.OnTimeSetListener { s.split("-".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[0] + "-" + time } Log.d(TAG, "insert :$insertStr") - timeMap[typeInfo[0]] = insertStr + timeMap[typeInfo[0].toInt()] = insertStr setText(typeInfo[0]) } } diff --git a/app/src/main/java/top/itning/yunshuclassschedule/ui/activity/LoginActivity.kt b/app/src/main/java/top/itning/yunshuclassschedule/ui/activity/LoginActivity.kt index 5cafe43..97811ef 100644 --- a/app/src/main/java/top/itning/yunshuclassschedule/ui/activity/LoginActivity.kt +++ b/app/src/main/java/top/itning/yunshuclassschedule/ui/activity/LoginActivity.kt @@ -125,23 +125,19 @@ class LoginActivity : BaseActivity() { .setTitle("警告") .setMessage("即将导入课程数据,这会将原有课程信息清空,确定导入吗?") .setPositiveButton("确定") { _, _ -> - val timeMap = TreeMap() - timeMap["1"] = timeList[0] - timeMap["2"] = timeList[1] - timeMap["3"] = timeList[2] - timeMap["4"] = timeList[3] - timeMap["5"] = timeList[4] + val timeMap = TreeMap() + for ((index, value) in timeList.withIndex()) { + timeMap[index + 1] = value + } if (!DateUtils.isDataLegitimate(timeMap, this)) { Toast.makeText(this, "解析失败", Toast.LENGTH_LONG).show() return@setPositiveButton } - if (App.sharedPreferences.edit() - .putString("1", timeList[0]) - .putString("2", timeList[1]) - .putString("3", timeList[2]) - .putString("4", timeList[3]) - .putString("5", timeList[4]) - .commit()) { + val edit = App.sharedPreferences.edit() + for ((index, value) in timeList.withIndex()) { + edit.putString((index + 1).toString(), value) + } + if (edit.commit()) { DateUtils.refreshTimeList() } else { Toast.makeText(this, "解析失败", Toast.LENGTH_LONG).show() diff --git a/app/src/main/java/top/itning/yunshuclassschedule/ui/activity/ShareActivity.kt b/app/src/main/java/top/itning/yunshuclassschedule/ui/activity/ShareActivity.kt index e5703d0..9db3b5f 100644 --- a/app/src/main/java/top/itning/yunshuclassschedule/ui/activity/ShareActivity.kt +++ b/app/src/main/java/top/itning/yunshuclassschedule/ui/activity/ShareActivity.kt @@ -211,23 +211,19 @@ class ShareActivity : BaseActivity() { .setTitle("警告") .setMessage("即将导入课程数据,这会将原有课程信息清空,确定导入吗?") .setPositiveButton("确定") { _, _ -> - val timeMap = TreeMap() - timeMap["1"] = timeList[0] - timeMap["2"] = timeList[1] - timeMap["3"] = timeList[2] - timeMap["4"] = timeList[3] - timeMap["5"] = timeList[4] + val timeMap = TreeMap() + for ((index, value) in timeList.withIndex()) { + timeMap[index + 1] = value + } if (!DateUtils.isDataLegitimate(timeMap, this)) { Toast.makeText(this, "解析失败", Toast.LENGTH_LONG).show() return@setPositiveButton } - if (App.sharedPreferences.edit() - .putString("1", timeList[0]) - .putString("2", timeList[1]) - .putString("3", timeList[2]) - .putString("4", timeList[3]) - .putString("5", timeList[4]) - .commit()) { + val edit = App.sharedPreferences.edit() + for ((index, value) in timeList.withIndex()) { + edit.putString((index + 1).toString(), value) + } + if (edit.commit()) { DateUtils.refreshTimeList() } else { Toast.makeText(this, "解析失败", Toast.LENGTH_LONG).show() diff --git a/app/src/main/java/top/itning/yunshuclassschedule/util/DateUtils.kt b/app/src/main/java/top/itning/yunshuclassschedule/util/DateUtils.kt index ec8a76f..f0aa7de 100644 --- a/app/src/main/java/top/itning/yunshuclassschedule/util/DateUtils.kt +++ b/app/src/main/java/top/itning/yunshuclassschedule/util/DateUtils.kt @@ -4,9 +4,11 @@ import android.content.Context import android.util.Log import androidx.annotation.CheckResult import androidx.appcompat.app.AlertDialog +import androidx.preference.PreferenceManager import top.itning.yunshuclassschedule.common.App import top.itning.yunshuclassschedule.common.ConstantPool import top.itning.yunshuclassschedule.entity.ClassSchedule +import top.itning.yunshuclassschedule.ui.fragment.setting.SettingsFragment import java.text.ParseException import java.text.SimpleDateFormat import java.util.* @@ -220,21 +222,23 @@ object DateUtils { * * @return 合法返回真 */ - fun isDataLegitimate(timeMap: TreeMap, context: Context): Boolean { - var lastEntry: Map.Entry? = null + fun isDataLegitimate(timeMap: TreeMap, context: Context): Boolean { + val nowWeekNum = PreferenceManager.getDefaultSharedPreferences(context).getString(SettingsFragment.NOW_WEEK_NUM, "1") + var lastEntry: Map.Entry? = null for (entry in timeMap.entries) { val timeArray = entry.value.split("-".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() //检查每节课上下课时间合法性 + Log.e(TAG, "${timeArray[0]} ${timeArray[1]}") if (DateUtils.isTimeIintervalLegitimate(timeArray[0], timeArray[1])) { Log.d(TAG, "error1: " + timeArray[0] + "-->" + timeArray[1]) - showTimeErrorDialog(entry.key, 1, context) + showTimeErrorDialog(entry.key.toString(), 1, context) return false } - if (lastEntry != null && "5" != entry.key) { + if (lastEntry != null && nowWeekNum != entry.key.toString()) { val lastTimeArray = lastEntry.value.split("-".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() if (DateUtils.isTimeIintervalLegitimate(lastTimeArray[1], timeArray[0])) { Log.d(TAG, "error2: " + lastTimeArray[1] + "-->" + timeArray[0]) - showTimeErrorDialog(lastEntry.key, 2, context) + showTimeErrorDialog(lastEntry.key.toString(), 2, context) return false } }