Skip to content

Commit

Permalink
Merge pull request #283 from RedrockMobile/GuoXR/bugfix2
Browse files Browse the repository at this point in the history
Guo xr/bugfix2
  • Loading branch information
985892345 authored Aug 30, 2022
2 parents 867e394 + 861c1ad commit 0337aee
Show file tree
Hide file tree
Showing 34 changed files with 444 additions and 674 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.mredrock.cyxbs.lib.base.spi
import android.annotation.SuppressLint
import android.app.ActivityManager
import android.app.Application
import android.os.Build
import android.os.Process
import android.provider.Settings
import android.text.TextUtils
Expand Down Expand Up @@ -31,7 +32,11 @@ interface InitialManager {

fun applicationId() = application.packageName
fun applicationVersion() = application.packageManager.getPackageInfo(application.packageName, 0).versionName
fun applicationCode() = application.packageManager.getPackageInfo(application.packageName, 0).longVersionCode
fun applicationCode() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
application.packageManager.getPackageInfo(application.packageName, 0).longVersionCode
} else {
application.packageManager.getPackageInfo(application.packageName, 0).versionCode.toLong()
}

/*
* 使用 androidId 来代替设备 id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.content.Context
import android.content.res.TypedArray
import android.graphics.*
import android.util.AttributeSet
import android.util.Log
import android.util.TypedValue
import android.view.Gravity
import android.view.View
Expand Down Expand Up @@ -173,12 +172,10 @@ open class JCardViewPlus(context: Context, attrs: AttributeSet?, defStyleAttr: I
}
val parentLeft = paddingLeft + contentPaddingLeft
val parentTop = paddingTop + contentPaddingTop
Log.d("ggg", "(JCardViewPlus.kt:175) -> parentTop = $parentTop paddingTop = $paddingTop contentPaddingTop = $contentPaddingTop")
val child = getChildAt(0)
val lp = child.layoutParams as LayoutParams
val childLeft = parentLeft + lp.leftMargin
val childTop = parentTop + lp.topMargin
Log.d("ggg", "(JCardViewPlus.kt:181) -> childTop = $childTop")
child.layout(childLeft, childTop, childLeft + child.measuredWidth, childTop + child.measuredHeight)
// layoutChildren(left, top, right, bottom)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import android.app.ProgressDialog
import android.os.Bundle
import android.widget.Toast
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.mredrock.cyxbs.common.component.CyxbsToast
import com.mredrock.cyxbs.common.viewmodel.BaseViewModel
Expand Down Expand Up @@ -39,9 +38,9 @@ abstract class BaseViewModelFragment<T : BaseViewModel> : BaseFragment() {
}

viewModel.apply {
toastEvent.observe { str -> str?.let { CyxbsToast.makeText(requireContext(), it, Toast.LENGTH_SHORT).show() } }
longToastEvent.observe { str -> str?.let { CyxbsToast.makeText(requireContext(), it, Toast.LENGTH_LONG).show() } }
progressDialogEvent.observe {
toastEvent.observe(this@BaseViewModelFragment) { str -> str?.let { CyxbsToast.makeText(requireContext(), it, Toast.LENGTH_SHORT).show() } }
longToastEvent.observe(this@BaseViewModelFragment) { str -> str?.let { CyxbsToast.makeText(requireContext(), it, Toast.LENGTH_LONG).show() } }
progressDialogEvent.observe(this@BaseViewModelFragment) {
it ?: return@observe
// 确保只有一个对话框会被弹出
if (it != ProgressDialogEvent.DISMISS_DIALOG_EVENT && progressDialog?.isShowing != true) {
Expand All @@ -56,12 +55,9 @@ abstract class BaseViewModelFragment<T : BaseViewModel> : BaseFragment() {

protected open fun getViewModelFactory(): ViewModelProvider.Factory? = null

inline fun <T> LiveData<T>.observe(crossinline onChange: (T?) -> Unit) = observe(this@BaseViewModelFragment, Observer { onChange(value) })
inline fun <T> LiveData<T>.observeNotNull(crossinline onChange: (T) -> Unit) = observe(this@BaseViewModelFragment, Observer {
it ?: return@Observer
onChange(it)
})

inline fun <T> LiveData<T>.observe(crossinline onChange: (T) -> Unit) = observe(viewLifecycleOwner
) { onChange(it) }

override fun onDestroyView() {
super.onDestroyView()
if (progressDialog?.isShowing == true) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.mredrock.cyxbs.common.utils

import androidx.core.content.edit
import com.mredrock.cyxbs.common.BaseApp
import com.mredrock.cyxbs.common.utils.extensions.defaultSharedPreferences
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*
import java.util.concurrent.TimeUnit

/**
* 学校日历工具类
Expand All @@ -14,33 +16,33 @@ import java.util.*
open class SchoolCalendar {
var firstDay: Calendar = GregorianCalendar(2015, Calendar.SEPTEMBER, 7)
var calendar: Calendar

constructor() {
// 鄙人认为,在这个时候,我们有必要去更新一下firstDay
val first = BaseApp.appContext.defaultSharedPreferences.getLong(FIRST_DAY, firstDay.timeInMillis)
val first = BaseApp.appContext.defaultSharedPreferences.getLong(FIRST_MON_DAY, firstDay.timeInMillis)
firstDay.timeZone = TimeZone.getTimeZone("GMT+8:00")
firstDay.timeInMillis = first
calendar = GregorianCalendar()
calendar.timeZone = TimeZone.getTimeZone("GMT+8:00")
}

constructor(date: Date?) : this() {
calendar.time = date
}

constructor(timestamp: Long) : this(Date(timestamp * 1000))
constructor(year: Int, month: Int, day: Int) {
calendar = GregorianCalendar(year, month, day)
}

constructor(week: Int, weekDay: Int) : this() {
var mWeekDay = weekDay
calendar = firstDay
calendar.add(Calendar.DATE, (week - 1) * 7)
mWeekDay = if (mWeekDay == 0) 7 else mWeekDay
calendar.add(Calendar.DATE, mWeekDay - dayOfWeek)
}

/**
* 取这学期过去了多少天
*
Expand All @@ -54,7 +56,7 @@ open class SchoolCalendar {
}
return days
}

/**
* 取当前第几周
*
Expand All @@ -74,7 +76,7 @@ open class SchoolCalendar {
}
return weeks
}

private fun getDeltaT(end: Calendar, begin: Calendar): Int {
val mBegin = begin.clone() as Calendar
mBegin[Calendar.HOUR_OF_DAY] = 0
Expand All @@ -88,7 +90,7 @@ open class SchoolCalendar {
mEnd[Calendar.MILLISECOND] = 0
return ((mEnd.timeInMillis - mBegin.timeInMillis) / (1000 * 86400)).toInt()
}

/**
* 日期加减
*
Expand All @@ -98,7 +100,7 @@ open class SchoolCalendar {
calendar.add(Calendar.DATE, day)
return this
}

/**
* 格式化输出日期
* 年:y 月:M 日:d 时:h(12制)/H(24值) 分:m 秒:s 毫秒:S
Expand All @@ -109,7 +111,7 @@ open class SchoolCalendar {
val format = SimpleDateFormat(formatString, Locale.CHINA)
return format.format(calendar.time)
}

/**
* 格式化解析日期文本
* 年:y 月:M 日:d 时:h(12制)/H(24值) 分:m 秒:s 毫秒:S
Expand All @@ -125,15 +127,15 @@ open class SchoolCalendar {
null
}
}

fun setDate(year: Int, month: Int, day: Int): SchoolCalendar {
calendar[year, month] = day
return this
}

val date: Date
get() = calendar.time

val dayOfWeek: Int
get() {
val weekDay = calendar[Calendar.DAY_OF_WEEK]
Expand All @@ -143,29 +145,148 @@ open class SchoolCalendar {
weekDay - 1
}
}

val day: Int
get() = calendar[Calendar.DATE]

val month: Int
get() = calendar[Calendar.MONTH] + 1

val year: Int
get() = calendar[Calendar.YEAR]

override fun equals(other: Any?): Boolean {
return if (other is SchoolCalendar) {
dayOfTerm == other.dayOfTerm
} else false
}

override fun hashCode(): Int {
var result = firstDay.hashCode()
result = 31 * result + calendar.hashCode()
return result
}

companion object {
const val FIRST_DAY = "first_day"
private const val FIRST_MON_DAY = "first_day"

/**
* 得到这学期过去了多少天
*
* 返回 null,则说明不知道开学第一天是好久
*
* # 注意:存在返回负数的情况!!!
*/
fun getDayOfTerm(): Int? {
return checkFirstDay {
val diff = System.currentTimeMillis() - mFirstMonDayCalendar.timeInMillis
TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS).toInt()
}
}

/**
* 得到当前周数
*
* @return 返回 null,则说明不知道开学第一天是好久;返回 0,则表示还没有开学
*
* # 注意:存在返回负数的情况!!!
*/
fun getWeekOfTerm(): Int? {
return getDayOfTerm()?.div(7)
}

/**
* 是否是上学期(即秋季学期),否则是下学期(春季学期)
*/
fun isFirstSemester() : Boolean {
return Calendar.getInstance()[Calendar.MONTH + 1] > 8
}

/**
* 得到开学第一周的星期一
*
* @return 返回 null,则说明不知道开学第一天是好久
*/
fun getFirstMonDayOfTerm(): Calendar? {
return checkFirstDay {
mFirstMonDayCalendar.clone() as Calendar
}
}

fun getFirstMonDayTimestamp(): Long? {
return checkFirstDay {
mFirstMonDayCalendar.timeInMillis
}
}

/**
* 更新开学时间
* @param nowWeek 当前周数,支持负数
*/
fun updateFirstCalendar(nowWeek: Int) {
val calendar = Calendar.getInstance()
calendar.add(
Calendar.DATE,
-((nowWeek - 1) * 7 + (calendar.get(Calendar.DAY_OF_WEEK) + 5) % 7)
)
/*
* (calendar.get(Calendar.DAY_OF_WEEK) + 5) % 7 的逻辑如下:
* 星期天:1 -> 6
* 星期一:2 -> 0
* 星期二:3 -> 1
* 星期三:4 -> 2
* 星期四:5 -> 3
* 星期五:6 -> 4
* 星期六:7 -> 5
*
* 左边一栏是 Calendar.get(Calendar.DAY_OF_WEEK) 得到的数字,
* 右边一栏是该数字距离周一的天数差
*
* 如果 nowWeek = 0,且今天是星期一:
* (nowWeek - 1) * 7 = -7
* (calendar.get(Calendar.DAY_OF_WEEK) + 5) % 7 = 0
* 那么 合在一起就是 -(-7 + 0) = 7
*
* 再使用 add(Calendar.DATE, 7)
*
* 得到的就是开学第一周星期一的 calendar
* */

// 保证是绝对的第一天的开始
calendar.apply {
set(Calendar.HOUR_OF_DAY, 0)
set(Calendar.MINUTE, 0)
set(Calendar.SECOND, 0)
set(Calendar.MILLISECOND, 0)
}
mFirstMonDayCalendar.timeInMillis = calendar.timeInMillis
BaseApp.appContext.defaultSharedPreferences.edit {
// 因为那边 lib_common 有类还需要使用这个,所以需要保存在 defaultSp 中
putLong(FIRST_MON_DAY, calendar.timeInMillis)
}
}

private var mFirstMonDayCalendar = Calendar.getInstance().apply {
timeInMillis = BaseApp.appContext.defaultSharedPreferences.getLong(FIRST_MON_DAY, 0L)
}

/**
* 检查 [mFirstMonDayCalendar] 是否正确
*
* 只有他第一次安装且没有网络时才会出现这个情况,只要之后加载了网络,都不会再出现问题
*/
private inline fun <T> checkFirstDay(action: () -> T): T? {
// 不知道第一天的时间戳,说明之前都没有登录过课表
mFirstMonDayCalendar.timeInMillis = BaseApp.appContext.defaultSharedPreferences.getLong(FIRST_MON_DAY, 0L)
if (mFirstMonDayCalendar.timeInMillis == 0L) return null
mFirstMonDayCalendar.apply {
// 保证是绝对的第一天的开始
set(Calendar.HOUR_OF_DAY, 0)
set(Calendar.MINUTE, 0)
set(Calendar.SECOND, 0)
set(Calendar.MILLISECOND, 0)
}
return action.invoke()
}
}
}
Loading

0 comments on commit 0337aee

Please sign in to comment.