diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 1b8e29c28907c6d7715ef1e4f9943d603a3659e0..cbec2387eb09fb9a2399a5e5c2367be19071f29a 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -101,4 +101,5 @@ dependencies { // Others implementation("androidx.work:work-runtime-ktx:2.9.0") + implementation("com.google.android.gms:play-services-location:21.2.0") } \ No newline at end of file diff --git a/app/src/main/java/com/example/transactionapp/ui/screen/mainmenu/fragment/TransactionForm.kt b/app/src/main/java/com/example/transactionapp/ui/screen/mainmenu/fragment/TransactionForm.kt index 53b50ace77d3663940979b3b58ffededc8bb50b6..f30e6f916c42e3d64158f5c76498e2ede3dfa443 100644 --- a/app/src/main/java/com/example/transactionapp/ui/screen/mainmenu/fragment/TransactionForm.kt +++ b/app/src/main/java/com/example/transactionapp/ui/screen/mainmenu/fragment/TransactionForm.kt @@ -1,19 +1,155 @@ package com.example.transactionapp.ui.screen.mainmenu.fragment +import android.content.pm.PackageManager +import android.location.Geocoder +import android.location.Location +import android.location.LocationRequest +import android.os.Build import android.os.Bundle +import android.os.Looper +import android.util.Log import androidx.fragment.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.AdapterView +import android.widget.ArrayAdapter +import android.widget.Toast +import androidx.annotation.RequiresApi +import androidx.core.app.ActivityCompat +import androidx.lifecycle.MutableLiveData +import com.example.transactionapp.R import com.example.transactionapp.databinding.FragmentTransactionFormBinding +import com.example.transactionapp.utils.changeDateTypeToStandardDateLocal +import com.google.android.gms.location.FusedLocationProviderClient +import com.google.android.gms.location.LocationCallback +import com.google.android.gms.location.LocationResult +import com.google.android.gms.location.LocationServices +import dagger.hilt.android.AndroidEntryPoint +import kotlinx.coroutines.DelicateCoroutinesApi +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext +import java.util.Date +import java.util.Locale +@AndroidEntryPoint class TransactionForm : Fragment() { + lateinit var fusedLocationProviderClient: FusedLocationProviderClient + private var cityName : MutableLiveData<String> = MutableLiveData() + @OptIn(DelicateCoroutinesApi::class) + @RequiresApi(Build.VERSION_CODES.S) override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { + RequestPermission() + NewLocationData() + val binding = FragmentTransactionFormBinding.inflate(inflater) + val categories = arrayOf("Income", "Expense", "Savings") + val arrayAdp = ArrayAdapter(requireActivity(), R.layout.selected_dropdown_item, categories) + arrayAdp.setDropDownViewResource(R.layout.dropdown_item) + binding.categoryInput.adapter = arrayAdp + + binding.categoryInput?.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { + + override fun onItemSelected( + parent: AdapterView<*>?, + view: View?, + position: Int, + id: Long + ) { + Toast.makeText(requireActivity(), "Selected: " + categories[position], Toast.LENGTH_SHORT).show() + } + + override fun onNothingSelected(parent: AdapterView<*>?) { + Toast.makeText(requireActivity(), "Nothing Selected", Toast.LENGTH_SHORT).show() + } + } + + binding.dateInput.text = changeDateTypeToStandardDateLocal(Date()) + + Log.d("Debug:","City Name: $cityName") + cityName.observe(viewLifecycleOwner, { + binding.locationInput.text = it + }) + return binding.root } + + fun RequestPermission(){ + ActivityCompat.requestPermissions( + requireActivity(), + arrayOf(android.Manifest.permission.ACCESS_COARSE_LOCATION,android.Manifest.permission.ACCESS_FINE_LOCATION), + 1010 + ) + } + + @RequiresApi(Build.VERSION_CODES.S) + override fun onRequestPermissionsResult( + requestCode: Int, + permissions: Array<out String>, + grantResults: IntArray + ) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults) + if(requestCode == 1010){ + if(grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED){ + Log.d("Debug:","You have the Permission") + NewLocationData() + } + } + } + + @RequiresApi(Build.VERSION_CODES.S) + fun NewLocationData(){ + var locationRequest = com.google.android.gms.location.LocationRequest() + locationRequest.priority = com.google.android.gms.location.LocationRequest.PRIORITY_HIGH_ACCURACY + locationRequest.interval = 0 + locationRequest.fastestInterval = 0 + locationRequest.numUpdates = 1 + fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(requireActivity()) + if (ActivityCompat.checkSelfPermission(requireActivity(),android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED || + ActivityCompat.checkSelfPermission(requireActivity(),android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED + ) { + fusedLocationProviderClient!!.requestLocationUpdates( + locationRequest,locationCallback, Looper.myLooper() + ) + return + } + + } + + private val locationCallback = object : LocationCallback(){ + override fun onLocationResult(locationResult: LocationResult) { + var lastLocation: Location? = locationResult.lastLocation + if (lastLocation != null) { + cityName.postValue(getCityName(lastLocation.latitude, lastLocation.longitude)) + } + +// latidude = lastLocation.latitude +// longitude = lastLocation.longitude +// cityName = getCityName(latidude,longitude) +// weatherViewModel.getWeather( +// latidude, +// longitude, +// "minutely,daily,alerts", +// getString(R.string.code), +// "metric" +// ) + } + } + + private fun getCityName(lat: Double,long: Double):String{ + var cityName = "" + var countryName = "" + var geoCoder = Geocoder(requireActivity(), Locale.getDefault()) + var Address = geoCoder.getFromLocation(lat,long,3) + + cityName = Address!!.get(0).subAdminArea + return cityName + } + } \ No newline at end of file diff --git a/app/src/main/res/drawable/spinner_input_field.xml b/app/src/main/res/drawable/spinner_input_field.xml new file mode 100644 index 0000000000000000000000000000000000000000..e8a0d9bb7080ab185f416c204921ef30ce6c8c62 --- /dev/null +++ b/app/src/main/res/drawable/spinner_input_field.xml @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="utf-8"?> +<selector xmlns:android="http://schemas.android.com/apk/res/android"> + <item> + <layer-list> + <item> + <shape android:shape="rectangle"> + <solid android:color="@color/N8" /> + <corners android:radius="10dp" /> + <padding android:left="10dp" android:top="20dp" android:right="10dp" android:bottom="20dp"/> + </shape> + </item> + + <item android:drawable="@drawable/arrow_down" + android:gravity="end"/> + </layer-list> + </item> +</selector> \ No newline at end of file diff --git a/app/src/main/res/layout/dropdown_item.xml b/app/src/main/res/layout/dropdown_item.xml new file mode 100644 index 0000000000000000000000000000000000000000..91570c655126f94eba303dedab9a70bc315696a3 --- /dev/null +++ b/app/src/main/res/layout/dropdown_item.xml @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="utf-8"?> +<TextView xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:textSize="16sp" + android:textColor="@color/N1" + android:background="@color/N8" + android:fontFamily="@font/poppins_regular" + android:padding="5dp"> + +</TextView> \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_transaction_form.xml b/app/src/main/res/layout/fragment_transaction_form.xml index c6f1c493f6c41b8344a359a02a4ee7b48c79ab08..492fb4b745f4f2a40a4af1f38899115e9d16e268 100644 --- a/app/src/main/res/layout/fragment_transaction_form.xml +++ b/app/src/main/res/layout/fragment_transaction_form.xml @@ -22,7 +22,8 @@ android:fontFamily="@font/playfairdisplay_extrabold" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" - app:layout_constraintEnd_toEndOf="parent" /> + app:layout_constraintEnd_toEndOf="parent" + android:contentDescription="Transantion Text"/> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/formLayout" @@ -51,14 +52,16 @@ android:layout_width="0dp" android:layout_height="wrap_content" android:padding="7dp" - android:text="Transaction title" + android:hint="Transaction title" + android:textColorHint="@color/N6" android:textSize="16sp" - android:textColor="@color/N6" + android:textColor="@color/N1" android:fontFamily="@font/poppins_regular" app:layout_constraintTop_toBottomOf="@id/titleLabel" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="5dp" + android:maxLines="1" android:background="@drawable/rounded_input_field"/> <TextView @@ -102,21 +105,20 @@ app:layout_constraintTop_toBottomOf="@id/dateInput" app:layout_constraintStart_toStartOf="parent" /> - <EditText + <Spinner android:id="@+id/categoryInput" android:layout_width="0dp" android:layout_height="wrap_content" - android:padding="7dp" + android:paddingVertical="10dp" + android:paddingHorizontal="7dp" android:text="Choose category" android:textSize="16sp" - android:textColor="@color/N6" android:fontFamily="@font/poppins_regular" app:layout_constraintTop_toBottomOf="@id/categoryLabel" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="5dp" - android:background="@drawable/rounded_input_field" - android:drawableEnd="@drawable/arrow_down"/> + android:background="@drawable/spinner_input_field"/> <TextView android:id="@+id/amountLabel" @@ -136,14 +138,17 @@ android:layout_width="0dp" android:layout_height="wrap_content" android:padding="7dp" - android:text="0" + android:hint="0" + android:textColorHint="@color/N6" + android:inputType="number" android:textSize="16sp" - android:textColor="@color/N6" + android:textColor="@color/N1" android:fontFamily="@font/poppins_regular" app:layout_constraintTop_toBottomOf="@id/amountLabel" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="5dp" + android:maxLines="1" android:background="@drawable/rounded_input_field"/> <TextView @@ -164,7 +169,7 @@ android:layout_width="0dp" android:layout_height="wrap_content" android:padding="7dp" - android:text="Jl. Sayang No. 69" + android:text="" android:textSize="16sp" android:textColor="@color/N6" android:fontFamily="@font/poppins_regular" diff --git a/app/src/main/res/layout/selected_dropdown_item.xml b/app/src/main/res/layout/selected_dropdown_item.xml new file mode 100644 index 0000000000000000000000000000000000000000..143f8420a62d009293088212cfdc8da40f4171ca --- /dev/null +++ b/app/src/main/res/layout/selected_dropdown_item.xml @@ -0,0 +1,9 @@ +<?xml version="1.0" encoding="utf-8"?> +<TextView xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:textColor="@color/N1" + android:fontFamily="@font/poppins_regular" + android:textSize="16sp"> + +</TextView> \ No newline at end of file