Swipe to refresh detail view

This commit is contained in:
Philipp Heckel 2021-11-14 16:48:50 -05:00
parent 09a5d5b4f5
commit 1616b27816
6 changed files with 31 additions and 19 deletions

View file

@ -19,6 +19,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import io.heckel.ntfy.R
import io.heckel.ntfy.app.Application
import io.heckel.ntfy.data.Notification
@ -48,6 +49,7 @@ class DetailActivity : AppCompatActivity(), ActionMode.Callback {
// UI elements
private lateinit var adapter: DetailAdapter
private lateinit var mainList: RecyclerView
private lateinit var mainListContainer: SwipeRefreshLayout
private lateinit var menu: Menu
// Action mode stuff
@ -87,6 +89,11 @@ class DetailActivity : AppCompatActivity(), ActionMode.Callback {
howToExample.text = Html.fromHtml(howToText)
}
// Swipe to refresh
mainListContainer = findViewById(R.id.detail_notification_list_container)
mainListContainer.setOnRefreshListener { refresh() }
mainListContainer.setColorSchemeResources(R.color.primaryColor)
// Update main list based on viewModel (& its datasource/livedata)
val noEntriesText: View = findViewById(R.id.detail_no_notifications)
val onNotificationClick = { n: Notification -> onNotificationClick(n) }
@ -100,10 +107,10 @@ class DetailActivity : AppCompatActivity(), ActionMode.Callback {
it?.let {
adapter.submitList(it as MutableList<Notification>)
if (it.isEmpty()) {
mainList.visibility = View.GONE
mainListContainer.visibility = View.GONE
noEntriesText.visibility = View.VISIBLE
} else {
mainList.visibility = View.VISIBLE
mainListContainer.visibility = View.VISIBLE
noEntriesText.visibility = View.GONE
}
}
@ -128,10 +135,6 @@ class DetailActivity : AppCompatActivity(), ActionMode.Callback {
onTestClick()
true
}
R.id.detail_menu_refresh -> {
onRefreshClick()
true
}
R.id.detail_menu_enable_instant -> {
onInstantEnableClick(enable = true)
true
@ -181,7 +184,7 @@ class DetailActivity : AppCompatActivity(), ActionMode.Callback {
.show()
}
private fun onRefreshClick() {
private fun refresh() {
Log.d(TAG, "Fetching cached notifications for ${topicShortUrl(subscriptionBaseUrl, subscriptionTopic)}")
lifecycleScope.launch(Dispatchers.IO) {
@ -194,12 +197,16 @@ class DetailActivity : AppCompatActivity(), ActionMode.Callback {
getString(R.string.refresh_message_result, newNotifications.size)
}
newNotifications.forEach { notification -> repository.addNotification(notification) }
runOnUiThread { Toast.makeText(this@DetailActivity, toastMessage, Toast.LENGTH_LONG).show() }
runOnUiThread {
Toast.makeText(this@DetailActivity, toastMessage, Toast.LENGTH_LONG).show()
mainListContainer.isRefreshing = false
}
} catch (e: Exception) {
runOnUiThread {
Toast
.makeText(this@DetailActivity, getString(R.string.refresh_message_error, e.message), Toast.LENGTH_LONG)
.show()
mainListContainer.isRefreshing = false
}
}
}

View file

@ -71,8 +71,8 @@ class MainActivity : AppCompatActivity(), ActionMode.Callback {
onSubscribeButtonClick()
}
// Swipewn to refresh
mainListContainer = findViewById(R.id.main_subscriptions_list_swipe_container)
// Swipe to refresh
mainListContainer = findViewById(R.id.main_subscriptions_list_container)
mainListContainer.setOnRefreshListener { refreshAllSubscriptions() }
mainListContainer.setColorSchemeResources(R.color.primaryColor)

View file

@ -7,15 +7,22 @@
android:layout_height="match_parent"
tools:context=".ui.DetailActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/detail_notification_list"
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/detail_notification_list_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
android:layout_marginTop="10dp"
android:background="?android:attr/selectableItemBackground"
app:layoutManager="LinearLayoutManager" android:visibility="gone"/>
android:visibility="gone">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/detail_notification_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
android:layout_marginTop="10dp"
android:background="?android:attr/selectableItemBackground"
app:layoutManager="LinearLayoutManager"/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<LinearLayout
android:orientation="vertical"

View file

@ -5,7 +5,7 @@
android:layout_height="match_parent">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/main_subscriptions_list_swipe_container"
android:id="@+id/main_subscriptions_list_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"

View file

@ -3,7 +3,6 @@
<item android:id="@+id/detail_menu_enable_instant" android:title="@string/detail_menu_enable_instant"
/>
<item android:id="@+id/detail_menu_disable_instant" android:title="@string/detail_menu_disable_instant"/>
<item android:id="@+id/detail_menu_refresh" android:title="@string/detail_menu_refresh"/>
<item android:id="@+id/detail_menu_copy_url" android:title="@string/detail_menu_copy_url"/>
<item android:id="@+id/detail_menu_unsubscribe" android:title="@string/detail_menu_unsubscribe"/>
</menu>

View file

@ -68,7 +68,6 @@
<!-- Detail activity: Action bar -->
<string name="detail_menu_test">Send test notification</string>
<string name="detail_menu_copy_url">Copy topic address</string>
<string name="detail_menu_refresh">Force refresh</string>
<string name="detail_menu_enable_instant">Enable fast delivery</string>
<string name="detail_menu_disable_instant">Disable fast delivery</string>
<string name="detail_menu_unsubscribe">Unsubscribe</string>