Skip to content

Commit

Permalink
feat:+
Browse files Browse the repository at this point in the history
- add new titik tiang feature
- code refactoring and clean up
  • Loading branch information
hnfnfl committed Sep 19, 2024
1 parent 43a7c3e commit 8507c23
Show file tree
Hide file tree
Showing 27 changed files with 711 additions and 67 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ dependencies {
implementation 'com.google.android.gms:play-services-location:21.3.0'
implementation 'com.google.code.gson:gson:2.10.1'
implementation 'com.google.android.material:material:1.12.0'
implementation 'androidx.activity:activity:1.9.2'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
android:theme="@style/Theme.Ejartas"
tools:targetApi="tiramisu">
<activity
android:name=".view.manajemenTiang.ProviderActivity"
android:name=".view.manajemenTiang.titikTiang.TitikTiangActivity"
android:exported="false" />
<activity
android:name=".view.manajemenTiang.provider.ProviderActivity"
android:exported="false" />
<activity
android:name=".view.manajemenJalan.RuasJalanActivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ data class DefaultResponse(
)

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

data class AdditionalItem(
Expand All @@ -25,5 +26,6 @@ data class AdditionalItem(
data class TotalData(
@SerializedName("total_data") val totalData: Int,
@SerializedName("total_panjang_km") val totalPanjangKm: Int,
@SerializedName("total_panjang_meter") val totalPanjangMeter: Int
@SerializedName("total_panjang_meter") val totalPanjangMeter: Int,
@SerializedName("total_tiang_terdata") val totalTiang: Int
)
37 changes: 37 additions & 0 deletions app/src/main/java/com/jmnetwork/e_jartas/model/TiangModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.jmnetwork.e_jartas.model

import com.google.gson.annotations.SerializedName

data class TiangResponse(
@SerializedName("current_page") val currentPage: String,
@SerializedName("data") val data: List<TiangData>,
@SerializedName("data_tabel") val dataTabel: String,
@SerializedName("limit") val limit: String,
@SerializedName("message") val message: String,
@SerializedName("status") val status: String,
@SerializedName("totalData") val totalData: TotalData
)

data class TiangData(
@SerializedName("createddate") val createddate: String,
@SerializedName("idtiang") val idtiang: Int,
@SerializedName("idtiang_cetak_qrcode") val idtiangCetakQrcode: Int,
@SerializedName("json_provider") val tiangProvider: TiangProvider,
@SerializedName("jumlah_tiang") val jumlahTiang: String,
@SerializedName("lastupdate") val lastupdate: String,
@SerializedName("latlong") val latlong: Location,
@SerializedName("oleh") val oleh: Int,
@SerializedName("petugas_scan") val petugasScan: Any?,
@SerializedName("qrcode") val qrcode: String,
)

class TiangProvider : ArrayList<TiangProviderItem>()

data class TiangProviderItem(
@SerializedName("catatan") val catatan: String,
@SerializedName("id") val id: Int,
@SerializedName("idprovider") val idprovider: String,
@SerializedName("layak") val layak: String,
@SerializedName("provider") val provider: String,
@SerializedName("utilitas") val utilitas: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.jmnetwork.e_jartas.model.ProviderResponse
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.model.TiangResponse

interface BaseRepository {
fun login(context: Context, loginRequest: LoginRequest, deviceID: String, tokenAuth: String): LiveData<LoginResponse>
Expand All @@ -31,4 +32,6 @@ interface ManajemenTiangRepository {
fun blacklistProvider(context: Context, idadmin: Int, iddata: Int, isBlacklist: Boolean, tokenAuth: String): LiveData<DefaultResponse>
fun editProvider(context: Context, idadmin: Int, iddata: Int, requestData: ProviderRequest, tokenAuth: String): LiveData<DefaultResponse>
fun deleteProvider(context: Context, idadmin: Int, iddata: Int, tokenAuth: String): LiveData<DefaultResponse>

fun getTitikTiang(context: Context, limit: Int, page: Int, tokenAuth: String): LiveData<TiangResponse>
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.jmnetwork.e_jartas.model.Additional
import com.jmnetwork.e_jartas.model.DefaultResponse
import com.jmnetwork.e_jartas.model.Location
import com.jmnetwork.e_jartas.model.ProviderRequest
import com.jmnetwork.e_jartas.model.ProviderResponse
import com.jmnetwork.e_jartas.model.TiangProvider
import com.jmnetwork.e_jartas.model.TiangResponse
import com.jmnetwork.e_jartas.utils.AdditionalDeserializer
import com.jmnetwork.e_jartas.utils.CustomHandler
import com.jmnetwork.e_jartas.utils.LocationDeserializer
import com.jmnetwork.e_jartas.utils.TiangProviderDeserializer
import okhttp3.ResponseBody
import org.json.JSONObject
import retrofit2.Call
Expand Down Expand Up @@ -174,4 +179,32 @@ class ManajemenTiangRepositoryImpl : ManajemenTiangRepository {

return result
}

override fun getTitikTiang(context: Context, limit: Int, page: Int, tokenAuth: String): LiveData<TiangResponse> {
val result = MutableLiveData<TiangResponse>()

apiService.getAllData(limit, page, "tiang", tokenAuth).enqueue(object : retrofit2.Callback<ResponseBody> {
override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
if (response.isSuccessful) {
val gson = GsonBuilder()
.registerTypeAdapter(TiangProvider::class.java, TiangProviderDeserializer())
.registerTypeAdapter(Location::class.java, LocationDeserializer())
.create()
val responseString = response.body()?.string()
val data = gson.fromJson(responseString, TiangResponse::class.java)
result.postValue(data)
} else {
val errorBody = response.errorBody()?.string()
val errorMsg = errorBody?.let { JSONObject(it).getString("message") } ?: "Unknown error"
CustomHandler().responseHandler(context, "getTitikTiang|onResponse", errorMsg, response.code())
}
}

override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
CustomHandler().responseHandler(context, "getTitikTiang|onFailure", t.message.toString())
}
})

return result
}
}
47 changes: 24 additions & 23 deletions app/src/main/java/com/jmnetwork/e_jartas/utils/Deserializer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,42 @@ import com.google.gson.JsonDeserializer
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 com.jmnetwork.e_jartas.model.TiangProvider
import java.lang.reflect.Type

inline fun <reified T> parseJsonArray(json: JsonElement?, context: JsonDeserializationContext?, list: MutableList<T>): MutableList<T> {
if (json != null && json.isJsonPrimitive) {
val jsonString = json.asString
val jsonArray = JsonParser.parseString(jsonString).asJsonArray
jsonArray.forEach { jsonElement ->
val item = context?.deserialize<T>(jsonElement, T::class.java)
item?.let { list.add(it) }
}
}
return list
}

class AdditionalDeserializer : JsonDeserializer<Additional> {
override fun deserialize(json: JsonElement?, typeOfT: Type?, context: JsonDeserializationContext?): Additional {
val additional = Additional()
// 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 AdditionalItems
val jsonArray = JsonParser.parseString(jsonString).asJsonArray
jsonArray.forEach { it ->
val item = context?.deserialize<AdditionalItem>(it, AdditionalItem::class.java)
item?.let { additional.add(it) }
}
}
parseJsonArray(json, context, additional) // Reuse the generic function
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) }
}
}
parseJsonArray(json, context, location) // Reuse the generic function
return location
}
}
}

