diff --git a/bondoman/app/src/main/java/com/example/bondoman/EditPage.kt b/bondoman/app/src/main/java/com/example/bondoman/EditPage.kt new file mode 100644 index 0000000000000000000000000000000000000000..e5940055249841596d436e925ac4751bd9341185 --- /dev/null +++ b/bondoman/app/src/main/java/com/example/bondoman/EditPage.kt @@ -0,0 +1,106 @@ +package com.example.bondoman + +import android.os.Bundle +import android.util.Log +import androidx.fragment.app.Fragment +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.Button +import android.widget.EditText +import com.example.bondoman.room.TransactionDB +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch + + +class EditPage : Fragment(R.layout.fragment_edit_page) { + private lateinit var update_button: Button + private lateinit var delete_button: Button + private lateinit var field_judul: EditText + private lateinit var field_nominal: EditText + private lateinit var field_lokasi: EditText +// private val db by lazy { TransactionDB(requireContext()) } +private lateinit var db: TransactionDB + private var idTrans = 0 + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + db = TransactionDB(requireContext()) + Log.d("TransactionAdapter", "masuk ke edit") + } + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + // Inflate the layout for this fragment + return inflater.inflate(R.layout.fragment_edit_page, container, false) + } + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + update_button = view.findViewById(R.id.update_button) + delete_button = view.findViewById(R.id.delete_button) + field_judul = view.findViewById(R.id.field_judul) as EditText + field_nominal = view.findViewById(R.id.field_nominal) as EditText + field_lokasi = view.findViewById(R.id.field_lokasi) as EditText + + // Retrieve transaction ID from arguments + idTrans = requireArguments().getInt("transactionId", 0) + Log.d("EditPage", "idTrans: $idTrans") + + // Fetch transaction details from the database + CoroutineScope(Dispatchers.IO).launch { + val transaction = db.transactionDao().findIdTrans(idTrans) + if (transaction != null) { + // Update UI elements with transaction data + field_judul.setText(transaction.field_judul) + field_nominal.setText(transaction.field_nominal) + field_lokasi.setText(transaction.field_lokasi) + } else { + // Handle case where transaction is not found + Log.d("EditPage", "Transaction not found for ID: $idTrans") + } + } + + setUpListener() + } + + + fun setUpListener(){ + update_button.setOnClickListener { + CoroutineScope(Dispatchers.IO).launch { + val transaction = db.transactionDao().findIdTrans(idTrans) + if (transaction != null) { + // Update only the modified fields + transaction.field_judul = field_judul.text.toString() + // Update nominal only if the field is not empty + if (field_nominal.text.toString().isNotEmpty()) { + transaction.field_nominal = field_nominal.text.toString() + } + transaction.field_lokasi = field_lokasi.text.toString() + + db.transactionDao().updateTransaction(transaction) + } else { + // Handle case where transaction is not found + Log.d("EditPage", "Transaction not found for id: $idTrans") + } + } + } + + + delete_button.setOnClickListener{ + CoroutineScope(Dispatchers.IO).launch { + val transaction = db.transactionDao().findIdTrans(idTrans) + if (transaction != null) { + db.transactionDao().deleteTransaction(transaction) + } else { + // Handle case where transaction is null + } + } + } + } + + + +} \ No newline at end of file diff --git a/bondoman/app/src/main/java/com/example/bondoman/TransactionAdapter.kt b/bondoman/app/src/main/java/com/example/bondoman/TransactionAdapter.kt index f00ff29ce65e69cf844286a602d0c948b34f5280..c1d931c5ab22ddb14cb5f8f3322bc0d622a0f6f0 100644 --- a/bondoman/app/src/main/java/com/example/bondoman/TransactionAdapter.kt +++ b/bondoman/app/src/main/java/com/example/bondoman/TransactionAdapter.kt @@ -7,6 +7,7 @@ import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.Button import android.widget.ImageView import android.widget.LinearLayout import android.widget.TextView @@ -14,7 +15,8 @@ import androidx.recyclerview.widget.RecyclerView import com.example.bondoman.room.Transaction class TransactionAdapter( - private val list : List<Transaction> + private val list : List<Transaction>, + private val editTransactionListener: EditTransactionListener ) : RecyclerView.Adapter<TransactionAdapter.ViewHolder>() { override fun onCreateViewHolder( parent: ViewGroup, @@ -45,6 +47,11 @@ class TransactionAdapter( holder.itemView.context.startActivity(intent) Log.d("TransactionAdapter", "location: ${transaction.field_lokasi}") } + + holder.editButton.setOnClickListener { + editTransactionListener.onEditTransaction(transaction.id) + + } } override fun getItemCount(): Int { @@ -59,6 +66,7 @@ class TransactionAdapter( val date = view.findViewById<TextView>(R.id.date_transaction) val location = view.findViewById<TextView>(R.id.location_transaction) val button = view.findViewById<LinearLayout>(R.id.linear_layout_transaction) + val editButton = view.findViewById<Button>(R.id.edit_button) } fun Int.convert(): String { @@ -66,5 +74,9 @@ class TransactionAdapter( val regex = "(\\d)(?=(\\d{3})+\$)".toRegex() return str.replace(regex, "\$1.") } + interface EditTransactionListener { + fun onEditTransaction(transactionId: Int) + } + } \ No newline at end of file diff --git a/bondoman/app/src/main/java/com/example/bondoman/TransactionPage.kt b/bondoman/app/src/main/java/com/example/bondoman/TransactionPage.kt index 7554fb4cc1586c0c7a23a77347876d49d0c9a21d..aed645b8d9ca717f91a2fa06835940f6877dc146 100644 --- a/bondoman/app/src/main/java/com/example/bondoman/TransactionPage.kt +++ b/bondoman/app/src/main/java/com/example/bondoman/TransactionPage.kt @@ -19,7 +19,7 @@ import kotlinx.coroutines.launch import kotlinx.coroutines.withContext -class TransactionPage : Fragment(R.layout.fragment_transaction_page) { +class TransactionPage : Fragment(R.layout.fragment_transaction_page), TransactionAdapter.EditTransactionListener { val db by lazy { TransactionDB(requireContext()) } override fun onCreateView( @@ -64,7 +64,7 @@ class TransactionPage : Fragment(R.layout.fragment_transaction_page) { Log.d("TransactionPage", "transactions: $transactions") - recyclerView.adapter = TransactionAdapter(transactions) + recyclerView.adapter = TransactionAdapter(transactions, this@TransactionPage) Log.d("TransactionPage", "recyclerView: ${recyclerView.adapter}") } @@ -93,4 +93,22 @@ class TransactionPage : Fragment(R.layout.fragment_transaction_page) { val regex = "(\\d)(?=(\\d{3})+\$)".toRegex() return str.replace(regex, "\$1.") } + + override fun onEditTransaction(transactionId: Int) { + // Handle the edit transaction action here + // For example, navigate to the EditPage fragment passing the transactionId + val fragment = EditPage() + + // Pass transactionId to EditPage fragment + val bundle = Bundle().apply { + putInt("transactionId", transactionId) + } + fragment.arguments = bundle + + // Perform fragment transaction + requireActivity().supportFragmentManager.beginTransaction() + .replace(R.id.frame_layout, fragment) + .addToBackStack(null) + .commit() + } } \ No newline at end of file diff --git a/bondoman/app/src/main/java/com/example/bondoman/room/Transaction.kt b/bondoman/app/src/main/java/com/example/bondoman/room/Transaction.kt index 65410999e837d985586e3de05a0715b31aa8933a..98632540804fb2f9d02ccf452d3f01b349d83c3b 100644 --- a/bondoman/app/src/main/java/com/example/bondoman/room/Transaction.kt +++ b/bondoman/app/src/main/java/com/example/bondoman/room/Transaction.kt @@ -13,13 +13,13 @@ data class Transaction( @NonNull val email: String, @NonNull - val field_judul: String, + var field_judul: String, @NonNull - val field_nominal: String, + var field_nominal: String, @NonNull val field_kategori: String, @NonNull - val field_lokasi: String, + var field_lokasi: String, @NonNull val createdAt: String ) \ No newline at end of file diff --git a/bondoman/app/src/main/java/com/example/bondoman/room/TransactionDao.kt b/bondoman/app/src/main/java/com/example/bondoman/room/TransactionDao.kt index b0290db4b203975a47c9ea53f171a45b3eed2563..64eed51aaef95d66cf207409f3659e6c0e8439e7 100644 --- a/bondoman/app/src/main/java/com/example/bondoman/room/TransactionDao.kt +++ b/bondoman/app/src/main/java/com/example/bondoman/room/TransactionDao.kt @@ -11,14 +11,16 @@ import androidx.room.* interface TransactionDao { @Insert suspend fun addTransaction(transaction: Transaction) + @Query ("SELECT * FROM transactions WHERE id=:idTrans") + suspend fun findIdTrans(idTrans: Int):Transaction? @Update suspend fun updateTransaction(transaction: Transaction) - @Delete - suspend fun deleteTransaction(transaction: Transaction) - @Query("SELECT * FROM transactions ORDER BY createdAt DESC") fun getAllTransactions(): List<Transaction> + @Delete + suspend fun deleteTransaction(transaction: Transaction) + } \ No newline at end of file diff --git a/bondoman/app/src/main/res/drawable/baseline_edit_24.xml b/bondoman/app/src/main/res/drawable/baseline_edit_24.xml new file mode 100644 index 0000000000000000000000000000000000000000..1c9bd3e6bda2e7ee0e8fb5151b4b09c4ae0daac4 --- /dev/null +++ b/bondoman/app/src/main/res/drawable/baseline_edit_24.xml @@ -0,0 +1,5 @@ +<vector android:height="24dp" android:tint="#000000" + android:viewportHeight="24" android:viewportWidth="24" + android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> + <path android:fillColor="@android:color/white" android:pathData="M3,17.25V21h3.75L17.81,9.94l-3.75,-3.75L3,17.25zM20.71,7.04c0.39,-0.39 0.39,-1.02 0,-1.41l-2.34,-2.34c-0.39,-0.39 -1.02,-0.39 -1.41,0l-1.83,1.83 3.75,3.75 1.83,-1.83z"/> +</vector> diff --git a/bondoman/app/src/main/res/layout/fragment_add_transaction.xml b/bondoman/app/src/main/res/layout/fragment_add_transaction.xml index 7935b47c8aa713840d7f5125553c3b2e20e81d72..ba88df032dc7eb0ad5e101e34f5a982e366c8201 100644 --- a/bondoman/app/src/main/res/layout/fragment_add_transaction.xml +++ b/bondoman/app/src/main/res/layout/fragment_add_transaction.xml @@ -100,7 +100,7 @@ <RadioButton android:id="@+id/button_pengeluaran" android:layout_width="wrap_content" - android:layout_height="wrap_content" + android:layout_height="50sp" android:layout_marginHorizontal="2sp" android:checked="true" android:fontFamily="@font/font_poppins" @@ -112,7 +112,7 @@ <RadioButton android:id="@+id/button_pemasukan" android:layout_width="wrap_content" - android:layout_height="wrap_content" + android:layout_height="50sp" android:layout_marginHorizontal="2sp" android:checked="false" android:fontFamily="@font/font_poppins" @@ -158,7 +158,7 @@ android:id="@+id/add_button" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="Save" + android:text="Submit" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.501" diff --git a/bondoman/app/src/main/res/layout/fragment_edit_page.xml b/bondoman/app/src/main/res/layout/fragment_edit_page.xml new file mode 100644 index 0000000000000000000000000000000000000000..1b49c61fb2af2abed36a59e3743e5716a69fce95 --- /dev/null +++ b/bondoman/app/src/main/res/layout/fragment_edit_page.xml @@ -0,0 +1,186 @@ +<?xml version="1.0" encoding="utf-8"?> +<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent" + xmlns:app="http://schemas.android.com/apk/res-auto" + android:background="@color/primary" + tools:context=".EditPage"> + <androidx.constraintlayout.widget.ConstraintLayout + android:id="@+id/constraintLayout" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent"> + + <TextView + android:id="@+id/label_judul" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:fontFamily="@font/font_poppins" + android:text="@string/label_judul" + android:textColor="@color/secondary" + android:textSize="16sp" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintHorizontal_bias="0.054" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent" /> + + <EditText + android:id="@+id/field_judul" + android:layout_width="350sp" + android:layout_height="wrap_content" + android:background="@drawable/custom_field" + android:fontFamily="@font/font_poppins" + android:hint="Masukkan judul transaksi Anda" + android:inputType="text" + android:padding="12sp" + android:textSize="14sp" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@id/label_judul" /> + + <TextView + android:id="@+id/label_nominal" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginTop="4sp" + android:fontFamily="@font/font_poppins" + android:text="@string/label_nominal" + android:textColor="@color/secondary" + android:textSize="16sp" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintHorizontal_bias="0.051" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@id/field_judul" /> + + <EditText + android:id="@+id/field_nominal" + android:layout_width="350sp" + android:layout_height="wrap_content" + android:background="@drawable/custom_field" + android:fontFamily="@font/font_poppins" + android:hint="Masukkan judul nominal transaksi Anda" + android:inputType="text" + android:padding="12sp" + android:textSize="14sp" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@id/label_nominal" /> + + <TextView + android:id="@+id/label_kategori" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginTop="4sp" + android:fontFamily="@font/font_poppins" + android:text="@string/label_kategori" + android:textColor="@color/secondary" + android:textSize="16sp" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintHorizontal_bias="0.05" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@id/field_nominal" /> + + <RadioGroup + android:id="@+id/field_kategori" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:orientation="horizontal" + app:layout_constraintBottom_toTopOf="@id/label_lokasi" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + android:layout_marginLeft="8sp" + app:layout_constraintTop_toBottomOf="@+id/label_kategori" + android:editable="false" + tools:ignore="Deprecated"> + + <RadioButton + android:id="@+id/button_pengeluaran" + android:layout_width="wrap_content" + android:layout_height="50sp" + android:layout_marginHorizontal="2sp" + android:checked="true" + android:fontFamily="@font/font_poppins" + android:text="@string/kategori_pengeluaran" + android:textColor="@color/secondary" + + /> + + <RadioButton + android:id="@+id/button_pemasukan" + android:layout_width="wrap_content" + android:layout_height="50sp" + android:layout_marginHorizontal="2sp" + android:checked="false" + android:fontFamily="@font/font_poppins" + android:text="@string/kategori_pemasukan" + android:textColor="@color/secondary" + + /> + </RadioGroup> + + <TextView + android:id="@+id/label_lokasi" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginTop="4sp" + android:fontFamily="@font/font_poppins" + android:text="@string/label_lokasi" + android:textColor="@color/secondary" + android:textSize="16sp" + app:layout_constraintEnd_toEndOf="parent" + + app:layout_constraintHorizontal_bias="0.051" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@id/field_kategori" /> + + <EditText + android:id="@+id/field_lokasi" + android:layout_width="350sp" + android:layout_height="wrap_content" + android:background="@drawable/custom_field" + android:fontFamily="@font/font_poppins" + android:hint="Masukkan kategori Anda" + android:inputType="text" + android:padding="12sp" + android:textSize="14sp" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@id/label_lokasi" /> + + </androidx.constraintlayout.widget.ConstraintLayout> + + <Button + android:id="@+id/update_button" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginEnd="168dp" + android:background="@drawable/custom_button" + android:text="Save" + android:textColor="@color/white" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintLeft_toRightOf="@+id/delete_button" + app:layout_constraintTop_toBottomOf="@+id/constraintLayout" + app:layout_constraintVertical_bias="0.49" /> + + <Button + android:id="@+id/delete_button" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:background="@drawable/logout_button" + android:text="Delete" + android:textColor="@color/white" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintEnd_toStartOf="@+id/update_button" + app:layout_constraintHorizontal_bias="0.561" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@+id/constraintLayout" + app:layout_constraintVertical_bias="0.49" /> + + +</androidx.constraintlayout.widget.ConstraintLayout> \ No newline at end of file diff --git a/bondoman/app/src/main/res/layout/item_transaction.xml b/bondoman/app/src/main/res/layout/item_transaction.xml index 5796113bc05173d2e72cf6d9bab59fbd3473ce9a..4cc29fa72e4a4e8b78dfb8830e25f937fce33fc2 100644 --- a/bondoman/app/src/main/res/layout/item_transaction.xml +++ b/bondoman/app/src/main/res/layout/item_transaction.xml @@ -18,6 +18,7 @@ android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical" + android:layout_gravity="center" > <ImageView @@ -53,6 +54,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Kerupuk Kulit" + android:fontFamily="@font/font_poppins" android:textColor="@color/black" android:textSize="20sp" android:textStyle="normal" @@ -62,6 +64,7 @@ android:id="@+id/price_transaction" android:layout_width="match_parent" android:layout_height="wrap_content" + android:fontFamily="@font/font_poppins" android:text="IDR5.000" android:textColor="@color/black" android:textSize="20sp" @@ -83,6 +86,7 @@ android:id="@+id/date_transaction" android:layout_width="match_parent" android:layout_height="wrap_content" + android:fontFamily="@font/font_poppins" android:layout_gravity="end" android:gravity="end" android:text="14/03/2024" @@ -91,6 +95,20 @@ android:textStyle="normal" app:layout_constraintStart_toEndOf="@id/description_transaction" /> + <Button + android:id="@+id/edit_button" + android:layout_width="wrap_content" + android:layout_height="30sp" + android:layout_gravity="end" + android:gravity="end" + android:textSize="20sp" + android:fontFamily="@font/font_poppins" + android:background="@android:color/transparent" + android:drawableLeft="@drawable/baseline_edit_24" + android:paddingLeft="8dp" + android:textAllCaps="false" + android:text="Edit" /> + <LinearLayout android:id="@+id/linear_layout_transaction" android:layout_width="match_parent" android:layout_height="48dp" @@ -111,6 +129,7 @@ android:id="@+id/location_transaction" android:layout_width="wrap_content" android:layout_height="wrap_content" + android:fontFamily="@font/font_poppins" android:text="Seno Medika" android:textColor="@color/black" android:textSize="14sp"