Skip to content
This repository was archived by the owner on Jul 17, 2023. It is now read-only.

M3 refactor good practices #24

Merged
merged 16 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 6 additions & 9 deletions app/src/main/java/com/example/myapplication/APIService.kt
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
package com.example.myapplication

import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.Response
import retrofit2.http.*
import retrofit2.http.PUT




interface APIService {

@GET
suspend fun getProducts(@Url url:String): Response<List<DataProduct>>
suspend fun getProducts(@Url url: String): Response<List<DataProduct>>

@GET
suspend fun getUserData(@Url url:String):Response<User>
suspend fun getUserData(@Url url: String): Response<User>

@FormUrlEncoded
@POST("api/login")
suspend fun loginUser(@Field("email") email:String,
@Field("password")password:String):Response<ResponseBody>
suspend fun loginUser(
@Field("email") email: String,
@Field("password") password: String
): Response<ResponseBody>
}
120 changes: 59 additions & 61 deletions app/src/main/java/com/example/myapplication/AddressFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.content.Context
import android.content.SharedPreferences
import android.content.pm.PackageManager
import android.location.Geocoder
import android.location.Location
import android.location.LocationManager
import android.os.Bundle
import android.view.LayoutInflater
Expand All @@ -15,37 +16,33 @@ import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat.getSystemService
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationServices
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import java.util.*


