All Projects → martinraj → Food Ordering App Like Swiggy Uber Eats Mvvm And Room Database

martinraj / Food Ordering App Like Swiggy Uber Eats Mvvm And Room Database

Food ordering app using MVVM architecture patterns, Architecture Lifecycle components and Room database.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Food Ordering App Like Swiggy Uber Eats Mvvm And Room Database

Clean-MVVM-NewsApp
Android News app developed using Clean + MVVM architecture
Stars: ✭ 52 (-1.89%)
Mutual labels:  retrofit2, mvvm-architecture, livedata, room-persistence-library
Fountain
Android Kotlin paged endpoints made easy
Stars: ✭ 175 (+230.19%)
Mutual labels:  architecture-components, retrofit2, livedata, room-persistence-library
Base Mvvm
App built to showcase basic Android View components like ViewPager, RecyclerView(homogeneous and heterogeneous items), NavigationDrawer, Animated Vector Drawables, Collapsing Toolbar Layout etc. housed in a MVVM architecture
Stars: ✭ 18 (-66.04%)
Mutual labels:  architecture-components, retrofit2, livedata, room-persistence-library
Mvvmframe
🏰 MVVMFrame for Android 是一个基于Google官方推出的Architecture Components dependencies(现在叫JetPack){ Lifecycle,LiveData,ViewModel,Room } 构建的快速开发框架。有了MVVMFrame的加持,从此构建一个MVVM模式的项目变得快捷简单。
Stars: ✭ 218 (+311.32%)
Mutual labels:  mvvm-architecture, architecture-components, retrofit2, livedata
News Sample App
A sample news app which demonstrates clean architecture and best practices for developing android app
Stars: ✭ 334 (+530.19%)
Mutual labels:  mvvm-architecture, architecture-components, retrofit2, livedata
Android Clean Architecture Mvvm Dagger Rx
Implemented by Clean Architecture, Dagger2, MVVM, LiveData, RX, Retrofit2, Room, Anko
Stars: ✭ 138 (+160.38%)
Mutual labels:  sample-app, mvvm-architecture, retrofit2, livedata
Android Mvvm Rx3 Dagger2 Navcomponent
Implemented using MVVM, LiveData, Room, RX3, Dagger2, Coil, View Binding, Navigation Component and AndroidX
Stars: ✭ 72 (+35.85%)
Mutual labels:  mvvm-architecture, retrofit2, livedata, room-persistence-library
Android-MVVM-News-App
MVVM News Application with clean code architecture & android jetpack components.
Stars: ✭ 38 (-28.3%)
Mutual labels:  retrofit2, livedata, architecture-components, room-persistence-library
AndroidCleanArchitecture
Android Project with clean android architecture contain Dagger, Retrofit, Retrofit, Android archtecture components, LiveData with MVVM architecture
Stars: ✭ 22 (-58.49%)
Mutual labels:  retrofit2, mvvm-architecture, livedata, architecture-components
Android-Mvi-Starter
Android MVI Starter application
Stars: ✭ 19 (-64.15%)
Mutual labels:  retrofit2, livedata, architecture-components
AndroidGithubIssues
Android app to fetch issues for a given repository
Stars: ✭ 34 (-35.85%)
Mutual labels:  retrofit2, mvvm-architecture, livedata
Offlinesampleapp
Sample Offline-First MVVM app that uses Android Priority Job Queue, Room, Retrofit2, LiveData, LifecycleObserver, RxJava2, Dagger Android
Stars: ✭ 653 (+1132.08%)
Mutual labels:  mvvm-architecture, retrofit2, livedata
MockAppMVVM
A sample app structure using the MVVM architecture using Retrofit, Dagger2, LiveData, RxJava, ViewModel and Room.
Stars: ✭ 14 (-73.58%)
Mutual labels:  retrofit2, mvvm-architecture, livedata
movies
An example approach for modularization, reactive clean architecture and persistancy.
Stars: ✭ 110 (+107.55%)
Mutual labels:  retrofit2, livedata, architecture-components
Superhero-App
🦸🏻‍♂️🦹🏻‍♀️Superhero app built with Kotlin, ViewModel, LiveData, ViewBinding, Room, and Hilt
Stars: ✭ 27 (-49.06%)
Mutual labels:  retrofit2, livedata, architecture-components
Clean Notes
Clean Architecture by layer
Stars: ✭ 259 (+388.68%)
Mutual labels:  mvvm-architecture, retrofit2, room-persistence-library
Movie Zone
movies application using MVVM Architecture
Stars: ✭ 25 (-52.83%)
Mutual labels:  mvvm-architecture, retrofit2, livedata
Simple-Note-App-with-Online-Storage
✍️ Simple Note Making App use Sqllite Room 🧰 for caching the notes and 📥 Firebase Database for online storage
Stars: ✭ 42 (-20.75%)
Mutual labels:  mvvm-architecture, livedata, room-persistence-library
weather-app-android-mvvm
Simple MVVM practice repository for very very beginners. You don't need to know about Dagger, Coroutine or Rx for understanding MVVM and this project. To check the MVVM Architecture Bengali Tutorial visit my blog site
Stars: ✭ 32 (-39.62%)
Mutual labels:  retrofit2, mvvm-architecture, livedata
Android Architecture Components Kotlin
Clean code App with Kotlin and Android Architecture Components
Stars: ✭ 23 (-56.6%)
Mutual labels:  retrofit2, livedata, room-persistence-library

Food ordering app like Swiggy and Uber eats

Food ordering app using MVVM architecture patterns, Architecture Lifecycle components, Retrofit2 and Room database.

In this demo, I have covered Mediator Live data, Mutable live data, Observable, Observers, Retrofit and Room using same POJO.

APP UI

App UI

Room and Retrofit using same Model

I have used same models for Room and Retrofit2 library as both are configured using annotaions.

@Entity
public class FoodDetails {

    @PrimaryKey                   //Room annotation
    @SerializedName("item_name")  //Retrofit annotation
    @Expose
    @NonNull
    private String name;

    @SerializedName("item_price")
    @Expose
    private Double price;

    @SerializedName("average_rating")
    @Expose
    private Double rating;

    @SerializedName("image_url")
    @Expose
    private String imageUrl;

    @SerializedName("item_quantity")
    @Expose
    private Integer quantity = 0;

    // For Retrofit
    public FoodDetails(@NonNull String name, Double price, Double rating, String imageUrl,Integer quantity) {
        this.name = name;
        this.price = price;
        this.rating = rating;
        this.imageUrl = imageUrl;
        this.quantity = quantity;
    }
    
    // Getters and Setters for Room
    @NonNull
    public String getName() {
        return name;
    }

    public void setName(@NonNull String name) {
        this.name = name;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public Double getRating() {
        return rating;
    }

    public void setRating(Double rating) {
        this.rating = rating;
    }

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }
}

Sorting list elements in Room Database and observing using Mediator Live Data

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.

...\
public class FoodViewModel extends AndroidViewModel {
    MediatorLiveData<List<FoodDetails>> foodDetailsMediatorLiveData = new MediatorLiveData<>();

    private LiveData<List<FoodDetails>> foodDetailsLiveDataSortPrice;
    private LiveData<List<FoodDetails>> foodDetailsLiveDataSortRating;
    private AppDatabase db;
    private static String DEFAULT_SORT = ACTION_SORT_BY_PRICE;
    
    public FoodViewModel(@NonNull Application application) {
        super(application);
        init();
    }

    private void init() {
        db = AppDatabase.getDatabase(getApplication().getApplicationContext());
        subscribeToFoodChanges();
    }
    
    private void subscribeToFoodChanges() {
        if(DEFAULT_SORT.equals(ACTION_SORT_BY_PRICE)){
            foodDetailsLiveDataSortPrice = db.foodDetailsDao().getFoodsByPrice();
            foodDetailsMediatorLiveData.addSource(foodDetailsLiveDataSortPrice, new Observer<List<FoodDetails>>() {
                @Override
                public void onChanged(@Nullable List<FoodDetails> foodDetails) {
                    foodDetailsMediatorLiveData.setValue(foodDetails);
                }
            });
        }else if(DEFAULT_SORT.equals(ACTION_SORT_BY_RATING)){
            foodDetailsLiveDataSortRating = db.foodDetailsDao().getFoodsByRating();
            foodDetailsMediatorLiveData.addSource(foodDetailsLiveDataSortRating, new Observer<List<FoodDetails>>() {
                @Override
                public void onChanged(@Nullable List<FoodDetails> foodDetails) {
                    foodDetailsMediatorLiveData.setValue(foodDetails);
                }
            });
        }
    }

...\

Manually we are triggering observers by using foodDetailsMediatorLiveData.setValue(foodDetails);

Room DB query for Sorting food items

@Dao
public interface FoodDetailsDao {

    ...\

    @Query("SELECT * FROM fooddetails ORDER BY price ASC")
    LiveData<List<FoodDetails>> getFoodsByPrice();

    @Query("SELECT * FROM fooddetails ORDER BY rating DESC")
    LiveData<List<FoodDetails>> getFoodsByRating();

    ...\
}

Observing data changes in Activity/Fragment

We have to create observers for live data. Only then if any data is changed, observers will be notified with updated data by ViewModel.

...\

@Override
protected void onCreate(Bundle savedInstanceState) {
    .../
    
    // Define viewmodel
    FoodViewModel foodViewModel = ViewModelProviders.of(this).get(FoodViewModel.class);
    
    // Define Observer with actions to be done after update
    Observer<List<FoodDetails>> foodMenuObserver = new Observer<List<FoodDetails>>() {
            @Override
            public void onChanged(@Nullable List<FoodDetails> foodDetails) {
                if(foodDetails!=null){
                    foodListAdapter.setData(foodDetails);
                    foodListAdapter.notifyDataSetChanged();
                    runLayoutAnimation(foodList);
                }else{
                    Log.e("Food details","null");
                }
            }
        };
        
    // set observer to watch for data changes
    foodViewModel.getFoodDetailsMutableLiveData().observe(this, foodMenuObserver);
    
    .../
}

...\

Retrofit to get data from server

We have used Retrofit to get data from Rest API and updating db using Room in Intent Service

  public interface YummyAPIServices {

    @GET("/data.json")
    Call<List<FoodDetails>> getFoodData();
}

public class FoodRepository {

    private static FoodRepository instance;
    private static final String TAG = "FoodRepository";

    private YummyAPIServices yummyAPIServices = APIClient.getClient().create(YummyAPIServices.class);

    public MutableLiveData<Boolean> getFoodMenu(final Context context){

        final MutableLiveData<Boolean> isFoodCallOngoing = new MutableLiveData<>();
        isFoodCallOngoing.setValue(true);

        yummyAPIServices.getFoodData().enqueue(new Callback<List<FoodDetails>>() {
            @Override
            public void onResponse(Call<List<FoodDetails>> call, Response<List<FoodDetails>> response) {
                if(response.isSuccessful()) {
                    new SaveFoodMenu(AppDatabase.getDatabase(context), response.body()).execute();
                    isFoodCallOngoing.setValue(false); // on success we are updating empty mutable live data with new food menus
                }else{
                    Log.e(TAG,"response not successful");
                }
            }

            @Override
            public void onFailure(Call<List<FoodDetails>> call, Throwable t) {
                Log.e(TAG,t.toString());
            }
        });
        return isFoodCallOngoing; // returns empty mutable live data 
    }

    public static FoodRepository getInstance() {
        if(instance == null){
            synchronized (FoodRepository.class){
                if(instance == null){
                    instance = new FoodRepository();
                }
            }
        }
        return instance;
    }
}

API Client for Retrofit

We are using singleton to get Retrofit client.

public class APIClient {

    private static final String BASE_URL = "YOUR_SERVER_BASE_URL";
    private static Retrofit retrofit = null;


    public static Retrofit getClient() {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }

}

Project Structure

This is the overall project structure of this project.

Project Structure

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].