class TiangProviderDeserializer : JsonDeserializer<TiangProvider> {
override fun deserialize(json: JsonElement?, typeOfT: Type?, context: JsonDeserializationContext?): TiangProvider {
val tiangProvider = TiangProvider()
parseJsonArray(json, context, tiangProvider) // Reuse the generic function
return tiangProvider
}
}
10 changes: 8 additions & 2 deletions app/src/main/java/com/jmnetwork/e_jartas/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@ class Utils {
}
}

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

val rawLatLng = latLong[0]
val lat = rawLatLng.lat.toDouble()
val lng = rawLatLng.lng.toDouble()
return LatLng(lat, lng)
}

fun formatDate(date: String): String {
val dateArray = date.split(" ")
val dateArray2 = dateArray[0].split("-")
return "${dateArray2[2]}-${dateArray2[1]}-${dateArray2[0]}"
}
}
10 changes: 7 additions & 3 deletions app/src/main/java/com/jmnetwork/e_jartas/view/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import androidx.lifecycle.ViewModelProvider
import com.jmnetwork.e_jartas.R
import com.jmnetwork.e_jartas.databinding.FragmentHomeBinding
import com.jmnetwork.e_jartas.view.manajemenJalan.RuasJalanActivity
import com.jmnetwork.e_jartas.view.manajemenTiang.ProviderActivity
import com.jmnetwork.e_jartas.view.manajemenTiang.provider.ProviderActivity
import com.jmnetwork.e_jartas.view.manajemenTiang.titikTiang.TitikTiangActivity
import com.jmnetwork.e_jartas.viewModel.HomeViewModel
import com.jmnetwork.e_jartas.viewModel.ViewModelFactory
import java.util.Locale
Expand Down Expand Up @@ -54,11 +55,14 @@ class HomeFragment : Fragment() {
btnProvider.setOnClickListener {
activity?.startActivity(
Intent(requireContext(), ProviderActivity::class.java)
.putExtra(ProviderActivity.DESTINATION, "list")
)
}

btnTitikTiang.setOnClickListener { }
btnTitikTiang.setOnClickListener {
activity?.startActivity(
Intent(requireContext(), TitikTiangActivity::class.java)
)
}
}

