-
-
Save anandrex5/063669a5ed8dde6a46489061fa1d602c to your computer and use it in GitHub Desktop.
Retrofit with MVVM (Get Api)
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"?> | |
<layout | |
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" | |
> | |
<androidx.constraintlayout.widget.ConstraintLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".view.Activity.TestActivity"> | |
<androidx.recyclerview.widget.RecyclerView | |
android:id="@+id/recyclerView" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
/> | |
</androidx.constraintlayout.widget.ConstraintLayout> | |
</layout> |
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
package com.example.application.network | |
import retrofit2.Retrofit | |
import retrofit2.converter.gson.GsonConverterFactory | |
object ApiClient { | |
val BASE_URL: String = "https://jsonplaceholder.typicode.com/" | |
var retrofit: Retrofit? = null | |
fun getApiData(): Retrofit? { | |
if (retrofit == null) { | |
val interceptor = HttpLoggingInterceptor() | |
interceptor.level = HttpLoggingInterceptor.Level.BODY | |
val builder = OkHttpClient.Builder() | |
.addInterceptor(interceptor) | |
retrofit = Retrofit.Builder() | |
.baseUrl(BASE_URL) | |
.client(builder.build()) | |
.addConverterFactory(GsonConverterFactory.create()) | |
.build() | |
} | |
return retrofit | |
} | |
} |
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
package com.example.application.network | |
import com.example.testapi.model.dataModel.PojoItem | |
import retrofit2.Call | |
import retrofit2.http.GET | |
interface ApiInterface { | |
@GET("/posts") | |
fun getData(): Call<List<PojoItem>> | |
} |
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"?> | |
<layout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
> | |
<androidx.constraintlayout.widget.ConstraintLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
> | |
<androidx.cardview.widget.CardView | |
android:layout_width="match_parent" | |
android:layout_height="170dp" | |
android:layout_margin="10dp" | |
app:cardElevation="5dp" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" | |
> | |
<androidx.constraintlayout.widget.ConstraintLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
> | |
<androidx.appcompat.widget.AppCompatTextView | |
android:id="@+id/post_Id" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginStart="20dp" | |
android:layout_marginTop="5dp" | |
android:text="Id" | |
android:textSize="18dp" | |
android:textStyle="bold" | |
android:textColor="#6C0808" | |
app:layout_constraintTop_toTopOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
/> | |
<androidx.appcompat.widget.AppCompatTextView | |
android:id="@+id/post_title" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="5dp" | |
android:text="Title" | |
android:textSize="16dp" | |
android:textStyle="bold" | |
android:textColor="#051C6E" | |
app:layout_constraintTop_toBottomOf="@id/post_Id" | |
app:layout_constraintStart_toStartOf="@id/post_Id" | |
/> | |
<androidx.appcompat.widget.AppCompatTextView | |
android:id="@+id/post" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="5dp" | |
android:text="Post" | |
android:textSize="15dp" | |
android:textColor="#000" | |
app:layout_constraintTop_toBottomOf="@id/post_title" | |
app:layout_constraintStart_toStartOf="@id/post_title" | |
/> | |
</androidx.constraintlayout.widget.ConstraintLayout> | |
</androidx.cardview.widget.CardView> | |
</androidx.constraintlayout.widget.ConstraintLayout> | |
</layout> |
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
package com.example.testapi.model.dataModel | |
import android.os.Parcelable | |
import com.google.gson.annotations.SerializedName | |
import kotlinx.android.parcel.Parcelize | |
@Parcelize | |
data class PojoItem( | |
@SerializedName("id") | |
val id: String, | |
@SerializedName("body") | |
val body: String, | |
@SerializedName("title") | |
val title: String | |
):Parcelable |
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
package com.example.testapi.model.repository | |
import com.example.application.network.ApiInterface | |
class PostRepository( private val apiInterface: ApiInterface) { | |
fun getPost() = apiInterface.getData() | |
} |
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
package com.example.testapi.view.Activity | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import android.util.Log | |
import androidx.databinding.DataBindingUtil | |
import androidx.lifecycle.Observer | |
import androidx.lifecycle.ViewModelProvider | |
import androidx.lifecycle.observe | |
import androidx.recyclerview.widget.LinearLayoutManager | |
import com.example.application.network.ApiInterface | |
import com.example.application.network.ApiClient | |
import com.example.testapi.R | |
import com.example.testapi.databinding.ActivityTestBinding | |
import com.example.testapi.model.dataModel.PojoItem | |
import com.example.testapi.model.repository.PostRepository | |
import com.example.testapi.view.adapter.TestAdapter | |
import com.example.testapi.viewModel.TestViewModel | |
import com.example.testapi.viewModel.TestViewModelFactory | |
class TestActivity : AppCompatActivity() { | |
lateinit var binding: ActivityTestBinding | |
lateinit var viewModel: TestViewModel | |
var list: List<PojoItem> = ArrayList() | |
val adapter = TestAdapter(list) | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
binding = DataBindingUtil.setContentView(this, R.layout.activity_test) | |
val apiInterface = ApiClient.getApiData()?.create(ApiInterface::class.java) | |
val repository = PostRepository(apiInterface!!) | |
viewModel = ViewModelProvider(this, TestViewModelFactory(repository)).get(TestViewModel::class.java) | |
binding.recyclerView.adapter = adapter | |
binding.recyclerView.layoutManager = LinearLayoutManager(this) | |
viewModel.postList.observe(this, Observer { | |
Log.d("TAG", "movieList: $it") | |
adapter.setData(it) | |
}) | |
viewModel.getAllData() | |
} | |
} |
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
package com.example.testapi.view.adapter | |
import android.view.LayoutInflater | |
import android.view.ViewGroup | |
import androidx.databinding.DataBindingUtil | |
import androidx.recyclerview.widget.RecyclerView | |
import com.example.testapi.R | |
import com.example.testapi.databinding.DataItemBinding | |
import com.example.testapi.model.dataModel.PojoItem | |
class TestAdapter (var list: List<PojoItem>): RecyclerView.Adapter<TestAdapter.ViewHolder>() { | |
var dataBinding:DataItemBinding? = null | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | |
dataBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.context), | |
R.layout.data_item,parent ,false) | |
return TestAdapter.ViewHolder(dataBinding!!) | |
} | |
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | |
val item: PojoItem = list[position] | |
holder.bindData(item) | |
} | |
override fun getItemCount(): Int { | |
return list.size | |
} | |
fun setData(data: List<PojoItem>){ | |
this.list = data | |
notifyDataSetChanged() | |
} | |
class ViewHolder (val binding: DataItemBinding) : RecyclerView.ViewHolder(binding.root){ | |
fun bindData(pojoItem: PojoItem) { | |
binding.postTitle.text = pojoItem.title | |
binding.post.text = pojoItem.body | |
binding.postId.text = pojoItem.id | |
} | |
} | |
} |
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
package com.example.testapi.viewModel | |
import android.util.Log | |
import androidx.lifecycle.MutableLiveData | |
import androidx.lifecycle.ViewModel | |
import com.example.testapi.model.dataModel.PojoItem | |
import com.example.testapi.model.repository.PostRepository | |
import retrofit2.Call | |
import retrofit2.Callback | |
import retrofit2.Response | |
class TestViewModel(val repository: PostRepository): ViewModel() { | |
val postList = MutableLiveData<List<PojoItem>>() | |
// val list = PojoItem(id = "101", title = "Data", body = "TechTree IT") | |
fun getAllData(){ | |
val response = repository.getPost() | |
response.enqueue(object : Callback<List<PojoItem>> { | |
override fun onResponse(call: Call<List<PojoItem>>, | |
response: Response<List<PojoItem>>,){ | |
postList.postValue(response.body()) //?.plus(list) ?: listOf(list) ) | |
} | |
override fun onFailure(call: Call<List<PojoItem>>, t: Throwable) { | |
Log.e("Fail", "${t.message}") | |
} | |
}) | |
} | |
} |
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
package com.example.testapi.viewModel | |
import androidx.lifecycle.ViewModel | |
import androidx.lifecycle.ViewModelProvider | |
import com.example.testapi.model.repository.PostRepository | |
class TestViewModelFactory(private val repository: PostRepository): ViewModelProvider.Factory { | |
override fun <T : ViewModel?> create(modelClass: Class<T>): T { | |
return TestViewModel(repository) as T | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
//get api impimentation