├── .gitignore ├── .idea ├── assetWizardSettings.xml ├── caches │ └── build_file_checksums.ser ├── codeStyles │ └── Project.xml ├── dictionaries │ └── Marty.xml ├── gradle.xml ├── misc.xml └── runConfigurations.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── marty │ │ └── yummy │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── ic_launcher-web.png │ ├── java │ │ └── com │ │ │ └── marty │ │ │ └── yummy │ │ │ ├── dbutilities │ │ │ ├── AppDatabase.java │ │ │ ├── CartItemDao.java │ │ │ └── FoodDetailsDao.java │ │ │ ├── model │ │ │ ├── CartItem.java │ │ │ └── FoodDetails.java │ │ │ ├── services │ │ │ ├── APIClient.java │ │ │ ├── YummyAPIServices.java │ │ │ └── repository │ │ │ │ └── FoodRepository.java │ │ │ ├── ui │ │ │ ├── CartActivity.java │ │ │ ├── HomeScreenActivity.java │ │ │ ├── IndividualActivity.java │ │ │ ├── RatingTextView.java │ │ │ └── adapters │ │ │ │ ├── CartListAdapter.java │ │ │ │ └── FoodListAdapter.java │ │ │ ├── utility │ │ │ ├── MyAppGlideModule.java │ │ │ └── ObservableObject.java │ │ │ ├── viewmodel │ │ │ ├── CartViewModel.java │ │ │ ├── FoodDetailViewModel.java │ │ │ └── FoodViewModel.java │ │ │ └── worker │ │ │ ├── SaveFoodMenu.java │ │ │ └── UpdateCart.java │ └── res │ │ ├── anim │ │ ├── item_slide_from_bottom.xml │ │ └── layout_slide_from_bottom.xml │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ ├── ic_back.xml │ │ ├── ic_cart.xml │ │ ├── ic_food.xml │ │ ├── ic_launcher_background.xml │ │ ├── ic_minus.xml │ │ ├── ic_plus.xml │ │ ├── ic_sort.xml │ │ ├── ic_star.xml │ │ ├── logo.png │ │ └── rounded_button.xml │ │ ├── layout │ │ ├── activity_cart.xml │ │ ├── activity_detail_layout.xml │ │ ├── activity_home_screen.xml │ │ ├── cart_toolbar.xml │ │ ├── launcher.xml │ │ ├── layout_cart_item.xml │ │ └── layout_food_item.xml │ │ ├── menu │ │ ├── actions.xml │ │ └── menu_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ ├── ic_launcher_foreground.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ ├── ic_launcher_foreground.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ ├── ic_launcher_foreground.png │ │ ├── ic_launcher_round.png │ │ └── robo.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ ├── ic_launcher_foreground.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ ├── ic_launcher_foreground.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── attrs.xml │ │ ├── colors.xml │ │ ├── ic_launcher_background.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── marty │ └── yummy │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── screenshots and demo ├── Screenshot (114).png ├── Screenshot_20181205-024745.png ├── Screenshot_20181205-024753.png ├── Screenshot_20181205-024829.png ├── Screenshot_20181205-024851.png └── Screenshot_20181205-024911.png └── 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/assetWizardSettings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 95 | 96 | -------------------------------------------------------------------------------- /.idea/caches/build_file_checksums.ser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martinraj/Food-ordering-app-like-swiggy-uber-eats-MVVM-and-Room-database-/1baf19005a7990cb7f53c8f6a14153a4e39ba217/.idea/caches/build_file_checksums.ser -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 15 | 16 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /.idea/dictionaries/Marty.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | freedel 5 | 6 | 7 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 27 | 28 | 29 | 30 | 31 | 32 | 34 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Food ordering app like Swiggy and Uber eats 2 | Food ordering app using MVVM architecture patterns, Architecture Lifecycle components, Retrofit2 and Room database. 3 | 4 | In this demo, I have covered **Mediator Live data, Mutable live data, Observable, Observers, Retrofit and Room using same POJO.** 5 | 6 | ## APP UI 7 | ![App UI](https://github.com/martinraj/MVVM-architecture-lifecycle-components-and-Room-database-project/blob/master/screenshots%20and%20demo/Screenshot_20181205-024745.png) ![](https://github.com/martinraj/MVVM-architecture-lifecycle-components-and-Room-database-project/blob/master/screenshots%20and%20demo/Screenshot_20181205-024753.png) ![](https://github.com/martinraj/MVVM-architecture-lifecycle-components-and-Room-database-project/blob/master/screenshots%20and%20demo/Screenshot_20181205-024829.png) 8 | 9 | ## Room and Retrofit using same Model 10 | 11 | I have used same models for Room and Retrofit2 library as both are configured using annotaions. 12 | 13 | ``` 14 | @Entity 15 | public class FoodDetails { 16 | 17 | @PrimaryKey //Room annotation 18 | @SerializedName("item_name") //Retrofit annotation 19 | @Expose 20 | @NonNull 21 | private String name; 22 | 23 | @SerializedName("item_price") 24 | @Expose 25 | private Double price; 26 | 27 | @SerializedName("average_rating") 28 | @Expose 29 | private Double rating; 30 | 31 | @SerializedName("image_url") 32 | @Expose 33 | private String imageUrl; 34 | 35 | @SerializedName("item_quantity") 36 | @Expose 37 | private Integer quantity = 0; 38 | 39 | // For Retrofit 40 | public FoodDetails(@NonNull String name, Double price, Double rating, String imageUrl,Integer quantity) { 41 | this.name = name; 42 | this.price = price; 43 | this.rating = rating; 44 | this.imageUrl = imageUrl; 45 | this.quantity = quantity; 46 | } 47 | 48 | // Getters and Setters for Room 49 | @NonNull 50 | public String getName() { 51 | return name; 52 | } 53 | 54 | public void setName(@NonNull String name) { 55 | this.name = name; 56 | } 57 | 58 | public Double getPrice() { 59 | return price; 60 | } 61 | 62 | public void setPrice(Double price) { 63 | this.price = price; 64 | } 65 | 66 | public Double getRating() { 67 | return rating; 68 | } 69 | 70 | public void setRating(Double rating) { 71 | this.rating = rating; 72 | } 73 | 74 | public String getImageUrl() { 75 | return imageUrl; 76 | } 77 | 78 | public void setImageUrl(String imageUrl) { 79 | this.imageUrl = imageUrl; 80 | } 81 | 82 | public Integer getQuantity() { 83 | return quantity; 84 | } 85 | 86 | public void setQuantity(Integer quantity) { 87 | this.quantity = quantity; 88 | } 89 | } 90 | ``` 91 | 92 | ## Sorting list elements in Room Database and observing using Mediator Live Data 93 | 94 | Sorting of food items is done based on Pricing and rating. I have used Mediator Live data to observe same types of Live Data from different db queries. We are going to expose Mediator Live Data to UI which will observe data changes from other two Live Data. 95 | 96 | ``` 97 | ...\ 98 | public class FoodViewModel extends AndroidViewModel { 99 | MediatorLiveData> foodDetailsMediatorLiveData = new MediatorLiveData<>(); 100 | 101 | private LiveData> foodDetailsLiveDataSortPrice; 102 | private LiveData> foodDetailsLiveDataSortRating; 103 | private AppDatabase db; 104 | private static String DEFAULT_SORT = ACTION_SORT_BY_PRICE; 105 | 106 | public FoodViewModel(@NonNull Application application) { 107 | super(application); 108 | init(); 109 | } 110 | 111 | private void init() { 112 | db = AppDatabase.getDatabase(getApplication().getApplicationContext()); 113 | subscribeToFoodChanges(); 114 | } 115 | 116 | private void subscribeToFoodChanges() { 117 | if(DEFAULT_SORT.equals(ACTION_SORT_BY_PRICE)){ 118 | foodDetailsLiveDataSortPrice = db.foodDetailsDao().getFoodsByPrice(); 119 | foodDetailsMediatorLiveData.addSource(foodDetailsLiveDataSortPrice, new Observer>() { 120 | @Override 121 | public void onChanged(@Nullable List foodDetails) { 122 | foodDetailsMediatorLiveData.setValue(foodDetails); 123 | } 124 | }); 125 | }else if(DEFAULT_SORT.equals(ACTION_SORT_BY_RATING)){ 126 | foodDetailsLiveDataSortRating = db.foodDetailsDao().getFoodsByRating(); 127 | foodDetailsMediatorLiveData.addSource(foodDetailsLiveDataSortRating, new Observer>() { 128 | @Override 129 | public void onChanged(@Nullable List foodDetails) { 130 | foodDetailsMediatorLiveData.setValue(foodDetails); 131 | } 132 | }); 133 | } 134 | } 135 | 136 | ...\ 137 | ``` 138 | 139 | Manually we are triggering observers by using **foodDetailsMediatorLiveData.setValue(foodDetails);** 140 | 141 | ## Room DB query for Sorting food items 142 | ``` 143 | @Dao 144 | public interface FoodDetailsDao { 145 | 146 | ...\ 147 | 148 | @Query("SELECT * FROM fooddetails ORDER BY price ASC") 149 | LiveData> getFoodsByPrice(); 150 | 151 | @Query("SELECT * FROM fooddetails ORDER BY rating DESC") 152 | LiveData> getFoodsByRating(); 153 | 154 | ...\ 155 | } 156 | ``` 157 | 158 | ## Observing data changes in Activity/Fragment 159 | 160 | We have to create observers for live data. Only then if any data is changed, observers will be notified with updated data by ViewModel. 161 | 162 | ``` 163 | ...\ 164 | 165 | @Override 166 | protected void onCreate(Bundle savedInstanceState) { 167 | .../ 168 | 169 | // Define viewmodel 170 | FoodViewModel foodViewModel = ViewModelProviders.of(this).get(FoodViewModel.class); 171 | 172 | // Define Observer with actions to be done after update 173 | Observer> foodMenuObserver = new Observer>() { 174 | @Override 175 | public void onChanged(@Nullable List foodDetails) { 176 | if(foodDetails!=null){ 177 | foodListAdapter.setData(foodDetails); 178 | foodListAdapter.notifyDataSetChanged(); 179 | runLayoutAnimation(foodList); 180 | }else{ 181 | Log.e("Food details","null"); 182 | } 183 | } 184 | }; 185 | 186 | // set observer to watch for data changes 187 | foodViewModel.getFoodDetailsMutableLiveData().observe(this, foodMenuObserver); 188 | 189 | .../ 190 | } 191 | 192 | ...\ 193 | 194 | ``` 195 | ## Retrofit to get data from server 196 | 197 | We have used Retrofit to get data from Rest API and updating db using Room in Intent Service 198 | 199 | ``` 200 | public interface YummyAPIServices { 201 | 202 | @GET("/data.json") 203 | Call> getFoodData(); 204 | } 205 | 206 | ``` 207 | 208 | ``` 209 | public class FoodRepository { 210 | 211 | private static FoodRepository instance; 212 | private static final String TAG = "FoodRepository"; 213 | 214 | private YummyAPIServices yummyAPIServices = APIClient.getClient().create(YummyAPIServices.class); 215 | 216 | public MutableLiveData getFoodMenu(final Context context){ 217 | 218 | final MutableLiveData isFoodCallOngoing = new MutableLiveData<>(); 219 | isFoodCallOngoing.setValue(true); 220 | 221 | yummyAPIServices.getFoodData().enqueue(new Callback>() { 222 | @Override 223 | public void onResponse(Call> call, Response> response) { 224 | if(response.isSuccessful()) { 225 | new SaveFoodMenu(AppDatabase.getDatabase(context), response.body()).execute(); 226 | isFoodCallOngoing.setValue(false); // on success we are updating empty mutable live data with new food menus 227 | }else{ 228 | Log.e(TAG,"response not successful"); 229 | } 230 | } 231 | 232 | @Override 233 | public void onFailure(Call> call, Throwable t) { 234 | Log.e(TAG,t.toString()); 235 | } 236 | }); 237 | return isFoodCallOngoing; // returns empty mutable live data 238 | } 239 | 240 | public static FoodRepository getInstance() { 241 | if(instance == null){ 242 | synchronized (FoodRepository.class){ 243 | if(instance == null){ 244 | instance = new FoodRepository(); 245 | } 246 | } 247 | } 248 | return instance; 249 | } 250 | } 251 | ``` 252 | ## API Client for Retrofit 253 | 254 | We are using singleton to get Retrofit client. 255 | ``` 256 | public class APIClient { 257 | 258 | private static final String BASE_URL = "YOUR_SERVER_BASE_URL"; 259 | private static Retrofit retrofit = null; 260 | 261 | 262 | public static Retrofit getClient() { 263 | if (retrofit==null) { 264 | retrofit = new Retrofit.Builder() 265 | .baseUrl(BASE_URL) 266 | .addConverterFactory(GsonConverterFactory.create()) 267 | .build(); 268 | } 269 | return retrofit; 270 | } 271 | 272 | } 273 | ``` 274 | 275 | ## Project Structure 276 | 277 | This is the overall project structure of this project. 278 | 279 | 280 | ![Project Structure](https://github.com/martinraj/MVVM-architecture-lifecycle-components-and-Room-database-project/blob/master/screenshots%20and%20demo/Screenshot%20(114).png) 281 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 28 5 | defaultConfig { 6 | applicationId "com.marty.yummy" 7 | minSdkVersion 17 8 | targetSdkVersion 28 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | vectorDrawables.useSupportLibrary = true 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | implementation fileTree(dir: 'libs', include: ['*.jar']) 24 | implementation 'com.android.support:appcompat-v7:28.0.0' 25 | implementation 'com.android.support:design:28.0.0' 26 | implementation 'com.android.support.constraint:constraint-layout:1.1.3' 27 | implementation 'com.android.support:cardview-v7:28.0.0' 28 | implementation 'com.android.support:recyclerview-v7:28.0.0' 29 | //retrofit 30 | implementation 'com.squareup.retrofit2:retrofit:2.1.0' 31 | implementation 'com.squareup.retrofit2:converter-gson:2.1.0' 32 | //room 33 | implementation "android.arch.persistence.room:runtime:1.1.1" 34 | annotationProcessor "android.arch.persistence.room:compiler:1.1.1" 35 | 36 | //live data and view modals 37 | implementation 'android.arch.lifecycle:livedata:1.1.1' 38 | implementation "android.arch.lifecycle:extensions:1.1.1" 39 | //Glide 40 | implementation 'com.github.bumptech.glide:glide:4.6.1' 41 | annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0' 42 | 43 | 44 | testImplementation 'junit:junit:4.12' 45 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 46 | testImplementation 'org.mockito:mockito-core:1.10.19' 47 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 48 | } 49 | -------------------------------------------------------------------------------- /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 | -keepattributes Signature, InnerClasses, EnclosingMethod 23 | 24 | # Retain service method parameters when optimizing. 25 | -keepclassmembers,allowshrinking,allowobfuscation interface * { 26 | @retrofit2.http.* ; 27 | } 28 | -keepattributes *Annotation* 29 | -keepattributes SourceFile,LineNumberTable 30 | -keep public class * extends java.lang.Exception 31 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/marty/yummy/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.marty.yummy; 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.marty.yummy", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 13 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 25 | 26 | 27 | 30 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /app/src/main/ic_launcher-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martinraj/Food-ordering-app-like-swiggy-uber-eats-MVVM-and-Room-database-/1baf19005a7990cb7f53c8f6a14153a4e39ba217/app/src/main/ic_launcher-web.png -------------------------------------------------------------------------------- /app/src/main/java/com/marty/yummy/dbutilities/AppDatabase.java: -------------------------------------------------------------------------------- 1 | package com.marty.yummy.dbutilities; 2 | 3 | 4 | import android.arch.persistence.room.Database; 5 | import android.arch.persistence.room.Room; 6 | import android.arch.persistence.room.RoomDatabase; 7 | import android.content.Context; 8 | 9 | import com.marty.yummy.model.CartItem; 10 | import com.marty.yummy.model.FoodDetails; 11 | 12 | 13 | 14 | /** 15 | * Created by Marty on 12/29/2017. 16 | */ 17 | @Database(entities = {FoodDetails.class, CartItem.class}, version = 2,exportSchema = false) 18 | public abstract class AppDatabase extends RoomDatabase { 19 | private static AppDatabase INSTANCE; 20 | 21 | public abstract FoodDetailsDao foodDetailsDao(); 22 | public abstract CartItemDao cartItemDao(); 23 | 24 | public static AppDatabase getDatabase(final Context context) { 25 | if (INSTANCE == null) { 26 | synchronized (AppDatabase.class) { 27 | if (INSTANCE == null) { 28 | INSTANCE = Room.databaseBuilder(context.getApplicationContext(),AppDatabase.class,"yummy").fallbackToDestructiveMigration().allowMainThreadQueries().build(); 29 | } 30 | } 31 | } 32 | return INSTANCE; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /app/src/main/java/com/marty/yummy/dbutilities/CartItemDao.java: -------------------------------------------------------------------------------- 1 | package com.marty.yummy.dbutilities; 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.Query; 7 | 8 | import com.marty.yummy.model.CartItem; 9 | 10 | import java.util.List; 11 | 12 | import static android.arch.persistence.room.OnConflictStrategy.REPLACE; 13 | 14 | 15 | @Dao 16 | public interface CartItemDao { 17 | @Query("SELECT * FROM cartitem") 18 | LiveData> getCartItems(); 19 | 20 | @Insert(onConflict = REPLACE) 21 | void add(CartItem cartItem); 22 | 23 | @Query("DELETE FROM cartitem WHERE item_name = :name") 24 | void deleteCartItem(String name); 25 | 26 | @Query("SELECT quantity FROM cartitem WHERE item_name = :name") 27 | int getCartCount(String name); 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/java/com/marty/yummy/dbutilities/FoodDetailsDao.java: -------------------------------------------------------------------------------- 1 | package com.marty.yummy.dbutilities; 2 | 3 | 4 | import android.arch.lifecycle.LiveData; 5 | import android.arch.persistence.room.Dao; 6 | import android.arch.persistence.room.Insert; 7 | import android.arch.persistence.room.Query; 8 | 9 | import com.marty.yummy.model.FoodDetails; 10 | 11 | import java.util.List; 12 | 13 | import static android.arch.persistence.room.OnConflictStrategy.REPLACE; 14 | 15 | 16 | @Dao 17 | public interface FoodDetailsDao { 18 | 19 | @Insert(onConflict = REPLACE) 20 | void save(List foodDetails); 21 | 22 | @Insert(onConflict = REPLACE) 23 | void save(FoodDetails foodDetails); 24 | 25 | @Query("DELETE FROM fooddetails WHERE name NOT IN (:nameList)") 26 | void deleteOtherFoods(List nameList); 27 | 28 | @Query("DELETE FROM fooddetails") 29 | void deleteAll(); 30 | 31 | @Query("SELECT * FROM fooddetails ORDER BY price ASC") 32 | LiveData> getFoodsByPrice(); 33 | 34 | @Query("SELECT * FROM fooddetails ORDER BY rating DESC") 35 | LiveData> getFoodsByRating(); 36 | 37 | @Query("SELECT * FROM fooddetails WHERE name = :name") 38 | LiveData getFood(String name); 39 | } 40 | -------------------------------------------------------------------------------- /app/src/main/java/com/marty/yummy/model/CartItem.java: -------------------------------------------------------------------------------- 1 | package com.marty.yummy.model; 2 | 3 | 4 | import android.arch.persistence.room.ColumnInfo; 5 | import android.arch.persistence.room.Entity; 6 | import android.arch.persistence.room.ForeignKey; 7 | import android.arch.persistence.room.PrimaryKey; 8 | import android.support.annotation.NonNull; 9 | 10 | @Entity 11 | public class CartItem { 12 | @ForeignKey(entity = FoodDetails.class,parentColumns = "name",childColumns = "item_name",onDelete = ForeignKey.CASCADE) 13 | @PrimaryKey 14 | @NonNull 15 | @ColumnInfo(name = "item_name") 16 | private String name=""; 17 | 18 | @ColumnInfo(name = "item_price") 19 | private Double price; 20 | 21 | @ColumnInfo(name = "quantity") 22 | private Integer quantity = 1; 23 | 24 | @NonNull 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public void setName(@NonNull String name) { 30 | this.name = name; 31 | } 32 | 33 | public Double getPrice() { 34 | return price; 35 | } 36 | 37 | public void setPrice(Double price) { 38 | this.price = price; 39 | } 40 | 41 | public Integer getQuantity() { 42 | return quantity; 43 | } 44 | 45 | public void setQuantity(Integer quantity) { 46 | this.quantity = quantity; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /app/src/main/java/com/marty/yummy/model/FoodDetails.java: -------------------------------------------------------------------------------- 1 | package com.marty.yummy.model; 2 | 3 | import android.arch.persistence.room.Entity; 4 | import android.arch.persistence.room.PrimaryKey; 5 | import android.support.annotation.NonNull; 6 | import com.google.gson.annotations.Expose; 7 | import com.google.gson.annotations.SerializedName; 8 | 9 | @Entity 10 | public class FoodDetails { 11 | 12 | @PrimaryKey 13 | @SerializedName("item_name") 14 | @Expose 15 | @NonNull 16 | private String name; 17 | 18 | @SerializedName("item_price") 19 | @Expose 20 | private Double price; 21 | 22 | @SerializedName("average_rating") 23 | @Expose 24 | private Double rating; 25 | 26 | @SerializedName("image_url") 27 | @Expose 28 | private String imageUrl; 29 | 30 | @SerializedName("item_quantity") 31 | @Expose 32 | private Integer quantity = 0; 33 | 34 | public FoodDetails(@NonNull String name, Double price, Double rating, String imageUrl,Integer quantity) { 35 | this.name = name; 36 | this.price = price; 37 | this.rating = rating; 38 | this.imageUrl = imageUrl; 39 | this.quantity = quantity; 40 | } 41 | 42 | @NonNull 43 | public String getName() { 44 | return name; 45 | } 46 | 47 | public void setName(@NonNull String name) { 48 | this.name = name; 49 | } 50 | 51 | public Double getPrice() { 52 | return price; 53 | } 54 | 55 | public void setPrice(Double price) { 56 | this.price = price; 57 | } 58 | 59 | public Double getRating() { 60 | return rating; 61 | } 62 | 63 | public void setRating(Double rating) { 64 | this.rating = rating; 65 | } 66 | 67 | public String getImageUrl() { 68 | return imageUrl; 69 | } 70 | 71 | public void setImageUrl(String imageUrl) { 72 | this.imageUrl = imageUrl; 73 | } 74 | 75 | public Integer getQuantity() { 76 | return quantity; 77 | } 78 | 79 | public void setQuantity(Integer quantity) { 80 | this.quantity = quantity; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /app/src/main/java/com/marty/yummy/services/APIClient.java: -------------------------------------------------------------------------------- 1 | package com.marty.yummy.services; 2 | 3 | import retrofit2.Retrofit; 4 | import retrofit2.converter.gson.GsonConverterFactory; 5 | 6 | public class APIClient { 7 | 8 | private static final String BASE_URL = "https://android-full-time-task.firebaseio.com"; 9 | private static Retrofit retrofit = null; 10 | 11 | 12 | public static Retrofit getClient() { 13 | if (retrofit==null) { 14 | retrofit = new Retrofit.Builder() 15 | .baseUrl(BASE_URL) 16 | .addConverterFactory(GsonConverterFactory.create()) 17 | .build(); 18 | } 19 | return retrofit; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /app/src/main/java/com/marty/yummy/services/YummyAPIServices.java: -------------------------------------------------------------------------------- 1 | package com.marty.yummy.services; 2 | 3 | import com.marty.yummy.model.FoodDetails; 4 | 5 | import java.util.List; 6 | 7 | import retrofit2.Call; 8 | import retrofit2.http.GET; 9 | 10 | public interface YummyAPIServices { 11 | 12 | @GET("/data.json") 13 | Call> getFoodData(); 14 | } 15 | -------------------------------------------------------------------------------- /app/src/main/java/com/marty/yummy/services/repository/FoodRepository.java: -------------------------------------------------------------------------------- 1 | package com.marty.yummy.services.repository; 2 | 3 | import android.arch.lifecycle.MutableLiveData; 4 | import android.content.Context; 5 | import android.util.Log; 6 | 7 | import com.marty.yummy.dbutilities.AppDatabase; 8 | import com.marty.yummy.model.FoodDetails; 9 | import com.marty.yummy.services.APIClient; 10 | import com.marty.yummy.services.YummyAPIServices; 11 | import com.marty.yummy.worker.SaveFoodMenu; 12 | import com.marty.yummy.worker.UpdateCart; 13 | 14 | import java.util.List; 15 | 16 | import retrofit2.Call; 17 | import retrofit2.Callback; 18 | import retrofit2.Response; 19 | 20 | public class FoodRepository { 21 | 22 | private static FoodRepository instance; 23 | private static final String TAG = "FoodRepository"; 24 | 25 | private YummyAPIServices yummyAPIServices = APIClient.getClient().create(YummyAPIServices.class); 26 | 27 | public MutableLiveData getFoodMenu(final Context context){ 28 | 29 | final MutableLiveData isFoodCallOngoing = new MutableLiveData<>(); 30 | isFoodCallOngoing.setValue(true); 31 | 32 | yummyAPIServices.getFoodData().enqueue(new Callback>() { 33 | @Override 34 | public void onResponse(Call> call, Response> response) { 35 | if(response.isSuccessful()) { 36 | new SaveFoodMenu(AppDatabase.getDatabase(context), response.body()).execute(); 37 | isFoodCallOngoing.setValue(false); 38 | }else{ 39 | Log.e(TAG,"response not successful"); 40 | } 41 | } 42 | 43 | @Override 44 | public void onFailure(Call> call, Throwable t) { 45 | Log.e(TAG,t.toString()); 46 | } 47 | }); 48 | return isFoodCallOngoing; 49 | } 50 | 51 | public static FoodRepository getInstance() { 52 | if(instance == null){ 53 | synchronized (FoodRepository.class){ 54 | if(instance == null){ 55 | instance = new FoodRepository(); 56 | } 57 | } 58 | } 59 | return instance; 60 | } 61 | 62 | public void updateCart(final AppDatabase db, FoodDetails foodDetails) { 63 | new UpdateCart(db).execute(foodDetails); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /app/src/main/java/com/marty/yummy/ui/CartActivity.java: -------------------------------------------------------------------------------- 1 | package com.marty.yummy.ui; 2 | 3 | import android.arch.lifecycle.Observer; 4 | import android.arch.lifecycle.ViewModelProviders; 5 | import android.graphics.Paint; 6 | import android.os.Bundle; 7 | import android.support.annotation.Nullable; 8 | import android.support.design.widget.TextInputEditText; 9 | import android.support.design.widget.TextInputLayout; 10 | import android.support.v7.app.AppCompatActivity; 11 | import android.support.v7.widget.AppCompatButton; 12 | import android.support.v7.widget.AppCompatImageView; 13 | import android.support.v7.widget.LinearLayoutManager; 14 | import android.support.v7.widget.RecyclerView; 15 | import android.view.KeyEvent; 16 | import android.view.View; 17 | import android.view.inputmethod.EditorInfo; 18 | import android.widget.TextView; 19 | 20 | import com.marty.yummy.R; 21 | import com.marty.yummy.model.CartItem; 22 | import com.marty.yummy.ui.adapters.CartListAdapter; 23 | import com.marty.yummy.viewmodel.CartViewModel; 24 | 25 | import java.util.ArrayList; 26 | import java.util.List; 27 | 28 | public class CartActivity extends AppCompatActivity implements View.OnClickListener { 29 | 30 | RecyclerView cartList; 31 | TextView tDiscount,hDiscount,tItemsCost,tDelivery,hDelivery,tGrandTotal; 32 | TextInputEditText eCoupon; 33 | TextInputLayout eCouponLayout; 34 | AppCompatButton bApply; 35 | AppCompatImageView iRemoveCoupon; 36 | CartViewModel cartViewModel; 37 | Observer> cartObserver; 38 | Observer costObserver; 39 | Observer errorObserver; 40 | CartListAdapter cartListAdapter; 41 | 42 | @Override 43 | protected void onCreate(@Nullable Bundle savedInstanceState) { 44 | super.onCreate(savedInstanceState); 45 | setContentView(R.layout.activity_cart); 46 | setTitle(R.string.your_cart); 47 | if(getSupportActionBar()!=null){ 48 | getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_back); 49 | getSupportActionBar().setDisplayHomeAsUpEnabled(true); 50 | } 51 | 52 | cartList = findViewById(R.id.cart_list); 53 | tDiscount = findViewById(R.id.t_discount); 54 | tItemsCost = findViewById(R.id.t_total); 55 | hDiscount = findViewById(R.id.h_discount); 56 | tDelivery = findViewById(R.id.t_delivery); 57 | hDelivery = findViewById(R.id.h_delivery); 58 | iRemoveCoupon = findViewById(R.id.i_remove); 59 | tGrandTotal = findViewById(R.id.t_grand_total); 60 | eCouponLayout = findViewById(R.id.coupon_layout); 61 | eCoupon = findViewById(R.id.e_coupon); 62 | bApply = findViewById(R.id.b_apply); 63 | bApply.setOnClickListener(this); 64 | iRemoveCoupon.setOnClickListener(this); 65 | 66 | RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this); 67 | cartList.setLayoutManager(mLayoutManager); 68 | } 69 | 70 | @Override 71 | protected void onPostCreate(@Nullable Bundle savedInstanceState) { 72 | super.onPostCreate(savedInstanceState); 73 | cartObserver = new Observer>() { 74 | @Override 75 | public void onChanged(@Nullable List cartItems) { 76 | cartListAdapter.setData(cartItems); 77 | cartListAdapter.notifyDataSetChanged(); 78 | if(cartItems!=null && cartItems.size()==0){ 79 | finish(); 80 | } 81 | } 82 | }; 83 | costObserver = new Observer() { 84 | @Override 85 | public void onChanged(@Nullable Double aDouble) { 86 | updateUI(aDouble); 87 | } 88 | }; 89 | errorObserver = new Observer() { 90 | @Override 91 | public void onChanged(@Nullable String error) { 92 | if(error!=null && error.isEmpty()){ 93 | eCouponLayout.setError(null); 94 | eCouponLayout.setErrorEnabled(false); 95 | }else{ 96 | eCouponLayout.setError(error); 97 | eCouponLayout.setErrorEnabled(true); 98 | } 99 | } 100 | }; 101 | cartViewModel = ViewModelProviders.of(this).get(CartViewModel.class); 102 | cartListAdapter = new CartListAdapter(new ArrayList(),cartViewModel); 103 | cartList.setAdapter(cartListAdapter); 104 | cartViewModel.getCartItemsLiveData().observe(this,cartObserver); 105 | cartViewModel.getGrandTotal().observe(this,costObserver); 106 | cartViewModel.getErrorString().observe(this,errorObserver); 107 | eCoupon.setOnEditorActionListener(new TextView.OnEditorActionListener() { 108 | @Override 109 | public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) { 110 | if(i==EditorInfo.IME_ACTION_DONE){ 111 | applyCoupon(); 112 | return false; 113 | } 114 | return false; 115 | } 116 | }); 117 | } 118 | 119 | private void updateUI(Double grandTotal) { 120 | tItemsCost.setText(getString(R.string.rupee_symbol)+" "+cartViewModel.getTotalCost()); 121 | tGrandTotal.setText(getString(R.string.rupee_symbol)+" "+String.valueOf(grandTotal)); 122 | if(cartViewModel.getDiscountAmt()>0){ 123 | hDiscount.setVisibility(View.VISIBLE); 124 | tDiscount.setVisibility(View.VISIBLE); 125 | hDiscount.setText(getString(R.string.discount)+" ( 20% )"); 126 | tDiscount.setText(" - "+getString(R.string.rupee_symbol)+" "+String.valueOf(cartViewModel.getDiscountAmt())); 127 | }else{ 128 | hDiscount.setVisibility(View.GONE); 129 | tDiscount.setVisibility(View.GONE); 130 | } 131 | if(cartViewModel.getDeliveryCost()>0){ 132 | hDelivery.setText(getString(R.string.delivery_charges)); 133 | tDelivery.setText(" + "+getString(R.string.rupee_symbol)+" "+String.valueOf(cartViewModel.getDeliveryCost())); 134 | tDelivery.setPaintFlags(0); 135 | }else{ 136 | hDelivery.setText(getString(R.string.delivery_charges)+" ( Free )"); 137 | tDelivery.setText(" + "+getString(R.string.rupee_symbol)+" 30.00"); 138 | tDelivery.setPaintFlags(tDelivery.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG); 139 | } 140 | } 141 | 142 | @Override 143 | public void onClick(View view) { 144 | if(view.getId() == R.id.b_apply){ 145 | applyCoupon(); 146 | }else if(view.getId() == R.id.i_remove){ 147 | eCoupon.setFocusable(true); 148 | eCoupon.setFocusableInTouchMode(true); 149 | eCoupon.setText(""); 150 | eCoupon.setLongClickable(true); 151 | eCouponLayout.setErrorEnabled(false); 152 | eCouponLayout.setError(null); 153 | iRemoveCoupon.setVisibility(View.INVISIBLE); 154 | bApply.setVisibility(View.VISIBLE); 155 | cartViewModel.applyCoupon(""); 156 | } 157 | } 158 | 159 | private void applyCoupon() { 160 | if(eCoupon.getText()!=null) { 161 | String coupon = eCoupon.getText().toString().trim().toUpperCase(); 162 | if (!coupon.isEmpty() && (coupon.equals("FREEDEL") || coupon.equals("F22LABS"))) { 163 | eCouponLayout.setErrorEnabled(false); 164 | eCouponLayout.setError(null); 165 | cartViewModel.applyCoupon(coupon); 166 | eCoupon.setFocusable(false); 167 | eCoupon.setFocusableInTouchMode(false); 168 | eCoupon.setLongClickable(false); 169 | iRemoveCoupon.setVisibility(View.VISIBLE); 170 | bApply.setVisibility(View.INVISIBLE); 171 | } else { 172 | eCouponLayout.setErrorEnabled(true); 173 | eCouponLayout.setError("coupon not valid"); 174 | } 175 | } 176 | } 177 | 178 | @Override 179 | protected void onDestroy() { 180 | cartViewModel.getCartItemsLiveData().removeObserver(cartObserver); 181 | cartViewModel.getGrandTotal().removeObserver(costObserver); 182 | cartViewModel.getErrorString().removeObserver(errorObserver); 183 | super.onDestroy(); 184 | } 185 | } 186 | -------------------------------------------------------------------------------- /app/src/main/java/com/marty/yummy/ui/HomeScreenActivity.java: -------------------------------------------------------------------------------- 1 | package com.marty.yummy.ui; 2 | 3 | import android.arch.lifecycle.Observer; 4 | import android.arch.lifecycle.ViewModelProviders; 5 | import android.content.Context; 6 | import android.content.Intent; 7 | import android.os.Bundle; 8 | import android.support.annotation.Nullable; 9 | import android.support.v7.app.AppCompatActivity; 10 | import android.support.v7.widget.AppCompatButton; 11 | import android.support.v7.widget.LinearLayoutManager; 12 | import android.support.v7.widget.PopupMenu; 13 | import android.support.v7.widget.RecyclerView; 14 | import android.support.v7.widget.Toolbar; 15 | import android.util.Log; 16 | import android.view.Menu; 17 | import android.view.MenuInflater; 18 | import android.view.MenuItem; 19 | import android.view.View; 20 | import android.view.animation.AnimationUtils; 21 | import android.view.animation.LayoutAnimationController; 22 | import android.widget.ImageView; 23 | import android.widget.TextView; 24 | 25 | import com.bumptech.glide.Glide; 26 | import com.marty.yummy.R; 27 | import com.marty.yummy.model.CartItem; 28 | import com.marty.yummy.model.FoodDetails; 29 | import com.marty.yummy.ui.adapters.FoodListAdapter; 30 | import com.marty.yummy.utility.ObservableObject; 31 | import com.marty.yummy.viewmodel.FoodViewModel; 32 | 33 | import java.util.ArrayList; 34 | import java.util.List; 35 | import java.util.Observable; 36 | 37 | 38 | 39 | public class HomeScreenActivity extends AppCompatActivity implements java.util.Observer, PopupMenu.OnMenuItemClickListener { 40 | 41 | FoodViewModel foodViewModel; 42 | Observer> foodMenuObserver; 43 | Observer> cartObserver; 44 | Observer isFoodUpdateInProgressObserver; 45 | RecyclerView foodList; 46 | FoodListAdapter foodListAdapter; 47 | AppCompatButton bCart; 48 | LayoutAnimationController controller; 49 | ImageView infoImage; 50 | TextView tInfo,tTotalCost,tCartQuantity; 51 | Toolbar cartView; 52 | public static final String INTENT_UPDATE_FOOD = "UPDATE_FOOD"; 53 | public static final String INTENT_UPDATE_LIST = "UPDATE_LIST"; 54 | public static final String ACTION_SORT_BY_PRICE = "SORT_PRICE"; 55 | public static final String ACTION_SORT_BY_RATING = "SORT_RATING"; 56 | 57 | @Override 58 | protected void onCreate(Bundle savedInstanceState) { 59 | setTheme(R.style.AppTheme_Base); 60 | super.onCreate(savedInstanceState); 61 | setContentView(R.layout.activity_home_screen); 62 | Toolbar toolbar = findViewById(R.id.toolbar); 63 | toolbar.setTitle(R.string.app_name); 64 | setSupportActionBar(toolbar); 65 | 66 | foodViewModel = ViewModelProviders.of(this).get(FoodViewModel.class); 67 | foodList = findViewById(R.id.food_list); 68 | tInfo = findViewById(R.id.t_loading); 69 | infoImage = findViewById(R.id.i_loading); 70 | cartView = findViewById(R.id.cart_view); 71 | tTotalCost = cartView.findViewById(R.id.t_total_price); 72 | tCartQuantity = cartView.findViewById(R.id.t_cart_count); 73 | bCart = cartView.findViewById(R.id.b_cart); 74 | bCart.setOnClickListener(new View.OnClickListener() { 75 | @Override 76 | public void onClick(View view) { 77 | startActivity(new Intent(HomeScreenActivity.this,CartActivity.class)); 78 | } 79 | }); 80 | RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this); 81 | foodList.setLayoutManager(mLayoutManager); 82 | foodListAdapter = new FoodListAdapter(new ArrayList()); 83 | controller = AnimationUtils.loadLayoutAnimation(foodList.getContext(), R.anim.layout_slide_from_bottom); 84 | foodList.setAdapter(foodListAdapter); 85 | foodList.scheduleLayoutAnimation(); 86 | } 87 | 88 | @Override 89 | protected void onPostCreate(@Nullable Bundle savedInstanceState) { 90 | super.onPostCreate(savedInstanceState); 91 | foodMenuObserver = new Observer>() { 92 | @Override 93 | public void onChanged(@Nullable List foodDetails) { 94 | if(foodDetails!=null){ 95 | foodListAdapter.setData(foodDetails); 96 | foodListAdapter.notifyDataSetChanged(); 97 | runLayoutAnimation(foodList); 98 | }else{ 99 | Log.e("Food details","null"); 100 | } 101 | } 102 | }; 103 | isFoodUpdateInProgressObserver = new Observer() { 104 | @Override 105 | public void onChanged(@Nullable Boolean aBoolean) { 106 | if(aBoolean!=null && !aBoolean){ 107 | showProgress(false,true); 108 | subscribeToFoodObserver(); 109 | }else{ 110 | showProgress(true,false); 111 | } 112 | } 113 | }; 114 | cartObserver = new Observer>() { 115 | @Override 116 | public void onChanged(@Nullable List cartItems) { 117 | updateCartUI(cartItems); 118 | } 119 | }; 120 | foodViewModel.isFoodUpdateInProgress().observe(this,isFoodUpdateInProgressObserver); 121 | ObservableObject.getInstance().addObserver(this); 122 | } 123 | 124 | @Override 125 | public boolean onCreateOptionsMenu(Menu menu) { 126 | getMenuInflater().inflate(R.menu.menu_main, menu); 127 | return true; 128 | } 129 | 130 | @Override 131 | public boolean onOptionsItemSelected(MenuItem item) { 132 | if(item.getItemId() == R.id.action_sort){ 133 | showPopup(findViewById(R.id.action_sort)); 134 | } 135 | return super.onOptionsItemSelected(item); 136 | } 137 | 138 | public void showPopup(View v) { 139 | PopupMenu popup = new PopupMenu(this, v); 140 | MenuInflater inflater = popup.getMenuInflater(); 141 | inflater.inflate(R.menu.actions, popup.getMenu()); 142 | popup.setOnMenuItemClickListener(this); 143 | popup.show(); 144 | } 145 | 146 | @Override 147 | public boolean onMenuItemClick(MenuItem menuItem) { 148 | if(menuItem.getItemId() == R.id.action_sort_price){ 149 | foodViewModel.sortFood(ACTION_SORT_BY_PRICE); 150 | }else if(menuItem.getItemId() == R.id.action_sort_rating){ 151 | foodViewModel.sortFood(ACTION_SORT_BY_RATING); 152 | } 153 | return false; 154 | } 155 | 156 | private void updateCartUI(List cartItems) { 157 | if(cartItems!=null && cartItems.size()>0){ 158 | cartView.setVisibility(View.VISIBLE); 159 | Double cost = 0.0; 160 | int quantity = 0; 161 | for(CartItem cartItem:cartItems){ 162 | cost = cost+(cartItem.getPrice()*cartItem.getQuantity()); 163 | quantity = quantity+cartItem.getQuantity(); 164 | } 165 | tCartQuantity.setText(String.valueOf(quantity)); 166 | tTotalCost.setText(getString(R.string.rupee_symbol)+String.valueOf(cost)); 167 | }else{ 168 | cartView.setVisibility(View.GONE); 169 | tCartQuantity.setText("0"); 170 | tTotalCost.setText(getString(R.string.rupee_symbol)+"0"); 171 | } 172 | } 173 | 174 | private void subscribeToFoodObserver() { 175 | if(!foodViewModel.getFoodDetailsMutableLiveData().hasObservers()) { 176 | foodViewModel.getFoodDetailsMutableLiveData().observe(HomeScreenActivity.this, foodMenuObserver); 177 | } 178 | if(!foodViewModel.getCartItemsLiveData().hasObservers()){ 179 | foodViewModel.getCartItemsLiveData().observe(this,cartObserver); 180 | } 181 | } 182 | 183 | private void showProgress(boolean show, boolean showList) { 184 | foodList.setVisibility(showList?View.VISIBLE:View.GONE); 185 | tInfo.setVisibility(show?View.VISIBLE:View.GONE); 186 | infoImage.setVisibility(show?View.VISIBLE:View.GONE); 187 | } 188 | 189 | private void runLayoutAnimation(final RecyclerView recyclerView) { 190 | Context context = recyclerView.getContext(); 191 | LayoutAnimationController controller = 192 | AnimationUtils.loadLayoutAnimation(context, R.anim.layout_slide_from_bottom); 193 | 194 | recyclerView.setLayoutAnimation(controller); 195 | if(recyclerView.getAdapter()!=null) { 196 | recyclerView.getAdapter().notifyDataSetChanged(); 197 | recyclerView.scheduleLayoutAnimation(); 198 | } 199 | } 200 | 201 | @Override 202 | protected void onDestroy() { 203 | foodViewModel.getFoodDetailsMutableLiveData().removeObserver(foodMenuObserver); 204 | foodViewModel.isFoodUpdateInProgress().removeObserver(isFoodUpdateInProgressObserver); 205 | foodViewModel.getCartItemsLiveData().removeObserver(cartObserver); 206 | ObservableObject.getInstance().deleteObserver(this); 207 | Glide.get(this).clearMemory(); 208 | super.onDestroy(); 209 | } 210 | 211 | @Override 212 | public void update(Observable observable, Object o) { 213 | Intent intent = (Intent)o; 214 | if(intent!=null && intent.getAction() != null) { 215 | if (intent.getAction().equals(INTENT_UPDATE_FOOD)) { 216 | foodViewModel.updateCart(foodListAdapter.getItem(intent.getIntExtra("position",-1))); 217 | }else if(intent.getAction().equals(INTENT_UPDATE_LIST)){ 218 | foodListAdapter.notifyDataSetChanged(); 219 | } 220 | } 221 | } 222 | } 223 | -------------------------------------------------------------------------------- /app/src/main/java/com/marty/yummy/ui/IndividualActivity.java: -------------------------------------------------------------------------------- 1 | package com.marty.yummy.ui; 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.v7.app.ActionBar; 9 | import android.support.v7.app.AppCompatActivity; 10 | import android.support.v7.widget.AppCompatButton; 11 | import android.support.v7.widget.AppCompatImageView; 12 | import android.support.v7.widget.Toolbar; 13 | import android.view.View; 14 | import android.widget.ImageView; 15 | import android.widget.TextView; 16 | 17 | import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions; 18 | import com.marty.yummy.R; 19 | import com.marty.yummy.model.CartItem; 20 | import com.marty.yummy.model.FoodDetails; 21 | import com.marty.yummy.utility.GlideApp; 22 | import com.marty.yummy.viewmodel.FoodDetailViewModel; 23 | 24 | import java.util.List; 25 | 26 | public class IndividualActivity extends AppCompatActivity implements View.OnClickListener { 27 | 28 | private FoodDetailViewModel foodDetailViewModel; 29 | Observer foodDetailObserver; 30 | private ImageView iFoodImage; 31 | private TextView tName,tCost,tQuantity,tTotalCost,tCartQuantity; 32 | private Toolbar cartView; 33 | Observer> cartObserver; 34 | private FoodDetails duplicateFoodDetails; 35 | 36 | @Override 37 | protected void onCreate(@Nullable Bundle savedInstanceState) { 38 | super.onCreate(savedInstanceState); 39 | setContentView(R.layout.activity_detail_layout); 40 | ActionBar actionBar = getSupportActionBar(); 41 | if(actionBar!=null){ 42 | actionBar.setHomeAsUpIndicator(R.drawable.ic_back); 43 | actionBar.setDisplayHomeAsUpEnabled(true); 44 | } 45 | String foodId = ""; 46 | Bundle bundle = getIntent().getExtras(); 47 | if(bundle!=null){ 48 | if(bundle.containsKey("name")) { 49 | foodId = bundle.getString("name"); 50 | } 51 | } 52 | if(foodId !=null && !foodId.isEmpty()) { 53 | iFoodImage = findViewById(R.id.i_food_image); 54 | tName = findViewById(R.id.t_name); 55 | tCost = findViewById(R.id.t_cost); 56 | tQuantity = findViewById(R.id.t_quantity); 57 | AppCompatImageView iPlus = findViewById(R.id.i_plus); 58 | AppCompatImageView iMinus = findViewById(R.id.i_minus); 59 | iPlus.setOnClickListener(this); 60 | iMinus.setOnClickListener(this); 61 | cartView = findViewById(R.id.cart_view); 62 | tTotalCost = findViewById(R.id.t_total_price); 63 | tCartQuantity = findViewById(R.id.t_cart_count); 64 | AppCompatButton bCart = findViewById(R.id.b_cart); 65 | bCart.setOnClickListener(this); 66 | 67 | foodDetailViewModel = ViewModelProviders.of(this).get(FoodDetailViewModel.class); 68 | foodDetailViewModel.subscribeForFoodDetails(foodId); 69 | foodDetailObserver = new Observer() { 70 | @Override 71 | public void onChanged(@Nullable FoodDetails foodDetails) { 72 | updateUI(foodDetails); 73 | } 74 | }; 75 | cartObserver = new Observer>() { 76 | @Override 77 | public void onChanged(@Nullable List cartItems) { 78 | updateCartUI(cartItems); 79 | } 80 | }; 81 | foodDetailViewModel.getFoodDetailsLiveData().observe(this, foodDetailObserver); 82 | foodDetailViewModel.getCartItemsLiveData().observe(this, cartObserver); 83 | } 84 | } 85 | 86 | /*@Override 87 | public boolean onOptionsItemSelected(MenuItem item) { 88 | if(item.getItemId() == R.id.home){ 89 | onBackPressed(); 90 | } 91 | return true; 92 | }*/ 93 | 94 | private void updateUI(FoodDetails foodDetails) { 95 | duplicateFoodDetails = foodDetails; 96 | if(foodDetails==null){ 97 | return; 98 | } 99 | tName.setText(foodDetails.getName()); 100 | tCost.setText(getString(R.string.rupee_symbol) + String.valueOf(foodDetails.getPrice())); 101 | GlideApp.with(this).load(foodDetails.getImageUrl()) 102 | .transition(DrawableTransitionOptions.withCrossFade()) 103 | .placeholder(R.drawable.ic_food) 104 | .into(iFoodImage); 105 | tQuantity.setText(String.valueOf(foodDetails.getQuantity())); 106 | } 107 | 108 | @Override 109 | public void onClick(View view) { 110 | int id = view.getId(); 111 | switch (id){ 112 | case R.id.i_minus: 113 | if(duplicateFoodDetails.getQuantity()!=0) { 114 | duplicateFoodDetails.setQuantity(duplicateFoodDetails.getQuantity()-1); 115 | tQuantity.setText(String.valueOf(duplicateFoodDetails.getQuantity())); 116 | } 117 | foodDetailViewModel.updateCart(duplicateFoodDetails); 118 | break; 119 | case R.id.i_plus: 120 | duplicateFoodDetails.setQuantity(duplicateFoodDetails.getQuantity()+1); 121 | tQuantity.setText(String.valueOf(duplicateFoodDetails.getQuantity())); 122 | foodDetailViewModel.updateCart(duplicateFoodDetails); 123 | break; 124 | case R.id.b_cart: 125 | startActivity(new Intent(this,CartActivity.class)); 126 | break; 127 | 128 | } 129 | } 130 | 131 | private void updateCartUI(List cartItems) { 132 | if(cartItems!=null && cartItems.size()>0){ 133 | cartView.setVisibility(View.VISIBLE); 134 | Double cost = 0.0; 135 | int quantity = 0; 136 | for(CartItem cartItem:cartItems){ 137 | cost = cost+(cartItem.getPrice()*cartItem.getQuantity()); 138 | quantity = quantity+cartItem.getQuantity(); 139 | } 140 | tCartQuantity.setText(String.valueOf(quantity)); 141 | tTotalCost.setText(getString(R.string.rupee_symbol)+String.valueOf(cost)); 142 | }else{ 143 | cartView.setVisibility(View.GONE); 144 | tCartQuantity.setText("0"); 145 | tTotalCost.setText(getString(R.string.rupee_symbol)+"0"); 146 | } 147 | } 148 | 149 | @Override 150 | protected void onDestroy() { 151 | foodDetailViewModel.getFoodDetailsLiveData().removeObserver(foodDetailObserver); 152 | foodDetailViewModel.getCartItemsLiveData().removeObserver(cartObserver); 153 | super.onDestroy(); 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /app/src/main/java/com/marty/yummy/ui/RatingTextView.java: -------------------------------------------------------------------------------- 1 | package com.marty.yummy.ui; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.graphics.Canvas; 6 | import android.graphics.drawable.Drawable; 7 | import android.support.v4.content.ContextCompat; 8 | import android.support.v7.widget.AppCompatTextView; 9 | import android.util.AttributeSet; 10 | 11 | import com.marty.yummy.R; 12 | 13 | import static android.view.Gravity.CENTER; 14 | 15 | public class RatingTextView extends AppCompatTextView { 16 | 17 | private Float rating = 0.0f; 18 | 19 | public RatingTextView(Context context) { 20 | super(context); 21 | setUpRateStar(); 22 | drawRating(); 23 | } 24 | 25 | public RatingTextView(Context context, AttributeSet attrs) { 26 | super(context, attrs); 27 | setUpRateStar(); 28 | TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RatingTextView); 29 | rating = ta.getFloat(R.styleable.RatingTextView_rating,0.0f); 30 | ta.recycle(); 31 | drawRating(); 32 | } 33 | 34 | public RatingTextView(Context context, AttributeSet attrs, int defStyleAttr) { 35 | super(context, attrs, defStyleAttr); 36 | setUpRateStar(); 37 | TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RatingTextView); 38 | rating = ta.getFloat(R.styleable.RatingTextView_rating,0.0f); 39 | ta.recycle(); 40 | drawRating(); 41 | } 42 | 43 | private void setUpRateStar() { 44 | Drawable starImage = ContextCompat.getDrawable(getContext(), R.drawable.ic_star); 45 | setCompoundDrawablesRelativeWithIntrinsicBounds(null,null, starImage,null); 46 | } 47 | 48 | private void drawRating() { 49 | setTextColor(getResources().getColor(R.color.white)); 50 | setText(" "+String.valueOf(rating)); 51 | setGravity(CENTER); 52 | setPadding(8,8,8,8); 53 | if(rating>=4){ 54 | setBackgroundColor(getResources().getColor(R.color.ratingHigh)); 55 | }else{ 56 | setBackgroundColor(rating>=3?getResources().getColor(R.color.ratingMedium):getResources().getColor(R.color.ratingLow)); 57 | } 58 | } 59 | 60 | public void setRating(Float rate){ 61 | rating = rate; 62 | invalidate(); 63 | } 64 | 65 | @Override 66 | protected void onDraw(Canvas canvas) { 67 | super.onDraw(canvas); 68 | drawRating(); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /app/src/main/java/com/marty/yummy/ui/adapters/CartListAdapter.java: -------------------------------------------------------------------------------- 1 | package com.marty.yummy.ui.adapters; 2 | 3 | 4 | import android.annotation.SuppressLint; 5 | import android.support.annotation.NonNull; 6 | import android.support.v7.widget.AppCompatImageView; 7 | import android.support.v7.widget.RecyclerView; 8 | import android.view.LayoutInflater; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | import android.widget.TextView; 12 | 13 | import com.marty.yummy.R; 14 | import com.marty.yummy.model.CartItem; 15 | import com.marty.yummy.viewmodel.CartViewModel; 16 | 17 | import java.util.ArrayList; 18 | import java.util.List; 19 | 20 | public class CartListAdapter extends RecyclerView.Adapter { 21 | 22 | private List cartList; 23 | private CartViewModel cartViewModel; 24 | 25 | public CartListAdapter(ArrayList cartItems, CartViewModel cartViewModel) { 26 | this.cartList = cartItems; 27 | this.cartViewModel = cartViewModel; 28 | } 29 | 30 | public void setData(List data) { 31 | this.cartList = data; 32 | } 33 | 34 | public class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener { 35 | 36 | private AppCompatImageView iDelete; 37 | private TextView tName,tPrice,tTotalPrice,tQuantity; 38 | 39 | 40 | RecyclerViewHolders(View itemView) { 41 | super(itemView); 42 | 43 | iDelete = itemView.findViewById(R.id.i_delete); 44 | tName = itemView.findViewById(R.id.t_name); 45 | tPrice = itemView.findViewById(R.id.t_price); 46 | tTotalPrice = itemView.findViewById(R.id.t_total_price); 47 | tQuantity = itemView.findViewById(R.id.t_quantity); 48 | iDelete.setOnClickListener(this); 49 | } 50 | 51 | @Override 52 | public void onClick(View view) { 53 | if(view.getId()==R.id.i_delete){ 54 | cartViewModel.removeItem(cartList.get(getAdapterPosition()).getName()); 55 | } 56 | } 57 | } 58 | 59 | @NonNull 60 | @Override 61 | public RecyclerViewHolders onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { 62 | 63 | @SuppressLint("InflateParams") View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_cart_item, null); 64 | return new RecyclerViewHolders(layoutView); 65 | } 66 | 67 | @Override 68 | public void onBindViewHolder(@NonNull final RecyclerViewHolders holder, int position) { 69 | CartItem cartItem = cartList.get(holder.getAdapterPosition()); 70 | holder.tName.setText(cartItem.getName()); 71 | holder.tPrice.setText("₹ "+cartItem.getPrice()); 72 | holder.tQuantity.setText(String.valueOf(cartItem.getQuantity())); 73 | holder.tTotalPrice.setText("₹ "+String.valueOf(cartItem.getQuantity()*cartItem.getPrice())); 74 | } 75 | 76 | 77 | @Override 78 | public int getItemCount() { 79 | return this.cartList.size(); 80 | } 81 | 82 | 83 | public long getItemId(int position) { 84 | return super.getItemId(position); 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /app/src/main/java/com/marty/yummy/ui/adapters/FoodListAdapter.java: -------------------------------------------------------------------------------- 1 | package com.marty.yummy.ui.adapters; 2 | 3 | import android.annotation.SuppressLint; 4 | import android.content.Intent; 5 | import android.os.Handler; 6 | import android.support.annotation.NonNull; 7 | import android.support.v7.widget.AppCompatImageView; 8 | import android.support.v7.widget.RecyclerView; 9 | import android.view.LayoutInflater; 10 | import android.view.View; 11 | import android.view.ViewGroup; 12 | import android.widget.ImageView; 13 | import android.widget.RelativeLayout; 14 | import android.widget.TextView; 15 | 16 | import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions; 17 | import com.marty.yummy.R; 18 | import com.marty.yummy.dbutilities.AppDatabase; 19 | import com.marty.yummy.model.FoodDetails; 20 | import com.marty.yummy.ui.IndividualActivity; 21 | import com.marty.yummy.ui.RatingTextView; 22 | import com.marty.yummy.utility.GlideApp; 23 | import com.marty.yummy.utility.ObservableObject; 24 | 25 | import java.util.List; 26 | 27 | import static com.marty.yummy.ui.HomeScreenActivity.INTENT_UPDATE_FOOD; 28 | 29 | public class FoodListAdapter extends RecyclerView.Adapter { 30 | 31 | private List foodList; 32 | private Handler handler; 33 | 34 | public void setData(List data) { 35 | this.foodList = data; 36 | } 37 | 38 | public class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener { 39 | 40 | private TextView tName,tPrice,tCount; 41 | private ImageView iFood; 42 | private AppCompatImageView iPlus,iMinus; 43 | private RelativeLayout foodCard; 44 | private RatingTextView tRating; 45 | 46 | 47 | RecyclerViewHolders(View itemView) { 48 | super(itemView); 49 | foodCard = itemView.findViewById(R.id.food_card); 50 | tName = foodCard.findViewById(R.id.t_food_name); 51 | tPrice = foodCard.findViewById(R.id.t_price); 52 | tCount = foodCard.findViewById(R.id.t_count); 53 | tRating = foodCard.findViewById(R.id.t_rating); 54 | iPlus = foodCard.findViewById(R.id.i_plus); 55 | iMinus = foodCard.findViewById(R.id.i_minus); 56 | iFood = foodCard.findViewById(R.id.i_food_image); 57 | foodCard.setOnClickListener(this); 58 | } 59 | 60 | @Override 61 | public void onClick(View view) { 62 | if(view.getId()==R.id.food_card){ 63 | AppDatabase.getDatabase(view.getContext()).foodDetailsDao().save(foodList.get(getAdapterPosition())); 64 | Intent i = new Intent(view.getContext(),IndividualActivity.class); 65 | i.putExtra("name",foodList.get(getAdapterPosition()).getName()); 66 | view.getContext().startActivity(i); 67 | } 68 | } 69 | } 70 | 71 | private Intent getUpdateIntent(int position) { 72 | Intent i = new Intent(INTENT_UPDATE_FOOD); 73 | i.putExtra("position",position); 74 | return i; 75 | } 76 | 77 | public FoodListAdapter(List listDetails) { 78 | this.foodList = listDetails; 79 | this.handler = new Handler(); 80 | } 81 | 82 | @NonNull 83 | @Override 84 | public RecyclerViewHolders onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { 85 | 86 | @SuppressLint("InflateParams") View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_food_item, null); 87 | return new RecyclerViewHolders(layoutView); 88 | } 89 | 90 | @Override 91 | public void onBindViewHolder(@NonNull final RecyclerViewHolders holder, int position) { 92 | final FoodDetails foodDetails = foodList.get(holder.getAdapterPosition()); 93 | holder.tName.setText(foodDetails.getName()); 94 | holder.tPrice.setText("₹ " + foodDetails.getPrice()); 95 | holder.tRating.setRating(foodDetails.getRating().floatValue()); 96 | LoadImage li = new LoadImage(holder.iFood,holder.iMinus,holder.iPlus,holder.tCount,holder.getAdapterPosition()); 97 | handler.post(li); 98 | } 99 | 100 | 101 | @Override 102 | public int getItemCount() { 103 | return this.foodList.size(); 104 | } 105 | 106 | 107 | public long getItemId(int position) { 108 | return super.getItemId(position); 109 | } 110 | 111 | public FoodDetails getItem(int position){ 112 | if(position!= -1) { 113 | return foodList.get(position); 114 | }else { 115 | return null; 116 | } 117 | } 118 | 119 | private class LoadImage implements Runnable { 120 | ImageView imageView; 121 | TextView tCount; 122 | AppCompatImageView iPlus,iMinus; 123 | int position; 124 | 125 | LoadImage(ImageView imageView, AppCompatImageView iMinus, AppCompatImageView iPlus, TextView tCount, int adapterPosition) { 126 | this.imageView = imageView; 127 | this.iMinus = iMinus; 128 | this.iPlus = iPlus; 129 | this.tCount = tCount; 130 | this.position = adapterPosition; 131 | } 132 | 133 | @Override 134 | public void run() { 135 | GlideApp.with(imageView.getContext()).load(foodList.get(position).getImageUrl()) 136 | .transition(DrawableTransitionOptions.withCrossFade()) 137 | .placeholder(R.drawable.ic_food) 138 | .into(imageView); 139 | int quantity = AppDatabase.getDatabase(imageView.getContext()).cartItemDao().getCartCount(foodList.get(position).getName()); 140 | foodList.get(position).setQuantity(quantity); 141 | tCount.setText(String.valueOf(quantity)); 142 | iMinus.setOnClickListener(new View.OnClickListener() { 143 | @Override 144 | public void onClick(View view) { 145 | if(foodList.get(position).getQuantity()!=0) { 146 | foodList.get(position).setQuantity(foodList.get(position).getQuantity()-1); 147 | tCount.setText(String.valueOf(foodList.get(position).getQuantity())); 148 | ObservableObject.getInstance().updateValue(getUpdateIntent(position)); 149 | } 150 | } 151 | }); 152 | iPlus.setOnClickListener(new View.OnClickListener() { 153 | @Override 154 | public void onClick(View view) { 155 | foodList.get(position).setQuantity(foodList.get(position).getQuantity()+1); 156 | tCount.setText(String.valueOf(foodList.get(position).getQuantity())); 157 | ObservableObject.getInstance().updateValue(getUpdateIntent(position)); 158 | } 159 | }); 160 | } 161 | } 162 | } 163 | -------------------------------------------------------------------------------- /app/src/main/java/com/marty/yummy/utility/MyAppGlideModule.java: -------------------------------------------------------------------------------- 1 | package com.marty.yummy.utility; 2 | 3 | import com.bumptech.glide.annotation.GlideModule; 4 | import com.bumptech.glide.module.AppGlideModule; 5 | 6 | @GlideModule 7 | public final class MyAppGlideModule extends AppGlideModule {} 8 | -------------------------------------------------------------------------------- /app/src/main/java/com/marty/yummy/utility/ObservableObject.java: -------------------------------------------------------------------------------- 1 | package com.marty.yummy.utility; 2 | 3 | import java.util.Observable; 4 | 5 | public class ObservableObject extends Observable { 6 | private static ObservableObject instance = new ObservableObject(); 7 | 8 | public static ObservableObject getInstance() { 9 | return instance; 10 | } 11 | 12 | private ObservableObject() { 13 | } 14 | 15 | public void updateValue(Object data) { 16 | synchronized (this) { 17 | setChanged(); 18 | notifyObservers(data); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /app/src/main/java/com/marty/yummy/viewmodel/CartViewModel.java: -------------------------------------------------------------------------------- 1 | package com.marty.yummy.viewmodel; 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.content.Intent; 10 | import android.support.annotation.NonNull; 11 | import android.support.annotation.Nullable; 12 | 13 | import com.marty.yummy.dbutilities.AppDatabase; 14 | import com.marty.yummy.model.CartItem; 15 | import com.marty.yummy.utility.ObservableObject; 16 | 17 | import java.util.List; 18 | 19 | import static com.marty.yummy.ui.HomeScreenActivity.INTENT_UPDATE_LIST; 20 | 21 | public class CartViewModel extends AndroidViewModel { 22 | 23 | private AppDatabase db; 24 | private Double totalCost=0.0,discount=0.0,deliveryCost=0.0; 25 | private MutableLiveData grandTotal = new MutableLiveData<>(); 26 | private MediatorLiveData> mediatorLiveData = new MediatorLiveData<>(); 27 | private String couponApplied=""; 28 | private MutableLiveData errorString = new MutableLiveData<>(); 29 | 30 | public CartViewModel(@NonNull Application application) { 31 | super(application); 32 | init(); 33 | } 34 | 35 | private void init() { 36 | db = AppDatabase.getDatabase(getApplication().getApplicationContext()); 37 | subscribeToCartChanges(); 38 | } 39 | 40 | private void subscribeToCartChanges() { 41 | LiveData> cartItemsLiveData = db.cartItemDao().getCartItems(); 42 | mediatorLiveData.addSource(cartItemsLiveData, new Observer>() { 43 | @Override 44 | public void onChanged(@Nullable List cartItems) { 45 | mediatorLiveData.setValue(cartItems); 46 | calculateGrandTotalCost(); 47 | } 48 | }); 49 | } 50 | 51 | private void calculateGrandTotalCost() { 52 | List cartItemList = mediatorLiveData.getValue(); 53 | totalCost = 0.0; 54 | if(cartItemList!=null) { 55 | for (CartItem cartItem : cartItemList) { 56 | totalCost = totalCost+(cartItem.getPrice()*cartItem.getQuantity()); 57 | } 58 | discount = calculateDiscount(couponApplied); 59 | deliveryCost = calculateDeliveryCost(couponApplied); 60 | grandTotal.setValue(totalCost - discount + deliveryCost); 61 | } 62 | } 63 | 64 | private Double calculateDeliveryCost(String couponApplied) { 65 | if(couponApplied.equals("FREEDEL") && totalCost>100){ 66 | errorString.setValue(""); 67 | return 0.0; 68 | }else if(couponApplied.equals("FREEDEL")) { 69 | errorString.setValue("Cart value should be > 100"); 70 | } 71 | return 30.0; 72 | } 73 | 74 | private Double calculateDiscount(String couponApplied) { 75 | if(couponApplied.equals("F22LABS") && totalCost>400){ 76 | errorString.setValue(""); 77 | return (totalCost*20)/100; 78 | }else if(couponApplied.equals("F22LABS")){ 79 | errorString.setValue("Cart value should be > 400"); 80 | } 81 | return 0.0; 82 | } 83 | 84 | public Double getDiscountAmt(){ 85 | return discount; 86 | } 87 | 88 | public Double getDeliveryCost(){ 89 | return deliveryCost; 90 | } 91 | 92 | public Double getTotalCost(){ 93 | return totalCost; 94 | } 95 | 96 | public MutableLiveData getGrandTotal(){ 97 | return grandTotal; 98 | } 99 | 100 | 101 | public void applyCoupon(String coupon) { 102 | couponApplied = coupon; 103 | calculateGrandTotalCost(); 104 | } 105 | 106 | public MediatorLiveData> getCartItemsLiveData() { 107 | return mediatorLiveData; 108 | } 109 | 110 | public void removeItem(String name){ 111 | db.cartItemDao().deleteCartItem(name); 112 | ObservableObject.getInstance().updateValue(new Intent(INTENT_UPDATE_LIST)); 113 | } 114 | 115 | public MutableLiveData getErrorString(){ 116 | return errorString; 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /app/src/main/java/com/marty/yummy/viewmodel/FoodDetailViewModel.java: -------------------------------------------------------------------------------- 1 | package com.marty.yummy.viewmodel; 2 | 3 | import android.app.Application; 4 | import android.arch.lifecycle.AndroidViewModel; 5 | import android.arch.lifecycle.LiveData; 6 | import android.support.annotation.NonNull; 7 | 8 | import com.marty.yummy.dbutilities.AppDatabase; 9 | import com.marty.yummy.model.CartItem; 10 | import com.marty.yummy.model.FoodDetails; 11 | import com.marty.yummy.services.repository.FoodRepository; 12 | 13 | import java.util.List; 14 | 15 | public class FoodDetailViewModel extends AndroidViewModel { 16 | 17 | private AppDatabase db; 18 | private LiveData> cartItemsLiveData; 19 | private LiveData foodDetailsLiveData; 20 | 21 | public FoodDetailViewModel(@NonNull Application application) { 22 | super(application); 23 | init(); 24 | } 25 | 26 | private void init() { 27 | db = AppDatabase.getDatabase(getApplication().getApplicationContext()); 28 | subscribeToCartChanges(); 29 | } 30 | 31 | private void subscribeToCartChanges() { 32 | cartItemsLiveData = db.cartItemDao().getCartItems(); 33 | } 34 | 35 | public void subscribeForFoodDetails(String name){ 36 | foodDetailsLiveData = db.foodDetailsDao().getFood(name); 37 | } 38 | 39 | public LiveData getFoodDetailsLiveData(){ 40 | return foodDetailsLiveData; 41 | } 42 | 43 | public LiveData> getCartItemsLiveData() { 44 | return cartItemsLiveData; 45 | } 46 | 47 | public void updateCart(FoodDetails foodDetails){ 48 | FoodRepository.getInstance().updateCart(db,foodDetails); 49 | db.foodDetailsDao().save(foodDetails); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /app/src/main/java/com/marty/yummy/viewmodel/FoodViewModel.java: -------------------------------------------------------------------------------- 1 | package com.marty.yummy.viewmodel; 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.support.annotation.NonNull; 10 | import android.support.annotation.Nullable; 11 | 12 | import com.marty.yummy.dbutilities.AppDatabase; 13 | import com.marty.yummy.model.CartItem; 14 | import com.marty.yummy.model.FoodDetails; 15 | import com.marty.yummy.services.repository.FoodRepository; 16 | 17 | import java.util.List; 18 | 19 | import static com.marty.yummy.ui.HomeScreenActivity.ACTION_SORT_BY_PRICE; 20 | import static com.marty.yummy.ui.HomeScreenActivity.ACTION_SORT_BY_RATING; 21 | 22 | 23 | public class FoodViewModel extends AndroidViewModel { 24 | 25 | private AppDatabase db; 26 | private MediatorLiveData> foodDetailsMediatorLiveData = new MediatorLiveData<>(); 27 | private LiveData> foodDetailsLiveDataSortPrice; 28 | private LiveData> foodDetailsLiveDataSortRating; 29 | private LiveData> cartItemsLiveData; 30 | private MutableLiveData isFoodCallInProgress = new MutableLiveData<>(); 31 | private static String DEFAULT_SORT = ACTION_SORT_BY_PRICE; 32 | 33 | public FoodViewModel(@NonNull Application application) { 34 | super(application); 35 | init(); 36 | } 37 | 38 | private void init() { 39 | db = AppDatabase.getDatabase(getApplication().getApplicationContext()); 40 | updateFoodMenu(); 41 | subscribeToFoodChanges(); 42 | subscribeToCartChanges(); 43 | } 44 | 45 | private void subscribeToCartChanges() { 46 | cartItemsLiveData = db.cartItemDao().getCartItems(); 47 | } 48 | 49 | private void updateFoodMenu() { 50 | isFoodCallInProgress = FoodRepository.getInstance().getFoodMenu(getApplication().getApplicationContext()); 51 | } 52 | 53 | private void subscribeToFoodChanges() { 54 | if(DEFAULT_SORT.equals(ACTION_SORT_BY_PRICE)){ 55 | foodDetailsLiveDataSortPrice = db.foodDetailsDao().getFoodsByPrice(); 56 | foodDetailsMediatorLiveData.addSource(foodDetailsLiveDataSortPrice, new Observer>() { 57 | @Override 58 | public void onChanged(@Nullable List foodDetails) { 59 | foodDetailsMediatorLiveData.setValue(foodDetails); 60 | } 61 | }); 62 | }else if(DEFAULT_SORT.equals(ACTION_SORT_BY_RATING)){ 63 | foodDetailsLiveDataSortRating = db.foodDetailsDao().getFoodsByRating(); 64 | foodDetailsMediatorLiveData.addSource(foodDetailsLiveDataSortRating, new Observer>() { 65 | @Override 66 | public void onChanged(@Nullable List foodDetails) { 67 | foodDetailsMediatorLiveData.setValue(foodDetails); 68 | } 69 | }); 70 | } 71 | } 72 | 73 | public MediatorLiveData> getFoodDetailsMutableLiveData() { 74 | return foodDetailsMediatorLiveData; 75 | } 76 | 77 | public void sortFood(String action){ 78 | removeSource(DEFAULT_SORT); 79 | DEFAULT_SORT = action; 80 | subscribeToFoodChanges(); 81 | } 82 | 83 | private void removeSource(String default_sort) { 84 | switch (default_sort){ 85 | case ACTION_SORT_BY_PRICE: 86 | foodDetailsMediatorLiveData.removeSource(foodDetailsLiveDataSortPrice); 87 | break; 88 | case ACTION_SORT_BY_RATING: 89 | foodDetailsMediatorLiveData.removeSource(foodDetailsLiveDataSortRating); 90 | break; 91 | } 92 | } 93 | 94 | public LiveData isFoodUpdateInProgress(){ 95 | return isFoodCallInProgress; 96 | } 97 | 98 | public LiveData> getCartItemsLiveData() { 99 | return cartItemsLiveData; 100 | } 101 | 102 | public void updateCart(FoodDetails foodDetails){ 103 | FoodRepository.getInstance().updateCart(db,foodDetails); 104 | } 105 | 106 | } 107 | -------------------------------------------------------------------------------- /app/src/main/java/com/marty/yummy/worker/SaveFoodMenu.java: -------------------------------------------------------------------------------- 1 | package com.marty.yummy.worker; 2 | 3 | import android.os.AsyncTask; 4 | 5 | import com.marty.yummy.dbutilities.AppDatabase; 6 | import com.marty.yummy.model.FoodDetails; 7 | 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | 11 | public class SaveFoodMenu extends AsyncTask { 12 | private AppDatabase db; 13 | private List foodDetails; 14 | public SaveFoodMenu(AppDatabase db,List foodDetails) { 15 | this.db = db; 16 | this.foodDetails = foodDetails; 17 | } 18 | 19 | @Override 20 | protected Void doInBackground(Void... voids) { 21 | if(db!=null){ 22 | if(foodDetails!=null && foodDetails.size()>0) { 23 | List nameList = new ArrayList<>(); 24 | for(int i=0;i { 10 | private AppDatabase db; 11 | public UpdateCart(AppDatabase db) { 12 | this.db = db; 13 | } 14 | 15 | @Override 16 | protected Void doInBackground(FoodDetails... foodDetails) { 17 | if(db!=null){ 18 | if(foodDetails[0]!=null) { 19 | if (foodDetails[0].getQuantity() == 0) { 20 | db.cartItemDao().deleteCartItem(foodDetails[0].getName()); 21 | return null; 22 | } 23 | CartItem cartItem = new CartItem(); 24 | cartItem.setName(foodDetails[0].getName()); 25 | cartItem.setPrice(foodDetails[0].getPrice()); 26 | cartItem.setQuantity(foodDetails[0].getQuantity()); 27 | db.cartItemDao().add(cartItem); 28 | } 29 | } 30 | return null; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /app/src/main/res/anim/item_slide_from_bottom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 10 | 11 | 16 | 17 | -------------------------------------------------------------------------------- /app/src/main/res/anim/layout_slide_from_bottom.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /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_back.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_cart.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_food.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /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_minus.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_plus.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 7 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_sort.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_star.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martinraj/Food-ordering-app-like-swiggy-uber-eats-MVVM-and-Room-database-/1baf19005a7990cb7f53c8f6a14153a4e39ba217/app/src/main/res/drawable/logo.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/rounded_button.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_cart.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 10 | 15 | 22 | 32 | 43 | 44 | 45 | 54 | 64 | 75 | 85 | 94 | 103 | 112 | 121 | 130 | 139 | 148 | 159 | 160 | 161 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_detail_layout.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 14 | 20 | 34 | 46 | 47 | 57 | 58 | 69 | 70 | 79 | 90 | 91 | 92 | 98 | 99 | 102 | 103 | 112 | 113 | 123 | 124 | 134 | 135 | 146 | 147 | 157 | 158 | 159 | 160 | 161 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_home_screen.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 17 | 18 | 23 | 24 | 25 | 26 | 38 | 39 | 55 | 56 | 69 | 70 | 79 | 80 | 83 | 84 | 93 | 94 | 104 | 105 | 115 | 116 | 127 | 128 | 138 | 139 | 140 | 141 | 142 | -------------------------------------------------------------------------------- /app/src/main/res/layout/cart_toolbar.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 13 | 14 | 23 | 24 | 34 | 35 | 45 | 46 | 57 | 58 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /app/src/main/res/layout/launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 10 | 11 | -------------------------------------------------------------------------------- /app/src/main/res/layout/layout_cart_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 14 | 15 | 19 | 20 | 32 | 33 | 42 | 43 | 55 | 65 | 72 | 73 | 74 | 75 | 85 | 86 | -------------------------------------------------------------------------------- /app/src/main/res/layout/layout_food_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 19 | 23 | 30 | 43 | 44 | 53 | 64 | 65 | 75 | 76 | 89 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /app/src/main/res/menu/actions.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 10 | 14 | 15 | -------------------------------------------------------------------------------- /app/src/main/res/menu/menu_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 12 | 13 | -------------------------------------------------------------------------------- /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/martinraj/Food-ordering-app-like-swiggy-uber-eats-MVVM-and-Room-database-/1baf19005a7990cb7f53c8f6a14153a4e39ba217/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_foreground.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martinraj/Food-ordering-app-like-swiggy-uber-eats-MVVM-and-Room-database-/1baf19005a7990cb7f53c8f6a14153a4e39ba217/app/src/main/res/mipmap-hdpi/ic_launcher_foreground.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martinraj/Food-ordering-app-like-swiggy-uber-eats-MVVM-and-Room-database-/1baf19005a7990cb7f53c8f6a14153a4e39ba217/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martinraj/Food-ordering-app-like-swiggy-uber-eats-MVVM-and-Room-database-/1baf19005a7990cb7f53c8f6a14153a4e39ba217/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_foreground.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martinraj/Food-ordering-app-like-swiggy-uber-eats-MVVM-and-Room-database-/1baf19005a7990cb7f53c8f6a14153a4e39ba217/app/src/main/res/mipmap-mdpi/ic_launcher_foreground.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martinraj/Food-ordering-app-like-swiggy-uber-eats-MVVM-and-Room-database-/1baf19005a7990cb7f53c8f6a14153a4e39ba217/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martinraj/Food-ordering-app-like-swiggy-uber-eats-MVVM-and-Room-database-/1baf19005a7990cb7f53c8f6a14153a4e39ba217/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_foreground.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martinraj/Food-ordering-app-like-swiggy-uber-eats-MVVM-and-Room-database-/1baf19005a7990cb7f53c8f6a14153a4e39ba217/app/src/main/res/mipmap-xhdpi/ic_launcher_foreground.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martinraj/Food-ordering-app-like-swiggy-uber-eats-MVVM-and-Room-database-/1baf19005a7990cb7f53c8f6a14153a4e39ba217/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/robo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martinraj/Food-ordering-app-like-swiggy-uber-eats-MVVM-and-Room-database-/1baf19005a7990cb7f53c8f6a14153a4e39ba217/app/src/main/res/mipmap-xhdpi/robo.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martinraj/Food-ordering-app-like-swiggy-uber-eats-MVVM-and-Room-database-/1baf19005a7990cb7f53c8f6a14153a4e39ba217/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_foreground.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martinraj/Food-ordering-app-like-swiggy-uber-eats-MVVM-and-Room-database-/1baf19005a7990cb7f53c8f6a14153a4e39ba217/app/src/main/res/mipmap-xxhdpi/ic_launcher_foreground.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martinraj/Food-ordering-app-like-swiggy-uber-eats-MVVM-and-Room-database-/1baf19005a7990cb7f53c8f6a14153a4e39ba217/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martinraj/Food-ordering-app-like-swiggy-uber-eats-MVVM-and-Room-database-/1baf19005a7990cb7f53c8f6a14153a4e39ba217/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martinraj/Food-ordering-app-like-swiggy-uber-eats-MVVM-and-Room-database-/1baf19005a7990cb7f53c8f6a14153a4e39ba217/app/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martinraj/Food-ordering-app-like-swiggy-uber-eats-MVVM-and-Room-database-/1baf19005a7990cb7f53c8f6a14153a4e39ba217/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 | #C62828 4 | #b71c1c 5 | #ffffff 6 | #D60000 7 | #FFA500 8 | #00A300 9 | #ffffff 10 | #eeeeee 11 | #4c4c4d 12 | 13 | -------------------------------------------------------------------------------- /app/src/main/res/values/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #C62828 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | Yummy 3 | Food Image 4 | 5 | 0 6 | Updating Menu 7 | Loading... 8 | View Cart 9 | Sort Food 10 | Sort by price low to high 11 | Sort by rating 12 | Delivery Charges 13 | Discount 14 | Cart Total 15 | Bill Details 16 | Coupon 17 | Your Cart 18 | 19 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 15 | 16 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /app/src/test/java/com/marty/yummy/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.marty.yummy; 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 | 18 | 19 | } -------------------------------------------------------------------------------- /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 | dependencies { 10 | classpath 'com.android.tools.build:gradle:3.2.1' 11 | 12 | 13 | // NOTE: Do not place your application dependencies here; they belong 14 | // in the individual module build.gradle files 15 | } 16 | } 17 | 18 | allprojects { 19 | repositories { 20 | google() 21 | jcenter() 22 | } 23 | } 24 | 25 | task clean(type: Delete) { 26 | delete rootProject.buildDir 27 | } 28 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # 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/martinraj/Food-ordering-app-like-swiggy-uber-eats-MVVM-and-Room-database-/1baf19005a7990cb7f53c8f6a14153a4e39ba217/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 03 17:17:47 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.6-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 | -------------------------------------------------------------------------------- /screenshots and demo/Screenshot (114).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martinraj/Food-ordering-app-like-swiggy-uber-eats-MVVM-and-Room-database-/1baf19005a7990cb7f53c8f6a14153a4e39ba217/screenshots and demo/Screenshot (114).png -------------------------------------------------------------------------------- /screenshots and demo/Screenshot_20181205-024745.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martinraj/Food-ordering-app-like-swiggy-uber-eats-MVVM-and-Room-database-/1baf19005a7990cb7f53c8f6a14153a4e39ba217/screenshots and demo/Screenshot_20181205-024745.png -------------------------------------------------------------------------------- /screenshots and demo/Screenshot_20181205-024753.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martinraj/Food-ordering-app-like-swiggy-uber-eats-MVVM-and-Room-database-/1baf19005a7990cb7f53c8f6a14153a4e39ba217/screenshots and demo/Screenshot_20181205-024753.png -------------------------------------------------------------------------------- /screenshots and demo/Screenshot_20181205-024829.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martinraj/Food-ordering-app-like-swiggy-uber-eats-MVVM-and-Room-database-/1baf19005a7990cb7f53c8f6a14153a4e39ba217/screenshots and demo/Screenshot_20181205-024829.png -------------------------------------------------------------------------------- /screenshots and demo/Screenshot_20181205-024851.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martinraj/Food-ordering-app-like-swiggy-uber-eats-MVVM-and-Room-database-/1baf19005a7990cb7f53c8f6a14153a4e39ba217/screenshots and demo/Screenshot_20181205-024851.png -------------------------------------------------------------------------------- /screenshots and demo/Screenshot_20181205-024911.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/martinraj/Food-ordering-app-like-swiggy-uber-eats-MVVM-and-Room-database-/1baf19005a7990cb7f53c8f6a14153a4e39ba217/screenshots and demo/Screenshot_20181205-024911.png -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | --------------------------------------------------------------------------------