11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright 2018 Gaurav Kumar
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/MVPArchBlueprint.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gauravk95/mvp-android-template/8f55f4d62a58f4b09d70497acb30fded033fa69e/MVPArchBlueprint.jpg
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # MVP Android Template
2 | A MVP Android Template to give you a Head Start for your next Android Project. It contains the boilerplate code to quickly setup your project for MVP Architecture using industry standard practices and popular libraries like RxJava, Dagger, Retrofit and Room.
3 |
4 | 
5 |
6 | ### This project uses:
7 | * **RxJava 2** - For Reactive Programming
8 | * **Dagger 2** - For Dependency Injection
9 | * **Retrofit 2** - As Type Safe HTTP client
10 | * **Room** - For persistence storage(abstraction of SQLite)
11 |
12 | ### MVP Architecture Blueprint
13 | 
14 |
15 | ### The app has following packages:
16 | 1. **base:** It contains base classes
17 | 2. **data:** It contains all the data accessing and manipulating components like data models, sources, repositories
18 | * model
19 | * local
20 | * remote
21 | * source
22 | * db
23 | * network
24 | * prefs
25 | * repository
26 | * local
27 | * remote
28 | 3. **di:** Dependency providing classes using Dagger2.
29 | * component
30 | * module
31 | 4. **ui:** View classes along with their corresponding Presenters.
32 | * custom
33 | * adapters
34 | * main
35 | 5. **service:** Services for the application.
36 | 6. **utils:** Utility classes.
37 | * fonts
38 | * rx
39 |
40 | ### License:
41 | ```
42 | Copyright 2018 Gaurav Kumar
43 |
44 | Licensed under the Apache License, Version 2.0 (the "License");
45 | you may not use this file except in compliance with the License.
46 | You may obtain a copy of the License at
47 |
48 | http://www.apache.org/licenses/LICENSE-2.0
49 |
50 | Unless required by applicable law or agreed to in writing, software
51 | distributed under the License is distributed on an "AS IS" BASIS,
52 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
53 | See the License for the specific language governing permissions and
54 | limitations under the License.
55 | ```
56 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion rootProject.ext.compileSdkVersion
5 | buildToolsVersion rootProject.ext.buildToolsVersion
6 | defaultConfig {
7 | applicationId "com.github.mvpbasearchitecture"
8 | minSdkVersion rootProject.ext.minSdkVersion
9 | targetSdkVersion rootProject.ext.targetSdkVersion
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 | }
14 | compileOptions {
15 | sourceCompatibility JavaVersion.VERSION_1_8
16 | targetCompatibility JavaVersion.VERSION_1_8
17 | }
18 | packagingOptions {
19 | exclude 'META-INF/rxjava.properties'
20 | }
21 | defaultConfig {
22 | javaCompileOptions {
23 | annotationProcessorOptions {
24 | arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
25 | }
26 | }
27 | }
28 | buildTypes {
29 | debug {
30 | buildConfigField "String", "BASE_URL", "\"https://demo3330795.mockable.io/\""
31 | }
32 |
33 | release {
34 | minifyEnabled true
35 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
36 | buildConfigField "String", "BASE_URL", "\"https://demo3330795.mockable.io/\""
37 | }
38 | }
39 | }
40 |
41 | dependencies {
42 | implementation fileTree(dir: 'libs', include: ['*.jar'])
43 |
44 | //support dependencies
45 | implementation "com.android.support:design:$supportLibraryVersion"
46 | implementation "com.android.support:appcompat-v7:$supportLibraryVersion"
47 | implementation "com.android.support:support-v4:$supportLibraryVersion"
48 | implementation "com.android.support.constraint:constraint-layout:$constraintLayoutVersion"
49 |
50 | //network related dependencies
51 | implementation "com.google.code.gson:gson:$gsonVersion"
52 | implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
53 | implementation "com.squareup.retrofit2:converter-gson:$retrofitVersion"
54 | implementation "com.squareup.retrofit2:adapter-rxjava2:$retrofitVersion"
55 | implementation "com.github.bumptech.glide:glide:$glideVersion"
56 | implementation "com.github.bumptech.glide:okhttp3-integration:$glideOkhttpIntegrationVersion"
57 |
58 | //rx java dependencies
59 | implementation "io.reactivex.rxjava2:rxandroid:$rxAndroidVersion"
60 | implementation "io.reactivex.rxjava2:rxjava:$rxJavaVersion"
61 |
62 | //database dependencies
63 | implementation "android.arch.persistence.room:runtime:$roomDBVersion"
64 | implementation "android.arch.persistence.room:rxjava2:$roomDBVersion"
65 | annotationProcessor "android.arch.persistence.room:compiler:$roomDBVersion"
66 |
67 | //dependency injection
68 | implementation "com.google.dagger:dagger:$dagger2Version"
69 | annotationProcessor "com.google.dagger:dagger-compiler:$dagger2Version"
70 | compileOnly 'javax.annotation:jsr250-api:1.0'
71 | implementation 'javax.inject:javax.inject:1'
72 |
73 | //test dependencies
74 | testImplementation "junit:junit:$jUnitVersion"
75 | androidTestImplementation "com.android.support.test:runner:$testRunnerVersion"
76 | androidTestImplementation "com.android.support.test.espresso:espresso-core:$expressoVersion"
77 |
78 | }
79 |
--------------------------------------------------------------------------------
/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/schemas/com.github.mvpbasearchitecture.data.source.db.AppDatabase/1.json:
--------------------------------------------------------------------------------
1 | {
2 | "formatVersion": 1,
3 | "database": {
4 | "version": 1,
5 | "identityHash": "2da692e07d6a9b4a3637be0a1105e258",
6 | "entities": [
7 | {
8 | "tableName": "item",
9 | "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`item_id` TEXT NOT NULL, `item_name` TEXT NOT NULL, `item_image_link` TEXT, PRIMARY KEY(`item_id`))",
10 | "fields": [
11 | {
12 | "fieldPath": "itemId",
13 | "columnName": "item_id",
14 | "affinity": "TEXT",
15 | "notNull": true
16 | },
17 | {
18 | "fieldPath": "itemName",
19 | "columnName": "item_name",
20 | "affinity": "TEXT",
21 | "notNull": true
22 | },
23 | {
24 | "fieldPath": "itemImageLink",
25 | "columnName": "item_image_link",
26 | "affinity": "TEXT",
27 | "notNull": false
28 | }
29 | ],
30 | "primaryKey": {
31 | "columnNames": [
32 | "item_id"
33 | ],
34 | "autoGenerate": false
35 | },
36 | "indices": [],
37 | "foreignKeys": []
38 | }
39 | ],
40 | "setupQueries": [
41 | "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
42 | "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"2da692e07d6a9b4a3637be0a1105e258\")"
43 | ]
44 | }
45 | }
--------------------------------------------------------------------------------
/app/schemas/com.github.mvpbasearchitecture.data.source.local.AppDatabase/1.json:
--------------------------------------------------------------------------------
1 | {
2 | "formatVersion": 1,
3 | "database": {
4 | "version": 1,
5 | "identityHash": "2da692e07d6a9b4a3637be0a1105e258",
6 | "entities": [
7 | {
8 | "tableName": "item",
9 | "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`item_id` TEXT NOT NULL, `item_name` TEXT NOT NULL, `item_image_link` TEXT, PRIMARY KEY(`item_id`))",
10 | "fields": [
11 | {
12 | "fieldPath": "itemId",
13 | "columnName": "item_id",
14 | "affinity": "TEXT",
15 | "notNull": true
16 | },
17 | {
18 | "fieldPath": "itemName",
19 | "columnName": "item_name",
20 | "affinity": "TEXT",
21 | "notNull": true
22 | },
23 | {
24 | "fieldPath": "itemImageLink",
25 | "columnName": "item_image_link",
26 | "affinity": "TEXT",
27 | "notNull": false
28 | }
29 | ],
30 | "primaryKey": {
31 | "columnNames": [
32 | "item_id"
33 | ],
34 | "autoGenerate": false
35 | },
36 | "indices": [],
37 | "foreignKeys": []
38 | }
39 | ],
40 | "setupQueries": [
41 | "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
42 | "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"2da692e07d6a9b4a3637be0a1105e258\")"
43 | ]
44 | }
45 | }
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/github/mvpbasearchitecture/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.github.mvpbasearchitecture;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.github.mvpbasearchitecture", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/app/src/main/assets/fonts/OpenSans-Bold.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gauravk95/mvp-android-template/8f55f4d62a58f4b09d70497acb30fded033fa69e/app/src/main/assets/fonts/OpenSans-Bold.ttf
--------------------------------------------------------------------------------
/app/src/main/assets/fonts/OpenSans-Light.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gauravk95/mvp-android-template/8f55f4d62a58f4b09d70497acb30fded033fa69e/app/src/main/assets/fonts/OpenSans-Light.ttf
--------------------------------------------------------------------------------
/app/src/main/assets/fonts/OpenSans-Regular.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gauravk95/mvp-android-template/8f55f4d62a58f4b09d70497acb30fded033fa69e/app/src/main/assets/fonts/OpenSans-Regular.ttf
--------------------------------------------------------------------------------
/app/src/main/assets/fonts/OpenSans-SemiBold.ttf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gauravk95/mvp-android-template/8f55f4d62a58f4b09d70497acb30fded033fa69e/app/src/main/assets/fonts/OpenSans-SemiBold.ttf
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/base/BaseActivity.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.base;
17 |
18 | import android.content.Intent;
19 | import android.os.Bundle;
20 | import android.support.annotation.Nullable;
21 | import android.support.v7.app.AppCompatActivity;
22 |
23 | import com.github.mvpbasearchitecture.di.component.ActivityComponent;
24 | import com.github.mvpbasearchitecture.di.component.DaggerActivityComponent;
25 | import com.github.mvpbasearchitecture.di.module.ActivityModule;
26 |
27 | /**
28 | * Base activity without any MVP component
29 | * Used to declare Activities that are dumb and only act as holder to other fragments
30 | *
31 | * Created by gk
32 | */
33 |
34 | public abstract class BaseActivity extends AppCompatActivity {
35 |
36 | protected ActivityComponent mActivityComponent;
37 |
38 | @Override
39 | protected void onCreate(@Nullable Bundle savedInstanceState) {
40 | super.onCreate(savedInstanceState);
41 | mActivityComponent = DaggerActivityComponent.builder()
42 | .activityModule(new ActivityModule(this))
43 | .applicationComponent(((MainApplication) getApplication()).getComponent())
44 | .build();
45 |
46 | }
47 |
48 | protected ActivityComponent getActivityComponent() {
49 | return mActivityComponent;
50 | }
51 |
52 | @Override
53 | public void onStart() {
54 | super.onStart();
55 | }
56 |
57 | @Override
58 | public void onNewIntent(Intent intent) {
59 | this.setIntent(intent);
60 | }
61 |
62 | }
63 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/base/BaseContract.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.base;
17 |
18 | import android.support.annotation.NonNull;
19 | import android.support.annotation.StringRes;
20 |
21 | /**
22 | * The base contract
23 | * Consists of the Presenter and View interface
24 | * Helps in communication/establish contract between View and Presenter
25 | *
26 | * Created by gk
27 | */
28 |
29 | public interface BaseContract {
30 |
31 | interface View {
32 |
33 | void setPresenter(T presenter);
34 |
35 | void showProgressDialog(String title, @NonNull String message);
36 |
37 | void showProgressDialog(@StringRes int resId);
38 |
39 | void dismissProgressDialog();
40 |
41 | void showToastMessage(@NonNull String message);
42 |
43 | void showToastMessage(@StringRes int stringResourceId);
44 |
45 | void showSnackBarMessage(@NonNull String message);
46 |
47 | void showSnackBarMessage(@StringRes int stringResourceId);
48 |
49 | void onError(String message);
50 |
51 | void onError(@StringRes int resId);
52 |
53 | }
54 |
55 | interface Presenter {
56 |
57 | void onAttach(V view);
58 |
59 | void onDetach();
60 |
61 | void handleApiError(Throwable throwable);
62 |
63 | }
64 |
65 | }
66 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/base/BaseMVPActivity.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.base;
17 |
18 | import android.app.Dialog;
19 | import android.content.Intent;
20 | import android.os.Bundle;
21 | import android.support.annotation.NonNull;
22 | import android.support.annotation.Nullable;
23 | import android.support.annotation.StringRes;
24 | import android.support.design.widget.Snackbar;
25 | import android.support.v4.content.ContextCompat;
26 | import android.support.v7.app.AppCompatActivity;
27 | import android.view.View;
28 | import android.widget.TextView;
29 | import android.widget.Toast;
30 |
31 | import com.github.mvpbasearchitecture.R;
32 | import com.github.mvpbasearchitecture.di.component.ActivityComponent;
33 | import com.github.mvpbasearchitecture.di.component.DaggerActivityComponent;
34 | import com.github.mvpbasearchitecture.di.module.ActivityModule;
35 | import com.github.mvpbasearchitecture.utils.DialogUtils;
36 | import com.github.mvpbasearchitecture.utils.GeneralUtils;
37 |
38 | /**
39 | * Acts a Base Activity class for all other activities which will act as View part of MVP
40 | * Implements the basic functions as described in {@link BaseContract.View}
41 | *
42 | * Created by gk
43 | */
44 |
45 | public abstract class BaseMVPActivity extends AppCompatActivity implements BaseContract.View {
46 |
47 | protected Dialog progressDialog;
48 | protected ActivityComponent mActivityComponent;
49 |
50 | @Override
51 | protected void onCreate(@Nullable Bundle savedInstanceState) {
52 | super.onCreate(savedInstanceState);
53 | mActivityComponent = DaggerActivityComponent.builder()
54 | .activityModule(new ActivityModule(this))
55 | .applicationComponent(((MainApplication) getApplication()).getComponent())
56 | .build();
57 |
58 | }
59 |
60 | protected ActivityComponent getActivityComponent() {
61 | return mActivityComponent;
62 | }
63 |
64 | @Override
65 | public void onStart() {
66 | super.onStart();
67 | }
68 |
69 | @Override
70 | public void onNewIntent(Intent intent) {
71 | this.setIntent(intent);
72 | }
73 |
74 | /**
75 | * Custom Progress Dialog with loading dots animation
76 | * @param message
77 | */
78 | @Override
79 | public void showProgressDialog(String title, @NonNull String message) {
80 | progressDialog = DialogUtils.createProgressDialog(BaseMVPActivity.this);
81 | }
82 |
83 | /**
84 | * Custom Progress Dialog with loading dots animation
85 | * @param resId
86 | */
87 | @Override
88 | public void showProgressDialog(@StringRes int resId) {
89 | String message = getString(resId);
90 | if(GeneralUtils.checkStringNotEmpty(message))
91 | showProgressDialog(null,message);
92 | }
93 |
94 | @Override
95 | public void dismissProgressDialog() {
96 | if (progressDialog != null && progressDialog.isShowing())
97 | progressDialog.dismiss();
98 | }
99 |
100 | @Override
101 | public void showToastMessage(@NonNull String message) {
102 | if (GeneralUtils.checkStringNotEmpty(message))
103 | Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
104 | }
105 |
106 | @Override
107 | public void showToastMessage(@StringRes int resId) {
108 | showToastMessage(getString(resId));
109 | }
110 |
111 | @Override
112 | public void showSnackBarMessage(@NonNull String message){
113 | if(GeneralUtils.checkStringNotEmpty(message))
114 | showSnackBar(message);
115 | }
116 |
117 | @Override
118 | public void showSnackBarMessage(@StringRes int resId){
119 | showSnackBarMessage(getString(resId));
120 | }
121 |
122 | /**
123 | * Creates a SnackBar for message display
124 | */
125 | private void showSnackBar(String message) {
126 | Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content),
127 | message, Snackbar.LENGTH_SHORT);
128 | View sbView = snackbar.getView();
129 | TextView textView = (TextView) sbView
130 | .findViewById(android.support.design.R.id.snackbar_text);
131 | textView.setTextColor(ContextCompat.getColor(this, R.color.white));
132 | snackbar.show();
133 | }
134 |
135 | @Override
136 | public void onError(String message) {
137 | if (message != null) {
138 | showSnackBar(message);
139 | } else {
140 | showSnackBar(getString(R.string.default_error_message));
141 | }
142 | }
143 |
144 | @Override
145 | public void onError(@StringRes int resId) {
146 | onError(getString(resId));
147 | }
148 |
149 | @Override
150 | protected void onStop() {
151 | super.onStop();
152 | dismissProgressDialog();
153 | }
154 |
155 | @Override
156 | protected void onDestroy() {
157 | super.onDestroy();
158 | dismissProgressDialog();
159 | }
160 |
161 | protected abstract void initViews();
162 |
163 | }
164 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/base/BaseMVPDialog.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.base;
17 |
18 | import android.app.Dialog;
19 | import android.content.Context;
20 | import android.os.Bundle;
21 | import android.support.annotation.NonNull;
22 | import android.support.annotation.Nullable;
23 | import android.support.annotation.StringRes;
24 | import android.support.design.widget.Snackbar;
25 | import android.support.v4.app.DialogFragment;
26 | import android.support.v4.content.ContextCompat;
27 | import android.view.View;
28 | import android.widget.TextView;
29 | import android.widget.Toast;
30 |
31 | import com.github.mvpbasearchitecture.R;
32 | import com.github.mvpbasearchitecture.utils.DialogUtils;
33 | import com.github.mvpbasearchitecture.utils.GeneralUtils;
34 |
35 | /**
36 | * Acts a Base Dialog class for all other {@link DialogFragment} which will act as View part of MVP
37 | * Implements the basic functions as described in {@link BaseContract.View}
38 | *
39 | * Created by gk
40 | */
41 |
42 | public abstract class BaseMVPDialog extends DialogFragment implements BaseContract.View {
43 |
44 | protected Dialog progressDialog;
45 |
46 | @Override
47 | public void onAttach(Context context) {
48 | super.onAttach(context);
49 | }
50 |
51 | @Override
52 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
53 | super.onViewCreated(view, savedInstanceState);
54 | }
55 |
56 | /**
57 | * Set listeners and callback attached to the parent activity as null
58 | * in order to avoid activity leaking
59 | */
60 |
61 | @Override
62 | public void onDetach() {
63 | super.onDetach();
64 | }
65 |
66 | /**
67 | * Custom Progress Dialog with loading dots animation
68 | * @param message
69 | */
70 | @Override
71 | public void showProgressDialog(String title, @NonNull String message) {
72 | progressDialog = DialogUtils.createProgressDialog(getContext());
73 | }
74 |
75 | /**
76 | * Custom Progress Dialog with loading dots animation
77 | * @param resId
78 | */
79 | @Override
80 | public void showProgressDialog(@StringRes int resId) {
81 | String message = getString(resId);
82 | if(GeneralUtils.checkStringNotEmpty(message))
83 | showProgressDialog(null,message);
84 | }
85 |
86 | @Override
87 | public void dismissProgressDialog() {
88 | if (progressDialog != null && progressDialog.isShowing())
89 | progressDialog.dismiss();
90 | }
91 |
92 | @Override
93 | public void showToastMessage(@NonNull String message) {
94 | if (GeneralUtils.checkStringNotEmpty(message))
95 | Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
96 | }
97 |
98 | @Override
99 | public void showToastMessage(@StringRes int resId) {
100 | showToastMessage(getString(resId));
101 | }
102 |
103 | @Override
104 | public void showSnackBarMessage(@NonNull String message){
105 | if(GeneralUtils.checkStringNotEmpty(message))
106 | showSnackBar(message);
107 | }
108 |
109 | @Override
110 | public void showSnackBarMessage(@StringRes int resId){
111 | showSnackBarMessage(getString(resId));
112 | }
113 |
114 | /**
115 | * Creates a SnackBar for message display
116 | */
117 | private void showSnackBar(String message) {
118 | if(getActivity()!=null) {
119 | Snackbar snackbar = Snackbar.make(getActivity().findViewById(android.R.id.content),
120 | message, Snackbar.LENGTH_SHORT);
121 | View sbView = snackbar.getView();
122 | TextView textView = sbView
123 | .findViewById(android.support.design.R.id.snackbar_text);
124 | textView.setTextColor(ContextCompat.getColor(getActivity(), R.color.white));
125 | snackbar.show();
126 | }
127 | }
128 |
129 | @Override
130 | public void onDestroy() {
131 | super.onDestroy();
132 | dismissProgressDialog();
133 | }
134 |
135 | }
136 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/base/BaseMVPFragment.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.base;
17 |
18 | import android.app.Dialog;
19 | import android.content.Context;
20 | import android.os.Bundle;
21 | import android.support.annotation.NonNull;
22 | import android.support.annotation.Nullable;
23 | import android.support.annotation.StringRes;
24 | import android.support.design.widget.Snackbar;
25 | import android.support.v4.app.Fragment;
26 | import android.support.v4.content.ContextCompat;
27 | import android.view.View;
28 | import android.widget.TextView;
29 | import android.widget.Toast;
30 |
31 | import com.github.mvpbasearchitecture.R;
32 | import com.github.mvpbasearchitecture.di.component.ActivityComponent;
33 | import com.github.mvpbasearchitecture.utils.DialogUtils;
34 | import com.github.mvpbasearchitecture.utils.GeneralUtils;
35 |
36 | /**
37 | * Acts a Base Fragment class for all other {@link Fragment} which will act as View part of MVP
38 | * Implements the basic functions as described in {@link BaseContract.View}
39 | *
40 | * Created by gk
41 | */
42 |
43 | public abstract class BaseMVPFragment extends Fragment implements BaseContract.View {
44 |
45 | protected Dialog progressDialog;
46 | protected BaseActivity mActivity;
47 |
48 | @Override
49 | public void onAttach(Context context) {
50 | super.onAttach(context);
51 | if (context instanceof BaseActivity) {
52 | this.mActivity = (BaseActivity) context;
53 | }
54 | }
55 |
56 | @Override
57 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
58 | super.onViewCreated(view, savedInstanceState);
59 | }
60 |
61 | /**
62 | * Set listeners and callback attached to the parent activity as null
63 | * in order to avoid activity leaking
64 | */
65 |
66 | @Override
67 | public void onDetach() {
68 | super.onDetach();
69 | }
70 |
71 | /**
72 | * Custom Progress Dialog with loading dots animation
73 | * @param message
74 | */
75 | @Override
76 | public void showProgressDialog(String title, @NonNull String message) {
77 | progressDialog = DialogUtils.createProgressDialog(getContext());
78 | }
79 |
80 | /**
81 | * Custom Progress Dialog with loading dots animation
82 | * @param resId
83 | */
84 | @Override
85 | public void showProgressDialog(@StringRes int resId) {
86 | String message = getString(resId);
87 | if(GeneralUtils.checkStringNotEmpty(message))
88 | showProgressDialog(null,message);
89 | }
90 |
91 | @Override
92 | public void dismissProgressDialog() {
93 | if (progressDialog != null && progressDialog.isShowing())
94 | progressDialog.dismiss();
95 | }
96 |
97 | @Override
98 | public void showToastMessage(@NonNull String message) {
99 | if (GeneralUtils.checkStringNotEmpty(message))
100 | Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
101 | }
102 |
103 | @Override
104 | public void showToastMessage(@StringRes int resId) {
105 | showToastMessage(getString(resId));
106 | }
107 |
108 | @Override
109 | public void showSnackBarMessage(@NonNull String message){
110 | if(GeneralUtils.checkStringNotEmpty(message))
111 | showSnackBar(message);
112 | }
113 |
114 | @Override
115 | public void showSnackBarMessage(@StringRes int resId){
116 | showSnackBarMessage(getString(resId));
117 | }
118 |
119 | /**
120 | * Creates a SnackBar for message display
121 | */
122 | private void showSnackBar(String message) {
123 | if(getActivity()!=null) {
124 | Snackbar snackbar = Snackbar.make(getActivity().findViewById(android.R.id.content),
125 | message, Snackbar.LENGTH_SHORT);
126 | View sbView = snackbar.getView();
127 | TextView textView = (TextView) sbView
128 | .findViewById(android.support.design.R.id.snackbar_text);
129 | textView.setTextColor(ContextCompat.getColor(getActivity(), R.color.white));
130 | snackbar.show();
131 | }
132 | }
133 |
134 | @Override
135 | public void onError(String message) {
136 | if (message != null) {
137 | showSnackBar(message);
138 | } else {
139 | showSnackBar(getString(R.string.default_error_message));
140 | }
141 | }
142 |
143 | @Override
144 | public void onError(@StringRes int resId) {
145 | onError(getString(resId));
146 | }
147 |
148 | protected ActivityComponent getActivityComponent() {
149 | if (mActivity != null) {
150 | return mActivity.getActivityComponent();
151 | }
152 | return null;
153 | }
154 |
155 | @Override
156 | public void onDestroy() {
157 | super.onDestroy();
158 | dismissProgressDialog();
159 | }
160 |
161 | protected abstract void initViews();
162 |
163 | }
164 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/base/BasePresenter.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.base;
17 |
18 | import com.github.mvpbasearchitecture.R;
19 | import com.github.mvpbasearchitecture.data.source.repository.AppDataSource;
20 | import com.github.mvpbasearchitecture.data.source.repository.AppRepository;
21 | import com.github.mvpbasearchitecture.data.source.network.NetworkError;
22 | import com.github.mvpbasearchitecture.utils.GeneralUtils;
23 | import com.github.mvpbasearchitecture.utils.rx.SchedulerProvider;
24 |
25 | import io.reactivex.disposables.CompositeDisposable;
26 |
27 | /**
28 | * Base Presenter class that abstracts some of the repetitive work of a Presenter
29 | * Implements the basic functions as described in {@link BaseContract.Presenter}
30 | *
31 | * Created by gk
32 | */
33 |
34 | public abstract class BasePresenter implements BaseContract.Presenter{
35 |
36 | private final AppRepository mAppDataRepository;
37 | private final SchedulerProvider mSchedulerProvider;
38 | private final CompositeDisposable mCompositeDisposable;
39 |
40 | private V mView;
41 |
42 | public BasePresenter(AppRepository appRepository,
43 | SchedulerProvider schedulerProvider,
44 | CompositeDisposable compositeDisposable) {
45 | this.mAppDataRepository = appRepository;
46 | this.mSchedulerProvider = schedulerProvider;
47 | this.mCompositeDisposable = compositeDisposable;
48 | }
49 |
50 | @Override
51 | public void onAttach(V view) {
52 | this.mView = view;
53 | }
54 |
55 | @Override
56 | public void onDetach() {
57 | mCompositeDisposable.clear();
58 | mView = null;
59 | }
60 |
61 | @Override
62 | public void handleApiError(Throwable throwable) {
63 |
64 | if (throwable == null) {
65 | getView().onError(R.string.default_error_message);
66 | return;
67 | }
68 |
69 | NetworkError networkError = new NetworkError(throwable);
70 | String errorMsg = networkError.getAppErrorMessage();
71 | if (GeneralUtils.checkStringNotEmpty(errorMsg))
72 | getView().onError(errorMsg);
73 | else
74 | getView().onError(R.string.default_error_message);
75 |
76 | }
77 |
78 | protected boolean isViewAttached() {
79 | return mView != null;
80 | }
81 |
82 | protected V getView(){
83 | return mView;
84 | }
85 |
86 | protected AppRepository getDataSource(){
87 | return mAppDataRepository;
88 | }
89 |
90 | protected SchedulerProvider getSchedulerProvider(){
91 | return mSchedulerProvider;
92 | }
93 |
94 | protected CompositeDisposable getCompositeDisposable() {return mCompositeDisposable; }
95 |
96 | }
97 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/base/MainApplication.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.base;
17 |
18 | import android.app.Application;
19 | import android.content.Context;
20 |
21 | import com.github.mvpbasearchitecture.data.source.repository.AppDataSource;
22 | import com.github.mvpbasearchitecture.data.source.repository.AppRepository;
23 | import com.github.mvpbasearchitecture.di.component.ApplicationComponent;
24 | import com.github.mvpbasearchitecture.di.component.DaggerApplicationComponent;
25 | import com.github.mvpbasearchitecture.di.module.ApplicationModule;
26 | import com.github.mvpbasearchitecture.di.module.DataModule;
27 | import com.github.mvpbasearchitecture.di.module.NetworkModule;
28 | import com.github.mvpbasearchitecture.utils.AppLogger;
29 |
30 | import javax.inject.Inject;
31 |
32 | /**
33 | * Entry place when application start
34 | * Good place to initialize stuff that has an Application Scope
35 | *
36 | * Created by gk
37 | */
38 |
39 | public class MainApplication extends Application {
40 |
41 | @Inject
42 | AppRepository mRepository;
43 |
44 | private static Application sInstance;
45 |
46 | private ApplicationComponent mApplicationComponent;
47 |
48 | // Anywhere in the application where an instance is required, this method
49 | // can be used to retrieve it.
50 | public static Application getInstance() {
51 | return sInstance;
52 | }
53 |
54 | @Override
55 | protected void attachBaseContext(Context context) {
56 | super.attachBaseContext(context);
57 | }
58 |
59 | @Override
60 | public void onCreate() {
61 | super.onCreate();
62 |
63 | mApplicationComponent = DaggerApplicationComponent.builder()
64 | .applicationModule(new ApplicationModule(this))
65 | .dataModule(new DataModule())
66 | .networkModule(new NetworkModule())
67 | .build();
68 |
69 | mApplicationComponent.inject(this);
70 |
71 | sInstance = this;
72 |
73 | ((MainApplication) sInstance).initializeInstance();
74 |
75 | }
76 |
77 | // Here we do one-off initialisation which should apply to all activities
78 | // in the application.
79 | protected void initializeInstance() {
80 | //globally initialize the App Logger
81 | AppLogger.init();
82 | }
83 |
84 | public ApplicationComponent getComponent() {
85 | return mApplicationComponent;
86 | }
87 |
88 | // Needed to replace the component with a test specific one
89 | public void setComponent(ApplicationComponent applicationComponent) {
90 | mApplicationComponent = applicationComponent;
91 | }
92 |
93 | }
94 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/data/models/local/Item.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.data.models.local;
17 |
18 | import android.arch.persistence.room.ColumnInfo;
19 | import android.arch.persistence.room.Entity;
20 | import android.arch.persistence.room.PrimaryKey;
21 | import android.support.annotation.NonNull;
22 | import android.support.annotation.Nullable;
23 |
24 | import com.google.gson.annotations.Expose;
25 | import com.google.gson.annotations.SerializedName;
26 |
27 | /**
28 | * Describes the data to be modeled
29 | * Note:
30 | * Uses both the Room specific annotations for Local DB
31 | * Uses Gson annotations for Json serialization for Network requests
32 | *
33 | * Created by gk
34 | */
35 |
36 | @Entity(tableName = "item")
37 | public class Item {
38 |
39 | @PrimaryKey
40 | @NonNull
41 | @ColumnInfo(name = "item_id")
42 | @SerializedName("id")
43 | @Expose
44 | private String itemId;
45 |
46 | @NonNull
47 | @ColumnInfo(name = "item_name")
48 | @SerializedName("name")
49 | @Expose
50 | private String itemName;
51 |
52 | @Nullable
53 | @ColumnInfo(name = "item_image_link")
54 | @SerializedName("imageLink")
55 | @Expose
56 | private String itemImageLink;
57 |
58 | public Item(@NonNull String itemId, @NonNull String itemName,@Nullable String itemImageLink) {
59 | this.itemId = itemId;
60 | this.itemName = itemName;
61 | this.itemImageLink = itemImageLink;
62 | }
63 |
64 | public String getItemId() {
65 | return itemId;
66 | }
67 |
68 | public void setItemId(String itemId) {
69 | this.itemId = itemId;
70 | }
71 |
72 | public String getItemName() {
73 | return itemName;
74 | }
75 |
76 | public void setItemName(String itemName) {
77 | this.itemName = itemName;
78 | }
79 |
80 | @Nullable
81 | public String getItemImageLink() {
82 | return itemImageLink;
83 | }
84 |
85 | public void setItemImageLink(@Nullable String itemImageLink) {
86 | this.itemImageLink = itemImageLink;
87 | }
88 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/data/models/remote/ResponseItemHolder.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.data.models.remote;
17 |
18 | import com.github.mvpbasearchitecture.data.models.local.Item;
19 | import com.google.gson.annotations.Expose;
20 | import com.google.gson.annotations.SerializedName;
21 |
22 | import java.io.Serializable;
23 | import java.util.ArrayList;
24 | import java.util.List;
25 |
26 | /**
27 | * Holds the list of items from the sever
28 | *
29 | * Created by gk
30 | */
31 |
32 | public class ResponseItemHolder implements Serializable{
33 |
34 | @SerializedName("itemList")
35 | @Expose
36 | private List items;
37 |
38 | public ResponseItemHolder(ArrayList items) {
39 | this.items = items;
40 | }
41 |
42 | public List getItems() {
43 | return items;
44 | }
45 |
46 | public void setItems(ArrayList items) {
47 | this.items = items;
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/data/source/db/AppDatabase.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.data.source.db;
17 |
18 | import android.arch.persistence.room.Database;
19 | import android.arch.persistence.room.RoomDatabase;
20 |
21 | import com.github.mvpbasearchitecture.data.models.local.Item;
22 |
23 | /**
24 | * The Room Database that contains the Item table.
25 | */
26 |
27 | @Database(entities = {Item.class}, version = 1)
28 | public abstract class AppDatabase extends RoomDatabase {
29 |
30 | public abstract ItemDao musicDao();
31 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/data/source/db/AppDbOpenHelper.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.data.source.db;
17 |
18 | import android.arch.persistence.room.Room;
19 | import android.content.Context;
20 |
21 | import com.github.mvpbasearchitecture.di.ApplicationContext;
22 | import com.github.mvpbasearchitecture.di.DatabaseInfo;
23 |
24 | import javax.inject.Inject;
25 | import javax.inject.Singleton;
26 |
27 | /**
28 | * A DB Helper class to build Room DB
29 | *
30 | * Created by gk
31 | */
32 |
33 | @Singleton
34 | public class AppDbOpenHelper{
35 |
36 | private final AppDatabase mDatabase;
37 |
38 | @Inject
39 | public AppDbOpenHelper(@ApplicationContext Context context, @DatabaseInfo String name) {
40 | mDatabase = Room.databaseBuilder(context, AppDatabase.class,name)
41 | .build();
42 | }
43 |
44 | public AppDatabase getDatabase() {
45 | return mDatabase;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/data/source/db/ItemDao.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.data.source.db;
17 |
18 | import android.arch.persistence.room.Dao;
19 | import android.arch.persistence.room.Delete;
20 | import android.arch.persistence.room.Insert;
21 | import android.arch.persistence.room.OnConflictStrategy;
22 | import android.arch.persistence.room.Query;
23 | import android.arch.persistence.room.Update;
24 |
25 | import com.github.mvpbasearchitecture.data.models.local.Item;
26 |
27 | import java.util.List;
28 |
29 | import io.reactivex.Flowable;
30 |
31 | /**
32 | * Data access objects for connection to SQLite DB using Room
33 | *
34 | * Created by gk
35 | */
36 |
37 | @Dao
38 | public interface ItemDao {
39 |
40 | @Insert(onConflict = OnConflictStrategy.REPLACE)
41 | void insertSingleItem(Item item);
42 |
43 | @Insert(onConflict = OnConflictStrategy.REPLACE)
44 | void insertMultipleItem(List itemList);
45 |
46 | @Query("SELECT * FROM Item WHERE item_id = :itemId")
47 | Flowable fetchItemByItemId(int itemId);
48 |
49 | @Query("SELECT * FROM Item")
50 | Flowable> fetchItems();
51 |
52 | @Update
53 | void updateItem(Item item);
54 |
55 | @Delete
56 | void deleteItem(Item item);
57 |
58 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/data/source/network/APIHelper.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.data.source.network;
17 |
18 | import javax.inject.Inject;
19 | import javax.inject.Singleton;
20 |
21 | /**
22 | * Helping with retrofit instance
23 | *
24 | * Created by gk
25 | */
26 |
27 | @Singleton
28 | public class APIHelper implements NetworkAPIs {
29 |
30 | private APIService mAPIService;
31 |
32 | @Inject
33 | public APIHelper(APIService service){
34 | this.mAPIService = service;
35 | }
36 |
37 | @Override
38 | public APIService getAPIService() {
39 | return mAPIService;
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/data/source/network/APIService.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.data.source.network;
17 |
18 | import com.github.mvpbasearchitecture.data.models.remote.ResponseItemHolder;
19 |
20 | import io.reactivex.Flowable;
21 | import retrofit2.http.GET;
22 |
23 | /**
24 | * Retrofit API services to connect to server
25 | *
26 | * Created by gk
27 | */
28 |
29 | public interface APIService {
30 |
31 | @GET("itemList")
32 | Flowable getItemList();
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/data/source/network/NetworkAPIs.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.data.source.network;
17 |
18 | /**
19 | * Created by gk
20 | */
21 |
22 | public interface NetworkAPIs {
23 |
24 | APIService getAPIService();
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/data/source/network/NetworkError.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.data.source.network;
17 |
18 | import org.json.JSONObject;
19 |
20 | import java.io.IOException;
21 |
22 | import okhttp3.ResponseBody;
23 | import retrofit2.HttpException;
24 |
25 | import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
26 | import static java.net.HttpURLConnection.HTTP_CONFLICT;
27 | import static java.net.HttpURLConnection.HTTP_GONE;
28 | import static java.net.HttpURLConnection.HTTP_NOT_ACCEPTABLE;
29 | import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
30 | import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;
31 |
32 | /**
33 | * Helps converting network exception to appropriate error type
34 | *
35 | * Created by gk
36 | */
37 |
38 | public class NetworkError extends Throwable {
39 | public static final String DEFAULT_ERROR_MESSAGE = "Something went wrong! Please try again.";
40 | public static final String NETWORK_ERROR_MESSAGE = "No Internet Connection!";
41 | private static final String ERROR_MESSAGE_HEADER = "NetworkError-Message";
42 | private static final String BAD_REQUEST_MESSAGE = "This operation is not allowed!";
43 | private final Throwable error;
44 |
45 | public NetworkError(Throwable e) {
46 | super(e);
47 | this.error = e;
48 | }
49 |
50 | public NetworkError() {
51 | error = null;
52 | }
53 |
54 | public String getMessage() {
55 | return error.getMessage();
56 | }
57 |
58 | public boolean isRefreshTokenFailure() {
59 | return error instanceof HttpException &&
60 | ((HttpException) error).code() == HTTP_GONE;
61 | }
62 |
63 | public boolean isAcceptanceFailure() {
64 | return error instanceof HttpException &&
65 | ((HttpException) error).code() == HTTP_NOT_ACCEPTABLE;
66 | }
67 |
68 | public boolean isAuthFailure() {
69 | return error instanceof HttpException &&
70 | ((HttpException) error).code() == HTTP_UNAUTHORIZED;
71 | }
72 |
73 | public boolean isNotFound() {
74 | return error instanceof HttpException &&
75 | ((HttpException) error).code() == HTTP_NOT_FOUND;
76 | }
77 |
78 | public boolean isBadRequest() {
79 | return error instanceof HttpException &&
80 | ((HttpException) error).code() == HTTP_BAD_REQUEST;
81 | }
82 |
83 | public boolean isConflictRequest() {
84 | return error instanceof HttpException &&
85 | ((HttpException) error).code() == HTTP_CONFLICT;
86 | }
87 |
88 | public boolean isResponseNull() {
89 | return error instanceof HttpException && ((HttpException) error).response() == null;
90 | }
91 |
92 | public String getAppErrorMessage() {
93 | if (this.error instanceof IOException) return NETWORK_ERROR_MESSAGE;
94 | if (this.error instanceof HttpException) {
95 | ResponseBody body = ((HttpException) error).response().errorBody();
96 | if (body != null) {
97 | try {
98 | JSONObject jObjError = new JSONObject(body.string());
99 | return jObjError.getString("message");
100 | } catch (Exception e) {
101 | e.printStackTrace();
102 | return DEFAULT_ERROR_MESSAGE;
103 | }
104 | } else return DEFAULT_ERROR_MESSAGE;
105 |
106 | } else {
107 | return DEFAULT_ERROR_MESSAGE;
108 | }
109 | }
110 |
111 | public Throwable getError() {
112 | return error;
113 | }
114 |
115 | @Override
116 | public boolean equals(Object o) {
117 | if (this == o) return true;
118 | if (o == null || getClass() != o.getClass()) return false;
119 |
120 | NetworkError that = (NetworkError) o;
121 |
122 | return error != null ? error.equals(that.error) : that.error == null;
123 |
124 | }
125 |
126 | @Override
127 | public int hashCode() {
128 | return error != null ? error.hashCode() : 0;
129 | }
130 | }
131 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/data/source/network/NetworkUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.data.source.network;
17 |
18 | import okhttp3.OkHttpClient;
19 | import okhttp3.Request;
20 |
21 | /**
22 | * Utilities for Network related stuffs
23 | *
24 | * Created by gk
25 | */
26 | public class NetworkUtils {
27 |
28 | // Adds token as a header to the OkHttpClient making the request.
29 | public static OkHttpClient getHttpClient(OkHttpClient.Builder httpClient) {
30 | httpClient.addInterceptor(chain -> {
31 | Request original = chain.request();
32 | Request request = original.newBuilder()
33 | .addHeader("Content-Type", "application/json")
34 | .method(original.method(), original.body())
35 | .build();
36 |
37 | return chain.proceed(request);
38 | });
39 | return httpClient.build();
40 | }
41 |
42 | // Adds token as a header to the OkHttpClient making the request.
43 | public static OkHttpClient getHttpClient() {
44 | OkHttpClient okHttpClient = new OkHttpClient.Builder()
45 | .addInterceptor(chain -> {
46 | Request original = chain.request();
47 | Request request = original.newBuilder()
48 | .addHeader("Content-Type", "application/json")
49 | .method(original.method(), original.body())
50 | .build();
51 |
52 | return chain.proceed(request);
53 | })
54 | .build();
55 |
56 | return okHttpClient;
57 | }
58 |
59 | }
60 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/data/source/prefs/AppPreferencesHelper.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.data.source.prefs;
17 |
18 | import android.content.Context;
19 | import android.content.SharedPreferences;
20 | import android.support.annotation.NonNull;
21 |
22 | import com.github.mvpbasearchitecture.di.ApplicationContext;
23 | import com.github.mvpbasearchitecture.di.PreferenceInfo;
24 |
25 | import javax.inject.Inject;
26 | import javax.inject.Singleton;
27 |
28 | /**
29 | * An Helper class for Shared Preferences
30 | *
31 | * Created by gk.
32 | */
33 |
34 | @Singleton
35 | public class AppPreferencesHelper implements PreferencesHelper {
36 |
37 | private final SharedPreferences mPrefs;
38 |
39 | @Inject
40 | public AppPreferencesHelper(@ApplicationContext Context context,
41 | @PreferenceInfo String prefFileName) {
42 | mPrefs = context.getSharedPreferences(prefFileName, Context.MODE_PRIVATE);
43 | }
44 |
45 | @Override
46 | public boolean getBoolean(@NonNull String key) {
47 | return PrefsUtils.getBooleanSharedPref(mPrefs,key);
48 | }
49 |
50 | @Override
51 | public long getLong(@NonNull String key) {
52 | return PrefsUtils.getLongSharedPref(mPrefs,key);
53 | }
54 |
55 | @Override
56 | public int getInt(@NonNull String key) {
57 | return PrefsUtils.getIntSharedPref(mPrefs,key);
58 | }
59 |
60 | @Override
61 | public String getString(@NonNull String key) {
62 | return PrefsUtils.getStringSharedPref(mPrefs, key);
63 | }
64 |
65 | @Override
66 | public void setBoolean(@NonNull String key, boolean value) {
67 | PrefsUtils.setBooleanSharedPref(mPrefs,key,value);
68 | }
69 |
70 | @Override
71 | public void setLong(@NonNull String key, long value) {
72 | PrefsUtils.setLongSharedPref(mPrefs,key,value);
73 | }
74 |
75 | @Override
76 | public void setInt(@NonNull String key, int value) {
77 | PrefsUtils.setIntSharedPref(mPrefs,key,value);
78 | }
79 |
80 | @Override
81 | public void getString(@NonNull String key, @NonNull String value) {
82 | PrefsUtils.setStringSharedPref(mPrefs,key,value);
83 | }
84 |
85 | }
86 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/data/source/prefs/PreferencesHelper.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.data.source.prefs;
17 |
18 | import android.support.annotation.NonNull;
19 |
20 | /**
21 | * Created by gk.
22 | */
23 |
24 | public interface PreferencesHelper {
25 |
26 | //GENERIC
27 | boolean getBoolean(@NonNull String key);
28 |
29 | long getLong(@NonNull String key);
30 |
31 | int getInt(@NonNull String key);
32 |
33 | String getString(@NonNull String key);
34 |
35 | void setBoolean(@NonNull String key, boolean value);
36 |
37 | void setLong(@NonNull String key, long value);
38 |
39 | void setInt(@NonNull String key, int value);
40 |
41 | void getString(@NonNull String key, @NonNull String value);
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/data/source/prefs/PrefsUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.data.source.prefs;
17 |
18 | import android.content.Context;
19 | import android.content.SharedPreferences;
20 | import android.preference.PreferenceManager;
21 |
22 | public class PrefsUtils {
23 |
24 | //clears all the shared prefs
25 | public static void clearSharedPrefs(SharedPreferences sp){
26 | sp.edit().clear().apply();
27 | }
28 |
29 | //GENERIC PREFS UTIL
30 | public static long getLongSharedPref(Context context, String key) {
31 | SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
32 | return settings.getLong(key, 0);
33 | }
34 |
35 | public static long getLongSharedPref(SharedPreferences sp, String key) {
36 | return sp.getLong(key, 0);
37 | }
38 |
39 | public static void setLongSharedPref(Context context, String key, long value) {
40 | SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
41 | SharedPreferences.Editor editor = settings.edit();
42 | editor.putLong(key, value);
43 | editor.apply();
44 | }
45 |
46 | public static void setLongSharedPref(SharedPreferences sp, String key, long value) {
47 | SharedPreferences.Editor editor = sp.edit();
48 | editor.putLong(key, value);
49 | editor.apply();
50 | }
51 |
52 | public static int getIntSharedPref(SharedPreferences sp, String key) {
53 | return sp.getInt(key, 0);
54 | }
55 |
56 | public static void setIntSharedPref(SharedPreferences sp, String key, int value) {
57 | SharedPreferences.Editor editor = sp.edit();
58 | editor.putInt(key, value);
59 | editor.apply();
60 | }
61 |
62 | public static boolean getBooleanSharedPref(SharedPreferences sp, String key) {
63 | return sp.getBoolean(key, false);
64 | }
65 |
66 | public static void setBooleanSharedPref(SharedPreferences sp, String key, boolean value) {
67 | SharedPreferences.Editor editor = sp.edit();
68 | editor.putBoolean(key, value);
69 | editor.apply();
70 | }
71 |
72 | public static String getStringSharedPref(SharedPreferences sp, String key) {
73 | return sp.getString(key, "");
74 | }
75 |
76 | public static void setStringSharedPref(SharedPreferences sp, String key, String value) {
77 | SharedPreferences.Editor editor = sp.edit();
78 | editor.putString(key, value);
79 | editor.apply();
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/data/source/repository/AppDataRepository.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.data.source.repository;
17 |
18 | import android.support.annotation.VisibleForTesting;
19 |
20 | import com.github.mvpbasearchitecture.data.models.local.Item;
21 | import com.github.mvpbasearchitecture.data.source.prefs.PreferencesHelper;
22 | import com.github.mvpbasearchitecture.di.Local;
23 | import com.github.mvpbasearchitecture.di.Remote;
24 |
25 | import java.util.List;
26 |
27 | import javax.inject.Inject;
28 | import javax.inject.Singleton;
29 |
30 | import io.reactivex.Flowable;
31 |
32 | /**
33 | * The central point to communicate to different data sources like DB, SERVER, SHARED PREFS
34 | *
35 | * Created by gk
36 | */
37 |
38 | @Singleton
39 | public class AppDataRepository implements AppRepository {
40 |
41 | private final AppDataSource mLocalAppDataSource;
42 |
43 | private final AppDataSource mRemoteAppDataSource;
44 |
45 | private final PreferencesHelper mPreferenceHelper;
46 |
47 | @VisibleForTesting
48 | List mCachedItemList;
49 |
50 | /**
51 | * Marks the cache as invalid, to force an update the next time data is requested. This variable
52 | * has package local visibility so it can be accessed from tests.
53 | */
54 | @VisibleForTesting
55 | boolean mCacheIsDirty = false;
56 |
57 | @Inject
58 | public AppDataRepository(@Remote AppDataSource remoteDataSource,
59 | @Local AppDataSource localDataSource,
60 | PreferencesHelper preferencesHelper) {
61 | mRemoteAppDataSource = remoteDataSource;
62 | mLocalAppDataSource = localDataSource;
63 | mPreferenceHelper = preferencesHelper;
64 | }
65 |
66 | @Override
67 | public Flowable> getItemList(boolean forceRemote) {
68 | if (forceRemote)
69 | return getItemFromServerDB();
70 |
71 | return getItemList();
72 | }
73 |
74 | @Override
75 | public Flowable> getItemList() {
76 | // Respond immediately with cache if available and not dirty
77 | if (mCachedItemList != null && !mCacheIsDirty) {
78 | return Flowable.just(mCachedItemList);
79 | }
80 |
81 | //if cache is dirty, get the data from server
82 | if (mCacheIsDirty) {
83 | return getItemFromServerDB();
84 | }
85 | return getItemFromLocalDB();
86 | }
87 |
88 | //get the items from the server
89 | private Flowable> getItemFromServerDB() {
90 | return mRemoteAppDataSource
91 | .getItemList()
92 | .doOnNext(items -> {
93 | mLocalAppDataSource.updateItemList(items);
94 | mCachedItemList = items;
95 | mCacheIsDirty = false;
96 | });
97 | }
98 |
99 | //get the elements from local db, and if empty get it from sever
100 | private Flowable> getItemFromLocalDB() {
101 | return mLocalAppDataSource.getItemList()
102 | .switchIfEmpty(getItemFromServerDB())
103 | .flatMap(items -> {
104 | if (items != null && items.size() > 0) {
105 | mCachedItemList = items;
106 | mCacheIsDirty = false;
107 | return Flowable.just(items);
108 | } else {
109 | mCacheIsDirty = true;
110 | return getItemFromServerDB();
111 | }
112 | });
113 | }
114 |
115 | @Override
116 | public void updateItemList(List items) {
117 | mLocalAppDataSource.updateItemList(items);
118 | }
119 |
120 | @Override
121 | public void refreshItems() {
122 | mCacheIsDirty = true;
123 | }
124 | }
125 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/data/source/repository/AppDataSource.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.data.source.repository;
17 |
18 | import com.github.mvpbasearchitecture.data.models.local.Item;
19 |
20 | import java.util.List;
21 |
22 | import io.reactivex.Flowable;
23 |
24 | /**
25 | * Created by gk
26 | */
27 |
28 | public interface AppDataSource {
29 |
30 | Flowable> getItemList();
31 |
32 | void updateItemList(List items);
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/data/source/repository/AppRepository.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.data.source.repository;
17 |
18 | import com.github.mvpbasearchitecture.data.models.local.Item;
19 |
20 | import java.util.List;
21 |
22 | import io.reactivex.Flowable;
23 |
24 | /**
25 | * Created by gk
26 | */
27 |
28 | public interface AppRepository extends AppDataSource {
29 |
30 | Flowable> getItemList(boolean forceLocal);
31 |
32 | void refreshItems();
33 | }
34 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/data/source/repository/local/AppLocalDataSource.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.data.source.repository.local;
17 |
18 | import android.support.annotation.NonNull;
19 |
20 | import com.github.mvpbasearchitecture.data.models.local.Item;
21 | import com.github.mvpbasearchitecture.data.source.db.AppDatabase;
22 | import com.github.mvpbasearchitecture.data.source.db.ItemDao;
23 | import com.github.mvpbasearchitecture.data.source.repository.AppDataSource;
24 |
25 | import java.util.List;
26 |
27 | import javax.inject.Inject;
28 | import javax.inject.Singleton;
29 |
30 | import io.reactivex.Flowable;
31 |
32 | /**
33 | * Concrete implementation of a data source as a db using room.
34 | */
35 | @Singleton
36 | public class AppLocalDataSource implements AppDataSource {
37 |
38 | private final ItemDao mItemDao;
39 |
40 | @Inject
41 | public AppLocalDataSource(@NonNull AppDatabase mDatabase) {
42 | mItemDao = mDatabase.musicDao();
43 | }
44 |
45 | @Override
46 | public Flowable> getItemList() {
47 | return mItemDao.fetchItems();
48 | }
49 |
50 | @Override
51 | public void updateItemList(List items) {
52 | mItemDao.insertMultipleItem(items);
53 | }
54 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/data/source/repository/remote/AppRemoteDataSource.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.data.source.repository.remote;
17 |
18 | import com.github.mvpbasearchitecture.data.models.local.Item;
19 | import com.github.mvpbasearchitecture.data.models.remote.ResponseItemHolder;
20 | import com.github.mvpbasearchitecture.data.source.repository.AppDataSource;
21 | import com.github.mvpbasearchitecture.data.source.network.NetworkAPIs;
22 |
23 | import java.util.List;
24 |
25 | import javax.inject.Inject;
26 | import javax.inject.Singleton;
27 |
28 | import io.reactivex.Flowable;
29 |
30 | /**
31 | * Concrete implementation of a data source from remote server, using retrofit
32 | *
33 | * Created by gk
34 | */
35 |
36 | @Singleton
37 | public class AppRemoteDataSource implements AppDataSource {
38 |
39 | private final NetworkAPIs mNetworkAPIs;
40 |
41 | @Inject
42 | public AppRemoteDataSource(NetworkAPIs api) {
43 | this.mNetworkAPIs = api;
44 | }
45 |
46 | @Override
47 | public Flowable> getItemList() {
48 | return mNetworkAPIs.getAPIService()
49 | .getItemList()
50 | .map(ResponseItemHolder::getItems);
51 | }
52 |
53 | @Override
54 | public void updateItemList(List items) {
55 | //do nothing
56 | }
57 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/di/ActivityContext.java:
--------------------------------------------------------------------------------
1 | package com.github.mvpbasearchitecture.di;
2 |
3 | import java.lang.annotation.Retention;
4 | import java.lang.annotation.RetentionPolicy;
5 |
6 | import javax.inject.Qualifier;
7 |
8 | /**
9 | * Created by gk.
10 | */
11 |
12 | @Qualifier
13 | @Retention(RetentionPolicy.RUNTIME)
14 | public @interface ActivityContext {
15 | }
16 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/di/ApplicationContext.java:
--------------------------------------------------------------------------------
1 | package com.github.mvpbasearchitecture.di;
2 |
3 | import java.lang.annotation.Retention;
4 | import java.lang.annotation.RetentionPolicy;
5 |
6 | import javax.inject.Qualifier;
7 |
8 | /**
9 | * Created by gk.
10 | */
11 |
12 | @Qualifier
13 | @Retention(RetentionPolicy.RUNTIME)
14 | public @interface ApplicationContext {
15 | }
16 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/di/DatabaseInfo.java:
--------------------------------------------------------------------------------
1 | package com.github.mvpbasearchitecture.di;
2 |
3 | import java.lang.annotation.Retention;
4 | import java.lang.annotation.RetentionPolicy;
5 |
6 | import javax.inject.Qualifier;
7 |
8 | /**
9 | * Created by gk.
10 | */
11 |
12 | @Qualifier
13 | @Retention(RetentionPolicy.RUNTIME)
14 | public @interface DatabaseInfo {
15 | }
16 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/di/Local.java:
--------------------------------------------------------------------------------
1 | package com.github.mvpbasearchitecture.di;
2 |
3 | import java.lang.annotation.Retention;
4 | import java.lang.annotation.RetentionPolicy;
5 |
6 | import javax.inject.Qualifier;
7 |
8 | @Qualifier
9 | @Retention(RetentionPolicy.RUNTIME)
10 | public @interface Local {
11 | }
12 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/di/PerActivity.java:
--------------------------------------------------------------------------------
1 | package com.github.mvpbasearchitecture.di;
2 |
3 | import java.lang.annotation.Retention;
4 | import java.lang.annotation.RetentionPolicy;
5 |
6 | import javax.inject.Scope;
7 |
8 | /**
9 | * Created by gk.
10 | */
11 |
12 | @Scope
13 | @Retention(RetentionPolicy.RUNTIME)
14 | public @interface PerActivity {
15 | }
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/di/PreferenceInfo.java:
--------------------------------------------------------------------------------
1 | package com.github.mvpbasearchitecture.di;
2 |
3 | import java.lang.annotation.Retention;
4 | import java.lang.annotation.RetentionPolicy;
5 |
6 | import javax.inject.Qualifier;
7 |
8 | /**
9 | * Created by gk
10 | */
11 |
12 | @Qualifier
13 | @Retention(RetentionPolicy.RUNTIME)
14 | public @interface PreferenceInfo {
15 | }
16 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/di/Remote.java:
--------------------------------------------------------------------------------
1 | package com.github.mvpbasearchitecture.di;
2 |
3 | import java.lang.annotation.Retention;
4 | import java.lang.annotation.RetentionPolicy;
5 |
6 | import javax.inject.Qualifier;
7 |
8 | @Qualifier
9 | @Retention(RetentionPolicy.RUNTIME)
10 | public @interface Remote {
11 | }
12 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/di/component/ActivityComponent.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.di.component;
17 |
18 | import com.github.mvpbasearchitecture.di.PerActivity;
19 | import com.github.mvpbasearchitecture.di.module.ActivityModule;
20 | import com.github.mvpbasearchitecture.ui.main.MainActivity;
21 | import com.github.mvpbasearchitecture.ui.main.MainFragment;
22 |
23 | import dagger.Component;
24 |
25 | /**
26 | * Activity component connecting modules that have Activity scope
27 | *
28 | * Created by gk.
29 | */
30 |
31 | @PerActivity
32 | @Component(dependencies = ApplicationComponent.class, modules = ActivityModule.class)
33 | public interface ActivityComponent {
34 |
35 | void inject(MainActivity activity);
36 |
37 | void inject(MainFragment fragment);
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/di/component/ApplicationComponent.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.di.component;
17 |
18 | import android.app.Application;
19 | import android.content.Context;
20 |
21 | import com.github.mvpbasearchitecture.base.MainApplication;
22 | import com.github.mvpbasearchitecture.data.source.repository.AppDataRepository;
23 | import com.github.mvpbasearchitecture.data.source.repository.AppRepository;
24 | import com.github.mvpbasearchitecture.di.ApplicationContext;
25 | import com.github.mvpbasearchitecture.di.module.ApplicationModule;
26 | import com.github.mvpbasearchitecture.di.module.DataModule;
27 | import com.github.mvpbasearchitecture.di.module.NetworkModule;
28 |
29 | import javax.inject.Singleton;
30 |
31 | import dagger.Component;
32 |
33 | /**
34 | * Application component connecting modules that have application scope
35 | *
36 | * Created by gk
37 | */
38 |
39 | @Singleton
40 | @Component(modules = {ApplicationModule.class, DataModule.class, NetworkModule.class})
41 | public interface ApplicationComponent {
42 |
43 | void inject(MainApplication app);
44 |
45 | @ApplicationContext
46 | Context context();
47 |
48 | Application application();
49 |
50 | AppRepository getAppRepository();
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/di/module/ActivityModule.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.di.module;
17 |
18 | import android.content.Context;
19 | import android.support.v7.app.AppCompatActivity;
20 |
21 | import com.github.mvpbasearchitecture.data.source.repository.AppDataSource;
22 | import com.github.mvpbasearchitecture.data.source.repository.AppRepository;
23 | import com.github.mvpbasearchitecture.di.ActivityContext;
24 | import com.github.mvpbasearchitecture.ui.main.MainContract;
25 | import com.github.mvpbasearchitecture.ui.main.MainPresenter;
26 | import com.github.mvpbasearchitecture.utils.rx.AppSchedulerProvider;
27 | import com.github.mvpbasearchitecture.utils.rx.SchedulerProvider;
28 |
29 | import dagger.Module;
30 | import dagger.Provides;
31 | import io.reactivex.disposables.CompositeDisposable;
32 |
33 | /**
34 | * Modules related to activity
35 | *
36 | * Created by gk.
37 | */
38 |
39 | @Module
40 | public class ActivityModule {
41 |
42 | private AppCompatActivity mActivity;
43 |
44 | public ActivityModule(AppCompatActivity activity) {
45 | this.mActivity = activity;
46 | }
47 |
48 | @Provides
49 | @ActivityContext
50 | Context provideContext() {
51 | return mActivity;
52 | }
53 |
54 | @Provides
55 | AppCompatActivity provideActivity() {
56 | return mActivity;
57 | }
58 |
59 | @Provides
60 | CompositeDisposable provideCompositeDisposable() {
61 | return new CompositeDisposable();
62 | }
63 |
64 | @Provides
65 | SchedulerProvider provideSchedulerProvider() {
66 | return new AppSchedulerProvider();
67 | }
68 |
69 | @Provides
70 | MainContract.Presenter provideMainPresenter(AppRepository appRepository,
71 | SchedulerProvider schedulerProvider,
72 | CompositeDisposable compositeDisposable) {
73 | return new MainPresenter(appRepository, schedulerProvider, compositeDisposable);
74 | }
75 |
76 | }
77 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/di/module/ApplicationModule.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.di.module;
17 |
18 | import android.app.Application;
19 | import android.content.Context;
20 |
21 | import com.github.mvpbasearchitecture.data.source.repository.AppDataRepository;
22 | import com.github.mvpbasearchitecture.data.source.repository.AppRepository;
23 | import com.github.mvpbasearchitecture.di.ApplicationContext;
24 | import com.github.mvpbasearchitecture.di.Remote;
25 |
26 | import javax.inject.Singleton;
27 |
28 | import dagger.Module;
29 | import dagger.Provides;
30 |
31 | /**
32 | * Modules related to application
33 | *
34 | * Created by gk.
35 | */
36 |
37 | @Module
38 | public class ApplicationModule {
39 |
40 | private Application mApplication;
41 |
42 | public ApplicationModule(Application mApplication) {
43 | this.mApplication = mApplication;
44 | }
45 |
46 | @Provides
47 | @ApplicationContext
48 | Context provideContext() {
49 | return mApplication;
50 | }
51 |
52 | @Provides
53 | Application provideApplication() {
54 | return mApplication;
55 | }
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/di/module/DataModule.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.di.module;
17 |
18 | import com.github.mvpbasearchitecture.data.source.db.AppDatabase;
19 | import com.github.mvpbasearchitecture.data.source.db.AppDbOpenHelper;
20 | import com.github.mvpbasearchitecture.data.source.repository.local.AppLocalDataSource;
21 | import com.github.mvpbasearchitecture.data.source.prefs.AppPreferencesHelper;
22 | import com.github.mvpbasearchitecture.data.source.prefs.PreferencesHelper;
23 | import com.github.mvpbasearchitecture.data.source.repository.remote.AppRemoteDataSource;
24 | import com.github.mvpbasearchitecture.data.source.repository.AppDataRepository;
25 | import com.github.mvpbasearchitecture.data.source.repository.AppDataSource;
26 | import com.github.mvpbasearchitecture.data.source.repository.AppRepository;
27 | import com.github.mvpbasearchitecture.di.DatabaseInfo;
28 | import com.github.mvpbasearchitecture.di.Local;
29 | import com.github.mvpbasearchitecture.di.PreferenceInfo;
30 | import com.github.mvpbasearchitecture.di.Remote;
31 | import com.github.mvpbasearchitecture.utils.AppConstants;
32 |
33 | import javax.inject.Singleton;
34 |
35 | import dagger.Module;
36 | import dagger.Provides;
37 |
38 | /**
39 | * Modules related to data and repository
40 | *
41 | * Created by gk.
42 | */
43 |
44 | @Module
45 | public class DataModule {
46 |
47 | @Provides
48 | @Singleton
49 | @DatabaseInfo
50 | String provideDatabaseName() {
51 | return AppConstants.DB_NAME;
52 | }
53 |
54 | @Provides
55 | @Singleton
56 | @PreferenceInfo
57 | String providePreferenceName() {
58 | return AppConstants.PREF_NAME;
59 | }
60 |
61 | @Provides
62 | @Singleton
63 | @Local
64 | AppDataSource provideAppLocalDataSource(AppLocalDataSource appLocalDataSource) {
65 | return appLocalDataSource;
66 | }
67 |
68 | @Provides
69 | @Singleton
70 | @Remote
71 | AppDataSource provideAppRemoteDataSource(AppRemoteDataSource appRemoteDataSource) {
72 | return appRemoteDataSource;
73 | }
74 |
75 | @Provides
76 | @Singleton
77 | AppRepository provideAppRepository(AppDataRepository dataRepository) {
78 | return dataRepository;
79 | }
80 |
81 | @Provides
82 | @Singleton
83 | AppDatabase provideAppDb(AppDbOpenHelper appDbOpenHelper) {
84 | return appDbOpenHelper.getDatabase();
85 | }
86 |
87 | @Provides
88 | @Singleton
89 | PreferencesHelper providePreferencesHelper(AppPreferencesHelper appPreferencesHelper) {
90 | return appPreferencesHelper;
91 | }
92 |
93 | }
94 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/di/module/NetworkModule.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.di.module;
17 |
18 | import com.github.mvpbasearchitecture.BuildConfig;
19 | import com.github.mvpbasearchitecture.data.source.network.APIService;
20 | import com.github.mvpbasearchitecture.data.source.network.NetworkAPIs;
21 | import com.github.mvpbasearchitecture.data.source.network.NetworkUtils;
22 | import com.github.mvpbasearchitecture.data.source.network.APIHelper;
23 | import com.github.mvpbasearchitecture.utils.AppConstants;
24 |
25 | import java.util.concurrent.TimeUnit;
26 |
27 | import javax.inject.Singleton;
28 |
29 | import dagger.Module;
30 | import dagger.Provides;
31 | import okhttp3.OkHttpClient;
32 | import retrofit2.Retrofit;
33 | import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
34 | import retrofit2.converter.gson.GsonConverterFactory;
35 |
36 | /**
37 | * Modules related to network
38 | *
39 | * Created by gk.
40 | */
41 |
42 | @Module
43 | public class NetworkModule {
44 |
45 | private OkHttpClient.Builder client;
46 |
47 | public NetworkModule() {
48 | }
49 |
50 | public NetworkModule(OkHttpClient.Builder client) {
51 | this.client = client;
52 | }
53 |
54 | @Provides
55 | @Singleton
56 | Retrofit provideCall() {
57 | Retrofit retrofit;
58 | if (client == null) {
59 | client = new OkHttpClient.Builder()
60 | .connectTimeout(AppConstants.NETWORK_TIMEOUT_IN_SEC, TimeUnit.SECONDS)
61 | .writeTimeout(AppConstants.NETWORK_TIMEOUT_IN_SEC, TimeUnit.SECONDS)
62 | .readTimeout(AppConstants.NETWORK_TIMEOUT_IN_SEC, TimeUnit.SECONDS);
63 |
64 | retrofit = new Retrofit.Builder()
65 | .baseUrl(BuildConfig.BASE_URL)
66 | .client(NetworkUtils.getHttpClient(client))
67 | .addConverterFactory(GsonConverterFactory.create())
68 | .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
69 | .build();
70 |
71 | }else{
72 | retrofit = new Retrofit.Builder()
73 | .baseUrl(BuildConfig.BASE_URL)
74 | .client(NetworkUtils.getHttpClient())
75 | .addConverterFactory(GsonConverterFactory.create())
76 | .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
77 | .build();
78 | }
79 |
80 | return retrofit;
81 | }
82 |
83 | @Provides
84 | @Singleton
85 | public APIService providesNetworkService(Retrofit retrofit) {
86 | return retrofit.create(APIService.class);
87 | }
88 |
89 | @Provides
90 | @Singleton
91 | public NetworkAPIs providesRetrofitHelper(APIHelper apiHelper) {
92 | return apiHelper;
93 | }
94 |
95 | }
96 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/service/SyncService.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.service;
17 |
18 | import android.app.Service;
19 | import android.content.Context;
20 | import android.content.Intent;
21 | import android.os.IBinder;
22 | import android.support.annotation.Nullable;
23 |
24 | import com.github.mvpbasearchitecture.utils.AppLogger;
25 |
26 | /**
27 | * This is a dummy sync class and is not registered in the manifest for the same reason
28 | * Modify this for your own use cases
29 | *
30 | * Created by gk
31 | */
32 |
33 | public class SyncService extends Service {
34 |
35 | private static final String TAG = "SyncService";
36 |
37 | public static Intent getStartIntent(Context context) {
38 | return new Intent(context, SyncService.class);
39 | }
40 |
41 | public static void start(Context context) {
42 | Intent starter = new Intent(context, SyncService.class);
43 | context.startService(starter);
44 | }
45 |
46 | public static void stop(Context context) {
47 | context.stopService(new Intent(context, SyncService.class));
48 | }
49 |
50 | @Override
51 | public void onCreate() {
52 | super.onCreate();
53 | }
54 |
55 | @Override
56 | public int onStartCommand(Intent intent, int flags, int startId) {
57 | AppLogger.d(TAG, "SyncService started");
58 | return START_STICKY;
59 | }
60 |
61 | @Override
62 | public void onDestroy() {
63 | AppLogger.d(TAG, "SyncService stopped");
64 | super.onDestroy();
65 | }
66 |
67 | @Nullable
68 | @Override
69 | public IBinder onBind(Intent intent) {
70 | return null;
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/ui/adapter/MainItemListAdapter.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.ui.adapter;
17 |
18 | import android.content.Context;
19 | import android.support.v7.widget.RecyclerView;
20 | import android.view.LayoutInflater;
21 | import android.view.View;
22 | import android.view.ViewGroup;
23 | import android.widget.ImageView;
24 | import android.widget.TextView;
25 |
26 | import com.github.mvpbasearchitecture.R;
27 | import com.github.mvpbasearchitecture.data.models.local.Item;
28 | import com.github.mvpbasearchitecture.utils.GeneralUtils;
29 |
30 | import java.util.List;
31 |
32 | /**
33 | * Adapter that used to display {@link Item} in a recycler view
34 | *
35 | * Created by gk
36 | */
37 | public class MainItemListAdapter extends RecyclerView.Adapter {
38 |
39 | private Context mContext;
40 | private List mItems;
41 |
42 | public MainItemListAdapter(Context mContext, List items) {
43 | this.mContext = mContext;
44 | this.mItems = items;
45 | }
46 |
47 | @Override
48 | public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
49 | return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_main, parent, false));
50 | }
51 |
52 | @Override
53 | public void onBindViewHolder(MyViewHolder holder, int position) {
54 | Item item = mItems.get(position);
55 | if (item != null) {
56 | holder.id.setText(item.getItemId());
57 | holder.name.setText(item.getItemName());
58 | GeneralUtils.loadImageFromLink(mContext, holder.image, item.getItemImageLink());
59 | }
60 | }
61 |
62 | @Override
63 | public int getItemCount() {
64 | return mItems.size();
65 | }
66 |
67 | /**
68 | * View Holder for recycler view.
69 | */
70 | class MyViewHolder extends RecyclerView.ViewHolder {
71 | ImageView image;
72 | TextView id, name;
73 |
74 | MyViewHolder(View itemView) {
75 | super(itemView);
76 | image = itemView.findViewById(R.id.item_image);
77 | id = itemView.findViewById(R.id.item_id);
78 | name = itemView.findViewById(R.id.item_name);
79 | }
80 | }
81 |
82 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/ui/custom/CustomTextView.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.ui.custom;
17 |
18 | import android.content.Context;
19 | import android.support.v7.widget.AppCompatTextView;
20 | import android.util.AttributeSet;
21 |
22 | import com.github.mvpbasearchitecture.utils.font.CustomFontHelper;
23 |
24 | /**
25 | * A Custom text view
26 | *
27 | * Created by gk
28 | */
29 | public class CustomTextView extends AppCompatTextView {
30 |
31 | public CustomTextView(Context context) {
32 | super(context);
33 | }
34 |
35 | public CustomTextView(Context context, AttributeSet attrs) {
36 | super(context, attrs);
37 | CustomFontHelper.setCustomFont(this, context, attrs);
38 | }
39 |
40 | public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
41 | super(context, attrs, defStyle);
42 | CustomFontHelper.setCustomFont(this, context, attrs);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/ui/main/MainActivity.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.ui.main;
17 |
18 | import android.os.Bundle;
19 | import android.support.v7.widget.Toolbar;
20 | import android.view.MenuItem;
21 |
22 | import com.github.mvpbasearchitecture.R;
23 | import com.github.mvpbasearchitecture.base.BaseActivity;
24 | import com.github.mvpbasearchitecture.utils.ActivityUtils;
25 | import com.github.mvpbasearchitecture.utils.ViewUtils;
26 |
27 | /**
28 | * The main activity the application when launched
29 | *
30 | * Created by gk
31 | */
32 | public class MainActivity extends BaseActivity {
33 |
34 | @Override
35 | protected void onCreate(Bundle savedInstanceState) {
36 | super.onCreate(savedInstanceState);
37 | setContentView(R.layout.activity_main);
38 |
39 | setupToolbar();
40 |
41 | MainFragment mainFragment = (MainFragment) getSupportFragmentManager().findFragmentById(R.id.content_frame);
42 | if (mainFragment == null){
43 | mainFragment = MainFragment.newInstance();
44 | ActivityUtils.addFragmentToActivity(getSupportFragmentManager(), mainFragment,R.id.content_frame);
45 | }
46 |
47 | }
48 |
49 | private void setupToolbar() {
50 | Toolbar toolbar = findViewById(R.id.toolbar);
51 | setSupportActionBar(toolbar);
52 | getSupportActionBar().setDisplayHomeAsUpEnabled(true);
53 | getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_close_white);
54 | getSupportActionBar().setTitle(getText(R.string.toolbar_title));
55 |
56 | ViewUtils.applyFontForToolbarTitle(this, R.id.toolbar);
57 | }
58 |
59 | @Override
60 | public boolean onOptionsItemSelected(MenuItem item) {
61 | if (item.getItemId() == android.R.id.home) {
62 | finish();
63 | return true;
64 | }
65 | return super.onOptionsItemSelected(item);
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/ui/main/MainContract.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.ui.main;
17 |
18 | import android.support.annotation.NonNull;
19 |
20 | import com.github.mvpbasearchitecture.base.BaseContract;
21 | import com.github.mvpbasearchitecture.data.models.local.Item;
22 |
23 | import java.util.List;
24 |
25 | /**
26 | * The main contract, consists of Screen specific Presenter and View interface
27 | *
28 | * Created by gk
29 | */
30 | public interface MainContract {
31 |
32 | interface View extends BaseContract.View {
33 |
34 | void refreshItemList(@NonNull List itemList);
35 |
36 | void showEmptyListUI();
37 |
38 | }
39 |
40 | interface Presenter extends BaseContract.Presenter {
41 |
42 | void loadItems(boolean refresh);
43 |
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/ui/main/MainFragment.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.ui.main;
17 |
18 | import android.os.Bundle;
19 | import android.support.annotation.NonNull;
20 | import android.support.annotation.Nullable;
21 | import android.support.v7.widget.LinearLayoutManager;
22 | import android.support.v7.widget.RecyclerView;
23 | import android.view.LayoutInflater;
24 | import android.view.View;
25 | import android.view.ViewGroup;
26 | import android.widget.Button;
27 | import android.widget.TextView;
28 |
29 | import com.github.mvpbasearchitecture.R;
30 | import com.github.mvpbasearchitecture.base.BaseMVPFragment;
31 | import com.github.mvpbasearchitecture.data.models.local.Item;
32 | import com.github.mvpbasearchitecture.di.component.ActivityComponent;
33 | import com.github.mvpbasearchitecture.ui.adapter.MainItemListAdapter;
34 |
35 | import java.util.List;
36 |
37 | import javax.inject.Inject;
38 |
39 | /**
40 | * Main Fragment where most of the UI stuff happens
41 | * Extends functionality of {@link BaseMVPFragment}
42 | * Implements Screen specific ui tasks {@link MainContract.View}
43 | *
44 | * Created by gk
45 | */
46 | public class MainFragment extends BaseMVPFragment implements MainContract.View {
47 |
48 | private View inflatedView;
49 |
50 | @Inject
51 | MainContract.Presenter mPresenter;
52 |
53 | private RecyclerView mRecyclerView;
54 | private TextView mEmptyListText;
55 | private Button mRefreshItemBtn;
56 |
57 | public static MainFragment newInstance() {
58 | return new MainFragment();
59 | }
60 |
61 | public MainFragment() {
62 | }
63 |
64 | @Override
65 | public void onActivityCreated(Bundle savedInstanceState) {
66 | super.onActivityCreated(savedInstanceState);
67 | }
68 |
69 | @Nullable
70 | @Override
71 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
72 | this.inflatedView = inflater.inflate(R.layout.fragment_main, container, false);
73 |
74 | initViews();
75 | setupListeners();
76 |
77 | ActivityComponent component = getActivityComponent();
78 | if (component != null) {
79 | component.inject(this);
80 | mPresenter.onAttach(this);
81 |
82 | mPresenter.loadItems(false);
83 | }
84 |
85 | return inflatedView;
86 | }
87 |
88 | private void setupListeners() {
89 | mRefreshItemBtn.setOnClickListener(view -> {
90 | mPresenter.loadItems(true);
91 | });
92 | }
93 |
94 | @Override
95 | protected void initViews() {
96 | //initialize view here
97 | mRecyclerView = inflatedView.findViewById(R.id.item_recycler_view);
98 | mEmptyListText = inflatedView.findViewById(R.id.empty_list_text);
99 | mRefreshItemBtn = inflatedView.findViewById(R.id.refresh_items);
100 | }
101 |
102 | @Override
103 | public void refreshItemList(@NonNull List itemList) {
104 | mRecyclerView.setVisibility(View.VISIBLE);
105 | mEmptyListText.setVisibility(View.GONE);
106 |
107 | LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
108 | MainItemListAdapter itemAdapter = new MainItemListAdapter(getContext(), itemList);
109 |
110 | mRecyclerView.setLayoutManager(linearLayoutManager);
111 | mRecyclerView.setAdapter(itemAdapter);
112 | }
113 |
114 | @Override
115 | public void showEmptyListUI() {
116 | mRecyclerView.setVisibility(View.GONE);
117 | mEmptyListText.setVisibility(View.VISIBLE);
118 | }
119 |
120 | @Override
121 | public void setPresenter(MainContract.Presenter presenter) {
122 | this.mPresenter = presenter;
123 | }
124 |
125 | @Override
126 | public void onDestroy() {
127 | mPresenter.onDetach();
128 | super.onDestroy();
129 | }
130 | }
131 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/ui/main/MainPresenter.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.ui.main;
17 |
18 | import com.github.mvpbasearchitecture.R;
19 | import com.github.mvpbasearchitecture.base.BasePresenter;
20 | import com.github.mvpbasearchitecture.data.source.repository.AppRepository;
21 | import com.github.mvpbasearchitecture.utils.rx.SchedulerProvider;
22 |
23 | import javax.inject.Inject;
24 |
25 | import io.reactivex.disposables.CompositeDisposable;
26 | import io.reactivex.disposables.Disposable;
27 |
28 | /**
29 | * Main Fragment where most of the UI stuff happens
30 | * Extends functionality of {@link BasePresenter}
31 | * Implements Screen specific Presenter tasks {@link MainContract.Presenter}
32 | *
33 | * Created by gk
34 | */
35 | public class MainPresenter extends BasePresenter implements MainContract.Presenter {
36 |
37 | private Disposable mDisposable;
38 |
39 | @Inject
40 | public MainPresenter(AppRepository appRepository,
41 | SchedulerProvider schedulerProvider,
42 | CompositeDisposable compositeDisposable) {
43 | super(appRepository, schedulerProvider, compositeDisposable);
44 | }
45 |
46 | @Override
47 | public void loadItems(boolean refresh) {
48 | getView().showProgressDialog(R.string.please_wait);
49 |
50 | if (refresh)
51 | getDataSource().refreshItems();
52 |
53 | //remove the previous disposable from composite disposable, for multiple load items calls
54 | if (mDisposable != null)
55 | getCompositeDisposable().delete(mDisposable);
56 |
57 | mDisposable = getDataSource().getItemList()
58 | .subscribeOn(getSchedulerProvider().io())
59 | .observeOn(getSchedulerProvider().ui())
60 | .subscribe(items -> {
61 | if (!isViewAttached())
62 | return;
63 |
64 | getView().dismissProgressDialog();
65 | if (items != null && items.size() > 0)
66 | getView().refreshItemList(items);
67 | else
68 | getView().showEmptyListUI();
69 | }, throwable -> {
70 | if (!isViewAttached())
71 | return;
72 |
73 | getView().dismissProgressDialog();
74 | handleApiError(throwable);
75 | });
76 |
77 | getCompositeDisposable().add(mDisposable);
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/utils/ActivityUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.utils;
17 |
18 | import android.support.annotation.NonNull;
19 | import android.support.v4.app.Fragment;
20 | import android.support.v4.app.FragmentManager;
21 | import android.support.v4.app.FragmentTransaction;
22 |
23 | /**
24 | * Utilities for Activities related stuffs
25 | *
26 | * Created by gk
27 | */
28 |
29 | public final class ActivityUtils {
30 |
31 | /**
32 | * The {@code fragment} is added to the container view with id {@code frameId}. The operation is
33 | * performed by the {@code fragmentManager}.
34 | *
35 | */
36 | public static void addFragmentToActivity (@NonNull FragmentManager fragmentManager,
37 | @NonNull Fragment fragment, int frameId) {
38 | FragmentTransaction transaction = fragmentManager.beginTransaction();
39 | transaction.add(frameId, fragment);
40 | transaction.commit();
41 | }
42 |
43 | /**
44 | * The {@code fragment} is added to the container view with id {@code frameId}. The operation is
45 | * performed by the {@code fragmentManager}.
46 | *
47 | */
48 | public static void addFragmentToActivity (@NonNull android.app.FragmentManager fragmentManager,
49 | @NonNull android.app.Fragment fragment, int frameId) {
50 | android.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
51 | transaction.add(frameId, fragment);
52 | transaction.commit();
53 | }
54 |
55 | /**
56 | * The {@code fragment} is added to the activity with tag {@code tag}. The operation is
57 | * performed by the {@code fragmentManager}.
58 | *
59 | */
60 | public static void addFragmentToActivity (@NonNull FragmentManager fragmentManager,
61 | @NonNull Fragment fragment, String tag) {
62 | FragmentTransaction transaction = fragmentManager.beginTransaction();
63 | transaction.add(fragment, tag);
64 | transaction.commit();
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/utils/AppConstants.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.utils;
17 |
18 | /**
19 | * Constants that will be used through the app
20 | *
21 | * Created by gk
22 | */
23 |
24 | public final class AppConstants {
25 |
26 | public static final String DB_NAME = "app.db";
27 | public static final String PREF_NAME = "app_pref";
28 |
29 | public static final long NETWORK_TIMEOUT_IN_SEC = 30;
30 | }
31 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/utils/AppLogger.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.utils;
17 |
18 | import android.util.Log;
19 |
20 | import com.github.mvpbasearchitecture.BuildConfig;
21 |
22 | /**
23 | * An App logger to only log in Debug mode
24 | *
25 | * Created by gk
26 | */
27 |
28 | public class AppLogger {
29 |
30 | private static boolean isDebugMode = true;
31 |
32 | public static void init(){
33 | if (!BuildConfig.DEBUG){
34 | isDebugMode = false;
35 | }
36 | }
37 |
38 | public static void d(String tag, String msg){
39 | if(isDebugMode)
40 | Log.d(tag, msg);
41 | }
42 |
43 | public static void e(String tag, String msg){
44 | if(isDebugMode)
45 | Log.e(tag, msg);
46 | }
47 |
48 | public static void w(String tag, String msg){
49 | if(isDebugMode)
50 | Log.w(tag, msg);
51 | }
52 |
53 | public static void i(String tag, String msg){
54 | if(isDebugMode)
55 | Log.i(tag, msg);
56 | }
57 |
58 | public static void v(String tag, String msg){
59 | if(isDebugMode)
60 | Log.v(tag, msg);
61 | }
62 |
63 | }
64 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/utils/DialogUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.utils;
17 |
18 | import android.app.ProgressDialog;
19 | import android.content.Context;
20 | import android.graphics.Color;
21 | import android.graphics.drawable.ColorDrawable;
22 |
23 | import com.github.mvpbasearchitecture.R;
24 |
25 | /**
26 | * Utilities for Dialog related stuffs
27 | *
28 | * Note: This used the {@link ProgressDialog} that is Deprecated
29 | * Its advisable to use {@link android.app.DialogFragment} to Create your own
30 | * custom progress dialog
31 | *
32 | * Created by gk
33 | */
34 |
35 | @SuppressWarnings("deprecation")
36 | public final class DialogUtils {
37 |
38 | /**
39 | * Creates a Progress Dialog
40 | * @param context
41 | * @return
42 | */
43 | public static ProgressDialog createProgressDialog(Context context) {
44 | ProgressDialog progressDialog = new ProgressDialog(context);
45 | progressDialog.show();
46 | if (progressDialog.getWindow() != null) {
47 | progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
48 | }
49 | progressDialog.setContentView(R.layout.progress_dialog);
50 | progressDialog.setIndeterminate(true);
51 | progressDialog.setCancelable(false);
52 | progressDialog.setCanceledOnTouchOutside(false);
53 | return progressDialog;
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/utils/FileUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.utils;
17 |
18 | import android.content.ContentResolver;
19 | import android.content.Context;
20 | import android.database.Cursor;
21 | import android.net.Uri;
22 | import android.os.Environment;
23 | import android.provider.MediaStore;
24 | import android.support.annotation.NonNull;
25 | import android.support.annotation.RawRes;
26 |
27 | import java.io.BufferedInputStream;
28 | import java.io.File;
29 | import java.io.FileInputStream;
30 | import java.io.FileNotFoundException;
31 | import java.io.FileOutputStream;
32 | import java.io.IOException;
33 | import java.io.InputStream;
34 |
35 | /**
36 | * Utilities for File related stuffs
37 | *
38 | * Created by gk
39 | */
40 |
41 | public final class FileUtils {
42 |
43 | private static final String TAG = "FileUtils";
44 |
45 | public static String getPathFromContentResolver(ContentResolver contentResolver, Uri uri) {
46 |
47 | String path = null;
48 | String[] projection = {MediaStore.Files.FileColumns.DATA};
49 | Cursor cursor = contentResolver.query(uri, projection, null, null, null);
50 |
51 | if (cursor == null) {
52 | path = uri.getPath();
53 | } else {
54 | cursor.moveToFirst();
55 | int column_index = cursor.getColumnIndexOrThrow(projection[0]);
56 | path = cursor.getString(column_index);
57 | cursor.close();
58 | }
59 |
60 | return ((path == null || path.isEmpty()) ? (uri.getPath()) : path);
61 | }
62 |
63 | public static void copyFileFromRawToOthers(@NonNull final Context context, @RawRes int id, @NonNull final String targetPath) {
64 |
65 | //if files already exists, no need to copy
66 | if (checkIfFileExists(targetPath))
67 | return;
68 |
69 | AppLogger.i(TAG, "Copying files from raw to directory!");
70 | InputStream in = context.getResources().openRawResource(id);
71 | FileOutputStream out = null;
72 | try {
73 | out = new FileOutputStream(targetPath);
74 | byte[] buff = new byte[1024];
75 | int read = 0;
76 | while ((read = in.read(buff)) > 0) {
77 | out.write(buff, 0, read);
78 | }
79 | } catch (Exception e) {
80 | e.printStackTrace();
81 | } finally {
82 | try {
83 | if (in != null) {
84 | in.close();
85 | }
86 | if (out != null) {
87 | out.close();
88 | }
89 | } catch (IOException e) {
90 | e.printStackTrace();
91 | }
92 | }
93 | }
94 |
95 | public static byte[] fileToBytes(File file) {
96 | int size = (int) file.length();
97 | byte[] bytes = new byte[size];
98 | try {
99 | BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
100 | buf.read(bytes, 0, bytes.length);
101 | buf.close();
102 | } catch (FileNotFoundException e) {
103 | e.printStackTrace();
104 | } catch (IOException e) {
105 | e.printStackTrace();
106 | }
107 | return bytes;
108 | }
109 |
110 | public static boolean checkIfFileExists(String filePath) {
111 | return new File(filePath).exists();
112 | }
113 |
114 | public static String loadJSONFromAsset(Context context, String fileName) {
115 | String json = null;
116 | try {
117 | InputStream is = context.getAssets().open(fileName);
118 | int size = is.available();
119 | byte[] buffer = new byte[size];
120 | is.read(buffer);
121 | is.close();
122 | json = new String(buffer, "UTF-8");
123 | } catch (IOException ex) {
124 | ex.printStackTrace();
125 | return null;
126 | }
127 | return json;
128 | }
129 |
130 | /* Checks if external storage is available for read and write */
131 | public boolean isExternalStorageWritable() {
132 | String state = Environment.getExternalStorageState();
133 | if (Environment.MEDIA_MOUNTED.equals(state)) {
134 | return true;
135 | }
136 | return false;
137 | }
138 |
139 | /* Checks if external storage is available to at least read */
140 | public boolean isExternalStorageReadable() {
141 | String state = Environment.getExternalStorageState();
142 | if (Environment.MEDIA_MOUNTED.equals(state) ||
143 | Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
144 | return true;
145 | }
146 | return false;
147 | }
148 |
149 | }
150 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/utils/GeneralUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.utils;
17 |
18 | import android.content.Context;
19 | import android.widget.ImageView;
20 |
21 | import com.bumptech.glide.Glide;
22 | import com.bumptech.glide.signature.StringSignature;
23 | import com.github.mvpbasearchitecture.R;
24 |
25 | /**
26 | * Utilities for other general stuffs
27 | *
28 | * Created by gk
29 | */
30 |
31 | public final class GeneralUtils {
32 |
33 | public static boolean checkStringNotEmpty(String str) {
34 | return (str != null && !str.equals(""));
35 | }
36 |
37 | public static void loadImageFromLink(Context context, ImageView pic, String link) {
38 | if (GeneralUtils.checkStringNotEmpty(link)) {
39 | Glide.with(context.getApplicationContext())
40 | .load(link)
41 | .signature(new StringSignature(link))
42 | .dontAnimate()
43 | .placeholder(R.drawable.ic_image)
44 | .into(pic);
45 | }
46 | }
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/utils/KeyboardUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.utils;
17 |
18 | import android.app.Activity;
19 | import android.view.View;
20 | import android.view.inputmethod.InputMethodManager;
21 |
22 | /**
23 | * Utilities for Keyboard related stuffs
24 | *
25 | * Created by gk
26 | */
27 |
28 | public final class KeyboardUtils {
29 |
30 | public static void hideKeyboard(Activity activity) {
31 | InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
32 | //Find the currently focused view, so we can grab the correct window token from it.
33 | View view = activity.getCurrentFocus();
34 | //If no view currently has focus, create a new one, just so we can grab a window token from it
35 | if (view == null) {
36 | view = new View(activity);
37 | }
38 | imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
39 | }
40 |
41 | public static void hideKeyboard(Activity activity, int flags) {
42 | InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
43 | //Find the currently focused view, so we can grab the correct window token from it.
44 | View view = activity.getCurrentFocus();
45 | //If no view currently has focus, create a new one, just so we can grab a window token from it
46 | if (view == null) {
47 | view = new View(activity);
48 | }
49 | imm.hideSoftInputFromWindow(view.getWindowToken(), flags);
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/utils/ScreenUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.utils;
17 |
18 | import android.app.Activity;
19 | import android.graphics.Point;
20 | import android.view.Display;
21 | import android.view.View;
22 | import android.view.Window;
23 | import android.view.WindowManager;
24 |
25 | /**
26 | * Utilities for Screen related stuffs
27 | *
28 | * Created by gk
29 | */
30 |
31 | public final class ScreenUtils {
32 |
33 | /**
34 | * Convenience method for getting into Immersive mode.
35 | */
36 | public static void goFullscreen(Window window) {
37 | window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
38 | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
39 | window.getDecorView()
40 | .setSystemUiVisibility(
41 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
42 | | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
43 | | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
44 | | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
45 | | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
46 | | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
47 | );
48 | }
49 |
50 | /**
51 | * Get Device Screen width in pixels
52 | */
53 | public static int getScreenWidth(Activity activity){
54 | Display display = activity.getWindowManager().getDefaultDisplay();
55 | Point size = new Point();
56 | display.getSize(size);
57 | return size.x;
58 | }
59 |
60 | /**
61 | * Get Device Screen height in pixels
62 | */
63 | public static int getScreenHeight(Activity activity){
64 | Display display = activity.getWindowManager().getDefaultDisplay();
65 | Point size = new Point();
66 | display.getSize(size);
67 | return size.y;
68 | }
69 |
70 | }
71 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/utils/ViewUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.utils;
17 |
18 | import android.app.Activity;
19 | import android.content.res.Resources;
20 | import android.graphics.Color;
21 | import android.graphics.Typeface;
22 | import android.support.v7.widget.Toolbar;
23 | import android.util.TypedValue;
24 | import android.view.View;
25 | import android.widget.TextView;
26 |
27 | import com.github.mvpbasearchitecture.R;
28 | import com.github.mvpbasearchitecture.utils.font.TypefaceHelper;
29 |
30 | /**
31 | * Utilities for View related stuffs
32 | *
33 | * Created by gk
34 | */
35 |
36 | public final class ViewUtils {
37 |
38 | public static float pxToDp(float px) {
39 | float densityDpi = Resources.getSystem().getDisplayMetrics().densityDpi;
40 | return px / (densityDpi / 160f);
41 | }
42 |
43 | public static int dpToPx(float dp) {
44 | float density = Resources.getSystem().getDisplayMetrics().density;
45 | return Math.round(dp * density);
46 | }
47 |
48 | public static void applyFontForToolbarTitle(Activity context, int resId) {
49 | Toolbar toolbar = (Toolbar) context.findViewById(resId);
50 | for (int i = 0; i < toolbar.getChildCount(); i++) {
51 | View view = toolbar.getChildAt(i);
52 | if (view instanceof TextView) {
53 | TextView tv = (TextView) view;
54 | Typeface titleFont = TypefaceHelper.get(context.getAssets(), "fonts/OpenSans-SemiBold.ttf");
55 | if (tv.getText().equals(toolbar.getTitle())) {
56 | tv.setTypeface(titleFont);
57 | tv.setTextColor(Color.WHITE);
58 | tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources().getDimension(R.dimen.toolbar_title));
59 | break;
60 | }
61 | }
62 | }
63 | }
64 |
65 | }
66 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/utils/font/CustomFontHelper.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.utils.font;
17 |
18 | import android.content.Context;
19 | import android.content.res.TypedArray;
20 | import android.graphics.Typeface;
21 | import android.util.AttributeSet;
22 | import android.widget.TextView;
23 |
24 | import com.github.mvpbasearchitecture.R;
25 |
26 | /**
27 | * Helper function to set Custom fonts
28 | *
29 | * Created by gk
30 | */
31 |
32 | public class CustomFontHelper {
33 |
34 | /**
35 | * Sets a font on a textview based on the custom com.my.package:typeface attribute
36 | * If the custom font attribute isn't found in the attributes nothing happens
37 | * @param textview
38 | * @param context
39 | * @param attrs
40 | */
41 | public static void setCustomFont(TextView textview, Context context, AttributeSet attrs) {
42 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomFont);
43 | String font = a.getString(R.styleable.CustomFont_typeface);
44 | setCustomFont(textview, font, context);
45 | a.recycle();
46 | }
47 |
48 | /**
49 | * Sets a font on a textview
50 | * @param textview
51 | * @param font
52 | * @param context
53 | */
54 | public static void setCustomFont(TextView textview, String font, Context context) {
55 | if(font == null) {
56 | font = "fonts/OpenSans-Light.ttf";
57 | }
58 | Typeface tf = FontCache.get(font, context);
59 | if(tf != null) {
60 | textview.setTypeface(tf);
61 | }
62 | }
63 |
64 | }
65 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/utils/font/FontCache.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.utils.font;
17 |
18 | import android.content.Context;
19 | import android.graphics.Typeface;
20 |
21 | import java.util.Hashtable;
22 |
23 | /**
24 | * Helper function to cache font
25 | *
26 | * Created by gk
27 | */
28 |
29 | public class FontCache {
30 |
31 | private static Hashtable fontCache = new Hashtable();
32 |
33 | public static Typeface get(String name, Context context) {
34 | Typeface tf = fontCache.get(name);
35 | if(tf == null) {
36 | try {
37 | tf = Typeface.createFromAsset(context.getAssets(), name);
38 | }
39 | catch (Exception e) {
40 | return null;
41 | }
42 | fontCache.put(name, tf);
43 | }
44 | return tf;
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/utils/font/FontChangeCrawler.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.utils.font;
17 |
18 | import android.content.res.AssetManager;
19 | import android.graphics.Typeface;
20 | import android.view.View;
21 | import android.view.ViewGroup;
22 | import android.widget.TextView;
23 |
24 | /**
25 | * Used to change the font of a {@link ViewGroup} and its child
26 | *
27 | * Created by gk
28 | */
29 |
30 | public class FontChangeCrawler
31 | {
32 | private Typeface typeface;
33 |
34 | public FontChangeCrawler(Typeface typeface)
35 | {
36 | this.typeface = typeface;
37 | }
38 |
39 | public FontChangeCrawler(AssetManager assets, String assetsFontFileName)
40 | {
41 | typeface = TypefaceHelper.get(assets,assetsFontFileName);
42 | }
43 |
44 | public void replaceFonts(ViewGroup viewTree)
45 | {
46 | View child;
47 | for(int i = 0; i < viewTree.getChildCount(); ++i)
48 | {
49 | child = viewTree.getChildAt(i);
50 | if(child instanceof ViewGroup)
51 | {
52 | // recursive call
53 | replaceFonts((ViewGroup)child);
54 | }
55 | else if(child instanceof TextView)
56 | {
57 | // base case
58 | ((TextView) child).setTypeface(typeface);
59 | }
60 | }
61 | }
62 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/utils/font/TypefaceHelper.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.utils.font;
17 |
18 | import android.content.res.AssetManager;
19 | import android.graphics.Typeface;
20 |
21 | import com.github.mvpbasearchitecture.utils.AppLogger;
22 |
23 | import java.util.Hashtable;
24 |
25 | /**
26 | * Another helper function to cache and provide typeface
27 | *
28 | * Created by gk
29 | */
30 |
31 | public class TypefaceHelper {
32 | private static final String TAG = "TypefaceHelper";
33 |
34 | private static final Hashtable cache = new Hashtable();
35 |
36 | public static Typeface get(AssetManager am, String assetPath) {
37 | if (!cache.containsKey(assetPath)) {
38 | try {
39 | Typeface t = Typeface.createFromAsset(am,
40 | assetPath);
41 | cache.put(assetPath, t);
42 | } catch (Exception e) {
43 | AppLogger.e(TAG, "Could not get typeface '" + assetPath
44 | + "' because " + e.getMessage());
45 | return null;
46 | }
47 | }
48 | return cache.get(assetPath);
49 | }
50 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/utils/rx/AppSchedulerProvider.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.utils.rx;
17 |
18 | import io.reactivex.Scheduler;
19 | import io.reactivex.android.schedulers.AndroidSchedulers;
20 | import io.reactivex.schedulers.Schedulers;
21 |
22 | /**
23 | * Helper class to provide {@link Schedulers} for RxJava related operations
24 | *
25 | * Created by gk.
26 | */
27 |
28 | public class AppSchedulerProvider implements SchedulerProvider {
29 |
30 | @Override
31 | public Scheduler ui() {
32 | return AndroidSchedulers.mainThread();
33 | }
34 |
35 | @Override
36 | public Scheduler computation() {
37 | return Schedulers.computation();
38 | }
39 |
40 | @Override
41 | public Scheduler io() {
42 | return Schedulers.io();
43 | }
44 |
45 | @Override
46 | public Scheduler trampoline() {
47 | return Schedulers.trampoline();
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/mvpbasearchitecture/utils/rx/SchedulerProvider.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2018 Gaurav Kumar
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | */
16 | package com.github.mvpbasearchitecture.utils.rx;
17 |
18 | import io.reactivex.Scheduler;
19 |
20 | /**
21 | * Created by gk.
22 | */
23 |
24 | public interface SchedulerProvider {
25 |
26 | Scheduler ui();
27 |
28 | Scheduler computation();
29 |
30 | Scheduler io();
31 |
32 | Scheduler trampoline();
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/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_close_white.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_image.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/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 |
8 |
9 |
13 |
14 |
18 |
19 |
20 |
26 |
27 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
16 |
17 |
34 |
35 |
46 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
18 |
19 |
32 |
33 |
47 |
48 |
54 |
55 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/progress_dialog.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
14 |
--------------------------------------------------------------------------------
/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/gauravk95/mvp-android-template/8f55f4d62a58f4b09d70497acb30fded033fa69e/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gauravk95/mvp-android-template/8f55f4d62a58f4b09d70497acb30fded033fa69e/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gauravk95/mvp-android-template/8f55f4d62a58f4b09d70497acb30fded033fa69e/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gauravk95/mvp-android-template/8f55f4d62a58f4b09d70497acb30fded033fa69e/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gauravk95/mvp-android-template/8f55f4d62a58f4b09d70497acb30fded033fa69e/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gauravk95/mvp-android-template/8f55f4d62a58f4b09d70497acb30fded033fa69e/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gauravk95/mvp-android-template/8f55f4d62a58f4b09d70497acb30fded033fa69e/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gauravk95/mvp-android-template/8f55f4d62a58f4b09d70497acb30fded033fa69e/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gauravk95/mvp-android-template/8f55f4d62a58f4b09d70497acb30fded033fa69e/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gauravk95/mvp-android-template/8f55f4d62a58f4b09d70497acb30fded033fa69e/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 | #ffffff
7 | #2a2a2a
8 | #E5E5E5
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16sp
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | MVPBaseArchitecture
3 | MVP Sample
4 |
5 | No items in the list!
6 |
7 | Something went wrong! Please try again.
8 | Please wait…
9 | Refresh
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
15 |
18 |
21 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/app/src/test/java/com/github/mvpbasearchitecture/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.github.mvpbasearchitecture;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 |
5 | repositories {
6 | google()
7 | jcenter()
8 | }
9 |
10 | dependencies {
11 | classpath 'com.android.tools.build:gradle:3.1.3'
12 |
13 |
14 | // NOTE: Do not place your application dependencies here; they belong
15 | // in the individual module build.gradle files
16 | }
17 | }
18 |
19 | allprojects {
20 | repositories {
21 | google()
22 | jcenter()
23 | }
24 | }
25 |
26 | task clean(type: Delete) {
27 | delete rootProject.buildDir
28 | }
29 |
30 | ext {
31 |
32 | //sdk and tools
33 | buildToolsVersion = '27.0.3'
34 | compileSdkVersion = 27
35 | minSdkVersion = 19
36 | targetSdkVersion = 23
37 |
38 | //dependencies
39 | playServicesVersion = '11.8.0'
40 | supportLibraryVersion = '27.0.2'
41 | rxJavaVersion = '2.1.3'
42 | rxAndroidVersion = '2.0.1'
43 | constraintLayoutVersion = '1.1.2'
44 | dagger2Version = '2.16'
45 | roomDBVersion = '1.1.1'
46 | gsonVersion = '2.7'
47 | retrofitVersion = '2.4.0'
48 | glideVersion = '3.7.0'
49 | glideOkhttpIntegrationVersion = '1.5.0'
50 |
51 | //test dependencies
52 | expressoVersion = '3.0.1'
53 | testRunnerVersion = '1.0.1'
54 | jUnitVersion = '4.12'
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gauravk95/mvp-android-template/8f55f4d62a58f4b09d70497acb30fded033fa69e/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Aug 20 15:29:16 IST 2018
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.4-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/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 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
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 Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/sample.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gauravk95/mvp-android-template/8f55f4d62a58f4b09d70497acb30fded033fa69e/sample.gif
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------