Skip to content

Instantly share code, notes, and snippets.

@ikhlaqmalik13
Created November 19, 2022 11:05
Show Gist options
  • Save ikhlaqmalik13/519c6e3fbfa96094b11190e3338c836c to your computer and use it in GitHub Desktop.
Save ikhlaqmalik13/519c6e3fbfa96094b11190e3338c836c to your computer and use it in GitHub Desktop.
- Android Quote App with MVVM
<!-
* Author Ikhlaq Yousuf Malik
* Created on 19-Nov-2022
!>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".quotes_learning.network.QuotesLearningActivity">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/srl_quotes"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/id_quote"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:padding="@dimen/_20dp"
android:text="quote"
android:textColor="@color/black"
android:textSize="32sp"
android:textStyle="italic" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout>
/*
* Author Ikhlaq Yousuf Malik
* Created on 19-Nov-2022
*/
package com.maple.kashin.core
data class GenericResponse<T>(val data: T?, val success: Boolean, val message: String?)
/*
* Author Ikhlaq Yousuf Malik
* Created on 19-Nov-2022
*/
package com.maple.kashin.quotes_learning.network
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
class QuotesAPIClasss {
private lateinit var retrofit: Retrofit
fun getClient() : Retrofit {
retrofit = Retrofit
.Builder()
.baseUrl("https://type.fit/api/")
.addConverterFactory(GsonConverterFactory.create())
.build()
return retrofit
}
}
/*
* Author Ikhlaq Yousuf Malik
* Created on 19-Nov-2022
*/
package com.maple.kashin.quotes_learning.network
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.lifecycle.ViewModelProvider
import com.maple.kashin.databinding.ActivityQuotesLearningBinding
import com.maple.kashin.quotes_learning.network.utils.QuotesLearningResponseModelItem
import com.maple.kashin.quotes_learning.network.utils.QuotesLearningViewModel
import kotlin.random.Random
class QuotesLearningActivity : AppCompatActivity() {
private lateinit var binding: ActivityQuotesLearningBinding
private lateinit var mQuotesLearningViewModel: QuotesLearningViewModel
private var mQuotes: List<QuotesLearningResponseModelItem?> = arrayListOf()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityQuotesLearningBinding.inflate(layoutInflater)
setContentView(binding.root)
mQuotesLearningViewModel = ViewModelProvider(this).get(QuotesLearningViewModel::class.java)
mQuotesLearningViewModel.getQuotes()
mQuotesLearningViewModel.getQuotesLearningLiveData().observe(this) {
if (!it.success) return@observe
if (it.data == null) return@observe
mQuotes = it.data
Log.d("DATA", it.data.toString())
}
binding.srlQuotes.setOnRefreshListener {
if (mQuotes.isNotEmpty()) {
binding.idQuote.text = mQuotes[Random.nextInt(0, mQuotes.size - 1)]?.text.toString()
}
binding.srlQuotes.isRefreshing = false
}
}
}
/*
* Author Ikhlaq Yousuf Malik
* Created on 19-Nov-2022
*/
package com.maple.kashin.quotes_learning.network.utils
import retrofit2.Call
import retrofit2.http.GET
interface QuotesLearningAPIInterface {
@GET("quotes")
fun getQuotes(): Call<List<QuotesLearningResponseModelItem?>?>
}
/*
* Author Ikhlaq Yousuf Malik
* Created on 19-Nov-2022
*/
package com.maple.kashin.quotes_learning.network.utils
import androidx.lifecycle.MutableLiveData
import com.maple.kashin.core.GenericResponse
import com.maple.kashin.quotes_learning.network.QuotesAPIClasss
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
class QuotesLearningNetworkDataSource {
fun getQuotes(
data: MutableLiveData<GenericResponse<List<QuotesLearningResponseModelItem?>?>>
) = QuotesAPIClasss().getClient().create(QuotesLearningAPIInterface::class.java)
.getQuotes()
.enqueue(object : Callback<List<QuotesLearningResponseModelItem?>?> {
override fun onResponse(
call: Call<List<QuotesLearningResponseModelItem?>?>,
response: Response<List<QuotesLearningResponseModelItem?>?>
) {
if (response.isSuccessful) {
data.value = GenericResponse<List<QuotesLearningResponseModelItem?>?>(
response.body(),
true,
null
)
} else {
data.value = GenericResponse<List<QuotesLearningResponseModelItem?>?>(
null,
false,
response.message()
)
}
}
override fun onFailure(
call: Call<List<QuotesLearningResponseModelItem?>?>,
t: Throwable
) {
data.value = GenericResponse<List<QuotesLearningResponseModelItem?>?>(
null,
false,
t.message
)
}
})
}
/*
* Author Ikhlaq Yousuf Malik
* Created on 19-Nov-2022
*/
package com.maple.kashin.quotes_learning.network.utils
import androidx.lifecycle.MutableLiveData
import com.maple.kashin.core.GenericResponse
class QuotesLearningRepository {
private var mQuotesLearningNetworkDataSource: QuotesLearningNetworkDataSource =
QuotesLearningNetworkDataSource()
fun getQuotes(
data: MutableLiveData<GenericResponse<List<QuotesLearningResponseModelItem?>?>>
) = mQuotesLearningNetworkDataSource.getQuotes(data)
}
/*
* Author Ikhlaq Yousuf Malik
* Created on 19-Nov-2022
*/
package com.maple.kashin.quotes_learning.network.utils
import android.os.Parcelable
import com.google.gson.annotations.SerializedName
import kotlinx.android.parcel.Parcelize
@Parcelize
data class QuotesLearningResponseModel(
@field:SerializedName("QuotesLearningResponseModel")
val quotesLearningResponseModel: List<QuotesLearningResponseModelItem?>? = null
) : Parcelable
@Parcelize
data class QuotesLearningResponseModelItem(
@field:SerializedName("author")
val author: String? = null,
@field:SerializedName("text")
val text: String? = null
) : Parcelable
package com.maple.kashin.quotes_learning.network.utils
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.maple.kashin.core.GenericResponse
/*
* Author Ikhlaq Yousuf Malik
* Created on 19-Nov-2022
*/
class QuotesLearningViewModel : ViewModel() {
private var mQuotesLearningRepository: QuotesLearningRepository = QuotesLearningRepository()
private val mLearningQuotesLiveData: MutableLiveData<GenericResponse<List<QuotesLearningResponseModelItem?>?>> =
MutableLiveData<GenericResponse<List<QuotesLearningResponseModelItem?>?>>()
fun getQuotesLearningLiveData(): LiveData<GenericResponse<List<QuotesLearningResponseModelItem?>?>> {
return mLearningQuotesLiveData
}
fun getQuotes() {
mQuotesLearningRepository.getQuotes(mLearningQuotesLiveData)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment