Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. package com.example.alamsyah.crudmysql.presenter
  2.  
  3. import android.util.Log
  4. import com.example.alamsyah.crudmysql.model.ResultStaff
  5. import com.example.alamsyah.crudmysql.model.ResultStatus
  6. import com.example.alamsyah.crudmysql.network.NetworkConfig
  7. import retrofit2.Call
  8. import retrofit2.Response
  9.  
  10. class Presenter (val crudView: CrudView) {
  11.  
  12. //Fungsi GetData
  13. fun getData(){
  14. NetworkConfig.getService().getData()
  15. .enqueue(object : retrofit2.Callback<ResultStaff>{
  16. override fun onFailure(call: Call<ResultStaff>, t: Throwable) {
  17. crudView.onFailedGet(t.localizedMessage)
  18. Log.d("Error", "Error Data")
  19. }
  20.  
  21. override fun onResponse(call: Call<ResultStaff>, response: Response<ResultStaff>) {
  22. if(response.isSuccessful){
  23. val status = response.body()?.status
  24. if (status == 200){
  25. val data = response.body()?.staff
  26. crudView.onSuccessGet(data)
  27. } else{
  28. crudView.onFailedGet("Error $status")
  29. }
  30. }
  31. }
  32.  
  33. })
  34. }
  35.  
  36.  
  37. //Add data
  38. fun addData(name : String, hp : String, alamat : String){
  39. NetworkConfig.getService()
  40. .addStaff(name, hp, alamat)
  41. .enqueue(object : retrofit2.Callback<ResultStatus>{
  42. override fun onFailure(call: Call<ResultStatus>, t: Throwable) {
  43. crudView.errorAdd(t.localizedMessage)
  44. }
  45.  
  46. override fun onResponse(call: Call<ResultStatus>, response: Response<ResultStatus>) {
  47. if (response.isSuccessful && response.body()?.status == 200) {
  48. crudView.successAdd(response.body()?.pesan ?: "")
  49. }else {
  50. crudView.errorAdd(response.body()?.pesan ?: "")
  51. }
  52. }
  53.  
  54. })
  55. }
  56.  
  57.  
  58. //Hapus Data
  59. fun hapusData(id: String?){
  60. NetworkConfig.getService()
  61. .deleteStaff(id)
  62. .enqueue(object : retrofit2.Callback<ResultStatus>{
  63. override fun onFailure(call: Call<ResultStatus>, t: Throwable) {
  64. crudView.onErrorDelete(t.localizedMessage)
  65. }
  66.  
  67. override fun onResponse(call: Call<ResultStatus>, response: Response<ResultStatus>) {
  68. if (response.isSuccessful && response.body()?.status == 200){
  69. crudView.onSuccessDelete(response.body()?.pesan ?: "")
  70. } else {
  71. crudView.onErrorDelete(response.body()?.pesan ?: "")
  72. }
  73. }
  74.  
  75. })
  76. }
  77.  
  78. //Update Data
  79. fun updateData(id: String, name: String, hp: String, alamat: String){
  80. NetworkConfig.getService()
  81. .updateStaff(id, name, hp, alamat)
  82. .enqueue(object : retrofit2.Callback<ResultStatus>{
  83. override fun onFailure(call: Call<ResultStatus>, t: Throwable) {
  84. crudView.onErrorUpdate(t.localizedMessage)
  85. }
  86.  
  87. override fun onResponse(call: Call<ResultStatus>, response: Response<ResultStatus>) {
  88. if (response.isSuccessful && response.body()?.status == 200){
  89. crudView.onSuccessUpdate(response.body()?.pesan ?: "")
  90. }else{
  91. crudView.onErrorUpdate(response.body()?.pesan ?: "")
  92. }
  93.  
  94. }
  95.  
  96. })
  97. }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement