Skip to content

Commit

Permalink
🐛 修改同步远端和本地的网络请求
Browse files Browse the repository at this point in the history
  • Loading branch information
sanhuzhen committed Nov 21, 2024
1 parent 4e145ca commit 0e45702
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ interface TodoApiService {
@GET("/magipoke-todo/sync-time")
fun getLastSyncTime(
@Query("sync_time")
sync_time: Long
syncTime: Long
): Single<ApiWrapper<SyncTime>>

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,12 @@ class TodoAllFragment : BaseFragment(), TodoAllAdapter.OnItemClickListener {
viewLifecycleOwner
)
} else {
item.remindMode.notifyDateTime = item.endTime
val syncTime = appContext.getSp("todo").getLong("TODO_LAST_SYNC_TIME", 0L)
mViewModel.pushTodo(TodoListPushWrapper(listOf(item), syncTime, 1, 0))
if (item.endTime != ""){
item.remindMode.notifyDateTime = item.endTime
val syncTime = appContext.getSp("todo").getLong("TODO_LAST_SYNC_TIME", 0L)
mViewModel.pushTodo(TodoListPushWrapper(listOf(item), syncTime, 1, 0))
}

}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.mredrock.cyxbs.lib.utils.extensions.appContext
import com.mredrock.cyxbs.lib.utils.extensions.getSp
import com.mredrock.cyxbs.lib.utils.extensions.gone
import com.mredrock.cyxbs.lib.utils.extensions.visible
import com.mredrock.cyxbs.lib.utils.utils.LogUtils
import com.mredrock.cyxbs.todo.model.bean.DelPushWrapper
import com.mredrock.cyxbs.todo.model.bean.RemindMode
import com.mredrock.cyxbs.todo.model.bean.TodoListPushWrapper
Expand Down Expand Up @@ -72,7 +73,7 @@ class TodoFeedFragment : BaseFragment() {

if (todoList[it].remindMode.repeatMode != RemindMode.NONE) {
val notifyDateTime = getNextNoticeTime(todoList[it])

notifyItemChanged(it)
if (!notifyDateTime.isNullOrEmpty()) {
// 如果有下次提醒时间,推送更新的todo
todoList[it].remindMode.notifyDateTime = notifyDateTime
Expand Down Expand Up @@ -136,7 +137,7 @@ class TodoFeedFragment : BaseFragment() {
}

private fun getNextNoticeTime(todo: Todo): String? {
val dateFormat = SimpleDateFormat("yyyy年MM月dd日HH:mm", Locale.getDefault())
val dateFormat = SimpleDateFormat("yyyy年M月d日HH:mm", Locale.getDefault())
val now = Calendar.getInstance()

// 初始化当前时间,如果 notifyDateTime 为空则使用当前时间
Expand Down Expand Up @@ -166,24 +167,19 @@ class TodoFeedFragment : BaseFragment() {
}

RemindMode.WEEK -> {
// 获取今天的星期几(将周日=1 转换为周一=1,周日=7)
val today = (startTime.get(Calendar.DAY_OF_WEEK) + 5) % 7 + 1
val sortedWeekDays = todo.remindMode.week.sorted()

// 找到下一个有效的提醒日
val nextValidDay = sortedWeekDays.firstOrNull { it > today } ?: sortedWeekDays.firstOrNull()
val nextValidDay =
sortedWeekDays.firstOrNull { it > today } ?: sortedWeekDays.firstOrNull()

if (nextValidDay != null) {
// 计算需要增加的天数
val daysToAdd = if (nextValidDay <= today) {
var daysToAdd = nextValidDay - today
if (daysToAdd <= 0) {
// 如果下一个有效提醒日已经过去,则推迟到下周
startTime.add(Calendar.WEEK_OF_YEAR, 1)
(nextValidDay - today + 7) % 7 // 计算下一个有效提醒日的天数
} else {
(nextValidDay - today) // 直接计算天数
daysToAdd += 7
}

// 增加天数到开始时间
startTime.add(Calendar.DAY_OF_MONTH, daysToAdd)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,11 @@ class TodoLifeFragment : BaseFragment(), TodoAllAdapter.OnItemClickListener {
viewLifecycleOwner
)
} else {
item.remindMode.notifyDateTime = item.endTime
val syncTime = appContext.getSp("todo").getLong("TODO_LAST_SYNC_TIME", 0L)
mViewModel.pushTodo(TodoListPushWrapper(listOf(item), syncTime, 1, 0))
if (item.endTime != ""){
item.remindMode.notifyDateTime = item.endTime
val syncTime = appContext.getSp("todo").getLong("TODO_LAST_SYNC_TIME", 0L)
mViewModel.pushTodo(TodoListPushWrapper(listOf(item), syncTime, 1, 0))
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,11 @@ class TodoOtherFragment : BaseFragment(), TodoAllAdapter.OnItemClickListener {
viewLifecycleOwner
)
} else {
item.remindMode.notifyDateTime = item.endTime
val syncTime = appContext.getSp("todo").getLong("TODO_LAST_SYNC_TIME", 0L)
mViewModel.pushTodo(TodoListPushWrapper(listOf(item), syncTime, 1, 0))
if (item.endTime != ""){
item.remindMode.notifyDateTime = item.endTime
val syncTime = appContext.getSp("todo").getLong("TODO_LAST_SYNC_TIME", 0L)
mViewModel.pushTodo(TodoListPushWrapper(listOf(item), syncTime, 1, 0))
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,11 @@ class TodoStudyFragment : BaseFragment(), TodoAllAdapter.OnItemClickListener {
viewLifecycleOwner
)
} else {
item.remindMode.notifyDateTime = item.endTime
val syncTime = appContext.getSp("todo").getLong("TODO_LAST_SYNC_TIME", 0L)
mViewModel.pushTodo(TodoListPushWrapper(listOf(item), syncTime, 1, 0))
if (item.endTime != ""){
item.remindMode.notifyDateTime = item.endTime
val syncTime = appContext.getSp("todo").getLong("TODO_LAST_SYNC_TIME", 0L)
mViewModel.pushTodo(TodoListPushWrapper(listOf(item), syncTime, 1, 0))
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.mredrock.cyxbs.lib.utils.network.mapOrInterceptException
import com.mredrock.cyxbs.todo.model.bean.DelPushWrapper
import com.mredrock.cyxbs.todo.model.bean.RemindMode.Companion.generateDefaultRemindMode
import com.mredrock.cyxbs.todo.model.bean.Todo
import com.mredrock.cyxbs.todo.model.bean.TodoListGetWrapper
import com.mredrock.cyxbs.todo.model.bean.TodoListPushWrapper
import com.mredrock.cyxbs.todo.model.bean.TodoListSyncTimeWrapper
import com.mredrock.cyxbs.todo.model.bean.TodoPinData
Expand All @@ -28,8 +27,6 @@ class TodoViewModel : BaseViewModel() {

private val _allTodo = MutableLiveData<TodoListSyncTimeWrapper>()
val allTodo: LiveData<TodoListSyncTimeWrapper> get() = _allTodo
private val _changedTodo = MutableLiveData<TodoListGetWrapper>()
val changedTodo: LiveData<TodoListGetWrapper> get() = _changedTodo
private val _categoryTodoStudy = MutableLiveData<TodoListSyncTimeWrapper>()
val categoryTodoStudy: LiveData<TodoListSyncTimeWrapper> get() = _categoryTodoStudy
private val _categoryTodoLife = MutableLiveData<TodoListSyncTimeWrapper>()
Expand All @@ -51,13 +48,19 @@ class TodoViewModel : BaseViewModel() {
_isChanged.value = state
}

fun judgeChange(todoAfterChange: Todo) {
_isChanged.value = todoAfterChange != rawTodo
}

init {
_isChanged.value = false
getAllTodo()
syncTodo()
}

private fun syncTodo() {
if (isLocalModify()){
//当本地有修改,则同步本地
syncWithLocal()
}else{
//如果本地没有修改,则直接同步远端
getAllTodo()
}
}

/**
Expand Down Expand Up @@ -95,14 +98,15 @@ class TodoViewModel : BaseViewModel() {
}
.safeSubscribeBy {
/**
* 由于是老接口,故在这里设置一个更新时间戳,用来添加新手教程,防止一些老用户没有出现新手教程
* 由于是老接口,这里设置一个sp用于判断是否是第一次使用,添加新手教程
*/
if ((it.todoArray.isNullOrEmpty() && it.syncTime == 0L) || it.syncTime < 1726843099) {
if ((it.todoArray.isNullOrEmpty() && it.syncTime == 0L) || isFirstUse()) {
val todoList = listOf(
Todo(1, "长按可以拖动我哟", "", 0, generateDefaultRemindMode(), System.currentTimeMillis(), "", "", 0, 0),
Todo(2, "左滑可置顶或者删除", "", 0, generateDefaultRemindMode(), System.currentTimeMillis(), "", "", 0, 0),
Todo(3, "点击查看代办详情", "", 0, generateDefaultRemindMode(), System.currentTimeMillis(), "", "", 0, 0)
)
setOldUse()
val syncTime = getLastSyncTime()
val firstPush = if (syncTime == 0L) 1 else 0
pushTodo(
Expand Down Expand Up @@ -136,7 +140,7 @@ class TodoViewModel : BaseViewModel() {
it.syncTime.apply {
setLastSyncTime(this)
}
syncTodo(todos)
syncWithRemote(todos)
}

}
Expand All @@ -161,13 +165,13 @@ class TodoViewModel : BaseViewModel() {
.safeSubscribeBy {
getAllTodo()
viewModelScope.launch(Dispatchers.IO) {
setLastModifyTime(it.syncTime)
TodoDatabase.instance.todoDao().insertAll(pushWrapper.todoList)
}
TodoWidget.sendAddTodoBroadcast(appContext)
_isPushed.postValue(true)
it.syncTime.apply {
setLastSyncTime(this)
setLastModifyTime(this)
}
}
}
Expand Down Expand Up @@ -299,52 +303,19 @@ class TodoViewModel : BaseViewModel() {
}
}

/**
* 同步远端与本地数据
*/
fun syncTodo(todoList: List<Todo>) {
if (getLastModifyTime() == getLastSyncTime()) {
return
}
// 本地数据为空,直接同步远端
if (getLastModifyTime() == 0L && getLastSyncTime() != 0L) {
syncWithRemote(todoList)
return
}

// 如果远端数据为空,直接同步本地数据
if (getLastSyncTime() == 0L) {
syncWithLocal()
return
}

// 检查同步时间
val syncTime = getLastSyncTime()
val modifyTime = getLastModifyTime()

// 根据时间戳决定同步方向
if (modifyTime > syncTime) {
syncRemoteFromLocal(syncTime)
} else {
if (allTodo.value?.todoArray?.isNotEmpty() == true) {
syncLocalFromRemote()
}
}

getAllTodo()
}

private fun syncWithRemote(todoList: List<Todo>) {
setLastModifyTime(getLastSyncTime())
viewModelScope.launch(Dispatchers.IO) {
TodoDatabase.instance.todoDao().apply {
deleteAll()
insertAll(todoList)
if (!isLocalModify()){
setLastModifyTime(getLastSyncTime())
viewModelScope.launch(Dispatchers.IO) {
TodoDatabase.instance.todoDao().apply {
deleteAll()
insertAll(todoList)
}
}
}
}

private fun syncWithLocal() {
setLastSyncTime(getLastModifyTime())
viewModelScope.launch(Dispatchers.IO) {
TodoDatabase.instance.todoDao().apply {
queryAll()?.let {
Expand All @@ -355,33 +326,20 @@ class TodoViewModel : BaseViewModel() {
}
}

private fun syncRemoteFromLocal(syncTime: Long) {
viewModelScope.launch(Dispatchers.IO) {
TodoDatabase.instance.todoDao().apply {
queryAll()?.let {
pushTodo(TodoListPushWrapper(it, getLastModifyTime(), 1, 0))
}

// 得到本地数据库中被删除的元素
val deletedIds = allTodo.value?.todoArray
?.filterNot { it in queryAll()!! }
?.map { it.todoId }
?.toLongArray() ?: longArrayOf()
// 是否是首次使用
private fun isFirstUse(): Boolean =
appContext.getSp("newUser").getBoolean("TODO_NEW_USE", true)

if (deletedIds.isNotEmpty()) {
delTodo(DelPushWrapper(deletedIds.toList(), syncTime))
}
}
private fun setOldUse() {
appContext.getSp("newUser").edit().apply {
putBoolean("TODO_NEW_USE", false)
commit()
}
}

private fun syncLocalFromRemote() {
viewModelScope.launch(Dispatchers.IO) {
TodoDatabase.instance.todoDao().apply {
deleteAll()
allTodo.value?.todoArray?.let { insertAll(it) }
}
}
// 是否是本地修改
private fun isLocalModify(): Boolean{
return getLastModifyTime() - getLastSyncTime() > 1000L
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="1dp"
android:height="1dp"
android:viewportWidth="1024"
android:viewportHeight="1024">
android:viewportWidth="128"
android:viewportHeight="128">
<path
android:pathData="M512,512m-445,0a445,445 0,1 0,890 0,445 445,0 1,0 -890,0Z"
android:pathData="M64,64m-55.625,0a55.625,55.625 0,1 0,111.25 0,55.625 55.625,0 1,0 -111.25,0Z"
android:fillColor="#5263FF"/>
</vector>

0 comments on commit 0e45702

Please sign in to comment.