-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathretrofit mvvm
More file actions
118 lines (101 loc) · 3.62 KB
/
retrofit mvvm
File metadata and controls
118 lines (101 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
https://www.youtube.com/watch?v=sBCE_hOFnQU&t=68s
add dependencys
// Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.6.0'
implementation 'com.squareup.okhttp3:logging-interceptor:4.2.1'
// Coroutine
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
Data
data class DataCart (
val cartId: Int,
val cartTitle: String,
val cartDescription: String,
val cartPrice: Int,
val cartQty: Int
)
Interface API
interface LoginAPI {
@GET("employee/login")
suspend fun loginValidation(
@Query("email") email: String,
@Query("password") password: String
): Response<Employee>
// <-- POST --> DONT INSERT THIS
@FormUrlEncoded
@POST("user/edit_address/")
suspend fun editUserAddress(
@Field("user_id") user_id: Int,
@Field("permission") permission: String,
@Field("address") address: String,
@Field("address_detail") addressDetail: String,
@Field("user_latitude") user_latitude: Double,
@Field("user_longitude") user_longitude: Double
): Response<User>
// <-- PATH --> DONT INSERT THIS
@GET("user/{user_id}")
suspend fun getUserById(
@Path("user_id") userId: Int
): Response<User>
}
Instance
object Instance {
val retrofit by lazy {
Retrofit.Builder()
.baseUrl("https://staging. .com/api/")
.addConverterFactory(GsonConverterFactory.create())
.build()
}
val loginAPI by lazy{
retrofit.create(LoginAPI::class.java)
}
}
Repository
class LoginRepository {
suspend fun loginValidation(email: String, password: String): Response<Employee>{
return Instance.loginAPI.loginValidation(email, password)
}
}
ViewModel
class LoginViewModel(private val repository: LoginRepository): ViewModel() {
val responseLogin: MutableLiveData<Response<Employee>> = MutableLiveData()
fun loginValidation(email: String, password: String){
viewModelScope.launch {
val response = repository.loginValidation(email, password)
responseLogin.value = response
}
}
}
ViewModelFactory
class LoginViewModelFactory(
private val repository: LoginRepository
) : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return LoginViewModel(repository) as T
}
}
Activity
val repository = LoginRepository()
val viewModelFactory = LoginViewModelFactory(repository)
viewModel = ViewModelProvider(this, viewModelFactory).get(LoginViewModel::class.java)
viewModel.loginValidation(text_email, text_password)
viewModel.responseLogin.observe(this, Observer { response ->
if (response.isSuccessful()) {
sharedPreferences.run {
put(Constant.PREF_IS_LOGIN, true)
put(Constant.PREF_EMPLOYEE_ID, response.body()!!.employee_id.toString())
put(Constant.PREF_FULLNAME, response.body()!!.fullname)
put(Constant.PREF_USERNAME, response.body()!!.username)
put(Constant.PREF_GENDER, response.body()!!.gender)
put(Constant.PREF_ADRESS, response.body()!!.address)
put(Constant.PREF_EMAIL, response.body()!!.email)
put(Constant.PREF_PHONE, response.body()!!.phone)
}
startActivity(Intent(this, HomeActivity::class.java))
finish()
} else {
textViewResponse.text = "email or passord not available"
}
})