Last active
January 15, 2023 12:44
-
-
Save hamurcuabi/fe3ccd9840e0429e049f27e096a1fa4e to your computer and use it in GitHub Desktop.
Adım Adım Retrofit - https://www.youtube.com/watch?v=kETK1hS68BQ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context="com.example.retrofit.MainActivity"> | |
<TextView | |
android:id="@+id/txtPost" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_marginHorizontal="32dp" | |
android:textSize="24sp" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<TextView | |
android:id="@+id/txtError" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Bir hata oluştu" | |
android:visibility="gone" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<ProgressBar | |
android:id="@+id/progress" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ktx | |
implementation "androidx.activity:activity-ktx:1.6.1" | |
// Retrofit | |
implementation 'com.squareup.retrofit2:retrofit:2.9.0' | |
implementation 'com.squareup.retrofit2:converter-gson:2.9.0' | |
// Coroutines | |
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.6' | |
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4' | |
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Bundle | |
import android.widget.ProgressBar | |
import android.widget.TextView | |
import androidx.activity.viewModels | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.core.view.isVisible | |
class MainActivity : AppCompatActivity() { | |
private val mainViewModel: MainViewModel by viewModels() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
mainViewModel.post.observe(this) { myPost -> | |
findViewById<TextView>(R.id.txtPost).text = myPost.title | |
} | |
mainViewModel.isLoading.observe(this) { isLoading -> | |
findViewById<ProgressBar>(R.id.progress).isVisible = isLoading | |
} | |
mainViewModel.hasError.observe(this) { hasError -> | |
findViewById<TextView>(R.id.txtError).isVisible = hasError | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.lifecycle.LiveData | |
import androidx.lifecycle.MutableLiveData | |
import androidx.lifecycle.ViewModel | |
import androidx.lifecycle.viewModelScope | |
import kotlinx.coroutines.delay | |
import kotlinx.coroutines.launch | |
class MainViewModel : ViewModel() { | |
private val _post = MutableLiveData<MyPost>() | |
val post: LiveData<MyPost> | |
get() = _post | |
private val _isLoading = MutableLiveData<Boolean>() | |
val isLoading: LiveData<Boolean> | |
get() = _isLoading | |
private val _hasError = MutableLiveData<Boolean>() | |
val hasError: LiveData<Boolean> | |
get() = _hasError | |
init { | |
fetchPost() | |
} | |
fun fetchPost() { | |
viewModelScope.launch { | |
_isLoading.value = true | |
delay(2000) | |
val response = RetrofitInstance.api.fetchPost() | |
if (response.isSuccessful) { | |
response.body()?.let { post -> | |
_post.value = post | |
_hasError.value = false | |
} ?: run { | |
_hasError.value = true | |
} | |
} else { | |
_hasError.value = true | |
} | |
_isLoading.value = false | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import retrofit2.Retrofit | |
import retrofit2.converter.gson.GsonConverterFactory | |
object RetrofitInstance { | |
//https://jsonplaceholder.typicode.com/posts/1 | |
private val retrofit by lazy { | |
Retrofit.Builder() | |
.baseUrl("https://jsonplaceholder.typicode.com") | |
.addConverterFactory(GsonConverterFactory.create()) | |
.build() | |
} | |
val api: SimpleApi by lazy { | |
retrofit.create(SimpleApi::class.java) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import retrofit2.Response | |
import retrofit2.http.GET | |
interface SimpleApi { | |
@GET("posts/1") | |
suspend fun fetchPost(): Response<MyPost> | |
} | |
data class MyPost( | |
val title: String, | |
val body: String | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment