Skip to content

Commit

Permalink
implement mtotschnig#640
Browse files Browse the repository at this point in the history
  • Loading branch information
tillgraeger committed Apr 5, 2021
1 parent ee4801a commit e3d2677
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import android.text.TextUtils;
import android.util.SparseBooleanArray;
import android.util.SparseIntArray;
import android.view.ActionMode;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
Expand All @@ -52,6 +53,7 @@
import com.google.android.material.snackbar.Snackbar;

import org.apache.commons.lang3.ArrayUtils;
import org.jetbrains.annotations.NotNull;
import org.threeten.bp.LocalDateTime;
import org.threeten.bp.temporal.ChronoUnit;
import org.totschnig.myexpenses.MyApplication;
Expand Down Expand Up @@ -273,7 +275,7 @@ protected int getMenuResource() {
columnIndexPayee, columnIndexCrStatus, columnIndexYearOfMonthStart,
columnIndexLabelMain, columnIndexAccountId, columnIndexAmount, columnIndexCurrency;
private boolean indexesCalculated = false;
private Account mAccount;
protected Account mAccount;
private Money budget = null;
protected TransactionListViewModel viewModel;
@Nullable
Expand Down Expand Up @@ -350,6 +352,13 @@ public void onCreate(Bundle savedInstanceState) {
true, budgetsObserver);
}
}
/*
@Override
public void setTitle(@NotNull ActionMode mode, @NotNull AbsListView lv, int position, boolean checked) {
Object test = binding.list.getItemAtPosition(position).getClass();
super.setTitle(mode, lv, position, checked);
}
*/

@Override
public void onDestroy() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import android.widget.ListView
import androidx.fragment.app.Fragment
import org.totschnig.myexpenses.R
import org.totschnig.myexpenses.activity.ProtectedFragmentActivity
import org.totschnig.myexpenses.model.Template

/**
* @author Michael Totschnig
Expand All @@ -31,7 +32,7 @@ abstract class ContextualActionBarFragment : Fragment(), OnGroupClickListener, O
@JvmField
var expandableListSelectionType = ExpandableListView.PACKED_POSITION_TYPE_NULL
private val menuSingleIds = intArrayOf(R.id.EDIT_COMMAND,
R.id.CREATE_PLAN_INSTANCE_EDIT_COMMAND,
R.id.CREATE_PLAN_INSTANCE_EDIT_COMMAND, R.id.CREATE_PLAN_INSTANCE_SAVE_COMMAND,
R.id.SELECT_COMMAND, R.id.VIEW_COMMAND,R.id.CREATE_INSTANCE_EDIT_COMMAND,
R.id.CREATE_TEMPLATE_COMMAND, R.id.CLONE_TRANSACTION_COMMAND)
private val menuSingleGroupIds = intArrayOf(R.id.CREATE_SUB_COMMAND, R.id.COLOR_COMMAND)
Expand Down Expand Up @@ -77,6 +78,11 @@ abstract class ContextualActionBarFragment : Fragment(), OnGroupClickListener, O

protected open fun shouldStartActionMode() = true

open fun setTitle(mode : ActionMode ,lv: AbsListView, position: Int, checked: Boolean) {
val count = lv.checkedItemCount
mode.title = count.toString()
}

fun registerForContextualActionBar(lv: AbsListView) {
lv.choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL
lv.setMultiChoiceModeListener(object : MultiChoiceModeListener {
Expand All @@ -87,7 +93,8 @@ abstract class ContextualActionBarFragment : Fragment(), OnGroupClickListener, O
expandableListSelectionType = ExpandableListView.getPackedPositionType(
lv.getExpandableListPosition(position))
}
mode.title = count.toString()
setTitle(mode, lv, position, checked)
//val test = lv.getItemAtPosition(position)
configureMenu(mode.menu, lv)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ package org.totschnig.myexpenses.fragment
import android.app.Activity
import android.app.Dialog
import android.content.Intent
import android.database.Cursor
import android.graphics.Color
import android.os.Bundle
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.view.ActionMode
import android.view.Menu
import android.widget.AbsListView
import androidx.fragment.app.DialogFragment
Expand All @@ -15,11 +20,16 @@ import org.totschnig.myexpenses.activity.CONFIRM_MAP_TAG_REQUEST
import org.totschnig.myexpenses.activity.MAP_TAG_REQUEST
import org.totschnig.myexpenses.activity.MyExpenses
import org.totschnig.myexpenses.dialog.TransactionDetailFragment
import org.totschnig.myexpenses.provider.DatabaseConstants.KEY_AMOUNT
import org.totschnig.myexpenses.util.CurrencyFormatter
import org.totschnig.myexpenses.viewmodel.data.Tag

const val KEY_REPLACE = "replace"

class TransactionList : BaseTransactionList() {

private var transactionsum : Long = 0

private fun handleTagResult(intent: Intent) {
ConfirmTagDialogFragment().also {
it.arguments = Bundle().apply {
Expand Down Expand Up @@ -48,6 +58,33 @@ class TransactionList : BaseTransactionList() {
}
}

override fun setTitle(mode: ActionMode, lv: AbsListView, position: Int, checked: Boolean) {
mTransactionsCursor.moveToPosition(position)
val cost = mTransactionsCursor.getLong(mTransactionsCursor.getColumnIndex(KEY_AMOUNT))
getSum(cost, checked)
val count = lv.checkedItemCount
mode.title = setColor(count.toString() + " " + currencyFormatter.convAmount(transactionsum, mAccount.currencyUnit), count+1)
}

private fun setColor (text : String , count : Int) : SpannableString {
val spanText = SpannableString(text)
if(transactionsum <= 0) {
spanText.setSpan(ForegroundColorSpan(Color.RED), count.toString().count(), spanText.length,0)
} else {
spanText.setSpan(ForegroundColorSpan(Color.GREEN), count.toString().count(),spanText.length,0)
}
return spanText
}

private fun getSum(cost: Long, checked: Boolean) {
if(checked) {
transactionsum += cost
} else {
transactionsum -= cost
}
}


override fun configureMenu(menu: Menu, lv: AbsListView) {
super.configureMenu(menu, lv)
val checkedItemPositions = lv.checkedItemPositions
Expand Down

0 comments on commit e3d2677

Please sign in to comment.