├── .gitignore ├── .idea ├── caches │ └── build_file_checksums.ser ├── encodings.xml ├── gradle.xml ├── misc.xml ├── runConfigurations.xml └── vcs.xml ├── LICENSE ├── app ├── .gitignore ├── build.gradle ├── img │ └── Espresso.jpeg ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── anjan │ │ └── architecturecomponent │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── anjan │ │ │ └── architecturecomponent │ │ │ ├── MoviesDatabase.java │ │ │ ├── dao │ │ │ ├── DirectorDao.java │ │ │ └── MovieDao.java │ │ │ ├── entity │ │ │ ├── DateTypeConverter.java │ │ │ ├── DirectorEntity.java │ │ │ └── MovieEntity.java │ │ │ └── ui │ │ │ ├── add │ │ │ ├── AddActivity.java │ │ │ └── AddMoviesViewModel.java │ │ │ └── main │ │ │ ├── MainActivity.java │ │ │ ├── MainFactory.java │ │ │ ├── MoviesListAdapter.java │ │ │ └── MoviesViewModel.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ ├── ic_add_black_24dp.xml │ │ ├── ic_dashboard_black_24dp.xml │ │ ├── ic_home_black_24dp.xml │ │ ├── ic_launcher_background.xml │ │ └── ic_notifications_black_24dp.xml │ │ ├── layout │ │ ├── activity_add.xml │ │ ├── activity_main.xml │ │ └── item_list_movie.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── raw │ │ └── movies.json │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── anjan │ └── architecturecomponent │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── readme.md └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/libraries 5 | /.idea/modules.xml 6 | /.idea/workspace.xml 7 | .DS_Store 8 | /build 9 | /captures 10 | .externalNativeBuild 11 | -------------------------------------------------------------------------------- /.idea/caches/build_file_checksums.ser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anjandebnath/ArchitectureComponent/8868ad4dc9a14f4b153098b01cd8f97d13465c67/.idea/caches/build_file_checksums.ser -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 36 | 37 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 94 | 106 | 107 | 108 | 109 | 110 | 111 | 113 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 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 [yyyy] [name of copyright owner] 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 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 27 5 | defaultConfig { 6 | applicationId "com.anjan.architecturecomponent" 7 | minSdkVersion 15 8 | targetSdkVersion 27 9 | versionCode 1 10 | versionName "1.0" 11 | multiDexEnabled true 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | //to use lambda expression 21 | compileOptions { 22 | sourceCompatibility JavaVersion.VERSION_1_8 23 | targetCompatibility JavaVersion.VERSION_1_8 24 | } 25 | dexOptions { 26 | } 27 | buildToolsVersion '27.0.3' 28 | } 29 | 30 | dependencies { 31 | 32 | def lifecycle_version = "1.1.1" 33 | def room_version = "1.1.1" 34 | def paging_version = "1.0.1" 35 | def support_version = "27.1.1" 36 | def constraint_version = "1.1.2" 37 | def junit_version = "4.12" 38 | def database_debug = "1.0.2" 39 | 40 | implementation fileTree(include: ['*.jar'], dir: 'libs') 41 | implementation "com.android.support:appcompat-v7:$support_version" 42 | implementation "com.android.support:design:$support_version" 43 | implementation "com.android.support:recyclerview-v7:$support_version" 44 | 45 | implementation "com.android.support.constraint:constraint-layout:$constraint_version" 46 | testImplementation "junit:junit:$junit_version" 47 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 48 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 49 | 50 | // ViewModel and LiveData 51 | implementation "android.arch.lifecycle:runtime:$lifecycle_version" 52 | implementation "android.arch.lifecycle:extensions:$lifecycle_version" 53 | annotationProcessor "android.arch.lifecycle:compiler:$lifecycle_version" 54 | // Room 55 | implementation "android.arch.persistence.room:runtime:$room_version" 56 | implementation "android.arch.persistence.room:compiler:$room_version" 57 | annotationProcessor "android.arch.persistence.room:compiler:$room_version" 58 | 59 | // Paging 60 | implementation "android.arch.paging:runtime:$paging_version" 61 | debugImplementation "com.awesomedroidapps:inappstoragereader:$database_debug" 62 | 63 | implementation 'com.jakewharton:butterknife:8.8.1' 64 | implementation 'com.jakewharton:butterknife-annotations:8.8.1' 65 | } 66 | -------------------------------------------------------------------------------- /app/img/Espresso.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anjandebnath/ArchitectureComponent/8868ad4dc9a14f4b153098b01cd8f97d13465c67/app/img/Espresso.jpeg -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/anjan/architecturecomponent/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.anjan.architecturecomponent; 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.anjan.architecturecomponent", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /app/src/main/java/com/anjan/architecturecomponent/MoviesDatabase.java: -------------------------------------------------------------------------------- 1 | package com.anjan.architecturecomponent; 2 | 3 | import android.arch.persistence.db.SupportSQLiteDatabase; 4 | import android.arch.persistence.room.Database; 5 | import android.arch.persistence.room.Room; 6 | import android.arch.persistence.room.RoomDatabase; 7 | import android.arch.persistence.room.TypeConverters; 8 | import android.content.Context; 9 | import android.support.annotation.NonNull; 10 | import android.support.annotation.WorkerThread; 11 | import android.util.Log; 12 | 13 | import com.anjan.architecturecomponent.dao.DirectorDao; 14 | import com.anjan.architecturecomponent.dao.MovieDao; 15 | import com.anjan.architecturecomponent.entity.DateTypeConverter; 16 | import com.anjan.architecturecomponent.entity.DirectorEntity; 17 | import com.anjan.architecturecomponent.entity.MovieEntity; 18 | 19 | import org.json.JSONArray; 20 | import org.json.JSONException; 21 | import org.json.JSONObject; 22 | 23 | import java.io.BufferedReader; 24 | import java.io.IOException; 25 | import java.io.InputStream; 26 | import java.io.InputStreamReader; 27 | import java.util.ArrayList; 28 | import java.util.Date; 29 | import java.util.List; 30 | import java.util.concurrent.Executors; 31 | 32 | /** 33 | * Created by Anjan Debnath on 8/17/2018. 34 | * Copyright (c) 2018, W3 Engineers Ltd. All rights reserved. 35 | */ 36 | 37 | @Database(entities = {MovieEntity.class, DirectorEntity.class}, version = 1) 38 | @TypeConverters({DateTypeConverter.class}) 39 | public abstract class MoviesDatabase extends RoomDatabase { 40 | 41 | private static MoviesDatabase INSTANCE; 42 | private static final String DB_NAME = "movies.db"; 43 | 44 | public static MoviesDatabase getDatabase(final Context context) { 45 | if (INSTANCE == null) { 46 | synchronized (MoviesDatabase.class) { 47 | if (INSTANCE == null) { 48 | INSTANCE = Room.databaseBuilder(context.getApplicationContext(), 49 | MoviesDatabase.class, DB_NAME) 50 | .allowMainThreadQueries() // SHOULD NOT BE USED IN PRODUCTION !!! 51 | .addCallback(new RoomDatabase.Callback() { 52 | @Override 53 | public void onCreate(@NonNull SupportSQLiteDatabase db) { 54 | super.onCreate(db); 55 | Log.d("MoviesDatabase", "populating with data..."); 56 | Executors.newSingleThreadScheduledExecutor().execute(() -> fillWithDemoData(context)); 57 | } 58 | }) 59 | .build(); 60 | } 61 | } 62 | } 63 | 64 | return INSTANCE; 65 | } 66 | 67 | 68 | 69 | public abstract MovieDao movieDao(); 70 | 71 | public abstract DirectorDao directorDao(); 72 | 73 | 74 | @WorkerThread 75 | private static void fillWithDemoData(Context context) { 76 | 77 | MovieDao movieDao = getDatabase(context).movieDao(); 78 | DirectorDao directorDao = getDatabase(context).directorDao(); 79 | 80 | JSONArray movie = loadJsonArray(context); 81 | 82 | try { 83 | List allMovies = new ArrayList<>(); 84 | 85 | for (int i = 0; i < movie.length(); i++) { 86 | 87 | JSONObject item = movie.getJSONObject(i); 88 | 89 | String fullName = item.getString("full_name"); 90 | DirectorEntity directorEntity = new DirectorEntity(fullName); 91 | String movieName = item.getString("movieName"); 92 | MovieEntity movieEntity = new MovieEntity(movieName, (int) directorDao.insert(directorEntity)); 93 | 94 | 95 | movieDao.insert(movieEntity); 96 | 97 | allMovies.add(movieEntity); 98 | 99 | } 100 | 101 | //movieDao.insertAll(allMovies.toArray(new MovieEntity[allMovies.size()])); 102 | 103 | } catch (JSONException exception) { 104 | exception.printStackTrace(); 105 | } 106 | } 107 | 108 | private static JSONArray loadJsonArray(Context context) { 109 | StringBuilder builder = new StringBuilder(); 110 | InputStream in = context.getResources().openRawResource(R.raw.movies); 111 | BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 112 | 113 | String line; 114 | try { 115 | while ((line = reader.readLine()) != null) { 116 | builder.append(line); 117 | } 118 | JSONObject json = new JSONObject(builder.toString()); 119 | return json.getJSONArray("movies"); 120 | 121 | } catch (IOException | JSONException exception) { 122 | exception.printStackTrace(); 123 | } 124 | 125 | return null; 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /app/src/main/java/com/anjan/architecturecomponent/dao/DirectorDao.java: -------------------------------------------------------------------------------- 1 | package com.anjan.architecturecomponent.dao; 2 | 3 | import android.arch.lifecycle.LiveData; 4 | import android.arch.persistence.room.Dao; 5 | import android.arch.persistence.room.Insert; 6 | import android.arch.persistence.room.OnConflictStrategy; 7 | import android.arch.persistence.room.Query; 8 | import android.arch.persistence.room.Update; 9 | 10 | import com.anjan.architecturecomponent.entity.DirectorEntity; 11 | 12 | import java.util.List; 13 | 14 | /** 15 | * Created by Anjan Debnath on 8/17/2018. 16 | * Copyright (c) 2018, W3 Engineers Ltd. All rights reserved. 17 | */ 18 | 19 | @Dao 20 | public interface DirectorDao { 21 | 22 | @Query("SELECT * FROM director WHERE dirID = :id LIMIT 1") 23 | DirectorEntity findDirectorById(int id); 24 | 25 | @Query("SELECT * FROM director WHERE full_name = :fullName LIMIT 1") 26 | DirectorEntity findDirectorByName(String fullName); 27 | 28 | @Insert(onConflict = OnConflictStrategy.IGNORE) 29 | long insert(DirectorEntity director); 30 | 31 | @Insert(onConflict = OnConflictStrategy.IGNORE) 32 | void insert(DirectorEntity... directors); 33 | 34 | @Update(onConflict = OnConflictStrategy.IGNORE) 35 | void update(DirectorEntity director); 36 | 37 | @Query("DELETE FROM director") 38 | void deleteAll(); 39 | 40 | @Query("SELECT * FROM director ORDER BY full_name ASC") 41 | LiveData> getAllDirectors(); 42 | } 43 | -------------------------------------------------------------------------------- /app/src/main/java/com/anjan/architecturecomponent/dao/MovieDao.java: -------------------------------------------------------------------------------- 1 | package com.anjan.architecturecomponent.dao; 2 | 3 | import android.arch.lifecycle.LiveData; 4 | import android.arch.persistence.room.Dao; 5 | import android.arch.persistence.room.Insert; 6 | import android.arch.persistence.room.OnConflictStrategy; 7 | import android.arch.persistence.room.Query; 8 | import android.arch.persistence.room.Update; 9 | 10 | import com.anjan.architecturecomponent.entity.MovieEntity; 11 | 12 | import java.util.List; 13 | 14 | /** 15 | * Created by Anjan Debnath on 8/17/2018. 16 | * Copyright (c) 2018, W3 Engineers Ltd. All rights reserved. 17 | */ 18 | 19 | @Dao 20 | public interface MovieDao { 21 | 22 | @Query("SELECT * FROM movie WHERE movieName = :title LIMIT 1") 23 | MovieEntity findMovieByTitle(String title); 24 | 25 | @Insert(onConflict = OnConflictStrategy.REPLACE) 26 | void insertAll(MovieEntity... movieEntity); 27 | 28 | @Insert(onConflict = OnConflictStrategy.REPLACE) 29 | void insert(MovieEntity movieEntity); 30 | 31 | @Update(onConflict = OnConflictStrategy.IGNORE) 32 | void update(MovieEntity movieEntity); 33 | 34 | @Query("DELETE FROM movie") 35 | void deleteAll(); 36 | 37 | @Query("SELECT * FROM movie ORDER BY movieName ASC") 38 | LiveData> getAllMovies(); 39 | 40 | //@Query("select * from movie ORDER BY date(releaseDate) asc") 41 | @Query("select * from movie where releaseDate like :date") 42 | LiveData> getAllMoviesByDate(String date); 43 | } 44 | -------------------------------------------------------------------------------- /app/src/main/java/com/anjan/architecturecomponent/entity/DateTypeConverter.java: -------------------------------------------------------------------------------- 1 | package com.anjan.architecturecomponent.entity; 2 | 3 | import android.arch.persistence.room.TypeConverter; 4 | 5 | import java.text.DateFormat; 6 | import java.text.ParseException; 7 | import java.text.SimpleDateFormat; 8 | import java.util.Date; 9 | 10 | /** 11 | * Created by Anjan Debnath on 11/13/2018. 12 | * Copyright (c) 2018, W3 Engineers Ltd. All rights reserved. 13 | */ 14 | public class DateTypeConverter { 15 | 16 | private static DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 17 | 18 | @TypeConverter 19 | public static Date stringTimeToDate(String value) { 20 | if (value != null) { 21 | try { 22 | return df.parse(value); 23 | } catch (ParseException e) { 24 | e.printStackTrace(); 25 | } 26 | return null; 27 | } else { 28 | return null; 29 | } 30 | } 31 | 32 | @TypeConverter 33 | public static String dateToStringTime(Date value) { 34 | 35 | return value == null ? null : df.format(value); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /app/src/main/java/com/anjan/architecturecomponent/entity/DirectorEntity.java: -------------------------------------------------------------------------------- 1 | package com.anjan.architecturecomponent.entity; 2 | 3 | import android.arch.persistence.room.ColumnInfo; 4 | import android.arch.persistence.room.Entity; 5 | import android.arch.persistence.room.Ignore; 6 | import android.arch.persistence.room.Index; 7 | import android.arch.persistence.room.PrimaryKey; 8 | import android.support.annotation.NonNull; 9 | 10 | /** 11 | * Created by Anjan Debnath on 8/17/2018. 12 | * Copyright (c) 2018, W3 Engineers Ltd. All rights reserved. 13 | */ 14 | 15 | @Entity(tableName = "director", 16 | indices = {@Index(value = "full_name", unique = true)}) 17 | public class DirectorEntity { 18 | 19 | @PrimaryKey(autoGenerate = true) 20 | @ColumnInfo(name = "dirID") 21 | public int id; 22 | 23 | @Ignore //Room will not persist that field. 24 | public int age; 25 | 26 | @ColumnInfo(name = "full_name") 27 | public String fullName; 28 | 29 | public DirectorEntity(@NonNull String fullName) { 30 | this.fullName = fullName; 31 | } 32 | 33 | public int getId() { 34 | return id; 35 | } 36 | 37 | public void setId(int id) { 38 | this.id = id; 39 | } 40 | 41 | public int getAge() { 42 | return age; 43 | } 44 | 45 | public void setAge(int age) { 46 | this.age = age; 47 | } 48 | 49 | public String getFullName() { 50 | return fullName; 51 | } 52 | 53 | public void setFullName(String fullName) { 54 | this.fullName = fullName; 55 | } 56 | 57 | 58 | 59 | 60 | 61 | } 62 | -------------------------------------------------------------------------------- /app/src/main/java/com/anjan/architecturecomponent/entity/MovieEntity.java: -------------------------------------------------------------------------------- 1 | package com.anjan.architecturecomponent.entity; 2 | 3 | import android.arch.persistence.room.ColumnInfo; 4 | import android.arch.persistence.room.Entity; 5 | import android.arch.persistence.room.ForeignKey; 6 | import android.arch.persistence.room.Index; 7 | import android.arch.persistence.room.PrimaryKey; 8 | import android.arch.persistence.room.TypeConverters; 9 | import android.support.annotation.NonNull; 10 | 11 | import java.util.Date; 12 | 13 | /** 14 | * Created by Anjan Debnath on 8/17/2018. 15 | * Copyright (c) 2018, W3 Engineers Ltd. All rights reserved. 16 | */ 17 | 18 | @Entity(tableName = "movie", 19 | foreignKeys = @ForeignKey(entity = DirectorEntity.class, 20 | parentColumns = "dirID", 21 | childColumns = "directorId", 22 | onDelete = ForeignKey.CASCADE), 23 | indices = {@Index("movieName"), @Index("directorId")}) 24 | public class MovieEntity { 25 | 26 | 27 | @PrimaryKey(autoGenerate = true) 28 | @ColumnInfo(name = "movID") 29 | public int id; 30 | 31 | @ColumnInfo(name = "movieName") 32 | public String movieName; 33 | 34 | @ColumnInfo(name = "directorId") 35 | public int directorId; 36 | 37 | 38 | @ColumnInfo(name = "releaseDate") 39 | @TypeConverters({DateTypeConverter.class}) 40 | public Date releaseDate; 41 | 42 | public MovieEntity(@NonNull String movieName, int directorId) { 43 | this.movieName = movieName; 44 | this.directorId = directorId; 45 | } 46 | 47 | public int getId() { 48 | return id; 49 | } 50 | 51 | public void setId(int id) { 52 | this.id = id; 53 | } 54 | 55 | public Date getReleaseDate() { 56 | return releaseDate; 57 | } 58 | 59 | public void setReleaseDate(Date releaseDate) { 60 | this.releaseDate = releaseDate; 61 | } 62 | 63 | 64 | public String getMovieName() { 65 | return movieName; 66 | } 67 | 68 | public void setMovieName(String movieName) { 69 | this.movieName = movieName; 70 | } 71 | 72 | public int getDirectorId() { 73 | return directorId; 74 | } 75 | 76 | public void setDirectorId(int directorId) { 77 | this.directorId = directorId; 78 | } 79 | 80 | 81 | } 82 | -------------------------------------------------------------------------------- /app/src/main/java/com/anjan/architecturecomponent/ui/add/AddActivity.java: -------------------------------------------------------------------------------- 1 | package com.anjan.architecturecomponent.ui.add; 2 | 3 | import android.arch.lifecycle.ViewModelProviders; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.View; 7 | import android.widget.Button; 8 | import android.widget.EditText; 9 | 10 | import com.anjan.architecturecomponent.R; 11 | import com.anjan.architecturecomponent.entity.DirectorEntity; 12 | import com.anjan.architecturecomponent.entity.MovieEntity; 13 | import com.anjan.architecturecomponent.ui.main.MoviesViewModel; 14 | 15 | public class AddActivity extends AppCompatActivity { 16 | 17 | EditText editTextMovie; 18 | EditText editTextDirector; 19 | Button buttonSave; 20 | 21 | AddMoviesViewModel addMoviesViewModel; 22 | int directorId = -1; 23 | 24 | @Override 25 | protected void onCreate(Bundle savedInstanceState) { 26 | super.onCreate(savedInstanceState); 27 | setContentView(R.layout.activity_add); 28 | 29 | editTextMovie = findViewById(R.id.editText_movie); 30 | editTextDirector = findViewById(R.id.editText_director); 31 | buttonSave = findViewById(R.id.button_save); 32 | 33 | addMoviesViewModel = ViewModelProviders.of(this).get(AddMoviesViewModel.class); 34 | 35 | buttonSave.setOnClickListener(new View.OnClickListener() { 36 | @Override 37 | public void onClick(View v) { 38 | 39 | String movie = editTextMovie.getEditableText().toString(); 40 | String director = editTextDirector.getEditableText().toString(); 41 | DirectorEntity directorEntity = new DirectorEntity(director); 42 | 43 | //check duplicate id can not be inserted 44 | directorId = addMoviesViewModel.findDirector(director); 45 | if( directorId < 0){ 46 | directorId = (int) addMoviesViewModel.insertDirector(directorEntity); 47 | } 48 | MovieEntity movieEntity = new MovieEntity(movie, directorId); 49 | addMoviesViewModel.insertMovie(movieEntity); 50 | 51 | 52 | editTextMovie.setText(""); 53 | editTextDirector.setText(""); 54 | } 55 | }); 56 | 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /app/src/main/java/com/anjan/architecturecomponent/ui/add/AddMoviesViewModel.java: -------------------------------------------------------------------------------- 1 | package com.anjan.architecturecomponent.ui.add; 2 | 3 | import android.app.Application; 4 | import android.arch.lifecycle.AndroidViewModel; 5 | import android.arch.lifecycle.ViewModel; 6 | import android.support.annotation.NonNull; 7 | 8 | import com.anjan.architecturecomponent.MoviesDatabase; 9 | import com.anjan.architecturecomponent.dao.DirectorDao; 10 | import com.anjan.architecturecomponent.dao.MovieDao; 11 | import com.anjan.architecturecomponent.entity.DirectorEntity; 12 | import com.anjan.architecturecomponent.entity.MovieEntity; 13 | 14 | /** 15 | * Created by Anjan Debnath on 8/17/2018. 16 | * Copyright (c) 2018, W3 Engineers Ltd. All rights reserved. 17 | */ 18 | public class AddMoviesViewModel extends AndroidViewModel { 19 | 20 | private MovieDao movieDao; 21 | private DirectorDao directorDao; 22 | 23 | public AddMoviesViewModel(@NonNull Application application) { 24 | super(application); 25 | 26 | movieDao = MoviesDatabase.getDatabase(application).movieDao(); 27 | directorDao = MoviesDatabase.getDatabase(application).directorDao(); 28 | } 29 | 30 | public void insertMovie(MovieEntity... movies) { 31 | movieDao.insertAll(movies); 32 | } 33 | 34 | public long insertDirector(DirectorEntity director){ 35 | return directorDao.insert(director); 36 | } 37 | 38 | public int findDirector(String name){ 39 | DirectorEntity directorEntity = null; 40 | directorEntity = directorDao.findDirectorByName(name); 41 | if(directorEntity!= null){ 42 | return directorEntity.getId(); 43 | }else{ 44 | return -1; 45 | } 46 | } 47 | 48 | 49 | } 50 | -------------------------------------------------------------------------------- /app/src/main/java/com/anjan/architecturecomponent/ui/main/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.anjan.architecturecomponent.ui.main; 2 | 3 | import android.arch.lifecycle.Observer; 4 | import android.arch.lifecycle.ViewModelProviders; 5 | import android.content.Intent; 6 | import android.os.Bundle; 7 | import android.support.annotation.Nullable; 8 | import android.support.design.widget.FloatingActionButton; 9 | import android.support.v7.app.AppCompatActivity; 10 | import android.support.v7.widget.DividerItemDecoration; 11 | import android.support.v7.widget.LinearLayoutManager; 12 | import android.support.v7.widget.RecyclerView; 13 | import android.util.Log; 14 | import android.widget.TextView; 15 | 16 | import com.anjan.architecturecomponent.ui.add.AddActivity; 17 | import com.anjan.architecturecomponent.R; 18 | import com.anjan.architecturecomponent.entity.MovieEntity; 19 | 20 | import java.util.List; 21 | 22 | public class MainActivity extends AppCompatActivity{ 23 | 24 | 25 | FloatingActionButton floatingActionButton; 26 | 27 | 28 | RecyclerView recyclerView; 29 | MoviesListAdapter moviesListAdapter; 30 | MoviesViewModel moviesViewModel; 31 | MainFactory mainFactory; 32 | 33 | @Override 34 | protected void onCreate(Bundle savedInstanceState) { 35 | super.onCreate(savedInstanceState); 36 | 37 | setContentView(R.layout.activity_main); 38 | 39 | floatingActionButton = findViewById(R.id.floatingActionButton4); 40 | recyclerView = findViewById(R.id.recyclerview); 41 | 42 | mainFactory = new MainFactory(this, MainActivity.this.getApplication()); 43 | 44 | moviesViewModel = ViewModelProviders.of(this, mainFactory).get(MoviesViewModel.class); 45 | moviesListAdapter = new MoviesListAdapter(MainActivity.this); 46 | 47 | 48 | recyclerView.setHasFixedSize(true); 49 | recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL)); 50 | recyclerView.setLayoutManager(new LinearLayoutManager(this)); 51 | recyclerView.setAdapter(moviesListAdapter); 52 | 53 | 54 | /*moviesViewModel.getMoviesList().observe(this, new Observer>() { 55 | @Override 56 | public void onChanged(@Nullable List movies) { 57 | moviesListAdapter.setMovieList(movies); 58 | } 59 | });*/ 60 | 61 | moviesViewModel.getMediatorMoviesList().observe(this, new Observer>() { 62 | @Override 63 | public void onChanged(@Nullable List movieEntities) { 64 | moviesListAdapter.setMovieList(movieEntities); 65 | } 66 | }); 67 | 68 | floatingActionButton.setOnClickListener(v -> { 69 | Intent i = new Intent(MainActivity.this, AddActivity.class); 70 | startActivity(i); 71 | }); 72 | 73 | subscribe(); 74 | 75 | } 76 | 77 | @Override 78 | protected void onResume() { 79 | super.onResume(); 80 | } 81 | 82 | @Override 83 | protected void onPause() { 84 | super.onPause(); 85 | } 86 | 87 | private void subscribe() { 88 | final Observer elapsedTimeObserver = new Observer() { 89 | @Override 90 | public void onChanged(@Nullable final Long aLong) { 91 | String newText = "Elapsed time" +aLong; 92 | ((TextView) findViewById(R.id.timer_textview)).setText(newText); 93 | Log.d("MainActivity", "Updating timer"); 94 | } 95 | }; 96 | 97 | moviesViewModel.getElapsedTime().observe(this, elapsedTimeObserver); 98 | } 99 | 100 | 101 | 102 | } 103 | -------------------------------------------------------------------------------- /app/src/main/java/com/anjan/architecturecomponent/ui/main/MainFactory.java: -------------------------------------------------------------------------------- 1 | package com.anjan.architecturecomponent.ui.main; 2 | 3 | import android.app.Activity; 4 | import android.app.Application; 5 | import android.arch.lifecycle.ViewModel; 6 | import android.arch.lifecycle.ViewModelProvider; 7 | import android.content.Context; 8 | import android.content.SharedPreferences; 9 | import android.preference.PreferenceManager; 10 | import android.support.annotation.NonNull; 11 | 12 | import java.lang.reflect.InvocationTargetException; 13 | 14 | /** 15 | * Created by Anjan Debnath on 9/14/2018. 16 | * Copyright (c) 2018, W3 Engineers Ltd. All rights reserved. 17 | */ 18 | public class MainFactory implements ViewModelProvider.Factory { 19 | 20 | private Context mContext; 21 | private Application mApplication; 22 | 23 | public MainFactory(Context context, Application application){ 24 | mContext = context; 25 | mApplication = application; 26 | } 27 | 28 | @NonNull 29 | @Override 30 | public T create(@NonNull Class modelClass) { 31 | if (modelClass.isAssignableFrom(MoviesViewModel.class)) { 32 | return (T) new MoviesViewModel(mApplication); 33 | } 34 | throw new IllegalArgumentException("Unknown ViewModel class"); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /app/src/main/java/com/anjan/architecturecomponent/ui/main/MoviesListAdapter.java: -------------------------------------------------------------------------------- 1 | package com.anjan.architecturecomponent.ui.main; 2 | 3 | import android.content.Context; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.widget.TextView; 9 | 10 | import com.anjan.architecturecomponent.MoviesDatabase; 11 | import com.anjan.architecturecomponent.R; 12 | import com.anjan.architecturecomponent.entity.DirectorEntity; 13 | import com.anjan.architecturecomponent.entity.MovieEntity; 14 | 15 | import java.util.List; 16 | 17 | /** 18 | * Created by Anjan Debnath on 8/17/2018. 19 | * Copyright (c) 2018, W3 Engineers Ltd. All rights reserved. 20 | */ 21 | 22 | public class MoviesListAdapter extends RecyclerView.Adapter { 23 | private LayoutInflater layoutInflater; 24 | private List movieList; 25 | private Context context; 26 | 27 | public MoviesListAdapter(Context context) { 28 | this.layoutInflater = LayoutInflater.from(context); 29 | this.context = context; 30 | } 31 | 32 | public void setMovieList(List movieList) { 33 | this.movieList = movieList; 34 | notifyDataSetChanged(); 35 | } 36 | 37 | @Override 38 | public MoviesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 39 | final View itemView = layoutInflater.inflate(R.layout.item_list_movie, parent, false); 40 | return new MoviesViewHolder(itemView); 41 | } 42 | 43 | @Override 44 | public void onBindViewHolder(MoviesViewHolder holder, int position) { 45 | if (movieList == null) { 46 | return; 47 | } 48 | 49 | final MovieEntity movie = movieList.get(position); 50 | if (movie != null) { 51 | holder.titleText.setText(movie.movieName); 52 | 53 | final DirectorEntity director = MoviesDatabase.getDatabase(context).directorDao().findDirectorById(movie.directorId); 54 | final String directorFullName; 55 | if (director != null) { 56 | holder.directorText.setText(director.fullName); 57 | directorFullName = director.fullName; 58 | } else { 59 | directorFullName = ""; 60 | } 61 | holder.itemView.setOnClickListener(new View.OnClickListener() { 62 | @Override 63 | public void onClick(View v) { 64 | 65 | } 66 | }); 67 | } 68 | } 69 | 70 | @Override 71 | public int getItemCount() { 72 | if (movieList == null) { 73 | return 0; 74 | } else { 75 | return movieList.size(); 76 | } 77 | } 78 | 79 | static class MoviesViewHolder extends RecyclerView.ViewHolder { 80 | private TextView titleText; 81 | private TextView directorText; 82 | 83 | public MoviesViewHolder(View itemView) { 84 | super(itemView); 85 | 86 | titleText = itemView.findViewById(R.id.tvMovieTitle); 87 | directorText = itemView.findViewById(R.id.tvMovieDirectorFullName); 88 | } 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /app/src/main/java/com/anjan/architecturecomponent/ui/main/MoviesViewModel.java: -------------------------------------------------------------------------------- 1 | package com.anjan.architecturecomponent.ui.main; 2 | 3 | import android.app.Application; 4 | import android.arch.lifecycle.AndroidViewModel; 5 | import android.arch.lifecycle.LiveData; 6 | import android.arch.lifecycle.MediatorLiveData; 7 | import android.arch.lifecycle.MutableLiveData; 8 | import android.arch.lifecycle.Observer; 9 | import android.os.SystemClock; 10 | import android.support.annotation.NonNull; 11 | import android.support.annotation.Nullable; 12 | 13 | 14 | import com.anjan.architecturecomponent.MoviesDatabase; 15 | import com.anjan.architecturecomponent.dao.MovieDao; 16 | import com.anjan.architecturecomponent.entity.MovieEntity; 17 | 18 | import java.util.List; 19 | import java.util.Timer; 20 | import java.util.TimerTask; 21 | 22 | /** 23 | * Created by Anjan Debnath on 8/17/2018. 24 | * Copyright (c) 2018, W3 Engineers Ltd. All rights reserved. 25 | */ 26 | 27 | public class MoviesViewModel extends AndroidViewModel { 28 | 29 | private MovieDao movieDao; 30 | private LiveData> moviesLiveData; 31 | private MutableLiveData mElapsedTime = new MutableLiveData<>(); 32 | private MediatorLiveData> moviesMediatorLiveData = new MediatorLiveData<>(); 33 | 34 | private static final int ONE_SECOND = 1000; 35 | 36 | private long mInitialTime; 37 | 38 | public MoviesViewModel(@NonNull Application application) { 39 | super(application); 40 | movieDao = MoviesDatabase.getDatabase(application).movieDao(); 41 | 42 | setupMutableLiveData(); 43 | 44 | setupMediatorLiveData(); 45 | 46 | } 47 | 48 | /** 49 | * this will return all movies from db and notify LiveData object 50 | * @return 51 | */ 52 | private LiveData> getMoviesLiveData(){ 53 | 54 | moviesLiveData = movieDao.getAllMovies(); 55 | return moviesLiveData; 56 | 57 | } 58 | 59 | /** 60 | * MediatorLiveData 61 | * It will merge the LiveData object into Mediator Livedata 62 | * 63 | * It's maintain One -to -many relation 64 | * a single mediator livedata can manage 2 seperate list 65 | * and when any of them get updted it emmits data. 66 | */ 67 | private void setupMediatorLiveData(){ 68 | 69 | moviesMediatorLiveData.addSource(getMoviesLiveData(), new Observer>() { 70 | @Override 71 | public void onChanged(@Nullable List movieEntities) { 72 | moviesMediatorLiveData.setValue(movieEntities); 73 | } 74 | }); 75 | } 76 | 77 | /** 78 | * Mutable LiveData 79 | * It will simply notify the updated time 80 | */ 81 | private void setupMutableLiveData(){ 82 | 83 | mInitialTime = SystemClock.elapsedRealtime(); 84 | Timer timer = new Timer(); 85 | 86 | // Update the elapsed time every second. 87 | timer.scheduleAtFixedRate(new TimerTask() { 88 | @Override 89 | public void run() { 90 | final long newValue = (SystemClock.elapsedRealtime() - mInitialTime) / 1000; 91 | // setValue() cannot be called from a background thread so postValue is used. 92 | mElapsedTime.postValue(newValue); 93 | } 94 | }, ONE_SECOND, ONE_SECOND); 95 | 96 | } 97 | 98 | public LiveData getElapsedTime() { 99 | return mElapsedTime; 100 | } 101 | 102 | public LiveData> getMoviesList() { 103 | return moviesLiveData; 104 | } 105 | 106 | public LiveData> getMediatorMoviesList(){ 107 | return moviesMediatorLiveData; 108 | } 109 | 110 | public void insertAll(MovieEntity... movies) { 111 | movieDao.insertAll(movies); 112 | } 113 | 114 | public void update(MovieEntity movie) { 115 | movieDao.update(movie); 116 | } 117 | 118 | public void deleteAll() { 119 | movieDao.deleteAll(); 120 | } 121 | } -------------------------------------------------------------------------------- /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_add_black_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_dashboard_black_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_home_black_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /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/drawable/ic_notifications_black_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_add.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 22 | 23 | 35 | 36 |