Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 13 additions & 66 deletions app/src/main/java/com/medellinandroid/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,34 +1,11 @@
package com.medellinandroid

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.http.POST
import retrofit2.http.Query


data class User(
val email: String,
val name: String,
val id: String
)

interface LoginService {
@POST("login")
fun login(@Query("email") email: String?, @Query("password") password: String?)
: Call<User>?
}

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -43,57 +20,27 @@ class MainActivity : AppCompatActivity() {
val email = findViewById<EditText>(R.id.login_email).text.toString()
val password = findViewById<EditText>(R.id.login_password).text.toString()

val retrofit = Retrofit.Builder()
.client(OkHttpClient.Builder()
.addInterceptor(
HttpLoggingInterceptor().apply {
level = (HttpLoggingInterceptor.Level.BODY)
}
).build()
)
.baseUrl("https://602db10196eaad00176dcb6d.mockapi.io")
.addConverterFactory(MoshiConverterFactory.create())
.build()

val service: LoginService = retrofit.create(LoginService::class.java)

val call = service.login(email, password)
call?.enqueue(object : Callback<User> {
override fun onResponse(call: Call<User>, response: Response<User>) {
progressBar.visibility = View.GONE

if (response.isSuccessful) {
val user = response.body()

AlertDialog.Builder(this@MainActivity)
.setTitle("Hola!")
.setMessage("Bienvenido ${user?.name ?: ""}!")
.setPositiveButton(android.R.string.yes, null)
.setNegativeButton(android.R.string.no, null)
.show()
} else {
AlertDialog.Builder(this@MainActivity)
.setTitle("Error!")
.setMessage("Algo paso 1!")
.setPositiveButton(android.R.string.yes, null)
.setNegativeButton(android.R.string.no, null)
.show()
}
}

override fun onFailure(call: Call<User>, t: Throwable) {
progressBar.visibility = View.GONE
val userRepository = Api().buildUserRepository()

Log.e("Login", "Error", t)
userRepository.login(email, password) {
progressBar.visibility = View.GONE

if (it != null) {
AlertDialog.Builder(this@MainActivity)
.setTitle("Hola!")
.setMessage("Bienvenido ${it.name}!")
.setPositiveButton(android.R.string.yes, null)
.setNegativeButton(android.R.string.no, null)
.show()
} else {
AlertDialog.Builder(this@MainActivity)
.setTitle("Error!")
.setMessage("Algo paso 2!")
.setMessage("Algo paso 1!")
.setPositiveButton(android.R.string.yes, null)
.setNegativeButton(android.R.string.no, null)
.show()
}
})
}
}
}
}
64 changes: 64 additions & 0 deletions app/src/main/java/com/medellinandroid/UserRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.medellinandroid

import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.http.POST
import retrofit2.http.Query

class Api {
fun buildUserRepository(): UserRepository {
val retrofit = Retrofit.Builder()
.client(
OkHttpClient.Builder()
.addInterceptor(
HttpLoggingInterceptor().apply {
level = (HttpLoggingInterceptor.Level.BODY)
}
).build()
)
.baseUrl("https://602db10196eaad00176dcb6d.mockapi.io")
.addConverterFactory(MoshiConverterFactory.create())
.build()

val service: LoginService = retrofit.create(LoginService::class.java)
return UserRepository(service)
}
}

class UserRepository(private val service: LoginService) {
fun login(email: String, password: String, responseCallback: (User?) -> Unit) {
val call = service.login(email, password)

call?.enqueue(object : Callback<User> {
override fun onResponse(call: Call<User>, response: Response<User>) {
if (response.isSuccessful) {
val user = response.body()
responseCallback(user)
} else {
responseCallback(null)
}
}

override fun onFailure(call: Call<User>, t: Throwable) {
responseCallback(null)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How are we gonna manage an error! are we going to log it? how do we internally know that an error happened?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:O thanks, I didn't notice I missed it!

}
})
}
}

data class User(
val email: String,
val name: String,
val id: String
)

interface LoginService {
@POST("login")
fun login(@Query("email") email: String?, @Query("password") password: String?)
: Call<User>?
}