Skip to content

Commit

Permalink
feat:
Browse files Browse the repository at this point in the history
- move provider feature to new package
- refactor Location data class
  • Loading branch information
hnfnfl committed Sep 15, 2024
1 parent 0db56f9 commit 43a7c3e
Show file tree
Hide file tree
Showing 19 changed files with 81 additions and 68 deletions.
11 changes: 0 additions & 11 deletions app/src/main/java/com/jmnetwork/e_jartas/model/Additional.kt

This file was deleted.

This file was deleted.

29 changes: 29 additions & 0 deletions app/src/main/java/com/jmnetwork/e_jartas/model/GeneralClass.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.jmnetwork.e_jartas.model

import com.google.gson.annotations.SerializedName


class Location : ArrayList<LocationItem>()
class Additional : ArrayList<AdditionalItem>()

data class DefaultResponse(
@SerializedName("message") val message: String,
@SerializedName("status") val status: String
)

data class LocationItem(
@SerializedName("lat") val lat: String,
@SerializedName("lng") val lng: String
)

data class AdditionalItem(
@SerializedName("id") val id: Int,
@SerializedName("parameter") val parameter: String,
@SerializedName("value") val value: String
)

data class TotalData(
@SerializedName("total_data") val totalData: Int,
@SerializedName("total_panjang_km") val totalPanjangKm: Int,
@SerializedName("total_panjang_meter") val totalPanjangMeter: Int
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.jmnetwork.e_jartas.model

import com.google.gson.annotations.SerializedName
import org.json.JSONObject

data class ProviderResponse(
@SerializedName("current_page") val currentPage: String,
Expand All @@ -28,4 +27,4 @@ data class ProviderRequest(
@SerializedName("additional") val additional: Additional,
@SerializedName("alamat") val alamat: String,
@SerializedName("provider") val provider: String
)
)
13 changes: 3 additions & 10 deletions app/src/main/java/com/jmnetwork/e_jartas/model/RuasJalanModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ data class RuasJalanData(
@SerializedName("idruas_jalan") val idRuasJalan: Int,
@SerializedName("kecamatan") val kecamatan: String,
@SerializedName("lastupdate") val lastUpdate: String,
@SerializedName("latlong") val latLong: String?,
@SerializedName("latlong") val latLong: Location,
@SerializedName("nama_ruas_jalan") val namaRuasJalan: String,
@SerializedName("no_ruas") val noRuas: String,
@SerializedName("oleh") val oleh: Int,
Expand All @@ -41,12 +41,10 @@ data class RuasJalanRequest(
@SerializedName("status") val status: String,
@SerializedName("tipe") val tipe: String,
@SerializedName("fungsi") val fungsi: String,
@SerializedName("latlong") val latLong: List<Location>,
@SerializedName("latlong") val latLong: Location,
@SerializedName("additional") val additional: List<JSONObject>
) {
fun toRuasJalanData(idadmin: Int, data: RuasJalanRequest): RuasJalanData {
val latLong = Gson().toJson(data.latLong)

return RuasJalanData(
additional = data.additional.toString(),
createdDate = "",
Expand All @@ -55,7 +53,7 @@ data class RuasJalanRequest(
idRuasJalan = data.idRuasJalan,
kecamatan = data.kecamatan,
lastUpdate = "",
latLong = latLong,
latLong = data.latLong,
namaRuasJalan = data.namaRuasJalan,
noRuas = data.noRuas,
oleh = idadmin,
Expand All @@ -65,8 +63,3 @@ data class RuasJalanRequest(
)
}
}

data class Location(
@SerializedName("lat") val lat: String,
@SerializedName("lng") val lng: String
)
10 changes: 0 additions & 10 deletions app/src/main/java/com/jmnetwork/e_jartas/model/TotalData.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.jmnetwork.e_jartas.model.DefaultResponse
import com.jmnetwork.e_jartas.model.Location
import com.jmnetwork.e_jartas.model.RuasJalanRequest
import com.jmnetwork.e_jartas.model.RuasJalanResponse
import com.jmnetwork.e_jartas.model.SpinnerResponse
import com.jmnetwork.e_jartas.utils.CustomHandler
import com.jmnetwork.e_jartas.utils.LocationDeserializer
import okhttp3.ResponseBody
import org.json.JSONObject
import retrofit2.Call
Expand Down Expand Up @@ -54,8 +57,9 @@ class ManajemenJalanRepositoryImpl : ManajemenJalanRepository {
apiService.getAllData(limit, page, "ruas_jalan", tokenAuth).enqueue(object : retrofit2.Callback<ResponseBody> {
override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
if (response.isSuccessful) {
val gson = GsonBuilder().registerTypeAdapter(Location::class.java, LocationDeserializer()).create()
val responseString = response.body()?.string()
val data = Gson().fromJson(responseString, RuasJalanResponse::class.java)
val data = gson.fromJson(responseString, RuasJalanResponse::class.java)
ruasJalanData.postValue(data)
} else {
val errorBody = response.errorBody()?.string()
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/com/jmnetwork/e_jartas/utils/Deserializer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import com.google.gson.JsonElement
import com.google.gson.JsonParser
import com.jmnetwork.e_jartas.model.Additional
import com.jmnetwork.e_jartas.model.AdditionalItem
import com.jmnetwork.e_jartas.model.Location
import com.jmnetwork.e_jartas.model.LocationItem
import java.lang.reflect.Type

class AdditionalDeserializer : JsonDeserializer<Additional> {
Expand All @@ -23,4 +25,21 @@ class AdditionalDeserializer : JsonDeserializer<Additional> {
}
return additional
}
}

class LocationDeserializer : JsonDeserializer<Location> {
override fun deserialize(json: JsonElement?, typeOfT: Type?, context: JsonDeserializationContext?): Location {
val location = Location()
// Check if json is not null and is a JSON array string
if (json != null && json.isJsonPrimitive) {
val jsonString = json.asString
// Parse the string-escaped JSON into a list of LocationItems
val jsonArray = JsonParser.parseString(jsonString).asJsonArray
jsonArray.forEach { it ->
val item = context?.deserialize<LocationItem>(it, LocationItem::class.java)
item?.let { location.add(it) }
}
}
return location
}
}
10 changes: 5 additions & 5 deletions app/src/main/java/com/jmnetwork/e_jartas/utils/Utils.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.jmnetwork.e_jartas.utils

import com.google.android.gms.maps.model.LatLng
import org.json.JSONArray
import com.jmnetwork.e_jartas.model.Location

class Utils {
fun capitalizeFirstCharacter(input: String): String {
Expand All @@ -12,12 +12,12 @@ class Utils {
}
}

fun latLongConverter(latLong: String): LatLng? {
fun latLongConverter(latLong: Location): LatLng? {
if (latLong.isEmpty()) return null

val rawLatLng = JSONArray(latLong).getJSONObject(0)
val lat = rawLatLng.getString("lat").toDouble()
val lng = rawLatLng.getString("lng").toDouble()
val rawLatLng = latLong[0]
val lat = rawLatLng.lat.toDouble()
val lng = rawLatLng.lng.toDouble()
return LatLng(lat, lng)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.google.android.gms.maps.model.MarkerOptions
import com.jmnetwork.e_jartas.R
import com.jmnetwork.e_jartas.databinding.FormRuasJalanBinding
import com.jmnetwork.e_jartas.model.Location
import com.jmnetwork.e_jartas.model.LocationItem
import com.jmnetwork.e_jartas.view.MainActivity
import com.jmnetwork.e_jartas.viewModel.ManajemenJalanViewModel
import com.jmnetwork.e_jartas.viewModel.ViewModelFactory
Expand Down Expand Up @@ -146,10 +147,9 @@ class AddRuasJalanFragment : Fragment(), OnMapReadyCallback {
val inputTipe = spinnerTipe.selectedItem.takeIf { it != null }?.toString().orEmpty()
val inputFungsi = spinnerFungsi.selectedItem.takeIf { it != null }?.toString().orEmpty()

val latlng = Location(
latLng?.latitude.toString(),
latLng?.longitude.toString()
)
val location = Location().apply {
add(LocationItem(latLng?.latitude.toString(), latLng?.longitude.toString()))
}

val validate = viewModel.setRequestData(
0,
Expand All @@ -161,7 +161,7 @@ class AddRuasJalanFragment : Fragment(), OnMapReadyCallback {
inputStatus,
inputTipe,
inputFungsi,
listOf(latlng),
location,
emptyList()
)
btnRuasJalan.attachTextChangeAnimator()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ class BottomSheetItemRuasJalan : BottomSheetDialogFragment(), OnMapReadyCallback

override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
val location = Utils().latLongConverter(ruasjalanData?.latLong ?: "")

val location = Utils().latLongConverter(ruasjalanData!!.latLong)
if (location != null) {
binding.ruasDetilMap.visibility = View.VISIBLE
with(mMap) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.google.android.gms.maps.model.MarkerOptions
import com.jmnetwork.e_jartas.R
import com.jmnetwork.e_jartas.databinding.FormRuasJalanBinding
import com.jmnetwork.e_jartas.model.Location
import com.jmnetwork.e_jartas.model.LocationItem
import com.jmnetwork.e_jartas.utils.CustomHandler
import com.jmnetwork.e_jartas.utils.Utils
import com.jmnetwork.e_jartas.view.MainActivity
Expand Down Expand Up @@ -191,10 +192,9 @@ class EditRuasJalanFragment : Fragment(), OnMapReadyCallback {
val inputTipe = spinnerTipe.selectedItem.takeIf { it != null }?.toString().orEmpty()
val inputFungsi = spinnerFungsi.selectedItem.takeIf { it != null }?.toString().orEmpty()

val latlng = Location(
latLng?.latitude.toString(),
latLng?.longitude.toString()
)
val location = Location().apply {
add(LocationItem(latLng?.latitude.toString(), latLng?.longitude.toString()))
}

val validate = viewModel.setRequestData(
idRuasJalan,
Expand All @@ -206,7 +206,7 @@ class EditRuasJalanFragment : Fragment(), OnMapReadyCallback {
inputStatus,
inputTipe,
inputFungsi,
listOf(latlng),
location,
emptyList()
)
btnRuasJalan.attachTextChangeAnimator()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jmnetwork.e_jartas.view.manajemenTiang
package com.jmnetwork.e_jartas.view.manajemenTiang.provider

import android.content.Intent
import android.os.Bundle
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jmnetwork.e_jartas.view.manajemenTiang
package com.jmnetwork.e_jartas.view.manajemenTiang.provider

import android.os.Bundle
import android.view.LayoutInflater
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jmnetwork.e_jartas.view.manajemenTiang
package com.jmnetwork.e_jartas.view.manajemenTiang.provider

import android.content.Intent
import android.os.Bundle
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jmnetwork.e_jartas.view.manajemenTiang
package com.jmnetwork.e_jartas.view.manajemenTiang.provider

import android.content.Intent
import android.os.Bundle
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jmnetwork.e_jartas.view.manajemenTiang
package com.jmnetwork.e_jartas.view.manajemenTiang.provider

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jmnetwork.e_jartas.view.manajemenTiang
package com.jmnetwork.e_jartas.view.manajemenTiang.provider

import android.annotation.SuppressLint
import android.graphics.Color
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class ManajemenJalanViewModel(application: Application) : ViewModel() {
}

private var requestData: RuasJalanRequest = RuasJalanRequest(
0, "", "", "", "", "", "", "", "", emptyList(), emptyList()
0, "", "", "", "", "", "", "", "", Location(), emptyList()
)

private fun fetchSpinnerData(spinnerType: String, spinnerLiveData: MutableLiveData<SpinnerResponse>) {
Expand All @@ -76,7 +76,7 @@ class ManajemenJalanViewModel(application: Application) : ViewModel() {
status: String,
tipe: String,
fungsi: String,
latlong: List<Location>,
latlong: Location,
additional: List<JSONObject>
): String {
val fields = listOf(
Expand Down

0 comments on commit 43a7c3e

Please sign in to comment.