Skip to content

Commit

Permalink
refine HistoryFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
5ec1cff committed Sep 30, 2023
1 parent e5dd340 commit 0a9ca86
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import androidx.paging.LoadState
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.github.panpf.sketch.displayImage
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import io.github.a13e300.ro_tieba.App
import io.github.a13e300.ro_tieba.BaseFragment
import io.github.a13e300.ro_tieba.DEFAULT_FORUM_AVATAR
Expand Down Expand Up @@ -41,9 +44,15 @@ class HistoryFragment : BaseFragment() {
binding.toolbar.setOnMenuItemClickListener {
return@setOnMenuItemClickListener when (it.itemId) {
R.id.remove_all -> {
lifecycleScope.launch(Dispatchers.IO) {
App.instance.db.historyDao().removeAll()
}
MaterialAlertDialogBuilder(requireContext())
.setTitle("确定删除所有历史记录?")
.setPositiveButton("确定") { _, _ ->
lifecycleScope.launch(Dispatchers.IO) {
App.instance.db.historyDao().removeAll()
}
}
.setNegativeButton("取消", null)
.show()
true
}

Expand All @@ -57,6 +66,12 @@ class HistoryFragment : BaseFragment() {
adapter.submitData(it)
}
}
adapter.addLoadStateListener { state ->
val empty =
state.append is LoadState.NotLoading && state.append.endOfPaginationReached && adapter.itemCount == 0
binding.resultTips.isVisible = empty
binding.toolbar.menu.findItem(R.id.remove_all).isVisible = !empty
}
return binding.root
}

Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_history.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportHeight="24"
android:viewportWidth="24"
android:width="24dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<path
android:fillColor="@android:color/white"
android:pathData="M13,3c-4.97,0 -9,4.03 -9,9L1,12l3.89,3.89 0.07,0.14L9,12L6,12c0,-3.87 3.13,-7 7,-7s7,3.13 7,7 -3.13,7 -7,7c-1.93,0 -3.68,-0.79 -4.94,-2.06l-1.42,1.42C8.27,19.99 10.51,21 13,21c4.97,0 9,-4.03 9,-9s-4.03,-9 -9,-9zM12,8v5l4.28,2.54 0.72,-1.21 -3.5,-2.08L13.5,8L12,8z" />
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/layout/fragment_history.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,13 @@
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:clipToPadding="false" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?textAppearanceBody1"
android:id="@+id/result_tips"
android:visibility="gone"
app:layout_anchor="@id/list"
app:layout_anchorGravity="center"
android:text="@string/no_history" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
3 changes: 2 additions & 1 deletion app/src/main/res/menu/home_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
<item
android:id="@+id/history"
android:title="@string/history"
app:showAsAction="never" />
android:icon="@drawable/ic_history"
app:showAsAction="ifRoom" />
</menu>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,5 @@
<font face="monospace">pm set-app-links-user-selection --package %s --user 0 true tieba.baidu.com</font>]]>
</string>
<string name="version">当前版本</string>
<string name="no_history">还没有历史记录</string>
</resources>

0 comments on commit 0a9ca86

Please sign in to comment.