Skip to content

Commit 1cb810a

Browse files
added mvvm example
1 parent 44abd61 commit 1cb810a

41 files changed

Lines changed: 937 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx
15+
local.properties
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
plugins {
2+
id 'com.android.application'
3+
id 'kotlin-android'
4+
id 'kotlin-kapt'
5+
6+
}
7+
8+
android {
9+
compileSdkVersion 30
10+
buildToolsVersion "30.0.2"
11+
12+
defaultConfig {
13+
applicationId "com.velmurugan.mvvmretrofitrecyclerviewkotlin"
14+
minSdkVersion 24
15+
targetSdkVersion 30
16+
versionCode 1
17+
versionName "1.0"
18+
19+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
20+
}
21+
22+
buildTypes {
23+
release {
24+
minifyEnabled false
25+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
26+
}
27+
}
28+
compileOptions {
29+
sourceCompatibility JavaVersion.VERSION_1_8
30+
targetCompatibility JavaVersion.VERSION_1_8
31+
}
32+
kotlinOptions {
33+
jvmTarget = '1.8'
34+
}
35+
36+
buildFeatures {
37+
viewBinding = true
38+
}
39+
}
40+
41+
dependencies {
42+
43+
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
44+
implementation 'androidx.core:core-ktx:1.3.2'
45+
implementation 'androidx.appcompat:appcompat:1.2.0'
46+
implementation 'com.google.android.material:material:1.3.0'
47+
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
48+
testImplementation 'junit:junit:4.+'
49+
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
50+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
51+
//ViewModel and livedata
52+
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
53+
//Retrofit
54+
implementation 'com.google.code.gson:gson:2.8.6'
55+
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
56+
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
57+
//Glide
58+
implementation 'com.github.bumptech.glide:glide:4.12.0'
59+
kapt 'com.github.bumptech.glide:compiler:4.12.0'
60+
61+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.velmurugan.mvvmretrofitrecyclerviewkotlin
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
import org.junit.Test
6+
import org.junit.runner.RunWith
7+
import org.junit.Assert.*
8+
9+
/**
10+
* Instrumented test, which will execute on an Android device.
11+
*
12+
* See [testing documentation](http://d.android.com/tools/testing).
13+
*/
14+
@RunWith(AndroidJUnit4::class)
15+
class ExampleInstrumentedTest {
16+
@Test
17+
fun useAppContext() {
18+
// Context of the app under test.
19+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
20+
assertEquals("com.velmurugan.mvvmretrofitrecyclerviewkotlin", appContext.packageName)
21+
}
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.velmurugan.mvvmretrofitrecyclerviewkotlin">
4+
5+
<uses-permission android:name="android.permission.INTERNET"/>
6+
7+
<application
8+
android:allowBackup="true"
9+
android:icon="@mipmap/ic_launcher"
10+
android:label="@string/app_name"
11+
android:roundIcon="@mipmap/ic_launcher_round"
12+
android:supportsRtl="true"
13+
android:usesCleartextTraffic="true"
14+
android:theme="@style/Theme.MvvmRetrofitRecyclerviewKotlin">
15+
<activity android:name=".MainActivity">
16+
<intent-filter>
17+
<action android:name="android.intent.action.MAIN" />
18+
19+
<category android:name="android.intent.category.LAUNCHER" />
20+
</intent-filter>
21+
</activity>
22+
</application>
23+
24+
</manifest>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.velmurugan.mvvmretrofitrecyclerviewkotlin
2+
3+
import androidx.appcompat.app.AppCompatActivity
4+
import android.os.Bundle
5+
import android.util.Log
6+
import androidx.lifecycle.Observer
7+
import androidx.lifecycle.ViewModelProvider
8+
import androidx.lifecycle.ViewModelProviders
9+
import com.velmurugan.mvvmretrofitrecyclerviewkotlin.databinding.ActivityMainBinding
10+
11+
class MainActivity : AppCompatActivity() {
12+
private val TAG = "MainActivity"
13+
private lateinit var binding: ActivityMainBinding
14+
15+
lateinit var viewModel: MainViewModel
16+
17+
private val retrofitService = RetrofitService.getInstance()
18+
val adapter = MainAdapter()
19+
20+
override fun onCreate(savedInstanceState: Bundle?) {
21+
super.onCreate(savedInstanceState)
22+
setContentView(R.layout.activity_main)
23+
binding = ActivityMainBinding.inflate(layoutInflater)
24+
setContentView(binding.root)
25+
26+
viewModel = ViewModelProvider(this, MyViewModelFactory(MainRepository(retrofitService))).get(MainViewModel::class.java)
27+
28+
binding.recyclerview.adapter = adapter
29+
30+
viewModel.movieList.observe(this, Observer {
31+
Log.d(TAG, "onCreate: $it")
32+
adapter.setMovieList(it)
33+
})
34+
35+
viewModel.errorMessage.observe(this, Observer {
36+
37+
})
38+
viewModel.getAllMovies()
39+
}
40+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.velmurugan.mvvmretrofitrecyclerviewkotlin
2+
3+
import android.view.LayoutInflater
4+
import android.view.ViewGroup
5+
import androidx.recyclerview.widget.RecyclerView
6+
import com.bumptech.glide.Glide
7+
import com.velmurugan.mvvmretrofitrecyclerviewkotlin.databinding.AdapterMovieBinding
8+
9+
class MainAdapter: RecyclerView.Adapter<MainViewHolder>() {
10+
11+
var movies = mutableListOf<Movie>()
12+
13+
fun setMovieList(movies: List<Movie>) {
14+
this.movies = movies.toMutableList()
15+
notifyDataSetChanged()
16+
}
17+
18+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainViewHolder {
19+
val inflater = LayoutInflater.from(parent.context)
20+
21+
val binding = AdapterMovieBinding.inflate(inflater, parent, false)
22+
return MainViewHolder(binding)
23+
}
24+
25+
override fun onBindViewHolder(holder: MainViewHolder, position: Int) {
26+
val movie = movies[position]
27+
holder.binding.name.text = movie.name
28+
Glide.with(holder.itemView.context).load(movie.imageUrl).into(holder.binding.imageview)
29+
30+
}
31+
32+
override fun getItemCount(): Int {
33+
return movies.size
34+
}
35+
}
36+
37+
class MainViewHolder(val binding: AdapterMovieBinding) : RecyclerView.ViewHolder(binding.root) {
38+
39+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.velmurugan.mvvmretrofitrecyclerviewkotlin
2+
3+
import retrofit2.Response
4+
5+
class MainRepository constructor(private val retrofitService: RetrofitService) {
6+
7+
fun getAllMovies() = retrofitService.getAllMovies()
8+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.velmurugan.mvvmretrofitrecyclerviewkotlin
2+
3+
import androidx.lifecycle.MutableLiveData
4+
import androidx.lifecycle.ViewModel
5+
import retrofit2.Call
6+
import retrofit2.Callback
7+
import retrofit2.Response
8+
9+
class MainViewModel constructor(private val repository: MainRepository) : ViewModel() {
10+
11+
val movieList = MutableLiveData<List<Movie>>()
12+
val errorMessage = MutableLiveData<String>()
13+
14+
fun getAllMovies() {
15+
16+
val response = repository.getAllMovies()
17+
response.enqueue(object : Callback<List<Movie>> {
18+
override fun onResponse(call: Call<List<Movie>>, response: Response<List<Movie>>) {
19+
movieList.postValue(response.body())
20+
}
21+
22+
override fun onFailure(call: Call<List<Movie>>, t: Throwable) {
23+
errorMessage.postValue(t.message)
24+
}
25+
})
26+
}
27+
}

0 commit comments

Comments
 (0)