Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selective history delete #178 #460

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Feature implemented
  • Loading branch information
BIBLETUM committed May 31, 2024
commit e253ea81abf874730395b60be0584e856db6e891
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ class HistoryAdapter(
notifyDataSetChanged()
}

fun removeHistoryElement(position: Int){
val historyElement = history[position]
if (!historyElement.time.isNullOrEmpty()){
val nextHistoryElement = history.getOrNull(position + 1)
nextHistoryElement?.let {
if (it.time.isNullOrEmpty()){
this.history[position + 1] = History(
calculation = nextHistoryElement.calculation,
result = nextHistoryElement.result,
time = historyElement.time
)
}
}
}
this.history.removeAt(position)
notifyDataSetChanged()
}

fun removeFirstHistoryElement() {
this.history.removeAt(0)
notifyDataSetChanged()
Expand Down
39 changes: 39 additions & 0 deletions app/src/main/java/com/darkempire78/opencalculator/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.PopupMenu
import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.darkempire78.opencalculator.databinding.ActivityMainBinding
import com.sothree.slidinguppanel.PanelSlideListener
import com.sothree.slidinguppanel.PanelState
Expand Down Expand Up @@ -53,6 +55,8 @@ class MainActivity : AppCompatActivity() {

private var calculationResult = BigDecimal.ZERO

private lateinit var itemTouchHelper: ItemTouchHelper

private lateinit var binding: ActivityMainBinding
private lateinit var historyAdapter: HistoryAdapter
private lateinit var historyLayoutMgr: LinearLayoutManager
Expand Down Expand Up @@ -111,6 +115,8 @@ class MainActivity : AppCompatActivity() {
if (historyAdapter.itemCount > 0) {
binding.historyRecylcleView.scrollToPosition(historyAdapter.itemCount - 1)
}
setSwipeTouchHelperForRecyclerView()


// Disable history if setting enabled
val historySize = MyPreferences(this).historySize!!.toInt()
Expand Down Expand Up @@ -258,6 +264,39 @@ class MainActivity : AppCompatActivity() {

}

private fun setSwipeTouchHelperForRecyclerView() {
val callBack = object :
ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) {
override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
): Boolean {
return false
}
override fun isItemViewSwipeEnabled(): Boolean {
return MyPreferences(this@MainActivity).deleteHistoryOnSwipe
}

override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
val position = viewHolder.bindingAdapterPosition
historyAdapter.removeHistoryElement(position)
deleteElementFromHistory(position)
}
}

itemTouchHelper = ItemTouchHelper(callBack)
itemTouchHelper.attachToRecyclerView(binding.historyRecylcleView)
}

private fun deleteElementFromHistory(position: Int) {
lifecycleScope.launch(Dispatchers.Default) {
val history = MyPreferences(this@MainActivity).getHistory()
history.removeAt(position)
MyPreferences(this@MainActivity).saveHistory(this@MainActivity, history)
}
}

fun openAppMenu(view: View) {
val popup = PopupMenu(this, view)
val inflater = popup.menuInflater
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class MyPreferences(context: Context) {
private const val KEY_LONG_CLICK_TO_COPY_VALUE = "darkempire78.opencalculator.LONG_CLICK_TO_COPY_VALUE"
private const val KEY_ADD_MODULO_BUTTON = "darkempire78.opencalculator.ADD_MODULO_BUTTON"
private const val KEY_SPLIT_PARENTHESIS_BUTTON = "darkempire78.opencalculator.SPLIT_PARENTHESIS_BUTTON"
private const val KEY_DELETE_HISTORY_ON_SWIPE = "darkempire78.opencalculator.DELETE_HISTORY_ELEMENT_ON_SWIPE"
}

private val preferences = PreferenceManager.getDefaultSharedPreferences(context)
Expand Down Expand Up @@ -54,6 +55,8 @@ class MyPreferences(context: Context) {
set(value) = preferences.edit().putBoolean(KEY_ADD_MODULO_BUTTON, value).apply()
var splitParenthesisButton = preferences.getBoolean(KEY_SPLIT_PARENTHESIS_BUTTON, false)
set(value) = preferences.edit().putBoolean(KEY_SPLIT_PARENTHESIS_BUTTON, value).apply()
var deleteHistoryOnSwipe = preferences.getBoolean(KEY_DELETE_HISTORY_ON_SWIPE, false)
set(value) = preferences.edit().putBoolean(KEY_DELETE_HISTORY_ON_SWIPE, value).apply()



Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/drawable/swipe_left.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M19.95,5.53L17.85,5.53C17.27,5.53 16.8,5.977 16.8,6.527C16.8,7.078 17.27,7.525 17.85,7.525L19.95,7.525C20.53,7.525 21,7.078 21,6.527C21,5.977 20.53,5.53 19.95,5.53L19.95,5.53ZM4.046,8.498C3.715,8.184 3.949,7.525 4.417,7.525L9.81,7.525C10.391,7.525 10.86,7.078 10.86,6.527C10.86,5.977 10.391,5.53 9.81,5.53L4.419,5.53C3.951,5.53 3.717,5.114 4.048,4.799L5.813,3.139C6.223,2.749 6.223,2.17 5.813,1.78C5.403,1.389 4.738,1.416 4.327,1.805L0.988,4.992L0.614,5.355C-0.205,6.134 -0.205,7.402 0.614,8.181L4.328,11.716C4.738,12.106 5.402,12.117 5.813,11.728C6.222,11.338 6.222,10.626 5.813,10.236L4.046,8.498ZM20.566,14.551L20.218,18.099C20.021,19.032 19.159,19.497 18.159,19.497L13.082,19.497C12.736,19.497 12.417,19.542 12.222,19.272L9.958,15.354C9.674,14.962 9.732,14.432 10.086,14.096C10.542,13.663 11.316,13.725 11.686,14.226L12.6,15.416L12.6,6.94C12.6,6.39 13.035,5.943 13.615,5.943C14.361,5.943 14.631,6.587 14.631,6.94L14.638,11.973L19.172,12.37C20.177,12.687 20.774,13.564 20.566,14.551L20.566,14.551Z"
android:strokeWidth="1"
android:fillColor="#000000"
android:fillType="evenOdd"
android:strokeColor="#00000000"/>
</vector>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@

<string name="settings_history_size">Results saved in history</string>

<string name="settings_delete_history_element_on_swipe">Allow selective history deletion</string>
<string name="settings_delete_history_element_on_swipe_desc">Delete specific history element on swipe</string>

<string name="settings_advanced_long_click_copy">Long press to copy value</string>
<string name="settings_advanced_long_click_copy_desc">Long press copies the text of the item instead of selecting it</string>

Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/xml/root_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@
app:singleLineTitle="false"
app:icon="@drawable/history" />

<SwitchPreferenceCompat
app:key="darkempire78.opencalculator.DELETE_HISTORY_ELEMENT_ON_SWIPE"
app:title="@string/settings_delete_history_element_on_swipe"
app:summary="@string/settings_delete_history_element_on_swipe_desc"
app:useSimpleSummaryProvider="true"
app:singleLineTitle="false"
app:defaultValue="false"
app:icon="@drawable/swipe_left"
app:widgetLayout="@drawable/material_switch" />

</PreferenceCategory>

<PreferenceCategory
Expand Down