diff --git a/android/src/main/java/com/wbrawner/budget/ui/categories/AddEditCategoryActivity.kt b/android/src/main/java/com/wbrawner/budget/ui/categories/AddEditCategoryActivity.kt index 2f44724..afd066c 100644 --- a/android/src/main/java/com/wbrawner/budget/ui/categories/AddEditCategoryActivity.kt +++ b/android/src/main/java/com/wbrawner/budget/ui/categories/AddEditCategoryActivity.kt @@ -67,6 +67,9 @@ class AddEditCategoryActivity : AppCompatActivity(), CoroutineScope { menu?.findItem(R.id.action_delete)?.isVisible = true edit_category_name.setText(category.title) edit_category_amount.setText(String.format("%.02f", category.amount / 100.0f)) + expense.isChecked = category.expense + income.isChecked = !category.expense + archived.isChecked = category.archived } } @@ -103,7 +106,9 @@ class AddEditCategoryActivity : AppCompatActivity(), CoroutineScope { id = id, title = edit_category_name.text.toString(), amount = edit_category_amount.text.toLong(), - budgetId = (budgetSpinner.selectedItem as Budget).id!! + budgetId = (budgetSpinner.selectedItem as Budget).id!!, + expense = expense.isChecked, + archived = archived.isChecked )) finish() } diff --git a/android/src/main/java/com/wbrawner/budget/ui/categories/CategoryAdapter.kt b/android/src/main/java/com/wbrawner/budget/ui/categories/CategoryAdapter.kt deleted file mode 100644 index d82dd79..0000000 --- a/android/src/main/java/com/wbrawner/budget/ui/categories/CategoryAdapter.kt +++ /dev/null @@ -1,75 +0,0 @@ -package com.wbrawner.budget.ui.categories - -import android.app.Activity -import android.content.Intent -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import android.widget.ProgressBar -import android.widget.TextView -import androidx.core.content.ContextCompat.startActivity -import androidx.recyclerview.widget.RecyclerView -import com.wbrawner.budget.R -import com.wbrawner.budget.common.category.Category -import com.wbrawner.budget.ui.EXTRA_CATEGORY_ID -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.cancel -import kotlinx.coroutines.launch -import kotlin.coroutines.CoroutineContext - -class CategoryAdapter( - private val activity: Activity, - private val data: List, - private val viewModel: CategoryViewModel -) : RecyclerView.Adapter() { - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { - val view = LayoutInflater.from(parent.context) - .inflate(R.layout.list_item_category, parent, false) - return ViewHolder(view) - } - - override fun getItemCount(): Int = data.size - - override fun onBindViewHolder(holder: ViewHolder, position: Int) { - val category = data[position] - holder.title?.text = category.title - // TODO: Format according to budget's currency - holder.amount?.text = String.format("${'$'}%.02f", category.amount / 100.0f) - holder.progress?.max = category.amount.toInt() - holder.launch { - val balance = viewModel.getBalance(category.id!!) - holder.progress?.isIndeterminate = false - holder.progress?.setProgress( - (-1 * balance).toInt(), - true - ) - holder.amount?.text = holder.itemView.context.getString( - R.string.balance_remaning, - (category.amount + balance) / 100.0f - ) - } - holder.itemView.setOnClickListener { - startActivity( - activity, - Intent(it.context.applicationContext, AddEditCategoryActivity::class.java) - .apply { - putExtra(EXTRA_CATEGORY_ID, category.id!!) - }, - null - ) - } - } - - override fun onViewRecycled(holder: ViewHolder) { - holder.coroutineContext.cancel() - super.onViewRecycled(holder) - } - - inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), CoroutineScope { - override val coroutineContext: CoroutineContext = Dispatchers.Main - val title: TextView? = itemView.findViewById(R.id.category_title) - val amount: TextView? = itemView.findViewById(R.id.category_amount) - val progress: ProgressBar? = itemView.findViewById(R.id.category_progress) - } -} diff --git a/android/src/main/java/com/wbrawner/budget/ui/categories/CategoryListFragment.kt b/android/src/main/java/com/wbrawner/budget/ui/categories/CategoryListFragment.kt index e63d5d7..284b52b 100644 --- a/android/src/main/java/com/wbrawner/budget/ui/categories/CategoryListFragment.kt +++ b/android/src/main/java/com/wbrawner/budget/ui/categories/CategoryListFragment.kt @@ -73,17 +73,23 @@ class CategoryViewHolder( name.text = category.title // TODO: Format according to budget's currency amount.text = String.format("${'$'}%.02f", category.amount / 100.0f) + val tintColor = if (category.expense) R.color.colorTextRed else R.color.colorTextGreen + val colorStateList = with(itemView.context) { + android.content.res.ColorStateList.valueOf(getColor(tintColor)) + } + progressBar.progressTintList = colorStateList + progressBar.indeterminateTintList = colorStateList progressBar.max = category.amount.toInt() launch { - val balance = viewModel.getBalance(category.id!!) + val balance = viewModel.getBalance(category) progressBar.isIndeterminate = false progressBar.setProgress( - (-1 * balance).toInt(), + balance, true ) amount.text = itemView.context.getString( R.string.balance_remaning, - (category.amount + balance) / 100.0f + (category.amount - balance) / 100.0f ) } itemView.setOnClickListener { diff --git a/android/src/main/java/com/wbrawner/budget/ui/categories/CategoryViewModel.kt b/android/src/main/java/com/wbrawner/budget/ui/categories/CategoryViewModel.kt index e302f43..9d00e53 100644 --- a/android/src/main/java/com/wbrawner/budget/ui/categories/CategoryViewModel.kt +++ b/android/src/main/java/com/wbrawner/budget/ui/categories/CategoryViewModel.kt @@ -49,8 +49,10 @@ class CategoryViewModel @Inject constructor( categoryRepo.delete(id) } - suspend fun getBalance(id: Long) = showLoader { - categoryRepo.getBalance(id) + suspend fun getBalance(category: Category) = showLoader { + val balance = categoryRepo.getBalance(category.id!!) + val multiplier = if (category.expense) -1 else 1 + return@showLoader (balance * multiplier).toInt() } } diff --git a/android/src/main/res/layout/activity_add_edit_category.xml b/android/src/main/res/layout/activity_add_edit_category.xml index f0e40c0..0f5cea4 100644 --- a/android/src/main/res/layout/activity_add_edit_category.xml +++ b/android/src/main/res/layout/activity_add_edit_category.xml @@ -50,6 +50,32 @@ android:inputType="numberDecimal" /> + + + + + + + + + Cancel Month Year + Archived diff --git a/common/src/main/java/com/wbrawner/budget/common/category/Category.kt b/common/src/main/java/com/wbrawner/budget/common/category/Category.kt index 524ec26..80a567a 100644 --- a/common/src/main/java/com/wbrawner/budget/common/category/Category.kt +++ b/common/src/main/java/com/wbrawner/budget/common/category/Category.kt @@ -5,7 +5,9 @@ data class Category( val id: Long? = null, val title: String, val description: String? = null, - val amount: Long + val amount: Long, + val expense: Boolean = true, + val archived: Boolean = false ) { override fun toString(): String { return title