diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 594316c9178f220b8fd1e5c9e71e1887c53c7fdc..cacc6065b2db3a227fd11d87266c8bcd99d814d8 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -12,6 +12,9 @@ android:supportsRtl="true" android:theme="@style/Theme.AndroidHIT" tools:targetApi="31"> + <activity + android:name=".DetailTransactionActivity" + android:exported="false" /> <activity android:name=".MainActivity" android:exported="true"> @@ -21,15 +24,6 @@ <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> - <activity - android:name=".AddTransactionActivity" - android:exported="true" > - <intent-filter> - <action android:name="android.intent.action.VIEW" /> - <category android:name="android.intent.category.DEFAULT" /> - </intent-filter> - </activity> - </application> </manifest> \ No newline at end of file diff --git a/app/src/main/java/com/example/android_hit/AddTransactionActivity.kt b/app/src/main/java/com/example/android_hit/DetailTransaction.kt similarity index 69% rename from app/src/main/java/com/example/android_hit/AddTransactionActivity.kt rename to app/src/main/java/com/example/android_hit/DetailTransaction.kt index a3f10574d78ef9194b0083435c7e2e8ccfa70813..72f6558798dd2fa67947372784586a632ed06b9c 100644 --- a/app/src/main/java/com/example/android_hit/AddTransactionActivity.kt +++ b/app/src/main/java/com/example/android_hit/DetailTransaction.kt @@ -2,35 +2,37 @@ package com.example.android_hit import android.app.Activity import android.content.Intent -import androidx.appcompat.app.AppCompatActivity import android.os.Bundle -import android.widget.Button -import android.widget.EditText -import android.widget.RadioButton +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup import android.widget.Toast import androidx.fragment.app.Fragment -import com.example.android_hit.adapter.TransactionAdapter -import com.example.android_hit.databinding.ActivityAddTransactionBinding +import com.example.android_hit.databinding.FragmentDetailTransactionBinding import com.example.android_hit.room.TransactionDB import com.example.android_hit.room.TransactionEntity import java.text.SimpleDateFormat import java.util.Date import java.util.Locale -class AddTransactionActivity : AppCompatActivity() { - private lateinit var binding: ActivityAddTransactionBinding - private lateinit var database: TransactionDB - private lateinit var category: String - +class DetailTransaction : Fragment() { + private var _binding: FragmentDetailTransactionBinding? = null + private val binding get() = _binding!! - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - binding = ActivityAddTransactionBinding.inflate(layoutInflater) - setContentView(binding.root) - -// replaceFragment(HeaderDetailTransaction()) + private lateinit var database: TransactionDB + private var category: String = "" + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + _binding = FragmentDetailTransactionBinding.inflate(inflater, container, false) + return binding.root + } - database = TransactionDB.getInstance(applicationContext) + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + database = TransactionDB.getInstance(requireContext()) binding.radioExpense.setOnCheckedChangeListener { _, isChecked -> if (isChecked) { @@ -44,10 +46,13 @@ class AddTransactionActivity : AppCompatActivity() { } } - var intent = intent.extras + val intent = requireActivity().intent.extras if (intent != null) { val id = intent.getInt("id", 0) - var transaction = database.transactionDao.getId(id) + val transaction = database.transactionDao.getId(id) + + binding.radioExpense.isEnabled = false + binding.radioIncome.isEnabled = false binding.inputTitle.setText(transaction.title) binding.inputAmount.setText(transaction.amount.toString()) @@ -74,6 +79,7 @@ class AddTransactionActivity : AppCompatActivity() { val transaction = database.transactionDao.getId(id) timestamp = transaction?.timestamp } + if (intent != null) { database.transactionDao.updateTransaction( TransactionEntity( @@ -85,7 +91,7 @@ class AddTransactionActivity : AppCompatActivity() { timestamp.toString() ) ) - setResult(Activity.RESULT_OK) + requireActivity().setResult(Activity.RESULT_OK) } else { val amountValue = amount.toInt() @@ -104,36 +110,42 @@ class AddTransactionActivity : AppCompatActivity() { ) ) } - finish() + + Toast.makeText( + requireContext(), + "Transaction saved", + Toast.LENGTH_SHORT + ).show() + + val intent = Intent(requireContext(), MainActivity::class.java) + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK) + startActivity(intent) + } catch (e: NumberFormatException) { - // Handle kesalahan jika nilai amount tidak valid Toast.makeText( - applicationContext, + requireContext(), "Invalid amount entered", Toast.LENGTH_SHORT ).show() } catch (ex: Exception) { - // Handle kesalahan lainnya Toast.makeText( - applicationContext, + requireContext(), "Error occurred: ${ex.message}", Toast.LENGTH_SHORT ).show() } } else { - // Menampilkan pesan jika ada field yang kosong Toast.makeText( - applicationContext, + requireContext(), "Please fill all the fields", Toast.LENGTH_SHORT ).show() } } } - private fun replaceFragment(header: Fragment) { - val fragmentManager = supportFragmentManager - val fragmentTransaction = fragmentManager.beginTransaction() - fragmentTransaction.replace(R.id.header_add_transaction, header) - fragmentTransaction.commit() + + override fun onDestroyView() { + super.onDestroyView() + _binding = null } } diff --git a/app/src/main/java/com/example/android_hit/DetailTransactionActivity.kt b/app/src/main/java/com/example/android_hit/DetailTransactionActivity.kt new file mode 100644 index 0000000000000000000000000000000000000000..69c9b01de29d0ca91db0bdfa12f5abf966b75b3e --- /dev/null +++ b/app/src/main/java/com/example/android_hit/DetailTransactionActivity.kt @@ -0,0 +1,25 @@ +package com.example.android_hit + +import androidx.appcompat.app.AppCompatActivity +import android.os.Bundle +import androidx.fragment.app.Fragment +import com.example.android_hit.databinding.ActivityDetailTransactionBinding + +class DetailTransactionActivity : AppCompatActivity() { + private lateinit var binding: ActivityDetailTransactionBinding + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + binding = ActivityDetailTransactionBinding.inflate(layoutInflater) + setContentView(binding.root) + + setCurrentFragment(DetailTransaction(), HeaderDetailTransaction()) + } + + private fun setCurrentFragment(fragment: Fragment, header: Fragment) = + supportFragmentManager.beginTransaction().apply { + replace(R.id.frame_layout, fragment) + replace(R.id.header_layout, header) + commit() + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/android_hit/HeaderDetailTransaction.kt b/app/src/main/java/com/example/android_hit/HeaderDetailTransaction.kt index 7cc71fb3450083d51e1ef08e36222b83ff5efa95..3c645149b8ee7d10c1b73170c82bb90be3f8228b 100644 --- a/app/src/main/java/com/example/android_hit/HeaderDetailTransaction.kt +++ b/app/src/main/java/com/example/android_hit/HeaderDetailTransaction.kt @@ -1,10 +1,12 @@ package com.example.android_hit +import android.content.Intent import android.os.Bundle import androidx.fragment.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import com.example.android_hit.databinding.FragmentHeaderDetailTransactionBinding // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER @@ -17,26 +19,26 @@ private const val ARG_PARAM2 = "param2" * create an instance of this fragment. */ class HeaderDetailTransaction : Fragment() { - // TODO: Rename and change types of parameters - private var param1: String? = null - private var param2: String? = null + private var _binding: FragmentHeaderDetailTransactionBinding? = null + private val binding get() = _binding!! - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - arguments?.let { - param1 = it.getString(ARG_PARAM1) - param2 = it.getString(ARG_PARAM2) - } - } - - override fun onCreateView( - inflater: LayoutInflater, container: ViewGroup?, - savedInstanceState: Bundle? - ): View? { + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle?): View? { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_header_detail_transaction, container, false) } + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + _binding = FragmentHeaderDetailTransactionBinding.bind(view) + + binding.buttonBack.setOnClickListener { + val intent = Intent(requireContext(), MainActivity::class.java) + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK) + startActivity(intent) + } + } + companion object { /** * Use this factory method to create a new instance of @@ -47,13 +49,12 @@ class HeaderDetailTransaction : Fragment() { * @return A new instance of fragment HeaderDetailTransaction. */ // TODO: Rename and change types and number of parameters - @JvmStatic - fun newInstance(param1: String, param2: String) = - HeaderDetailTransaction().apply { - arguments = Bundle().apply { - putString(ARG_PARAM1, param1) - putString(ARG_PARAM2, param2) + @JvmStatic fun newInstance(param1: String, param2: String) = + HeaderDetailTransaction().apply { + arguments = Bundle().apply { + putString(ARG_PARAM1, param1) + putString(ARG_PARAM2, param2) + } } - } } } \ No newline at end of file diff --git a/app/src/main/java/com/example/android_hit/Transaction.kt b/app/src/main/java/com/example/android_hit/Transaction.kt index 7f55703e003b60ddf111364e590a7131ca925d30..c93df56bdb8e86df80b658921acd798af28aa84f 100644 --- a/app/src/main/java/com/example/android_hit/Transaction.kt +++ b/app/src/main/java/com/example/android_hit/Transaction.kt @@ -10,7 +10,6 @@ import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.example.android_hit.adapter.TransactionAdapter -import com.example.android_hit.databinding.ActivityMainBinding import com.example.android_hit.databinding.FragmentTransactionBinding import com.example.android_hit.room.TransactionDB import com.example.android_hit.room.TransactionEntity @@ -59,7 +58,7 @@ class Transaction : Fragment() { adapter = TransactionAdapter(list) recyclerView.adapter = adapter fab.setOnClickListener { - startActivity(Intent(requireContext(), AddTransactionActivity::class.java)) + startActivity(Intent(requireContext(), DetailTransactionActivity::class.java)) } adapter.setOnDeleteClickListener(object : TransactionAdapter.OnDeleteClickListener { diff --git a/app/src/main/java/com/example/android_hit/adapter/TransactionAdapter.kt b/app/src/main/java/com/example/android_hit/adapter/TransactionAdapter.kt index 16f3343aaddd4c72e4377ddd6ce705293a2533b5..e6bb02d00c71d2a87fac4fbdf408c4eb3a66349f 100644 --- a/app/src/main/java/com/example/android_hit/adapter/TransactionAdapter.kt +++ b/app/src/main/java/com/example/android_hit/adapter/TransactionAdapter.kt @@ -3,14 +3,10 @@ package com.example.android_hit.adapter import android.content.Intent import android.net.Uri import android.view.LayoutInflater -import android.view.View import android.view.ViewGroup -import android.widget.ImageButton -import android.widget.TextView -import android.widget.Toast +import androidx.core.content.ContextCompat.startActivity import androidx.recyclerview.widget.RecyclerView -import com.example.android_hit.AddTransactionActivity -import com.example.android_hit.R +import com.example.android_hit.DetailTransactionActivity import com.example.android_hit.databinding.RowTransactionBinding import com.example.android_hit.room.TransactionEntity @@ -23,7 +19,7 @@ class TransactionAdapter(private val list: MutableList<TransactionEntity>) : Rec amount.text = transaction.amount.toString() category.text = transaction.category location.text = transaction.location - date.text = transaction.timestamp.toString() + date.text = transaction.timestamp deleteButton.setOnClickListener { val position = adapterPosition @@ -31,8 +27,7 @@ class TransactionAdapter(private val list: MutableList<TransactionEntity>) : Rec } editButton.setOnClickListener { - val position = adapterPosition - val intent = Intent(binding.root.context, AddTransactionActivity::class.java) + val intent = Intent(binding.root.context, DetailTransactionActivity::class.java) intent.putExtra("id", transaction.id) binding.root.context.startActivity(intent) } @@ -41,11 +36,7 @@ class TransactionAdapter(private val list: MutableList<TransactionEntity>) : Rec val locationUri = "geo:0,0?q=${transaction.location}" val mapIntent = Intent(Intent.ACTION_VIEW, Uri.parse(locationUri)) mapIntent.setPackage("com.google.android.apps.maps") - if (mapIntent.resolveActivity(binding.root.context.packageManager) != null) { - binding.root.context.startActivity(mapIntent) - } else { - Toast.makeText(binding.root.context, "Google Maps app not found", Toast.LENGTH_SHORT).show() - } + startActivity(binding.root.context, mapIntent, null) } } } diff --git a/app/src/main/res/layout/activity_detail_transaction.xml b/app/src/main/res/layout/activity_detail_transaction.xml new file mode 100644 index 0000000000000000000000000000000000000000..03af554a896712a01a4522465f7a44192d37ff11 --- /dev/null +++ b/app/src/main/res/layout/activity_detail_transaction.xml @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="utf-8"?> +<RelativeLayout 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" + android:background="@color/primary_color_4" + tools:context=".DetailTransactionActivity"> + + <FrameLayout + android:id="@+id/header_layout" + android:layout_width="match_parent" + android:layout_height="wrap_content" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent" /> + + <FrameLayout + android:id="@+id/frame_layout" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_below="@id/header_layout"/> + +</RelativeLayout> \ No newline at end of file diff --git a/app/src/main/res/layout/activity_add_transaction.xml b/app/src/main/res/layout/fragment_detail_transaction.xml similarity index 76% rename from app/src/main/res/layout/activity_add_transaction.xml rename to app/src/main/res/layout/fragment_detail_transaction.xml index b4a91db9929b56de9b7722aeacf59746dc70c23b..78309f50deb3162e408883f082e46a0184a417bc 100644 --- a/app/src/main/res/layout/activity_add_transaction.xml +++ b/app/src/main/res/layout/fragment_detail_transaction.xml @@ -5,103 +5,117 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" - tools:context=".AddTransactionActivity"> + tools:context=".DetailTransaction"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" - android:orientation="vertical" - android:layout_margin="16dp"> + android:layout_marginStart="16dp" + android:layout_marginEnd="16dp" + android:layout_marginBottom="16dp" + android:orientation="vertical"> + <TextView android:layout_width="match_parent" android:layout_height="wrap_content" - android:text="Title" - android:textSize="16dp" android:layout_marginTop="16dp" - android:textColor="@color/primary_color_1"/> + android:text="Title" + android:textColor="@color/primary_color_1" + android:textSize="16sp" /> + <com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> + <EditText android:id="@+id/inputTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPersonName" - android:padding="8dp"/> + android:padding="8dp" /> </com.google.android.material.textfield.TextInputLayout> + <TextView android:layout_width="match_parent" android:layout_height="wrap_content" + android:layout_marginTop="16dp" android:text="How Much?" - android:textSize="16dp" android:textColor="@color/primary_color_1" - android:layout_marginTop="16dp"/> + android:textSize="16sp" /> + <com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> + <EditText android:id="@+id/inputAmount" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPersonName" - android:textSize="36dp" - android:padding="8dp"/> + android:padding="8dp" + android:textSize="36sp" /> </com.google.android.material.textfield.TextInputLayout> + <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="Category" - android:textSize="16sp" android:layout_marginTop="16dp" - android:textColor="@color/primary_color_1"/> + android:text="Category" + android:textColor="@color/primary_color_1" + android:textSize="16sp" /> <RadioGroup android:id="@+id/radioGroupCategory" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:orientation="horizontal" - android:layout_gravity="center"> + android:layout_gravity="center" + android:orientation="horizontal"> + <RadioButton android:id="@+id/radioIncome" android:layout_width="wrap_content" android:layout_height="wrap_content" + android:layout_marginRight="24dp" android:text="Income" - android:textSize="24dp" - android:layout_marginRight="24px" /> + android:textSize="24sp" /> + <RadioButton android:id="@+id/radioExpense" android:layout_width="wrap_content" android:layout_height="wrap_content" + android:layout_marginLeft="24dp" android:text="Expense" - android:textSize="24dp" - android:layout_marginLeft="24px"/> + android:textSize="24sp" /> </RadioGroup> + <TextView android:layout_width="match_parent" android:layout_height="wrap_content" + android:layout_marginTop="16dp" android:text="Location" - android:textSize="16dp" android:textColor="@color/primary_color_1" - android:layout_marginTop="16dp"/> + android:textSize="16sp" /> + <com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> + <EditText android:id="@+id/inputLocation" android:layout_width="match_parent" android:layout_height="wrap_content" - android:padding="8dp"/> + android:padding="8dp" /> </com.google.android.material.textfield.TextInputLayout> + <Button android:id="@+id/buttonSave" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="Save" - android:backgroundTint="@color/primary_color_1" - android:textColor="@color/white" - android:layout_marginTop="48dp" android:layout_gravity="center_horizontal" - /> + android:layout_marginTop="48dp" + android:backgroundTint="@color/primary_color_1" + android:text="Save" + android:textColor="@color/white" /> </LinearLayout> </RelativeLayout> \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_header_detail_transaction.xml b/app/src/main/res/layout/fragment_header_detail_transaction.xml index 64695a58ebcee279d1ec00d3a7cfabfec1fe8fe9..b5734b61c996c724fd2ccc050d06c6ae8cc25cd7 100644 --- a/app/src/main/res/layout/fragment_header_detail_transaction.xml +++ b/app/src/main/res/layout/fragment_header_detail_transaction.xml @@ -1,16 +1,54 @@ <?xml version="1.0" encoding="utf-8"?> -<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" +<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" - tools:context=".HeaderDetailTransaction" - android:id="@+id/header_add_transaction"> + android:background="@color/primary_color_4" + tools:context=".HeaderDetailTransaction"> - <!-- TODO: Update blank fragment layout --> - <TextView + <FrameLayout android:layout_width="match_parent" - android:layout_height="match_parent" - android:text="@string/hello_blank_fragment" - android:textSize="30dp"/> + android:layout_height="wrap_content" + android:layout_marginStart="16dp" + android:layout_marginTop="16dp" + android:layout_marginEnd="16dp" + android:layout_marginBottom="16dp" + android:background="@drawable/rounded_background" + android:orientation="horizontal"> -</FrameLayout> \ No newline at end of file + <ImageView + android:id="@+id/buttonBack" + android:layout_width="32dp" + android:layout_height="32dp" + android:layout_gravity="center_vertical" + android:layout_marginStart="20dp" + android:background="@drawable/baseline_arrow_back_ios_24" /> + + <LinearLayout + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_margin="16dp" + android:orientation="horizontal" + android:layout_gravity="center_horizontal"> + + <ImageView + android:id="@+id/imageView" + android:layout_width="wrap_content" + android:layout_height="match_parent" + android:src="@drawable/baseline_assignment_24" /> + + <TextView + android:id="@+id/textView" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginStart="12dp" + android:text="@string/fragment_transaction" + android:textColor="@color/primary_color_1" + android:textSize="25sp" + android:textStyle="bold" /> + + </LinearLayout> + + + </FrameLayout> +</RelativeLayout> \ No newline at end of file