Skip to content

Commit

Permalink
Merge pull request #485 from RedrockMobile/You/todo
Browse files Browse the repository at this point in the history
You/todo
  • Loading branch information
lytMoon authored Sep 9, 2024
2 parents acc7d35 + 3ad029a commit a4f5ae8
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ class AddAffairViewModel : BaseViewModel() {
getLastSyncTime()
)
AffairRepository.addTodo(pushWrapper)
.mapOrInterceptException { }
.doOnError{}
.safeSubscribeBy {
it.data.syncTime.apply {
setLastSyncTime(this)
}
setLastSyncTime(it.syncTime)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class TodoFeedAdapter :

// 定义日期格式
private val dateFormat = SimpleDateFormat("yyyy年MM月dd日HH:mm", Locale.getDefault())
private var mClick: ((Todo) -> Unit)? = null
fun onFinishCheck(listener: (Todo) -> Unit) {
private var mClick: ((Int) -> Unit)? = null
fun onFinishCheck(listener: (Int) -> Unit) {
mClick = listener
}

Expand All @@ -55,10 +55,8 @@ class TodoFeedAdapter :
val defaultCheckbox = itemView.findViewById<CheckLineView>(R.id.todo_iv_todo_feed)
init {
defaultCheckbox.setOnClickListener {
val position = absoluteAdapterPosition
if (position != RecyclerView.NO_POSITION && position in 0 until currentList.size) {
defaultCheckbox.setStatusWithAnime(true){
mClick?.invoke(currentList[position])
mClick?.invoke(absoluteAdapterPosition)
}
todoTitle.setTextColor(
ContextCompat.getColor(
Expand All @@ -74,7 +72,6 @@ class TodoFeedAdapter :
)
todoFeedIv.setImageResource(R.drawable.todo_ic_addtodo_notice2)
icRight.visible()
}
}
todoTitle.setOnClickListener {
startActivity(getItem(absoluteAdapterPosition),itemView.context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import java.io.Serializable
*/
data class TodoListSyncTimeWrapper(
@SerializedName("changed_todo_array")
var todoArray: List<Todo>, @SerializedName("sync_time")
var todoArray: List<Todo>?,
@SerializedName("sync_time")
val syncTime: Long
): Serializable
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ 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

/**
* description: 首页的邮子清单
Expand Down Expand Up @@ -64,33 +63,38 @@ class TodoFeedFragment : BaseFragment() {
layoutManager = LinearLayoutManager(context)
adapter = mAdapter.apply {
onFinishCheck {
it.isChecked = 1
val syncTime = appContext.getSp("todo").getLong("TODO_LAST_SYNC_TIME", 0L)
mViewModel.apply {
pushTodo(TodoListPushWrapper(listOf(it), syncTime, 1, 0))
getAllTodo()
if (it in todoList.indices){
todoList[it].isChecked = 1
val syncTime = appContext.getSp("todo").getLong("TODO_LAST_SYNC_TIME", 0L)
mViewModel.apply {
pushTodo(TodoListPushWrapper(listOf(todoList[it]), syncTime, 1, 0))
getAllTodo()
}
}

}
}
}
mViewModel.allTodo.observe(viewLifecycleOwner) {
val filteredList =
it.todoArray.filter { todo -> todo.isChecked == 0 && todo.todoId > 3 }
.take(3) // 只取未选中的前3个
todoList.apply {
clear()
addAll(filteredList)
}
LogUtils.d("TodoFeedFragment", "推送成功 ${todoList}")
if (todoList.isEmpty()) {
mTv.visible()
mRv.gone()
mTv.text = "还没有待做事项哦~快去添加吧!"
} else {
mRv.visible()
mTv.gone()
it.todoArray?.let{ todos ->
val filteredList =
todos.filter { todo -> todo.isChecked == 0 && todo.todoId > 3 }
.take(3) // 只取未选中的前3个
todoList.apply {
clear()
addAll(filteredList)
}
if (todoList.isEmpty()) {
mTv.visible()
mRv.gone()
mTv.text = "还没有待做事项哦~快去添加吧!"
} else {
mRv.visible()
mTv.gone()
}
mAdapter.submitList(filteredList.toList())
}
mAdapter.submitList(filteredList.toList())

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import com.mredrock.cyxbs.todo.repository.TodoRepository
import com.mredrock.cyxbs.todo.ui.widget.TodoWidget
import com.mredrock.cyxbs.lib.base.ui.BaseViewModel
import com.mredrock.cyxbs.lib.utils.extensions.getSp
import com.mredrock.cyxbs.lib.utils.network.mapOrInterceptException
import com.mredrock.cyxbs.lib.utils.utils.LogUtils
import com.mredrock.cyxbs.todo.model.bean.RemindMode.Companion.generateDefaultRemindMode
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

Expand Down Expand Up @@ -66,7 +68,7 @@ class TodoViewModel : BaseViewModel() {
fun getAllTodo() {
TodoRepository
.queryAllTodo()
.doOnError {
.mapOrInterceptException {
viewModelScope.launch(Dispatchers.IO) {
val modifyTime = System.currentTimeMillis() / 1000
TodoDatabase.instance.todoDao().apply {
Expand Down Expand Up @@ -94,31 +96,48 @@ class TodoViewModel : BaseViewModel() {
}
}
.safeSubscribeBy {
_allTodo.postValue(
TodoListSyncTimeWrapper(it.data.todoArray, it.data.syncTime)
)
_categoryTodoStudy.postValue(
TodoListSyncTimeWrapper(
it.data.todoArray.filter { todo -> todo.type == "study" },
it.data.syncTime
if (it.todoArray.isNullOrEmpty() && it.syncTime == 0L) {
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)
)
)
_categoryTodoLife.postValue(
TodoListSyncTimeWrapper(
it.data.todoArray.filter { todo -> todo.type == "life" },
it.data.syncTime
val syncTime = getLastSyncTime()
val firstPush = if (syncTime == 0L) 1 else 0
pushTodo(
TodoListPushWrapper(
todoList, syncTime, TodoListPushWrapper.NONE_FORCE, firstPush
)
)
)
_categoryTodoOther.postValue(
TodoListSyncTimeWrapper(
it.data.todoArray.filter { todo -> todo.type == "other" },
it.data.syncTime
}
it.todoArray?.let { todos ->
_allTodo.postValue(
TodoListSyncTimeWrapper(todos, it.syncTime)
)
)
it.data.syncTime.apply {
setLastSyncTime(this)
_categoryTodoStudy.postValue(
TodoListSyncTimeWrapper(
todos.filter { todo -> todo.type == "study" },
it.syncTime
)
)
_categoryTodoLife.postValue(
TodoListSyncTimeWrapper(
todos.filter { todo -> todo.type == "life" },
it.syncTime
)
)
_categoryTodoOther.postValue(
TodoListSyncTimeWrapper(
todos.filter { todo -> todo.type == "other" },
it.syncTime
)
)
it.syncTime.apply {
setLastSyncTime(this)
}
syncTodo(todos)
}
syncTodo(it.data.todoArray)

}
}

Expand All @@ -128,7 +147,7 @@ class TodoViewModel : BaseViewModel() {
fun pushTodo(pushWrapper: TodoListPushWrapper) {
TodoRepository
.pushTodo(pushWrapper)
.doOnError {
.mapOrInterceptException {
viewModelScope.launch {
val modifyTime = System.currentTimeMillis() / 1000
setLastModifyTime(modifyTime)
Expand All @@ -140,13 +159,13 @@ class TodoViewModel : BaseViewModel() {
}
.safeSubscribeBy {
getAllTodo()
viewModelScope.launch {
setLastModifyTime(it.data.syncTime)
viewModelScope.launch(Dispatchers.IO) {
setLastModifyTime(it.syncTime)
TodoDatabase.instance.todoDao().insertAll(pushWrapper.todoList)
}
TodoWidget.sendAddTodoBroadcast(appContext)
_isPushed.postValue(true)
it.data.syncTime.apply {
it.syncTime.apply {
setLastSyncTime(this)
}
}
Expand All @@ -165,20 +184,21 @@ class TodoViewModel : BaseViewModel() {
)

TodoRepository.pushTodo(pushWrapper)
.doOnError {
.mapOrInterceptException {
viewModelScope.launch {
val modifyTime = System.currentTimeMillis() / 1000
setLastModifyTime(modifyTime)
TodoDatabase.instance.todoDao().insertAll(pushWrapper.todoList)
TodoWidget.sendAddTodoBroadcast(appContext)
}
}.safeSubscribeBy {
}
.safeSubscribeBy {
viewModelScope.launch {
setLastModifyTime(it.data.syncTime)
setLastModifyTime(it.syncTime)
TodoDatabase.instance.todoDao().insertAll(pushWrapper.todoList)
}
TodoWidget.sendAddTodoBroadcast(appContext)
it.data.syncTime.apply {
it.syncTime.apply {
setLastSyncTime(this)
setLastModifyTime(this)
}
Expand All @@ -193,7 +213,7 @@ class TodoViewModel : BaseViewModel() {
fun delTodo(delPushWrapper: DelPushWrapper) {
TodoRepository
.delTodo(delPushWrapper)
.doOnError {
.mapOrInterceptException {
viewModelScope.launch {
val modifyTime = System.currentTimeMillis() / 1000
setLastModifyTime(modifyTime)
Expand All @@ -210,7 +230,7 @@ class TodoViewModel : BaseViewModel() {
TodoDatabase.instance.todoDao().deleteTodoById(todoId)
}
}
it.data.syncTime.apply {
it.syncTime.apply {
setLastSyncTime(this)
setLastModifyTime(this)
}
Expand All @@ -223,7 +243,7 @@ class TodoViewModel : BaseViewModel() {
fun pinTodo(todoPinData: TodoPinData) {
TodoRepository
.pinTodo(todoPinData)
.doOnError {
.mapOrInterceptException {
viewModelScope.launch(Dispatchers.IO) {
val modifyTime = System.currentTimeMillis() / 1000
setLastModifyTime(modifyTime)
Expand All @@ -243,7 +263,7 @@ class TodoViewModel : BaseViewModel() {
TodoDatabase.instance.todoDao().insert(todo)
}
}
it.data.syncTime.apply {
it.syncTime.apply {
setLastSyncTime(this)
setLastModifyTime(this)
}
Expand Down Expand Up @@ -280,6 +300,9 @@ class TodoViewModel : BaseViewModel() {
* 同步远端与本地数据
*/
fun syncTodo(todoList: List<Todo>) {
if (getLastModifyTime() == getLastSyncTime()) {
return
}
// 本地数据为空,直接同步远端
if (getLastModifyTime() == 0L && getLastSyncTime() != 0L) {
syncWithRemote(todoList)
Expand All @@ -296,10 +319,6 @@ class TodoViewModel : BaseViewModel() {
val syncTime = getLastSyncTime()
val modifyTime = getLastModifyTime()

if (syncTime == modifyTime) {
return // 数据已同步,无需操作
}

// 根据时间戳决定同步方向
if (modifyTime > syncTime) {
syncRemoteFromLocal(syncTime)
Expand All @@ -318,23 +337,27 @@ class TodoViewModel : BaseViewModel() {
TodoDatabase.instance.todoDao().apply {
deleteAll()
insertAll(todoList)
LogUtils.d("TodoDao", "${queryAll()}")
}
}
}

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

}
}
}

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

// 得到本地数据库中被删除的元素
val deletedIds = allTodo.value?.todoArray
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
android:layout_height="40dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_height="45dp"
android:layout_below="@id/todo_bt_confirm_timeselector"/>

</RelativeLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class DetailActivity : BaseActivity() {
tvMinutes.gone()
tvSeconds.gone()
layout.gone()
// tvAddTodo.gone()
tvAddTodo.gone()
} else {
startDownTimer(it.activityStartAt)
}
Expand Down Expand Up @@ -310,7 +310,6 @@ class DetailActivity : BaseActivity() {

private fun otherTrans(timestampInSeconds: Long): String {
val date = Date(timestampInSeconds * 1000L)

val format = SimpleDateFormat("yyyy年MM月dd日HH:mm", Locale.getDefault())
return format.format(date)
}
Expand Down
Loading

0 comments on commit a4f5ae8

Please sign in to comment.