2023-05-08 11:27:43 +02:00
|
|
|
package com.denizd.textbasic.fragment
|
|
|
|
|
|
|
|
|
|
import android.os.Bundle
|
|
|
|
|
import android.view.View
|
2023-05-15 14:16:48 +02:00
|
|
|
import androidx.recyclerview.widget.ItemTouchHelper
|
2023-05-08 11:27:43 +02:00
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
|
|
|
import com.denizd.textbasic.R
|
2023-05-28 11:39:29 +02:00
|
|
|
import com.denizd.textbasic.adapter.QuoteAdapter
|
2023-05-15 14:16:48 +02:00
|
|
|
import com.denizd.textbasic.adapter.RecyclerRowMoveCallback
|
2023-05-08 11:27:43 +02:00
|
|
|
import com.denizd.textbasic.databinding.FragmentQuoteBinding
|
2023-05-28 11:39:29 +02:00
|
|
|
import com.denizd.textbasic.db.QuoteStorage
|
2023-05-15 15:18:47 +02:00
|
|
|
import com.denizd.textbasic.util.showSnackBar
|
2023-05-28 11:39:29 +02:00
|
|
|
import com.denizd.textbasic.util.viewBinding
|
|
|
|
|
|
2023-05-15 15:18:47 +02:00
|
|
|
class QuoteFragment : BaseFragment(R.layout.fragment_quote), QuoteAdapter.OnDeleteListener {
|
2023-05-08 11:27:43 +02:00
|
|
|
|
|
|
|
|
private lateinit var storage: QuoteStorage
|
|
|
|
|
private lateinit var quoteAdapter: QuoteAdapter
|
|
|
|
|
|
|
|
|
|
private val binding: FragmentQuoteBinding by viewBinding(FragmentQuoteBinding::bind)
|
|
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
|
|
2023-05-15 22:10:58 +02:00
|
|
|
storage = QuoteStorage.getInstance(context)
|
2023-05-30 09:36:57 +02:00
|
|
|
|
|
|
|
|
if (storage.getAllQuotes().isEmpty()) {
|
|
|
|
|
storage.noMigrationNecessary()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (storage.needsMigration()) {
|
|
|
|
|
storage.migrateEntries()
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-15 15:18:47 +02:00
|
|
|
quoteAdapter = QuoteAdapter(storage.getAllQuotes(), this)
|
2023-05-08 11:27:43 +02:00
|
|
|
|
2023-05-15 15:18:47 +02:00
|
|
|
binding.recyclerView.layoutManager = LinearLayoutManager(context)
|
|
|
|
|
ItemTouchHelper(RecyclerRowMoveCallback(quoteAdapter))
|
|
|
|
|
.attachToRecyclerView(binding.recyclerView)
|
|
|
|
|
binding.recyclerView.adapter = quoteAdapter
|
|
|
|
|
|
|
|
|
|
// Set up scroll change listener to shrink and extend FAB accordingly
|
|
|
|
|
binding.recyclerView.setOnScrollChangeListener { _, _, scrollY, _, oldScrollY ->
|
|
|
|
|
// Calculate the vertical scroll difference
|
|
|
|
|
val dy = scrollY - oldScrollY
|
|
|
|
|
if (dy > 0) {
|
|
|
|
|
// If scrolling down, shrink the FAB
|
|
|
|
|
binding.fabAddQuote.shrink()
|
|
|
|
|
} else if (dy < 0) {
|
|
|
|
|
// If scrolling up, extend the FAB
|
|
|
|
|
binding.fabAddQuote.extend()
|
|
|
|
|
}
|
2023-05-08 11:27:43 +02:00
|
|
|
}
|
|
|
|
|
|
2023-05-19 11:15:04 +02:00
|
|
|
binding.buttonSave.setOnClickListener {
|
|
|
|
|
save()
|
|
|
|
|
context.theme.showSnackBar(
|
|
|
|
|
binding.coordinatorLayout,
|
|
|
|
|
getString(R.string.quote_fragment_force_save),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-15 15:18:47 +02:00
|
|
|
binding.fabAddQuote.setOnClickListener {
|
2023-05-08 11:27:43 +02:00
|
|
|
quoteAdapter.addNewQuote()
|
2023-05-28 11:39:29 +02:00
|
|
|
binding.recyclerView.smoothScrollToPosition(quoteAdapter.itemCount - 1)
|
2023-05-08 11:27:43 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun save() {
|
|
|
|
|
storage.saveQuotes(quoteAdapter.getAllQuotes())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onPause() {
|
|
|
|
|
save()
|
|
|
|
|
super.onPause()
|
|
|
|
|
}
|
2023-05-15 15:18:47 +02:00
|
|
|
|
|
|
|
|
override fun onDelete(entry: String, index: Int) {
|
|
|
|
|
context.theme.showSnackBar(
|
2023-05-28 11:39:29 +02:00
|
|
|
view = binding.coordinatorLayout,
|
|
|
|
|
text = getString(R.string.quote_fragment_snack_text),
|
|
|
|
|
actionText = getString(R.string.quote_fragment_snack_undo),
|
2023-05-15 15:18:47 +02:00
|
|
|
) {
|
|
|
|
|
quoteAdapter.addEntryAt(index, entry)
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-08 11:27:43 +02:00
|
|
|
}
|