Have a card for each entry, rendered from last visited first to oldest. Will probably start choking once the history has too many entries.main
parent
473b1e012c
commit
8231a7080a
@ -0,0 +1,64 @@
|
||||
package dev.lowrespalmtree.comet
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.core.os.bundleOf
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import androidx.navigation.fragment.findNavController
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import dev.lowrespalmtree.comet.History.HistoryEntry
|
||||
import dev.lowrespalmtree.comet.HistoryItemAdapter.HistoryItemAdapterListener
|
||||
import dev.lowrespalmtree.comet.databinding.FragmentHistoryListBinding
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@ExperimentalCoroutinesApi
|
||||
class HistoryFragment : Fragment(), HistoryItemAdapterListener {
|
||||
private lateinit var binding: FragmentHistoryListBinding
|
||||
private lateinit var historyViewModel: HistoryViewModel
|
||||
private lateinit var adapter: HistoryItemAdapter
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View {
|
||||
binding = FragmentHistoryListBinding.inflate(layoutInflater)
|
||||
return binding.root
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
historyViewModel = ViewModelProvider(this)[HistoryViewModel::class.java]
|
||||
adapter = HistoryItemAdapter(this)
|
||||
binding.list.layoutManager = LinearLayoutManager(requireContext())
|
||||
binding.list.adapter = adapter
|
||||
|
||||
historyViewModel.items.observe(viewLifecycleOwner, { adapter.setItems(it) })
|
||||
|
||||
historyViewModel.refreshHistory()
|
||||
}
|
||||
|
||||
override fun onItemClick(url: String) {
|
||||
val bundle = bundleOf("url" to url)
|
||||
findNavController().navigate(R.id.action_global_pageViewFragment, bundle)
|
||||
}
|
||||
|
||||
@ExperimentalCoroutinesApi
|
||||
class HistoryViewModel : ViewModel() {
|
||||
val items: MutableLiveData<List<HistoryEntry>>
|
||||
by lazy { MutableLiveData<List<HistoryEntry>>() }
|
||||
|
||||
fun refreshHistory() {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
items.postValue(History.getAll())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package dev.lowrespalmtree.comet
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import dev.lowrespalmtree.comet.History.HistoryEntry
|
||||
import dev.lowrespalmtree.comet.databinding.FragmentHistoryItemBinding
|
||||
|
||||
class HistoryItemAdapter(private val listener: HistoryItemAdapterListener) :
|
||||
RecyclerView.Adapter<HistoryItemAdapter.ViewHolder>() {
|
||||
private var items = listOf<HistoryEntry>()
|
||||
|
||||
interface HistoryItemAdapterListener {
|
||||
fun onItemClick(url: String)
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder =
|
||||
ViewHolder(
|
||||
FragmentHistoryItemBinding.inflate(
|
||||
LayoutInflater.from(parent.context),
|
||||
parent,
|
||||
false
|
||||
)
|
||||
)
|
||||
|
||||
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||
val item = items[position]
|
||||
holder.binding.uriText.text = item.uri
|
||||
holder.binding.titleText.visibility =
|
||||
if (item.title.isNullOrBlank()) View.GONE else View.VISIBLE
|
||||
holder.binding.titleText.text = item.title ?: ""
|
||||
holder.binding.card.setOnClickListener { listener.onItemClick(item.uri) }
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int = items.size
|
||||
|
||||
@SuppressLint("NotifyDataSetChanged")
|
||||
fun setItems(items: List<HistoryEntry>) {
|
||||
this.items = items.toList()
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
|
||||
inner class ViewHolder(val binding: FragmentHistoryItemBinding) :
|
||||
RecyclerView.ViewHolder(binding.root)
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package dev.lowrespalmtree.comet
|
||||
|
||||
import android.content.Context
|
||||
import androidx.preference.PreferenceManager
|
||||
|
||||
object Preferences {
|
||||
fun getHomeUrl(context: Context): String? =
|
||||
PreferenceManager.getDefaultSharedPreferences(context).getString("home", null)
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.cardview.widget.CardView
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:id="@+id/card"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:clickable="true"
|
||||
android:focusable="true"
|
||||
android:layout_margin="4dp"
|
||||
app:cardCornerRadius="4dp"
|
||||
app:cardElevation="4dp"
|
||||
android:foreground="?android:selectableItemBackground">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/uriText"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="16sp"
|
||||
android:textColor="@color/text"
|
||||
android:layout_margin="8dp"
|
||||
android:fontFamily="@font/preformatted"
|
||||
android:typeface="monospace" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/titleText"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="14sp"
|
||||
android:textColor="@color/text"
|
||||
android:layout_marginStart="8dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:layout_marginTop="0dp"
|
||||
android:layout_marginBottom="8dp"
|
||||
android:textAppearance="?attr/textAppearanceListItem" />
|
||||
</LinearLayout>
|
||||
|
||||
</androidx.cardview.widget.CardView>
|
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/list"
|
||||
android:name="dev.lowrespalmtree.comet.HistoryFragment"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginLeft="16dp"
|
||||
android:layout_marginRight="16dp"
|
||||
app:layoutManager="LinearLayoutManager"
|
||||
tools:context=".HistoryFragment"
|
||||
tools:listitem="@layout/fragment_history_item" />
|
@ -1,17 +1,16 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<item
|
||||
android:id="@+id/home"
|
||||
android:id="@+id/homeItem"
|
||||
android:icon="@android:drawable/ic_menu_myplaces"
|
||||
android:title="@string/home"
|
||||
android:onClick="goHome" />
|
||||
android:onClick="goHome"
|
||||
android:title="@string/home" />
|
||||
<item
|
||||
android:id="@+id/history"
|
||||
android:id="@+id/historyFragment"
|
||||
android:icon="@android:drawable/ic_menu_recent_history"
|
||||
android:title="@string/history" />
|
||||
<item
|
||||
android:id="@+id/settings"
|
||||
android:id="@+id/settingsFragment"
|
||||
android:icon="@android:drawable/ic_menu_preferences"
|
||||
android:title="@string/settings"
|
||||
android:onClick="openSettings" />
|
||||
android:title="@string/settings" />
|
||||
</menu>
|
Loading…
Reference in new issue