├── app
├── .gitignore
├── src
│ └── main
│ │ ├── res
│ │ ├── mipmap-hdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── values
│ │ │ ├── colors.xml
│ │ │ ├── strings.xml
│ │ │ └── styles.xml
│ │ ├── mipmap-anydpi-v26
│ │ │ ├── ic_launcher.xml
│ │ │ └── ic_launcher_round.xml
│ │ ├── layout
│ │ │ ├── activity_main.xml
│ │ │ ├── activity_holiday.xml
│ │ │ ├── item_holiday_java.xml
│ │ │ └── item_holiday_kotlin.xml
│ │ ├── drawable-v24
│ │ │ └── ic_launcher_foreground.xml
│ │ └── drawable
│ │ │ └── ic_launcher_background.xml
│ │ ├── java
│ │ └── com
│ │ │ └── github
│ │ │ └── easymvvm
│ │ │ ├── kotlin
│ │ │ ├── HolidayModelKotlin.kt
│ │ │ ├── HolidayViewModelKotlin.kt
│ │ │ ├── HolidayAdapterKotlin.kt
│ │ │ ├── HolidayRepoKotlin.kt
│ │ │ └── HolidayActivityKotlin.kt
│ │ │ ├── commons
│ │ │ ├── Constants.java
│ │ │ ├── ApiInterface.java
│ │ │ └── MyApplication.java
│ │ │ ├── java
│ │ │ ├── HolidayModel.java
│ │ │ ├── HolidayViewModel.java
│ │ │ ├── HolidayRepo.java
│ │ │ ├── HolidayAdapter.java
│ │ │ └── HolidayActivity.java
│ │ │ └── MainActivity.java
│ │ └── AndroidManifest.xml
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── README.md
├── .gitignore
├── gradle
└── wrapper
│ └── gradle-wrapper.properties
└── gradle.properties
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 | rootProject.name='Easy MVVM'
3 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kashifo/EasyMVVM/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kashifo/EasyMVVM/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kashifo/EasyMVVM/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kashifo/EasyMVVM/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kashifo/EasyMVVM/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kashifo/EasyMVVM/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kashifo/EasyMVVM/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kashifo/EasyMVVM/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kashifo/EasyMVVM/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kashifo/EasyMVVM/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # EasyMVVM
2 | The easiest guide to getting started with MVVM in Android instantly.
3 |
4 | This project contains source-code for the medium article
5 | https://medium.com/@kashifo/4-steps-to-mvvm-in-android-java-b05fb4148523
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #008577
4 | #00574B
5 | #D81B60
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Easy MVVM
3 | HolidayActivityJava
4 | HolidayActivityKotlin
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/easymvvm/kotlin/HolidayModelKotlin.kt:
--------------------------------------------------------------------------------
1 | package com.github.easymvvm.kotlin
2 |
3 | /**
4 | * Created by Kashif on 10/23/2019.
5 | */
6 | class HolidayModelKotlin{
7 |
8 | var date:String? = null
9 | var name:String? = null
10 |
11 | }
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
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 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/easymvvm/commons/Constants.java:
--------------------------------------------------------------------------------
1 | package com.github.easymvvm.commons;
2 |
3 | /**
4 | * Created by Kashif on 9/27/2019.
5 | */
6 | public class Constants {
7 |
8 | public static String BASE_URL = "https://date.nager.at/api/v2/";
9 |
10 | }
11 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sat Oct 19 20:37:35 IST 2019
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/easymvvm/java/HolidayModel.java:
--------------------------------------------------------------------------------
1 | package com.github.easymvvm.java;
2 |
3 | /**
4 | * Created by Kashif on 10/19/2019.
5 | */
6 | public class HolidayModel {
7 |
8 | private String date;
9 | private String name;
10 |
11 | public String getDate() {
12 | return date;
13 | }
14 |
15 | public void setDate(String date) {
16 | this.date = date;
17 | }
18 |
19 | public String getName() {
20 | return name;
21 | }
22 |
23 | public void setName(String name) {
24 | this.name = name;
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/easymvvm/commons/ApiInterface.java:
--------------------------------------------------------------------------------
1 | package com.github.easymvvm.commons;
2 |
3 | import com.github.easymvvm.java.HolidayModel;
4 | import com.github.easymvvm.kotlin.HolidayModelKotlin;
5 |
6 | import java.util.List;
7 |
8 | import retrofit2.Call;
9 | import retrofit2.http.GET;
10 |
11 | /**
12 | * Created by Kashif on 9/27/2019.
13 | */
14 | public interface ApiInterface {
15 |
16 | @GET("PublicHolidays/2019/us")
17 | Call> getHolidays();
18 |
19 | @GET("PublicHolidays/2019/us")
20 | Call> getHolidaysKotlin();
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/easymvvm/kotlin/HolidayViewModelKotlin.kt:
--------------------------------------------------------------------------------
1 | package com.github.easymvvm.kotlin
2 |
3 | import androidx.lifecycle.LiveData
4 | import androidx.lifecycle.MutableLiveData
5 | import androidx.lifecycle.ViewModel
6 |
7 | /**
8 | * Created by Kashif on 10/9/2019.
9 | */
10 | class HolidayViewModelKotlin() : ViewModel() {
11 |
12 | var holidayRepoKotlin: HolidayRepoKotlin? = null
13 | var mutableLiveData: MutableLiveData>? = null
14 |
15 | init {
16 | holidayRepoKotlin = HolidayRepoKotlin()
17 | }
18 |
19 | fun getHolidays(): LiveData> {
20 | if (mutableLiveData == null) {
21 | mutableLiveData = holidayRepoKotlin!!.fetchHolidays()
22 | }
23 | return mutableLiveData!!
24 | }
25 |
26 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/github/easymvvm/java/HolidayViewModel.java:
--------------------------------------------------------------------------------
1 | package com.github.easymvvm.java;
2 |
3 | import java.util.List;
4 | import androidx.lifecycle.LiveData;
5 | import androidx.lifecycle.MutableLiveData;
6 | import androidx.lifecycle.ViewModel;
7 |
8 | /**
9 | * Created by Kashif on 9/27/2019.
10 | */
11 | public class HolidayViewModel extends ViewModel {
12 |
13 | private HolidayRepo holidayRepo;
14 | private MutableLiveData> mutableLiveData;
15 |
16 | public HolidayViewModel(){
17 | holidayRepo = new HolidayRepo();
18 | }
19 |
20 | public LiveData> getHolidays() {
21 | if(mutableLiveData==null){
22 | mutableLiveData = holidayRepo.requestHolidays();
23 | }
24 | return mutableLiveData;
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
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
22 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app's APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Automatically convert third-party libraries to use AndroidX
19 | android.enableJetifier=true
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/easymvvm/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.github.easymvvm;
2 |
3 | import androidx.appcompat.app.AppCompatActivity;
4 | import androidx.databinding.DataBindingUtil;
5 | import android.content.Intent;
6 | import android.os.Bundle;
7 | import android.view.View;
8 | import com.github.easymvvm.databinding.ActivityMainBinding;
9 | import com.github.easymvvm.java.HolidayActivity;
10 | import com.github.easymvvm.kotlin.HolidayActivityKotlin;
11 |
12 | public class MainActivity extends AppCompatActivity {
13 |
14 | @Override
15 | protected void onCreate(Bundle savedInstanceState) {
16 | super.onCreate(savedInstanceState);
17 | ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
18 |
19 | binding.btnJava.setOnClickListener(new View.OnClickListener() {
20 | @Override
21 | public void onClick(View v) {
22 | startActivity(new Intent(MainActivity.this, HolidayActivity.class));
23 | }
24 | });
25 |
26 | binding.btnKotlin.setOnClickListener(new View.OnClickListener() {
27 | @Override
28 | public void onClick(View v) {
29 | startActivity(new Intent(MainActivity.this, HolidayActivityKotlin.class));
30 | }
31 | });
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
10 |
11 |
20 |
21 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_holiday.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
8 |
9 |
19 |
20 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 | apply plugin: 'kotlin-android'
3 | apply plugin: 'kotlin-android-extensions'
4 | apply plugin: 'kotlin-kapt'
5 |
6 | android {
7 | compileSdkVersion 28
8 | defaultConfig {
9 | applicationId "com.github.easymvvm"
10 | minSdkVersion 16
11 | targetSdkVersion 28
12 | versionCode 1
13 | versionName "1.0"
14 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
15 | }
16 | buildTypes {
17 | release {
18 | minifyEnabled false
19 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
20 | }
21 | }
22 | dataBinding{
23 | enabled = true
24 | }
25 | }
26 |
27 | dependencies {
28 | implementation fileTree(dir: 'libs', include: ['*.jar'])
29 | implementation 'androidx.appcompat:appcompat:1.1.0'
30 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
31 | implementation 'com.squareup.retrofit2:retrofit:2.6.0'
32 | implementation 'com.squareup.retrofit2:converter-gson:2.6.0'
33 | implementation 'com.squareup.retrofit2:converter-scalars:2.6.0'
34 | implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0'
35 | implementation 'androidx.recyclerview:recyclerview:1.0.0'
36 | implementation 'androidx.cardview:cardview:1.0.0'
37 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
38 | }
39 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/easymvvm/kotlin/HolidayAdapterKotlin.kt:
--------------------------------------------------------------------------------
1 | package com.github.easymvvm.kotlin
2 |
3 | import android.view.LayoutInflater
4 | import android.view.ViewGroup
5 | import androidx.databinding.DataBindingUtil
6 | import androidx.databinding.library.baseAdapters.BR
7 | import androidx.recyclerview.widget.RecyclerView
8 | import com.github.easymvvm.R
9 | import com.github.easymvvm.databinding.ItemHolidayKotlinBinding
10 |
11 | /**
12 | * Created by Kashif on 10/9/2019.
13 | */
14 | class HolidayAdapterKotlin() : RecyclerView.Adapter() {
15 |
16 | var holidayList: List
17 |
18 | init {
19 | holidayList = ArrayList()
20 | }
21 |
22 | fun addData(arrList: List){
23 | this.holidayList = arrList
24 | }
25 |
26 | override fun onCreateViewHolder(parent: ViewGroup, pos: Int): ViewHolder {
27 | val binding: ItemHolidayKotlinBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.context), R.layout.item_holiday_kotlin, parent, false)
28 | return ViewHolder(binding)
29 | }
30 |
31 | class ViewHolder(val binding: ItemHolidayKotlinBinding) : RecyclerView.ViewHolder(binding.root) {
32 | fun bind(data: Any) {
33 | binding.setVariable(BR.model, data)
34 | binding.executePendingBindings()
35 | }
36 | }
37 |
38 | override fun getItemCount(): Int {
39 | return holidayList.size
40 | }
41 |
42 | override fun onBindViewHolder(holder: ViewHolder, pos: Int) {
43 | holder.bind(holidayList.get(pos))
44 | }//onBind
45 |
46 |
47 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/github/easymvvm/kotlin/HolidayRepoKotlin.kt:
--------------------------------------------------------------------------------
1 | package com.github.easymvvm.kotlin
2 |
3 | import android.util.Log
4 | import androidx.lifecycle.MutableLiveData
5 | import com.github.easymvvm.commons.ApiInterface
6 | import com.github.easymvvm.commons.MyApplication
7 | import retrofit2.Call
8 | import retrofit2.Callback
9 | import retrofit2.Response
10 |
11 | /**
12 | * Created by Kashif on 10/9/2019.
13 | */
14 | class HolidayRepoKotlin {
15 |
16 | val TAG = javaClass.simpleName
17 |
18 | fun fetchHolidays(): MutableLiveData>{
19 | var mutableList: MutableLiveData> = MutableLiveData()
20 |
21 | val apiInterface = MyApplication.getRetrofitClient().create(ApiInterface::class.java)
22 |
23 | apiInterface.getHolidaysKotlin().enqueue(object: Callback>{
24 |
25 | override fun onResponse(call: Call>, response: Response>) {
26 | Log.e(TAG, "onResponse response="+response.toString() )
27 |
28 | if(response.isSuccessful){
29 | Log.e(TAG, "onResponse response.size="+response.body()?.size )
30 |
31 | if(response.body()!=null && response.body()?.size!!>0 ) {
32 | mutableList.value = response.body()!!
33 | }
34 | }
35 | }
36 |
37 | override fun onFailure(call: Call>, t: Throwable) {
38 | Log.e(TAG, "onFailure call="+call.toString() )
39 | }
40 |
41 | })
42 |
43 | return mutableList
44 | }
45 |
46 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_holiday_java.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
13 |
14 |
19 |
20 |
25 |
26 |
33 |
34 |
35 |
36 |
37 |
38 |
41 |
42 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
18 |
19 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
33 |
34 |
39 |
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_holiday_kotlin.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
13 |
14 |
19 |
20 |
25 |
26 |
33 |
34 |
35 |
36 |
37 |
38 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/easymvvm/java/HolidayRepo.java:
--------------------------------------------------------------------------------
1 | package com.github.easymvvm.java;
2 |
3 | import android.util.Log;
4 |
5 | import com.github.easymvvm.commons.ApiInterface;
6 | import com.github.easymvvm.commons.MyApplication;
7 |
8 | import java.util.List;
9 |
10 | import androidx.lifecycle.MutableLiveData;
11 | import retrofit2.Call;
12 | import retrofit2.Callback;
13 | import retrofit2.Response;
14 |
15 | /**
16 | * Created by Kashif on 9/27/2019.
17 | */
18 | public class HolidayRepo {
19 |
20 | private final String TAG = getClass().getSimpleName();
21 |
22 | public MutableLiveData> requestHolidays() {
23 | final MutableLiveData> mutableLiveData = new MutableLiveData<>();
24 |
25 | ApiInterface apiService =
26 | MyApplication.getRetrofitClient().create(ApiInterface.class);
27 |
28 | apiService.getHolidays().enqueue(new Callback>() {
29 | @Override
30 | public void onResponse(Call> call, Response> response) {
31 | Log.e(TAG, "getCurrencyList response="+response );
32 |
33 | if (response.isSuccessful() && response.body()!=null ) {
34 | Log.e(TAG, "requestHolidays response.size="+response.body().size() );
35 | mutableLiveData.setValue(response.body());
36 | }
37 | }
38 |
39 | @Override
40 | public void onFailure(Call> call, Throwable t) {
41 | Log.e(TAG, "getProdList onFailure" + call.toString());
42 | }
43 | });
44 |
45 | return mutableLiveData;
46 | }
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/easymvvm/java/HolidayAdapter.java:
--------------------------------------------------------------------------------
1 | package com.github.easymvvm.java;
2 |
3 | import android.view.LayoutInflater;
4 | import android.view.ViewGroup;
5 | import com.github.easymvvm.R;
6 | import com.github.easymvvm.databinding.ItemHolidayJavaBinding;
7 | import org.jetbrains.annotations.NotNull;
8 | import java.util.ArrayList;
9 | import java.util.List;
10 | import androidx.databinding.DataBindingUtil;
11 | import androidx.recyclerview.widget.RecyclerView;
12 |
13 | /**
14 | * Created by Kashif on 9/30/2019.
15 | */
16 | public class HolidayAdapter extends RecyclerView.Adapter {
17 |
18 | private List holidayList;
19 |
20 | public HolidayAdapter() {
21 | holidayList = new ArrayList<>();
22 | }
23 |
24 | public void addHolidayList(List currencyList) {
25 | this.holidayList = currencyList;
26 | }
27 |
28 | @Override
29 | public int getItemCount() {
30 | return holidayList != null ? holidayList.size() : 0;
31 | }
32 |
33 | @NotNull
34 | @Override
35 | public MyViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
36 | ItemHolidayJavaBinding binding = DataBindingUtil.inflate(
37 | LayoutInflater.from(parent.getContext()),
38 | R.layout.item_holiday_java, parent, false);
39 |
40 | return new MyViewHolder(binding);
41 | }
42 |
43 | static class MyViewHolder extends RecyclerView.ViewHolder {
44 | private ItemHolidayJavaBinding binding;
45 |
46 | MyViewHolder(ItemHolidayJavaBinding binding) {
47 | super(binding.getRoot());
48 | this.binding = binding;
49 | }
50 | }
51 |
52 | @Override
53 | public void onBindViewHolder(final MyViewHolder holder, final int position) {
54 | holder.binding.setModel( holidayList.get(position) );
55 | }
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/easymvvm/commons/MyApplication.java:
--------------------------------------------------------------------------------
1 | package com.github.easymvvm.commons;
2 |
3 | import android.app.Application;
4 | import android.content.Context;
5 | import android.net.ConnectivityManager;
6 | import android.net.NetworkInfo;
7 |
8 | import retrofit2.Retrofit;
9 | import retrofit2.converter.gson.GsonConverterFactory;
10 | import retrofit2.converter.scalars.ScalarsConverterFactory;
11 |
12 | /**
13 | * Created by Kashif on 9/27/2019.
14 | */
15 | public class MyApplication extends Application {
16 |
17 | final String TAG = getClass().getSimpleName();
18 | private static MyApplication mInstance;
19 | private static Retrofit retrofit = null;
20 |
21 | @Override
22 | public void onCreate() {
23 | super.onCreate();
24 | mInstance = this;
25 | }
26 |
27 | public static synchronized MyApplication getInstance() {
28 | return mInstance;
29 | }
30 |
31 | public boolean isNetworkAvailable() {
32 | ConnectivityManager connectivityManager
33 | = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
34 | NetworkInfo activeNetworkInfo = null;
35 | if (connectivityManager != null) {
36 | activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
37 | }
38 | return activeNetworkInfo != null && activeNetworkInfo.isConnected();
39 | }
40 |
41 | public static Retrofit getRetrofitClient() {
42 |
43 | if (retrofit == null) {
44 | okhttp3.OkHttpClient client = new okhttp3.OkHttpClient.Builder().build();
45 |
46 | retrofit = new Retrofit.Builder()
47 | .client(client)
48 | .addConverterFactory(ScalarsConverterFactory.create())
49 | .addConverterFactory(GsonConverterFactory.create())
50 | .baseUrl(Constants.BASE_URL)
51 | .build();
52 | }
53 | return retrofit;
54 | }
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/easymvvm/kotlin/HolidayActivityKotlin.kt:
--------------------------------------------------------------------------------
1 | package com.github.easymvvm.kotlin
2 |
3 | import android.os.Bundle
4 | import android.util.Log
5 | import android.view.View
6 | import android.widget.Toast
7 | import androidx.appcompat.app.AppCompatActivity
8 | import androidx.lifecycle.Observer
9 | import androidx.recyclerview.widget.DefaultItemAnimator
10 | import androidx.recyclerview.widget.LinearLayoutManager
11 | import com.github.easymvvm.R
12 | import com.github.easymvvm.commons.MyApplication
13 | import kotlinx.android.synthetic.main.activity_holiday.*
14 |
15 | class HolidayActivityKotlin : AppCompatActivity() {
16 |
17 | val TAG = javaClass.simpleName
18 | lateinit var holidayAdapterKotlin: HolidayAdapterKotlin
19 |
20 | override fun onCreate(savedInstanceState: Bundle?) {
21 | super.onCreate(savedInstanceState)
22 | setContentView(R.layout.activity_holiday)
23 | initUI()
24 |
25 | if(MyApplication.getInstance().isNetworkAvailable){
26 | progressBar.visibility = View.VISIBLE
27 |
28 | val holidayViewModelKotlin = HolidayViewModelKotlin()
29 |
30 | holidayViewModelKotlin.getHolidays().observe(this, object: Observer> {
31 |
32 | override fun onChanged(t: List?) {
33 | Log.e(TAG, "observe onChanged()="+t?.size )
34 | progressBar.setVisibility(View.GONE)
35 | holidayAdapterKotlin.addData(t!!)
36 | holidayAdapterKotlin.notifyDataSetChanged()
37 | }
38 |
39 | })
40 |
41 | }else{
42 | Toast.makeText(this, "No Network Available", Toast.LENGTH_LONG).show()
43 | }
44 |
45 | }
46 |
47 | fun initUI(){
48 |
49 | rvHolidayList.setHasFixedSize(true);
50 | val layoutManager = LinearLayoutManager(this)
51 | rvHolidayList.setLayoutManager(layoutManager)
52 | rvHolidayList.setItemAnimator(DefaultItemAnimator())
53 |
54 | holidayAdapterKotlin = HolidayAdapterKotlin()
55 | rvHolidayList.adapter = holidayAdapterKotlin
56 |
57 | }
58 |
59 | }
60 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/easymvvm/java/HolidayActivity.java:
--------------------------------------------------------------------------------
1 | package com.github.easymvvm.java;
2 |
3 | import android.os.Bundle;
4 | import android.util.Log;
5 | import android.view.View;
6 | import android.widget.Toast;
7 | import com.github.easymvvm.R;
8 | import com.github.easymvvm.commons.MyApplication;
9 | import com.github.easymvvm.databinding.ActivityHolidayBinding;
10 | import java.util.List;
11 | import androidx.appcompat.app.AppCompatActivity;
12 | import androidx.databinding.DataBindingUtil;
13 | import androidx.lifecycle.Observer;
14 | import androidx.recyclerview.widget.LinearLayoutManager;
15 |
16 | public class HolidayActivity extends AppCompatActivity {
17 |
18 | final String TAG = getClass().getSimpleName();
19 | ActivityHolidayBinding binding;
20 | HolidayAdapter adapter;
21 |
22 | @Override
23 | protected void onCreate(Bundle savedInstanceState) {
24 | super.onCreate(savedInstanceState);
25 | binding = DataBindingUtil.setContentView(this, R.layout.activity_holiday);
26 | initUI();
27 |
28 | if(MyApplication.getInstance().isNetworkAvailable()) {
29 | binding.progressBar.setVisibility(View.VISIBLE);
30 |
31 | HolidayViewModel holidayViewModel = new HolidayViewModel();
32 | holidayViewModel.getHolidays().observe(this, new Observer>() {
33 | @Override
34 | public void onChanged(List currencyPojos) {
35 | if (currencyPojos != null && !currencyPojos.isEmpty()) {
36 | Log.e(TAG, "observe onChanged()=" + currencyPojos.size());
37 | binding.progressBar.setVisibility(View.GONE);
38 | adapter.addHolidayList(currencyPojos);
39 | adapter.notifyDataSetChanged();
40 | }
41 | }
42 | });
43 |
44 | }else{
45 | Toast.makeText(this, "No Network Available", Toast.LENGTH_LONG).show();
46 | }
47 | }
48 |
49 | void initUI(){
50 | binding.rvHolidayList.setHasFixedSize(true);
51 | binding.rvHolidayList.setLayoutManager(new LinearLayoutManager(this));
52 |
53 | adapter = new HolidayAdapter();
54 | binding.rvHolidayList.setAdapter(adapter);
55 | }
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------