├── .gitignore
├── .idea
├── gradle.xml
├── inspectionProfiles
│ └── Project_Default.xml
├── misc.xml
├── modules.xml
├── runConfigurations.xml
└── vcs.xml
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── gauravgoyal
│ │ └── mvvm_with_testing
│ │ ├── ExampleInstrumentedTest.kt
│ │ ├── MainActivityJavaTest.java
│ │ ├── UnitTestSuite.java
│ │ └── view
│ │ └── ui
│ │ └── MainActivityTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── gauravgoyal
│ │ │ └── mvvm_with_testing
│ │ │ ├── lifecycle
│ │ │ └── SomeObserver.java
│ │ │ ├── service
│ │ │ ├── model
│ │ │ │ ├── Article.kt
│ │ │ │ ├── News.kt
│ │ │ │ └── Source.kt
│ │ │ └── repository
│ │ │ │ ├── NewsRepository.java
│ │ │ │ └── NewsService.java
│ │ │ ├── utility
│ │ │ ├── Constants.kt
│ │ │ └── DateUtils.kt
│ │ │ ├── view
│ │ │ ├── adapter
│ │ │ │ ├── CustomBindingAdapter.java
│ │ │ │ └── NewsAdapter.java
│ │ │ ├── callback
│ │ │ │ └── OnClickCallback.java
│ │ │ └── ui
│ │ │ │ ├── ArticleListFragment.java
│ │ │ │ ├── MainActivity.java
│ │ │ │ └── NewsDetailActivity.java
│ │ │ └── viewmodel
│ │ │ └── NewsViewModel.java
│ └── res
│ │ ├── drawable-v24
│ │ └── ic_launcher_foreground.xml
│ │ ├── drawable
│ │ └── ic_launcher_background.xml
│ │ ├── layout
│ │ ├── activity_main.xml
│ │ ├── activity_news_detail.xml
│ │ ├── fragment_news_list.xml
│ │ └── news_list_item.xml
│ │ ├── mipmap-anydpi-v26
│ │ ├── ic_launcher.xml
│ │ └── ic_launcher_round.xml
│ │ ├── 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
│ │ ├── dimens.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── com
│ └── gauravgoyal
│ └── mvvm_with_testing
│ ├── ExampleUnitTest.kt
│ └── UnitTest.kt
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
17 |
18 |
--------------------------------------------------------------------------------
/.idea/inspectionProfiles/Project_Default.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 | 1.8
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | A sample project which illustrate uses of MVVM Architecture and LiveData,ViewModel Architecture components.
2 | It displays list of news in a Recycler view and on click of any news, news will be opened in a WebView.
3 | https://android.jlelse.eu/android-architecture-pattern-components-mvvm-livedata-viewmodel-lifecycle-544e84e85177?source=friends_link&sk=a009475da1cf62720891f4526eb8ec05
4 |
5 |
6 | 
7 | 
8 | 
9 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | project.ext {
2 | appcompat = "26.1.0"
3 | arch = "1.0.0"
4 | retrofit = "2.0.2"
5 | constraintLayout = "1.0.2"
6 | }
7 |
8 | def NEWS_API_KEY = '"' + NewsApiKey + '"'
9 |
10 | apply plugin: 'com.android.application'
11 |
12 | apply plugin: 'kotlin-android'
13 |
14 | apply plugin: 'kotlin-android-extensions'
15 |
16 | android {
17 | compileSdkVersion 26
18 | defaultConfig {
19 | applicationId "com.gauravgoyal.mvvm_with_testing"
20 | minSdkVersion 16
21 | targetSdkVersion 26
22 | versionCode 1
23 | versionName "1.0"
24 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
25 | buildConfigField "String", "NewsApiKey", NEWS_API_KEY
26 | }
27 | buildTypes {
28 | release {
29 | minifyEnabled false
30 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
31 | }
32 | }
33 | dataBinding {
34 | enabled = true
35 | }
36 | }
37 |
38 | dependencies {
39 | implementation fileTree(dir: 'libs', include: ['*.jar'])
40 |
41 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
42 |
43 | // testing libraries
44 | testImplementation 'junit:junit:4.12'
45 | // androidTestCompile 'com.android.support:support-annotations:25.3.0'
46 | androidTestImplementation 'com.android.support.test:runner:1.0.1'
47 | androidTestCompile('com.android.support.test.espresso:espresso-core:3.0.1', {
48 | exclude group: 'com.android.support', module: 'support-annotations'
49 | })
50 | androidTestCompile 'com.android.support.test:rules:0.5'
51 | androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
52 | androidTestCompile 'org.mockito:mockito-core:2.6.8'
53 | // androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
54 |
55 | // views
56 | implementation "com.android.support:cardview-v7:$project.appcompat"
57 | implementation "com.android.support:recyclerview-v7:$project.appcompat"
58 | implementation "com.android.support.constraint:constraint-layout:$project.constraintLayout"
59 |
60 | // lifecycles
61 | implementation "android.arch.lifecycle:runtime:$project.arch"
62 | implementation "android.arch.lifecycle:extensions:$project.arch"
63 | annotationProcessor "android.arch.lifecycle:compiler:$project.arch"
64 |
65 | //networking
66 | implementation "com.squareup.retrofit2:retrofit:$project.retrofit"
67 | implementation "com.squareup.retrofit2:converter-gson:$project.retrofit"
68 |
69 | //support
70 | implementation "com.android.support:appcompat-v7:$project.appcompat"
71 | compile "com.android.support:support-v4:$project.appcompat"
72 | compile 'com.android.support:design:25.3.1', {
73 | exclude module: 'support-annotations'
74 | }
75 |
76 | androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2', {
77 | exclude group: 'com.android.support', module: 'support-annotations'
78 | exclude group: 'com.android.support', module: 'support-v4'
79 | exclude group: 'com.android.support', module: 'design'
80 | exclude group: 'com.android.support', module: 'recyclerview-v7'
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/gauravgoyal/mvvm_with_testing/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | package com.gauravgoyal.mvvm_with_testing
2 |
3 | import android.support.test.InstrumentationRegistry
4 | import android.support.test.runner.AndroidJUnit4
5 |
6 | import org.junit.Test
7 | import org.junit.runner.RunWith
8 |
9 | import org.junit.Assert.*
10 |
11 | /**
12 | * Instrumented test, which will execute on an Android device.
13 | *
14 | * See [testing documentation](http://d.android.com/tools/testing).
15 | */
16 | @RunWith(AndroidJUnit4::class)
17 | class ExampleInstrumentedTest {
18 | @Test
19 | fun useAppContext() {
20 | // Context of the app under test.
21 | val appContext = InstrumentationRegistry.getTargetContext()
22 | assertEquals("com.gauravgoyal.mvvm_with_testing", appContext.packageName)
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/gauravgoyal/mvvm_with_testing/MainActivityJavaTest.java:
--------------------------------------------------------------------------------
1 | package com.gauravgoyal.mvvm_with_testing;
2 |
3 | import android.arch.lifecycle.ViewModelProviders;
4 | import android.support.test.filters.MediumTest;
5 | import android.support.test.rule.ActivityTestRule;
6 | import android.support.test.runner.AndroidJUnit4;
7 | import android.support.v4.app.Fragment;
8 | import android.support.v7.widget.RecyclerView;
9 | import android.view.View;
10 | import android.widget.FrameLayout;
11 |
12 | import com.gauravgoyal.mvvm_with_testing.view.adapter.NewsAdapter;
13 | import com.gauravgoyal.mvvm_with_testing.view.ui.ArticleListFragment;
14 | import com.gauravgoyal.mvvm_with_testing.view.ui.MainActivity;
15 | import com.gauravgoyal.mvvm_with_testing.viewmodel.NewsViewModel;
16 |
17 | import org.junit.Rule;
18 | import org.junit.Test;
19 | import org.junit.runner.RunWith;
20 | import org.hamcrest.CoreMatchers;
21 |
22 | import java.util.List;
23 |
24 | import kotlin.jvm.JvmField;
25 |
26 | import static android.support.test.espresso.action.ViewActions.click;
27 | import static android.support.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition;
28 | import static org.hamcrest.Matchers.greaterThan;
29 | import static org.junit.Assert.assertEquals;
30 | import static org.junit.Assert.assertThat;
31 | import static org.hamcrest.Matchers.instanceOf;
32 |
33 | import org.mockito.Mock;
34 |
35 | /**
36 | * Created by gauravgoyal on 16/12/17.
37 | */
38 | @MediumTest
39 | @RunWith(AndroidJUnit4.class)
40 | public class MainActivityJavaTest {
41 |
42 | @Mock
43 | private NewsViewModel newsViewModel;
44 |
45 | @Mock
46 | private NewsAdapter newsAdapter;
47 |
48 | @Rule
49 | public ActivityTestRule rule = new ActivityTestRule<>(MainActivity.class);
50 |
51 | @Test
52 | public void ensureFrameLayoutIsPresent() throws Exception {
53 |
54 | MainActivity activity = rule.getActivity();
55 | FrameLayout viewById = activity.findViewById(R.id.fragment_container);
56 | assertThat(viewById, instanceOf(FrameLayout.class));
57 |
58 | List fragmentList = activity.getSupportFragmentManager().getFragments();
59 | assertEquals(fragmentList.size(), 1);
60 | Fragment fragment = (Fragment) fragmentList.get(0);
61 | assertThat(fragmentList.get(0), instanceOf(ArticleListFragment.class));
62 | fragment = (ArticleListFragment) fragment;
63 |
64 | // fragment testing
65 | View view = fragment.getView();
66 | View recyclerView = view.findViewById(R.id.project_list);
67 | assertThat(recyclerView, instanceOf(RecyclerView.class));
68 | assertEquals(view.findViewById(R.id.loading_projects).getVisibility(), View.VISIBLE);
69 | recyclerView = (RecyclerView) recyclerView;
70 |
71 | newsViewModel = ViewModelProviders.of(activity, new NewsViewModel.Factory(activity.getApplication()))
72 | .get(NewsViewModel.class);
73 |
74 | assertEquals(view.findViewById(R.id.loading_projects).getVisibility(), View.VISIBLE);
75 | newsAdapter = (NewsAdapter) ((RecyclerView) recyclerView).getAdapter();
76 |
77 | int count = newsAdapter.getItemCount();
78 | assertThat(count, greaterThan(0));
79 |
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/gauravgoyal/mvvm_with_testing/UnitTestSuite.java:
--------------------------------------------------------------------------------
1 | package com.gauravgoyal.mvvm_with_testing;
2 |
3 | import org.junit.runner.RunWith;
4 | import org.junit.runners.Suite;
5 |
6 | // Runs all unit tests.
7 | @RunWith(Suite.class)
8 | @Suite.SuiteClasses({MainActivityJavaTest.class})
9 | public class UnitTestSuite {}
10 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/gauravgoyal/mvvm_with_testing/view/ui/MainActivityTest.java:
--------------------------------------------------------------------------------
1 | package com.gauravgoyal.mvvm_with_testing.view.ui;
2 |
3 | import android.support.test.espresso.ViewInteraction;
4 | import android.support.test.rule.ActivityTestRule;
5 | import android.support.test.runner.AndroidJUnit4;
6 | import android.test.suitebuilder.annotation.LargeTest;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 | import android.view.ViewParent;
10 |
11 | import com.gauravgoyal.mvvm_with_testing.R;
12 |
13 | import org.hamcrest.Description;
14 | import org.hamcrest.Matcher;
15 | import org.hamcrest.TypeSafeMatcher;
16 | import org.junit.Rule;
17 | import org.junit.Test;
18 | import org.junit.runner.RunWith;
19 |
20 | import static android.support.test.espresso.Espresso.onView;
21 | import static android.support.test.espresso.action.ViewActions.click;
22 | import static android.support.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition;
23 | import static android.support.test.espresso.matcher.ViewMatchers.withClassName;
24 | import static android.support.test.espresso.matcher.ViewMatchers.withId;
25 | import static org.hamcrest.Matchers.allOf;
26 | import static org.hamcrest.Matchers.is;
27 |
28 | @LargeTest
29 | @RunWith(AndroidJUnit4.class)
30 | public class MainActivityTest {
31 |
32 | @Rule
33 | public ActivityTestRule mActivityTestRule = new ActivityTestRule<>(MainActivity.class);
34 |
35 | @Test
36 | public void mainActivityTest() {
37 | ViewInteraction recyclerView = onView(
38 | allOf(withId(R.id.project_list),
39 | childAtPosition(
40 | withClassName(is("android.widget.LinearLayout")),
41 | 1)));
42 | recyclerView.perform(actionOnItemAtPosition(0, click()));
43 |
44 | // Added a sleep statement to match the app's execution delay.
45 | // The recommended way to handle such scenarios is to use Espresso idling resources:
46 | // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
47 | try {
48 | Thread.sleep(700);
49 | } catch (InterruptedException e) {
50 | e.printStackTrace();
51 | }
52 |
53 | ViewInteraction recyclerView2 = onView(
54 | allOf(withId(R.id.project_list),
55 | childAtPosition(
56 | withClassName(is("android.widget.LinearLayout")),
57 | 1)));
58 | recyclerView2.perform(actionOnItemAtPosition(0, click()));
59 |
60 | }
61 |
62 | private static Matcher childAtPosition(
63 | final Matcher parentMatcher, final int position) {
64 |
65 | return new TypeSafeMatcher() {
66 | @Override
67 | public void describeTo(Description description) {
68 | description.appendText("Child at position " + position + " in parent ");
69 | parentMatcher.describeTo(description);
70 | }
71 |
72 | @Override
73 | public boolean matchesSafely(View view) {
74 | ViewParent parent = view.getParent();
75 | return parent instanceof ViewGroup && parentMatcher.matches(parent)
76 | && view.equals(((ViewGroup) parent).getChildAt(position));
77 | }
78 | };
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gauravgoyal/mvvm_with_testing/lifecycle/SomeObserver.java:
--------------------------------------------------------------------------------
1 | package com.gauravgoyal.mvvm_with_testing.lifecycle;
2 |
3 | import android.arch.lifecycle.Lifecycle;
4 | import android.arch.lifecycle.LifecycleObserver;
5 | import android.arch.lifecycle.OnLifecycleEvent;
6 | import android.util.Log;
7 |
8 | /**
9 | * Created by gauravgoyal on 20/12/17.
10 | */
11 |
12 | public class SomeObserver implements LifecycleObserver {
13 |
14 | final String TAG = this.getClass().getSimpleName().toString();
15 |
16 | @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
17 | public void onResume() {
18 | Log.d(TAG, "onResume called");
19 | }
20 |
21 | @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
22 | public void onPause() {
23 | Log.d(TAG, "onPause called");
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gauravgoyal/mvvm_with_testing/service/model/Article.kt:
--------------------------------------------------------------------------------
1 | package com.gauravgoyal.mvvm_with_testing.service.model
2 |
3 | /**
4 | * Created by gauravgoyal on 15/12/17.
5 | */
6 |
7 | import android.databinding.BaseObservable
8 | import android.databinding.ObservableField
9 | import com.google.gson.annotations.Expose
10 | import com.google.gson.annotations.SerializedName
11 |
12 | class Article {
13 | @SerializedName("source")
14 | @Expose
15 | var source: Source? = null
16 | @SerializedName("author")
17 | @Expose
18 | var author: String? = null
19 | @SerializedName("title")
20 | @Expose
21 | var title: String? = null
22 | @SerializedName("description")
23 | @Expose
24 | var description: String? = null
25 | @SerializedName("url")
26 | @Expose
27 | var url: String? = null
28 | @SerializedName("urlToImage")
29 | @Expose
30 | var urlToImage: String? = null
31 | @SerializedName("publishedAt")
32 | @Expose
33 | var publishedAt: String? = null
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gauravgoyal/mvvm_with_testing/service/model/News.kt:
--------------------------------------------------------------------------------
1 | package com.gauravgoyal.mvvm_with_testing.service.model
2 |
3 | import com.google.gson.annotations.Expose
4 | import com.google.gson.annotations.SerializedName
5 |
6 | /**
7 | * Created by gauravgoyal on 15/12/17.
8 | */
9 | class News {
10 | @SerializedName("status")
11 | @Expose
12 | val status: String? = null
13 | @SerializedName("totalResults")
14 | @Expose
15 | val totalResults: Int = 0
16 | @SerializedName("articles")
17 | @Expose
18 | val articles: List? = null
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gauravgoyal/mvvm_with_testing/service/model/Source.kt:
--------------------------------------------------------------------------------
1 | package com.gauravgoyal.mvvm_with_testing.service.model
2 |
3 | /**
4 | * Created by gauravgoyal on 15/12/17.
5 | */
6 |
7 |
8 | import com.google.gson.annotations.Expose;
9 | import com.google.gson.annotations.SerializedName;
10 |
11 | class Source {
12 | @SerializedName("id")
13 | @Expose
14 | private val id: String? = null
15 | @SerializedName("name")
16 | @Expose
17 | private val name: String? = null
18 |
19 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/gauravgoyal/mvvm_with_testing/service/repository/NewsRepository.java:
--------------------------------------------------------------------------------
1 | package com.gauravgoyal.mvvm_with_testing.service.repository;
2 |
3 | import android.arch.lifecycle.LiveData;
4 | import android.arch.lifecycle.MutableLiveData;
5 |
6 | import com.gauravgoyal.mvvm_with_testing.BuildConfig;
7 | import com.gauravgoyal.mvvm_with_testing.service.model.News;
8 |
9 | import java.io.IOException;
10 |
11 | import okhttp3.HttpUrl;
12 | import okhttp3.Interceptor;
13 | import okhttp3.OkHttpClient;
14 |
15 | import okhttp3.Request;
16 | import retrofit2.Response;
17 | import retrofit2.Call;
18 | import retrofit2.Callback;
19 |
20 | import retrofit2.Retrofit;
21 | import retrofit2.converter.gson.GsonConverterFactory;
22 |
23 | public class NewsRepository {
24 | private NewsService newsService;
25 | private static NewsRepository projectRepository;
26 |
27 | private NewsRepository() {
28 | OkHttpClient.Builder httpClient =
29 | new OkHttpClient.Builder();
30 | httpClient.addInterceptor(new Interceptor() {
31 | @Override
32 | public okhttp3.Response intercept(Chain chain) throws IOException {
33 | Request original = chain.request();
34 | HttpUrl originalHttpUrl = original.url();
35 |
36 | HttpUrl url = originalHttpUrl.newBuilder()
37 | .addQueryParameter("apiKey", BuildConfig.NewsApiKey)
38 | .build();
39 |
40 | Request request = original.newBuilder()
41 | .url(url).build();
42 | return chain.proceed(request);
43 | }
44 | });
45 |
46 | Retrofit retrofit = new Retrofit.Builder()
47 | .baseUrl(NewsService.URL)
48 | .client(httpClient.build())
49 | .addConverterFactory(GsonConverterFactory.create())
50 | .build();
51 |
52 | newsService = retrofit.create(NewsService.class);
53 | }
54 |
55 | public synchronized static NewsRepository getInstance() {
56 | if (projectRepository == null) {
57 | if (projectRepository == null) {
58 | projectRepository = new NewsRepository();
59 | }
60 | }
61 | return projectRepository;
62 | }
63 |
64 | public LiveData getNews(String source) {
65 | final MutableLiveData data = new MutableLiveData<>();
66 | newsService.getNews(source).enqueue(new Callback() {
67 | @Override
68 | public void onResponse(Call call, Response response) {
69 | data.setValue(response.body());
70 | }
71 |
72 | @Override
73 | public void onFailure(Call call, Throwable t) {
74 | data.setValue(null);
75 | }
76 | });
77 | return data;
78 | }
79 |
80 | }
81 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gauravgoyal/mvvm_with_testing/service/repository/NewsService.java:
--------------------------------------------------------------------------------
1 | package com.gauravgoyal.mvvm_with_testing.service.repository;
2 |
3 | import com.gauravgoyal.mvvm_with_testing.service.model.News;
4 | import com.gauravgoyal.mvvm_with_testing.utility.Constants;
5 | import java.util.List;
6 |
7 | import retrofit2.Call;
8 | import retrofit2.http.GET;
9 | import retrofit2.http.Path;
10 | import retrofit2.http.Query;
11 |
12 | interface NewsService {
13 | String URL = Constants.Companion.getAPI_URL();
14 |
15 | @GET("top-headlines")
16 | Call getNews(@Query("sources") String source);
17 | }
18 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gauravgoyal/mvvm_with_testing/utility/Constants.kt:
--------------------------------------------------------------------------------
1 | package com.gauravgoyal.mvvm_with_testing.utility
2 |
3 | /**
4 | * Created by gauravgoyal on 15/12/17.
5 | */
6 | class Constants{
7 | companion object {
8 | public final val API_URL = "https://newsapi.org/v2/"
9 | }
10 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/gauravgoyal/mvvm_with_testing/utility/DateUtils.kt:
--------------------------------------------------------------------------------
1 | package com.gauravgoyal.mvvm_with_testing.utility
2 |
3 | import java.text.SimpleDateFormat
4 | import java.util.*
5 |
6 | /**
7 | * Created by gauravgoyal on 16/12/17.
8 | */
9 | public class DateUtils {
10 | companion object {
11 | public fun convertToDateString(date: String): String {
12 | return SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'").parse(date))
13 | }
14 | }
15 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/gauravgoyal/mvvm_with_testing/view/adapter/CustomBindingAdapter.java:
--------------------------------------------------------------------------------
1 | package com.gauravgoyal.mvvm_with_testing.view.adapter;
2 |
3 | import android.databinding.BindingAdapter;
4 | import android.view.View;
5 | import android.webkit.WebView;
6 | import android.webkit.WebViewClient;
7 | import android.widget.TextView;
8 | import com.gauravgoyal.mvvm_with_testing.utility.DateUtils;
9 |
10 | public class CustomBindingAdapter {
11 | @BindingAdapter("visibleGone")
12 | public static void showHide(View view, boolean show) {
13 | view.setVisibility(show ? View.VISIBLE : View.GONE);
14 | }
15 |
16 | @BindingAdapter("dateText")
17 | public static void convertToDate(TextView view, String date) {
18 | view.setText(DateUtils.Companion.convertToDateString(date));
19 | }
20 |
21 |
22 | @BindingAdapter("loadurl")
23 | public static void loadurl(WebView mWebview, String url) {
24 | mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
25 |
26 | mWebview.setWebViewClient(new WebViewClient() {
27 | public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
28 | }
29 | });
30 | mWebview.loadUrl(url);
31 | }
32 |
33 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/gauravgoyal/mvvm_with_testing/view/adapter/NewsAdapter.java:
--------------------------------------------------------------------------------
1 | package com.gauravgoyal.mvvm_with_testing.view.adapter;
2 |
3 | import android.databinding.DataBindingUtil;
4 | import android.support.annotation.Nullable;
5 | import android.support.v7.util.DiffUtil;
6 | import android.support.v7.widget.RecyclerView;
7 | import android.view.LayoutInflater;
8 | import android.view.ViewGroup;
9 |
10 | import com.gauravgoyal.mvvm_with_testing.R;
11 | import com.gauravgoyal.mvvm_with_testing.databinding.NewsListItemBinding;
12 | import com.gauravgoyal.mvvm_with_testing.service.model.Article;
13 | import com.gauravgoyal.mvvm_with_testing.view.callback.OnClickCallback;
14 |
15 | import java.util.List;
16 |
17 | public class NewsAdapter extends RecyclerView.Adapter {
18 |
19 | List extends Article> articleList;
20 |
21 | public void setProjectList(final List extends Article> articleList) {
22 | if (this.articleList == null) {
23 | this.articleList = articleList;
24 | notifyItemRangeInserted(0, articleList.size());
25 | } else {
26 | DiffUtil.DiffResult result = DiffUtil.calculateDiff(new DiffUtil.Callback() {
27 | @Override
28 | public int getOldListSize() {
29 | return NewsAdapter.this.articleList.size();
30 | }
31 |
32 | @Override
33 | public int getNewListSize() {
34 | return articleList.size();
35 | }
36 |
37 | @Override
38 | public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
39 | return NewsAdapter.this.articleList.get(oldItemPosition).getUrl() ==
40 | NewsAdapter.this.articleList.get(newItemPosition).getUrl();
41 | }
42 |
43 | @Override
44 | public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
45 | Article newArticle = articleList.get(newItemPosition);
46 | Article oldArticle = articleList.get(oldItemPosition);
47 | return newArticle.getUrl().equals(oldArticle.getUrl())
48 | && oldArticle.getAuthor().equals(newArticle.getAuthor());
49 | }
50 | });
51 | this.articleList = articleList;
52 | result.dispatchUpdatesTo(this);
53 | }
54 | }
55 |
56 | @Override
57 | public ArticleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
58 | NewsListItemBinding binding = DataBindingUtil
59 | .inflate(LayoutInflater.from(parent.getContext()), R.layout.news_list_item,
60 | parent, false);
61 |
62 | binding.setCallback(new OnClickCallback());
63 |
64 | return new ArticleViewHolder(binding);
65 | }
66 |
67 | @Override
68 | public void onBindViewHolder(ArticleViewHolder holder, int position) {
69 | holder.binding.setArticle(articleList.get(position));
70 | holder.binding.executePendingBindings();
71 | }
72 |
73 | @Override
74 | public int getItemCount() {
75 | return articleList == null ? 0 : articleList.size();
76 | }
77 |
78 | static class ArticleViewHolder extends RecyclerView.ViewHolder {
79 |
80 | final NewsListItemBinding binding;
81 |
82 | public ArticleViewHolder(NewsListItemBinding binding) {
83 | super(binding.getRoot());
84 | this.binding = binding;
85 | }
86 | }
87 | }
88 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gauravgoyal/mvvm_with_testing/view/callback/OnClickCallback.java:
--------------------------------------------------------------------------------
1 | package com.gauravgoyal.mvvm_with_testing.view.callback;
2 |
3 | import android.content.Context;
4 | import android.content.Intent;
5 | import android.view.View;
6 |
7 | import com.gauravgoyal.mvvm_with_testing.service.model.Article;
8 | import com.gauravgoyal.mvvm_with_testing.service.model.News;
9 | import com.gauravgoyal.mvvm_with_testing.view.ui.NewsDetailActivity;
10 |
11 | public class OnClickCallback {
12 | public void onClick(View view, Article article) {
13 | Context context = view.getContext();
14 | Intent i = new Intent(context, NewsDetailActivity.class);
15 | i.putExtra("url", article.getUrl());
16 | context.startActivity(i);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gauravgoyal/mvvm_with_testing/view/ui/ArticleListFragment.java:
--------------------------------------------------------------------------------
1 | package com.gauravgoyal.mvvm_with_testing.view.ui;
2 |
3 | import android.arch.lifecycle.Lifecycle;
4 | import android.arch.lifecycle.LifecycleFragment;
5 | import android.arch.lifecycle.Observer;
6 | import android.arch.lifecycle.ViewModelProviders;
7 | import android.content.Intent;
8 | import android.databinding.DataBindingUtil;
9 | import android.os.Bundle;
10 | import android.support.annotation.Nullable;
11 | import android.support.v4.app.Fragment;
12 | import android.view.LayoutInflater;
13 | import android.view.View;
14 | import android.view.ViewGroup;
15 | import android.widget.Toast;
16 |
17 | import com.gauravgoyal.mvvm_with_testing.R;
18 | import com.gauravgoyal.mvvm_with_testing.databinding.FragmentNewsListBinding;
19 | import com.gauravgoyal.mvvm_with_testing.service.model.Article;
20 | import com.gauravgoyal.mvvm_with_testing.service.model.News;
21 | import com.gauravgoyal.mvvm_with_testing.view.adapter.NewsAdapter;
22 | import com.gauravgoyal.mvvm_with_testing.view.callback.OnClickCallback;
23 | import com.gauravgoyal.mvvm_with_testing.viewmodel.NewsViewModel;
24 |
25 | import java.util.List;
26 |
27 | public class ArticleListFragment extends Fragment {
28 | public static final String TAG = "ArticleListFragment";
29 | private NewsAdapter newsAdapter;
30 | private FragmentNewsListBinding binding;
31 |
32 | @Nullable
33 | @Override
34 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
35 | @Nullable Bundle savedInstanceState) {
36 | binding = DataBindingUtil.inflate(inflater, R.layout.fragment_news_list, container, false);
37 |
38 | newsAdapter = new NewsAdapter();
39 | binding.projectList.setAdapter(newsAdapter);
40 | binding.setIsLoading(true);
41 |
42 | return binding.getRoot();
43 | }
44 |
45 | @Override
46 | public void onActivityCreated(@Nullable Bundle savedInstanceState) {
47 | super.onActivityCreated(savedInstanceState);
48 |
49 | NewsViewModel.Factory factory = new NewsViewModel.Factory(
50 | getActivity().getApplication());
51 |
52 | final NewsViewModel viewModel = ViewModelProviders.of(this, factory)
53 | .get(NewsViewModel.class);
54 |
55 | binding.setIsLoading(true);
56 |
57 | observeViewModel(viewModel);
58 | }
59 |
60 | private void observeViewModel(NewsViewModel viewModel) {
61 | // Update the list when the data changes
62 | viewModel.getObservableProject().observe(this, new Observer() {
63 | @Override
64 | public void onChanged(@Nullable News news) {
65 | if (news != null) {
66 | binding.setIsLoading(false);
67 | newsAdapter.setProjectList(news.getArticles());
68 | }
69 | }
70 | });
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gauravgoyal/mvvm_with_testing/view/ui/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.gauravgoyal.mvvm_with_testing.view.ui;
2 |
3 | import android.arch.lifecycle.LifecycleActivity;
4 | import android.os.Bundle;
5 | import android.support.annotation.Nullable;
6 | import android.support.v7.app.AppCompatActivity;
7 | import android.util.Log;
8 |
9 | import com.gauravgoyal.mvvm_with_testing.R;
10 | import com.gauravgoyal.mvvm_with_testing.lifecycle.SomeObserver;
11 |
12 | public class MainActivity extends AppCompatActivity {
13 | final String TAG = this.getClass().getSimpleName().toString();
14 | @Override
15 | protected void onCreate(@Nullable Bundle savedInstanceState) {
16 | super.onCreate(savedInstanceState);
17 | setContentView(R.layout.activity_main);
18 |
19 | // Add project list fragment if this is first creation
20 | if (savedInstanceState == null) {
21 | ArticleListFragment fragment = new ArticleListFragment();
22 |
23 | getSupportFragmentManager().beginTransaction()
24 | .add(R.id.fragment_container, fragment, ArticleListFragment.TAG).commit();
25 | }
26 |
27 | // for shake of
28 | getLifecycle().addObserver(new SomeObserver());
29 | }
30 |
31 | @Override
32 | protected void onResume() {
33 | super.onResume();
34 | Log.d(TAG, "onResume called");
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gauravgoyal/mvvm_with_testing/view/ui/NewsDetailActivity.java:
--------------------------------------------------------------------------------
1 | package com.gauravgoyal.mvvm_with_testing.view.ui;
2 |
3 | import android.arch.lifecycle.LifecycleActivity;
4 | import android.databinding.BindingAdapter;
5 | import android.databinding.DataBindingUtil;
6 | import android.os.Bundle;
7 | import android.support.annotation.Nullable;
8 | import android.webkit.WebView;
9 | import android.webkit.WebViewClient;
10 |
11 | import com.gauravgoyal.mvvm_with_testing.R;
12 | import com.gauravgoyal.mvvm_with_testing.databinding.ActivityNewsDetailBinding;
13 |
14 | public class NewsDetailActivity extends LifecycleActivity {
15 |
16 | private ActivityNewsDetailBinding binding;
17 |
18 | @Override
19 | protected void onCreate(@Nullable Bundle savedInstanceState) {
20 | super.onCreate(savedInstanceState);
21 | binding = DataBindingUtil.setContentView(this, R.layout.activity_news_detail);
22 | binding.setUrl(getIntent().getStringExtra("url"));
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/app/src/main/java/com/gauravgoyal/mvvm_with_testing/viewmodel/NewsViewModel.java:
--------------------------------------------------------------------------------
1 | package com.gauravgoyal.mvvm_with_testing.viewmodel;
2 |
3 | import android.app.Application;
4 | import android.arch.lifecycle.AndroidViewModel;
5 | import android.arch.lifecycle.LiveData;
6 | import android.arch.lifecycle.ViewModel;
7 | import android.arch.lifecycle.ViewModelProvider;
8 | import android.databinding.ObservableField;
9 | import android.support.annotation.NonNull;
10 |
11 | import com.gauravgoyal.mvvm_with_testing.service.model.News;
12 | import com.gauravgoyal.mvvm_with_testing.service.repository.NewsRepository;
13 |
14 | public class NewsViewModel extends AndroidViewModel {
15 | private final LiveData newsLiveData;
16 |
17 | public ObservableField news = new ObservableField<>();
18 |
19 | public NewsViewModel(@NonNull Application application) {
20 | super(application);
21 | // a differnt source can be passed, here i am passing techcrunch
22 | newsLiveData = NewsRepository.getInstance().getNews("techcrunch");
23 | }
24 |
25 | public LiveData getObservableProject() {
26 | return newsLiveData;
27 | }
28 |
29 | public void setNews(News news) {
30 | this.news.set(news);
31 | }
32 |
33 | /**
34 | * A creator is used to inject the project ID into the ViewModel
35 | */
36 | public static class Factory extends ViewModelProvider.NewInstanceFactory {
37 |
38 | @NonNull
39 | private final Application application;
40 |
41 | public Factory(@NonNull Application application) {
42 | this.application = application;
43 | }
44 |
45 | @Override
46 | public T create(Class modelClass) {
47 | //noinspection unchecked
48 | return (T) new NewsViewModel(application);
49 | }
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/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/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 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_news_detail.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 |
6 |
7 |
8 |
11 |
12 |
13 |
18 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_news_list.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 |
6 |
7 |
8 |
11 |
12 |
13 |
18 |
19 |
27 |
28 |
34 |
35 |
45 |
46 |
51 |
52 |
53 |
54 |
55 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/news_list_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 |
6 |
7 |
8 |
11 |
12 |
15 |
16 |
17 |
25 |
26 |
33 |
34 |
42 |
43 |
49 |
50 |
56 |
57 |
63 |
64 |
65 |
66 |
67 |
--------------------------------------------------------------------------------
/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/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gauravgyal/MVVM-LIveData-ViewModel/fb5812f3e5490ce919c6eb380048ab09e4587275/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gauravgyal/MVVM-LIveData-ViewModel/fb5812f3e5490ce919c6eb380048ab09e4587275/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gauravgyal/MVVM-LIveData-ViewModel/fb5812f3e5490ce919c6eb380048ab09e4587275/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gauravgyal/MVVM-LIveData-ViewModel/fb5812f3e5490ce919c6eb380048ab09e4587275/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gauravgyal/MVVM-LIveData-ViewModel/fb5812f3e5490ce919c6eb380048ab09e4587275/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gauravgyal/MVVM-LIveData-ViewModel/fb5812f3e5490ce919c6eb380048ab09e4587275/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gauravgyal/MVVM-LIveData-ViewModel/fb5812f3e5490ce919c6eb380048ab09e4587275/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gauravgyal/MVVM-LIveData-ViewModel/fb5812f3e5490ce919c6eb380048ab09e4587275/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gauravgyal/MVVM-LIveData-ViewModel/fb5812f3e5490ce919c6eb380048ab09e4587275/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gauravgyal/MVVM-LIveData-ViewModel/fb5812f3e5490ce919c6eb380048ab09e4587275/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 | 16dp
3 | 8dp
4 | 16dp
5 | 16dp
6 |
7 | 16sp
8 | 150dp
9 | 125dp
10 |
11 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | mvvm-with-testing
3 | Loading news
4 | News list
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/gauravgoyal/mvvm_with_testing/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package com.gauravgoyal.mvvm_with_testing
2 |
3 | import org.junit.Test
4 |
5 | import org.junit.Assert.*
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * See [testing documentation](http://d.android.com/tools/testing).
11 | */
12 | class ExampleUnitTest {
13 | @Test
14 | fun addition_isCorrect() {
15 | assertEquals(4, 2 + 2)
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/app/src/test/java/com/gauravgoyal/mvvm_with_testing/UnitTest.kt:
--------------------------------------------------------------------------------
1 | package com.gauravgoyal.mvvm_with_testing
2 |
3 | import android.text.format.DateUtils
4 | import org.junit.Assert
5 | import org.junit.Test
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * See [testing documentation](http://d.android.com/tools/testing).
11 | */
12 | class UnitTest {
13 |
14 | fun addition_isCorrect() {
15 | Assert.assertEquals(4, 2 + 2)
16 | }
17 |
18 | @Test
19 | fun dateConversion_isCorrect(){
20 | val actual = "2017-12-15T20:00:53Z"
21 | val expected = "2017-12-15 20:00:53"
22 | Assert.assertEquals(expected,com.gauravgoyal.mvvm_with_testing.utility.DateUtils.convertToDateString(actual))
23 | }
24 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | ext.kotlin_version = '1.1.51'
5 | repositories {
6 | google()
7 | jcenter()
8 | }
9 | dependencies {
10 | classpath 'com.android.tools.build:gradle:3.0.0'
11 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
12 |
13 | // NOTE: Do not place your application dependencies here; they belong
14 | // in the individual module build.gradle files
15 | }
16 | }
17 |
18 | allprojects {
19 | repositories {
20 | google()
21 | jcenter()
22 | }
23 | }
24 |
25 | task clean(type: Delete) {
26 | delete rootProject.buildDir
27 | }
28 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
19 | NewsApiKey=466a36ba9e0647238afd0065be34860a
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gauravgyal/MVVM-LIveData-ViewModel/fb5812f3e5490ce919c6eb380048ab09e4587275/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Fri Dec 15 11:53:05 IST 2017
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.1-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------