return binding.root
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class AddRuasJalanFragment : Fragment(), OnMapReadyCallback {
val inputFungsi = spinnerFungsi.selectedItem.takeIf { it != null }?.toString().orEmpty()

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

val validate = viewModel.setRequestData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ class EditRuasJalanFragment : Fragment(), OnMapReadyCallback {

viewModel.apply {
if (ruasJalanData != null) {
if (ruasJalanData.latLong != null) {
latLng = Utils().latLongConverter(ruasJalanData.latLong)
}
latLng = Utils().latLongConverter(ruasJalanData.latLong)

inputNomorRuasJalan.setText(ruasJalanData.noRuas)
inputNamaRuasJalan.setText(ruasJalanData.namaRuasJalan)
Expand Down Expand Up @@ -193,7 +191,7 @@ class EditRuasJalanFragment : Fragment(), OnMapReadyCallback {
val inputFungsi = spinnerFungsi.selectedItem.takeIf { it != null }?.toString().orEmpty()

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

val validate = viewModel.setRequestData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class AddProviderFragment : Fragment() {
val namaProvider = inputNamaProvider.text.toString()
val alamatProvider = inputAlamatProvider.text.toString()

val validate = viewModel.setRequestData(
val validate = viewModel.setRequestDataProvider(
Additional(),
alamatProvider,
namaProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class EditProviderFragment : Fragment() {
btnProvider.attachTextChangeAnimator()
btnProvider.showProgress()

val validate = viewModel.setRequestData(
val validate = viewModel.setRequestDataProvider(
additional,
alamatProvider,
namaProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ class ListProviderFragment : Fragment() {
if (it != null) {
adapter.setItem(it.values.toList())
progressBar.visibility = View.GONE
totalPage = totalData.value?.div(limit) ?: 0
totalPage += if (totalData.value?.rem(limit) != 0) 1 else 0
totalPage = totalDataProvider.value?.div(limit) ?: 0
totalPage += if (totalDataProvider.value?.rem(limit) != 0) 1 else 0
} else {
adapter.setItem(emptyList())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ class ProviderActivity : AppCompatActivity() {
private lateinit var binding: ActivityProviderBinding
lateinit var viewModel: ManajemenTiangViewModel

companion object {
const val DESTINATION = "destination"
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityProviderBinding.inflate(layoutInflater)
Expand All @@ -26,15 +22,7 @@ class ProviderActivity : AppCompatActivity() {
viewModel = ViewModelProvider(this@ProviderActivity, factory)[ManajemenTiangViewModel::class.java]
viewModel.getProvider(5, 1) // Get Provider data from API

if (intent.hasExtra(DESTINATION)) {
when (intent.getStringExtra(DESTINATION)) {
"list" -> {
loadFragment(ListProviderFragment())
}
}
} else {
loadFragment(ListProviderFragment())
}
loadFragment(ListProviderFragment())
}

private fun loadFragment(fragment: Fragment) {
Expand Down
Loading

0 comments on commit 8507c23

Please sign in to comment.