class AddressFragment : BottomSheetDialogFragment() {


private lateinit var imageButtonClose :ImageView
private lateinit var buttonUpdateAddress : Button
private lateinit var tvActualDirection:TextView
private lateinit var tvOldDirection1 : TextView
private lateinit var tvOldDirection2 : TextView
private var sharedPreferences: SharedPreferences? = null
class AddressFragment : BottomSheetDialogFragment() {
private lateinit var imageButtonClose: ImageView
private lateinit var buttonUpdateAddress: Button
private lateinit var tvActualDirection: TextView
private lateinit var tvOldDirection1: TextView
private lateinit var tvOldDirection2: TextView
private lateinit var sharedPreferences: SharedPreferences
lateinit var mFusedLocationClient: FusedLocationProviderClient
private val PERMISSION_ID = 33
private val permissionId = 33

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_address, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

sharedPreferences = this.activity?.getSharedPreferences("org.bedu.sharedpreferences", Context.MODE_PRIVATE)
sharedPreferences = ConfigManager.prefs(requireActivity())
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(requireActivity())

imageButtonClose = view.findViewById(R.id.imageButtonClose)
Expand All @@ -56,81 +53,82 @@ class AddressFragment : BottomSheetDialogFragment() {

setSharedPreferencesAddress()

imageButtonClose.setOnClickListener {
dismiss()
}
buttonUpdateAddress.setOnClickListener {
getLocation()
}

imageButtonClose.setOnClickListener { dismiss() }
buttonUpdateAddress.setOnClickListener { getLocation() }
}

private fun setSharedPreferencesAddress() {
tvActualDirection.text = sharedPreferences?.getString("USER_ACTUAL_ADDRESS","")
tvOldDirection1.text = sharedPreferences?.getString("USER_OLD_ADDRESS1","")
tvOldDirection2.text = sharedPreferences?.getString("USER_OLD_ADDRESS2","")
tvActualDirection.text = sharedPreferences.getString("USER_ACTUAL_ADDRESS", "")
tvOldDirection1.text = sharedPreferences.getString("USER_OLD_ADDRESS1", "")
tvOldDirection2.text = sharedPreferences.getString("USER_OLD_ADDRESS2", "")
}


@SuppressLint("MissingPermission")
private fun getLocation() {
if (checkPermissions()) {
if (isLocationEnabled()) {

mFusedLocationClient.lastLocation.addOnSuccessListener(requireActivity()) { location ->

val geocoder = Geocoder(requireContext(), Locale.getDefault())
val addresses = location?.latitude?.let {
geocoder.getFromLocation(
it?.toDouble(),
location?.longitude.toDouble(),
1
)
}
val actualAddress = addresses?.get(0)?.getAddressLine(0)
if(tvActualDirection.text.toString() != actualAddress){
tvOldDirection2.text = tvOldDirection1.text.toString()
tvOldDirection1.text = tvActualDirection.text.toString()
tvActualDirection.text = actualAddress
sharedPreferences?.edit()
?.putString("USER_ACTUAL_ADDRESS",tvActualDirection.text.toString())
?.putString("USER_OLD_ADDRESS1",tvOldDirection1.text.toString())
?.putString("USER_OLD_ADDRESS2",tvOldDirection2.text.toString())
?.apply()
}
}
mFusedLocationClient
.lastLocation
.addOnSuccessListener(getLocationSuccessListener())
}
} else{
} else {
requestPermissions()
}
}

private fun getLocationSuccessListener(): ((Location) -> Unit) {
return { location: Location ->
val geocoder = Geocoder(requireContext(), Locale.getDefault())
val addresses = location.latitude.let {
geocoder.getFromLocation(
it.toDouble(),
location.longitude.toDouble(),
1
)
}
val actualAddress = addresses?.get(0)?.getAddressLine(0)
if (tvActualDirection.text.toString() != actualAddress) {
tvOldDirection2.text = tvOldDirection1.text.toString()
tvOldDirection1.text = tvActualDirection.text.toString()
tvActualDirection.text = actualAddress
sharedPreferences.edit()
.putString("USER_ACTUAL_ADDRESS", tvActualDirection.text.toString())
.putString("USER_OLD_ADDRESS1", tvOldDirection1.text.toString())
.putString("USER_OLD_ADDRESS2", tvOldDirection2.text.toString())
.apply()
}
}
}


private fun isLocationEnabled(): Boolean {
var locationManager: LocationManager = requireActivity().getSystemService(Context.LOCATION_SERVICE) as LocationManager
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(
LocationManager.NETWORK_PROVIDER
)
val locationManager: LocationManager = requireActivity()
.getSystemService(Context.LOCATION_SERVICE) as LocationManager
val isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
val isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
return isGpsEnabled || isNetworkEnabled
}

private fun requestPermissions() {
ActivityCompat.requestPermissions(
requireActivity(),
arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION),
PERMISSION_ID
arrayOf(
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION
),
permissionId
)
}

private fun checkPermissions(): Boolean {
if ( checkGranted(Manifest.permission.ACCESS_COARSE_LOCATION) &&
checkGranted(Manifest.permission.ACCESS_COARSE_LOCATION) ){
return true
}
return false
val isFineLocationGranted = checkGranted(Manifest.permission.ACCESS_FINE_LOCATION)
val isCoarseLocationGranted = checkGranted(Manifest.permission.ACCESS_COARSE_LOCATION)
return isFineLocationGranted || isCoarseLocationGranted
}

private fun checkGranted(permission: String): Boolean{
return ActivityCompat.checkSelfPermission(requireContext(), permission) == PackageManager.PERMISSION_GRANTED
private fun checkGranted(permission: String): Boolean {
val selfPermission = ActivityCompat.checkSelfPermission(requireContext(), permission)
return selfPermission == PackageManager.PERMISSION_GRANTED
}

}
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
package com.example.myapplication

import android.app.Activity
import android.content.Context
import android.content.SharedPreferences
import androidx.preference.PreferenceManager


class UserConfig {
class ConfigManager {
companion object {
private const val IS_DARK_THEME_DEFAULT: Boolean = true

fun setTheme(context: Context, isDark: Boolean) {
private fun setTheme(context: Context, isDark: Boolean) {
val userPreferences = PreferenceManager.getDefaultSharedPreferences(context)
val darkThemePrefKey = context.getString((R.string.pref_key_DARK_THEME))
userPreferences.edit().putBoolean(darkThemePrefKey, isDark).apply()
}

fun prefs(activity: Activity): SharedPreferences {
return activity.getSharedPreferences(
activity.getString(R.string.login_shared_preference_file),
Context.MODE_PRIVATE
)
}

fun isDarkTheme(context: Context): Boolean {
val userPrefs = PreferenceManager.getDefaultSharedPreferences(context)
val darkThemePrefKey = context.getString((R.string.pref_key_DARK_THEME))
return userPrefs.getBoolean(darkThemePrefKey, IS_DARK_THEME_DEFAULT)
}

fun getThemeResourceId(context: Context): Int {
return if(isDarkTheme(context)) R.style.Theme_Dark else R.style.Theme_Light
return if (isDarkTheme(context)) R.style.Theme_Dark else R.style.Theme_Light
}

fun switchTheme(context: Context) = setTheme(context, !isDarkTheme(context))
Expand Down
11 changes: 4 additions & 7 deletions app/src/main/java/com/example/myapplication/DataProduct.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ data class DataProduct(
val category: String,
val image: String,
val rating: Rating
):Parcelable {
) : Parcelable {
constructor(parcel: Parcel) : this(
parcel.readInt(),
parcel.readString().toString(),
Expand All @@ -21,12 +21,9 @@ data class DataProduct(
parcel.readString().toString(),
parcel.readString().toString(),
parcel.readParcelable(Rating::class.java.classLoader) ?: Rating(0f, 0)
) {
}
)

override fun describeContents(): Int {
TODO("Not yet implemented")
}
override fun describeContents(): Int = 0

override fun writeToParcel(parcel: Parcel, p1: Int) {
parcel.writeInt(id)
Expand All @@ -35,7 +32,7 @@ data class DataProduct(
parcel.writeString(description)
parcel.writeString(category)
parcel.writeString(image)
parcel.writeParcelable(rating,Parcelable.PARCELABLE_WRITE_RETURN_VALUE)
parcel.writeParcelable(rating, Parcelable.PARCELABLE_WRITE_RETURN_VALUE)
}

companion object CREATOR : Parcelable.Creator<DataProduct> {
Expand Down
13 changes: 3 additions & 10 deletions app/src/main/java/com/example/myapplication/DbProduct.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.os.Parcelable
import io.realm.RealmObject
import io.realm.annotations.PrimaryKey

open class Product(): RealmObject(), Parcelable {
open class Product() : RealmObject(), Parcelable {
@PrimaryKey
var id: Int? = null
var title: String? = null
Expand All @@ -15,7 +15,7 @@ open class Product(): RealmObject(), Parcelable {
var image: String? = null
var ratingCount: Int? = null
var ratingRate: Float? = null
var amountAddedToCart : Int? = null
var amountAddedToCart: Int? = null

constructor(parcel: Parcel) : this() {
id = parcel.readInt()
Expand All @@ -29,9 +29,7 @@ open class Product(): RealmObject(), Parcelable {
amountAddedToCart = parcel.readInt()
}

override fun describeContents(): Int {
TODO("Not yet implemented")
}
override fun describeContents(): Int = 0

override fun writeToParcel(parcel: Parcel, p1: Int) {
parcel.writeInt(id ?: -1)
Expand All @@ -53,8 +51,3 @@ open class Product(): RealmObject(), Parcelable {
override fun newArray(size: Int): Array<Product?> = arrayOfNulls(size)
}
}





Loading