diff --git a/app/src/main/java/com/example/tubespbd/database/TransactionAdapter.kt b/app/src/main/java/com/example/tubespbd/database/TransactionAdapter.kt new file mode 100644 index 0000000000000000000000000000000000000000..fa9e2d4b14e56cd3209c604697a65f8686f091ff --- /dev/null +++ b/app/src/main/java/com/example/tubespbd/database/TransactionAdapter.kt @@ -0,0 +1,38 @@ +package com.example.tubespbd.database + +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.TextView +import androidx.recyclerview.widget.RecyclerView +import com.example.tubespbd.R +import com.example.tubespbd.database.Transaction + +class TransactionAdapter(private val transactions: List<Transaction>) : + RecyclerView.Adapter<TransactionAdapter.TransactionViewHolder>() { + + inner class TransactionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { + val titleTextView: TextView = itemView.findViewById(R.id.titleTextView) + val categoryTextView: TextView = itemView.findViewById(R.id.categoryTextView) + val amountTextView: TextView = itemView.findViewById(R.id.amountTextView) + val dateTextView: TextView = itemView.findViewById(R.id.dateTextView) + val locationTextView: TextView = itemView.findViewById(R.id.locationTextView) + } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TransactionViewHolder { + val itemView = LayoutInflater.from(parent.context) + .inflate(R.layout.transaction_item, parent, false) + return TransactionViewHolder(itemView) + } + + override fun onBindViewHolder(holder: TransactionViewHolder, position: Int) { + val currentItem = transactions[position] + holder.titleTextView.text = currentItem.title + holder.categoryTextView.text = currentItem.category + holder.amountTextView.text = currentItem.amount.toString() + holder.dateTextView.text = currentItem.tanggal + holder.locationTextView.text = currentItem.location + } + + override fun getItemCount() = transactions.size +} diff --git a/app/src/main/java/com/example/tubespbd/database/TransactionDAO.kt b/app/src/main/java/com/example/tubespbd/database/TransactionDAO.kt index 22fd7547cd4a226b5f83fb10cd233bf4856bb7c7..cdf159e3fdebc7e88e6b2a45d83cca0558407818 100644 --- a/app/src/main/java/com/example/tubespbd/database/TransactionDAO.kt +++ b/app/src/main/java/com/example/tubespbd/database/TransactionDAO.kt @@ -5,6 +5,7 @@ import androidx.room.Delete import androidx.room.Insert import androidx.room.Query import androidx.room.Update +import androidx.lifecycle.LiveData @Dao interface TransactionDao { @@ -19,4 +20,6 @@ interface TransactionDao { @Update fun updateTransaction(transaction: Transaction): Int + @Query("SELECT * FROM transactions") + fun getAllTransactionsLiveData(): LiveData<List<Transaction>> } \ No newline at end of file diff --git a/app/src/main/java/com/example/tubespbd/database/TransactionRepository.kt b/app/src/main/java/com/example/tubespbd/database/TransactionRepository.kt index 1c8f8b658099e645ab492cdf738f6a69b9e91ec0..63c664837a9bdb5f784fc5a6567e3e899c578674 100644 --- a/app/src/main/java/com/example/tubespbd/database/TransactionRepository.kt +++ b/app/src/main/java/com/example/tubespbd/database/TransactionRepository.kt @@ -1,5 +1,7 @@ package com.example.tubespbd.database +import androidx.lifecycle.LiveData + class TransactionRepository(private val transactionDao: TransactionDao) { fun getAllTransactions(): List<Transaction> { @@ -17,4 +19,12 @@ class TransactionRepository(private val transactionDao: TransactionDao) { fun updateTransaction(transaction: Transaction): Int { return transactionDao.updateTransaction(transaction) } + + fun getAllTransactionsLiveData(): LiveData<List<Transaction>> { + return transactionDao.getAllTransactionsLiveData() + } + + suspend fun refreshTransactions() { + // Perform data refresh operations if needed + } } \ No newline at end of file diff --git a/app/src/main/java/com/example/tubespbd/ui/home/HomeFragment.kt b/app/src/main/java/com/example/tubespbd/ui/home/HomeFragment.kt index 603c6515dfaa70ab90b8785a588d7d00b919cfc8..132151fb101fcfee081a23091cc7d7dbd6653c57 100644 --- a/app/src/main/java/com/example/tubespbd/ui/home/HomeFragment.kt +++ b/app/src/main/java/com/example/tubespbd/ui/home/HomeFragment.kt @@ -8,14 +8,24 @@ import android.widget.TextView import androidx.fragment.app.Fragment import androidx.lifecycle.ViewModelProvider import com.example.tubespbd.R +import com.example.tubespbd.database.Transaction +import com.example.tubespbd.database.TransactionAdapter import com.example.tubespbd.databinding.FragmentHistoryBinding import com.example.tubespbd.databinding.FragmentHomeBinding - +import java.util.Date +import android.Manifest +import android.content.Context +import android.content.pm.PackageManager +import androidx.core.content.ContextCompat +import com.example.tubespbd.TransactionManager +import android.location.LocationManager class HomeFragment : Fragment() { private var _binding: FragmentHistoryBinding? = null private val binding get() = _binding!! - + private lateinit var transactionAdapter: TransactionAdapter + private val transactions = mutableListOf<Transaction>() + private lateinit var locationManager: LocationManager override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? @@ -24,8 +34,80 @@ class HomeFragment : Fragment() { return binding.root } + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + locationManager = requireContext().getSystemService(Context.LOCATION_SERVICE) as LocationManager + transactionAdapter = TransactionAdapter(transactions) + binding.transactionRecyclerView.adapter = transactionAdapter + + binding.addTransactionButton.setOnClickListener { + addTransaction() + } + } + override fun onDestroyView() { super.onDestroyView() _binding = null } -} \ No newline at end of file + + private fun addTransaction() { + val title = binding.titleEditText.text.toString() + val category = binding.categoryEditText.text.toString() + val amountStr = binding.amountEditText.text.toString() + val amount = if (amountStr.isNotEmpty()) amountStr.toFloat() else 0f + + // Get location and date + val locationString = getLocationString() + val currentDate = Date() + + val newTransaction = Transaction( + title = title, + category = category, + amount = amount, + location = locationString, + tanggal = currentDate.toString() // Assign current datetime to tanggal + ) + + transactions.add(newTransaction) + transactionAdapter.notifyDataSetChanged() + + // Clear input fields + binding.titleEditText.text.clear() + binding.categoryEditText.text.clear() + binding.amountEditText.text.clear() + } + + private fun getLocationString(): String { + return when { + hasLocationPermissions() && isLocationEnabled() -> { + // Get location if permission is granted and location is enabled + val transactionManager = TransactionManager(requireContext(), locationManager) + transactionManager.getLocationString() + } + !hasLocationPermissions() -> { + // Location permissions not granted + "Location denied" + } + else -> { + // Location not available + "Location unavailable" + } + } + } + + + + private fun hasLocationPermissions(): Boolean { + return ContextCompat.checkSelfPermission( + requireContext(), + Manifest.permission.ACCESS_FINE_LOCATION + ) == PackageManager.PERMISSION_GRANTED + } + + private fun isLocationEnabled(): Boolean { + return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled( + LocationManager.NETWORK_PROVIDER + ) + } +} + diff --git a/app/src/main/java/com/example/tubespbd/ui/home/HomeViewModel.kt b/app/src/main/java/com/example/tubespbd/ui/home/HomeViewModel.kt index 4be8896e2e2a3aa672de7e7af7d34ff0d5da4e1e..c64f075bb804332d7bf43b0c6a299bb293b42748 100644 --- a/app/src/main/java/com/example/tubespbd/ui/home/HomeViewModel.kt +++ b/app/src/main/java/com/example/tubespbd/ui/home/HomeViewModel.kt @@ -1,13 +1,20 @@ +// HomeViewModel.kt package com.example.tubespbd.ui.home import androidx.lifecycle.LiveData -import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import com.example.tubespbd.database.Transaction +import com.example.tubespbd.database.TransactionRepository +import kotlinx.coroutines.launch -class HomeViewModel : ViewModel() { +class HomeViewModel(private val transactionRepository: TransactionRepository) : ViewModel() { - private val _text = MutableLiveData<String>().apply { - value = "This is home Fragment" + val transactions: List<Transaction> = transactionRepository.getAllTransactions() + + init { + viewModelScope.launch { + transactionRepository.refreshTransactions() + } } - val text: LiveData<String> = _text -} \ No newline at end of file +} diff --git a/app/src/main/res/layout/fragment_history.xml b/app/src/main/res/layout/fragment_history.xml index b086171c4f1291df94067f59a7033ac0fa9740ba..263d7f393fa5418e7dfa22118b1d4fb0ba64cb2b 100644 --- a/app/src/main/res/layout/fragment_history.xml +++ b/app/src/main/res/layout/fragment_history.xml @@ -1,80 +1,53 @@ <?xml version="1.0" encoding="utf-8"?> -<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" +<androidx.constraintlayout.widget.ConstraintLayout + xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> - <TextView - android:id="@+id/textView" + <EditText + android:id="@+id/titleEditText" android:layout_width="match_parent" android:layout_height="wrap_content" - android:text="Transaksi" - android:textAlignment="center" - android:textSize="48sp" + android:hint="Title" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" - app:layout_constraintEnd_toEndOf="parent" - tools:layout_editor_absoluteX="0dp" - tools:layout_editor_absoluteY="16dp" /> + app:layout_constraintEnd_toEndOf="parent" /> - <LinearLayout - android:id="@+id/linearLayout2" - android:layout_width="410dp" - android:layout_height="110dp" - android:orientation="horizontal" - app:layout_constraintTop_toBottomOf="@id/textView" + <EditText + android:id="@+id/categoryEditText" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:hint="Category" + app:layout_constraintTop_toBottomOf="@id/titleEditText" app:layout_constraintStart_toStartOf="parent" - app:layout_constraintEnd_toEndOf="parent" - tools:layout_editor_absoluteX="0dp" - tools:layout_editor_absoluteY="105dp"> - - <LinearLayout - android:layout_width="match_parent" - android:layout_height="match_parent" - android:orientation="horizontal" - tools:layout_editor_absoluteX="0dp" - tools:layout_editor_absoluteY="105dp"> - - <LinearLayout - android:layout_width="194dp" - android:layout_height="match_parent" - android:orientation="vertical"> - - <TextView - android:id="@+id/Lorem" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:text="Lorem Ipsum" - android:textSize="30sp" /> - - <com.google.android.material.chip.Chip - android:id="@+id/Pembelian" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:text="Pembelian" /> - </LinearLayout> - - <LinearLayout - android:layout_width="match_parent" - android:layout_height="match_parent" - android:orientation="vertical"> - - <TextView - android:id="@+id/Amount1" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:text="Amount" /> + app:layout_constraintEnd_toEndOf="parent" /> - <TextView - android:id="@+id/date1" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:text="dd/mm/yy" /> - - </LinearLayout> - </LinearLayout> + <EditText + android:id="@+id/amountEditText" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:hint="Amount" + app:layout_constraintTop_toBottomOf="@id/categoryEditText" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintEnd_toEndOf="parent" /> - </LinearLayout> + <Button + android:id="@+id/addTransactionButton" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="Add Transaction" + app:layout_constraintTop_toBottomOf="@id/amountEditText" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintEnd_toEndOf="parent" /> -</androidx.constraintlayout.widget.ConstraintLayout> \ No newline at end of file + <androidx.recyclerview.widget.RecyclerView + android:id="@+id/transactionRecyclerView" + android:layout_width="match_parent" + android:layout_height="0dp" + app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" + app:layout_constraintTop_toBottomOf="@id/addTransactionButton" + app:layout_constraintBottom_toBottomOf="parent" + tools:listitem="@layout/transaction_item" /> +</androidx.constraintlayout.widget.ConstraintLayout> diff --git a/app/src/main/res/layout/transaction_item.xml b/app/src/main/res/layout/transaction_item.xml new file mode 100644 index 0000000000000000000000000000000000000000..20a501a190130b2f7643fd5f0a2f7dfaa6e8dcb1 --- /dev/null +++ b/app/src/main/res/layout/transaction_item.xml @@ -0,0 +1,36 @@ +<?xml version="1.0" encoding="utf-8"?> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:orientation="vertical"> + + <TextView + android:id="@+id/titleTextView" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="Title" /> + + <TextView + android:id="@+id/categoryTextView" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="Category" /> + + <TextView + android:id="@+id/amountTextView" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="Amount" /> + + <TextView + android:id="@+id/dateTextView" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="Date" /> + + <TextView + android:id="@+id/locationTextView" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:text="Location" /> +</LinearLayout>