Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.alamsyah.crudmysql.presenter
- import android.util.Log
- import com.example.alamsyah.crudmysql.model.ResultStaff
- import com.example.alamsyah.crudmysql.model.ResultStatus
- import com.example.alamsyah.crudmysql.network.NetworkConfig
- import retrofit2.Call
- import retrofit2.Response
- class Presenter (val crudView: CrudView) {
- //Fungsi GetData
- fun getData(){
- NetworkConfig.getService().getData()
- .enqueue(object : retrofit2.Callback<ResultStaff>{
- override fun onFailure(call: Call<ResultStaff>, t: Throwable) {
- crudView.onFailedGet(t.localizedMessage)
- Log.d("Error", "Error Data")
- }
- override fun onResponse(call: Call<ResultStaff>, response: Response<ResultStaff>) {
- if(response.isSuccessful){
- val status = response.body()?.status
- if (status == 200){
- val data = response.body()?.staff
- crudView.onSuccessGet(data)
- } else{
- crudView.onFailedGet("Error $status")
- }
- }
- }
- })
- }
- //Add data
- fun addData(name : String, hp : String, alamat : String){
- NetworkConfig.getService()
- .addStaff(name, hp, alamat)
- .enqueue(object : retrofit2.Callback<ResultStatus>{
- override fun onFailure(call: Call<ResultStatus>, t: Throwable) {
- crudView.errorAdd(t.localizedMessage)
- }
- override fun onResponse(call: Call<ResultStatus>, response: Response<ResultStatus>) {
- if (response.isSuccessful && response.body()?.status == 200) {
- crudView.successAdd(response.body()?.pesan ?: "")
- }else {
- crudView.errorAdd(response.body()?.pesan ?: "")
- }
- }
- })
- }
- //Hapus Data
- fun hapusData(id: String?){
- NetworkConfig.getService()
- .deleteStaff(id)
- .enqueue(object : retrofit2.Callback<ResultStatus>{
- override fun onFailure(call: Call<ResultStatus>, t: Throwable) {
- crudView.onErrorDelete(t.localizedMessage)
- }
- override fun onResponse(call: Call<ResultStatus>, response: Response<ResultStatus>) {
- if (response.isSuccessful && response.body()?.status == 200){
- crudView.onSuccessDelete(response.body()?.pesan ?: "")
- } else {
- crudView.onErrorDelete(response.body()?.pesan ?: "")
- }
- }
- })
- }
- //Update Data
- fun updateData(id: String, name: String, hp: String, alamat: String){
- NetworkConfig.getService()
- .updateStaff(id, name, hp, alamat)
- .enqueue(object : retrofit2.Callback<ResultStatus>{
- override fun onFailure(call: Call<ResultStatus>, t: Throwable) {
- crudView.onErrorUpdate(t.localizedMessage)
- }
- override fun onResponse(call: Call<ResultStatus>, response: Response<ResultStatus>) {
- if (response.isSuccessful && response.body()?.status == 200){
- crudView.onSuccessUpdate(response.body()?.pesan ?: "")
- }else{
- crudView.onErrorUpdate(response.body()?.pesan ?: "")
- }
- }
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement