├── .gitignore ├── .idea ├── caches │ └── gradle_models.ser ├── codeStyles │ └── Project.xml ├── gradle.xml ├── misc.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── codingwithmitch │ │ └── foodrecipes │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── codingwithmitch │ │ │ └── foodrecipes │ │ │ ├── AppExecutors.java │ │ │ ├── BaseActivity.java │ │ │ ├── RecipeActivity.java │ │ │ ├── RecipeListActivity.java │ │ │ ├── adapters │ │ │ ├── CategoryViewHolder.java │ │ │ ├── LoadingViewHolder.java │ │ │ ├── OnRecipeListener.java │ │ │ ├── RecipeRecyclerAdapter.java │ │ │ ├── RecipeViewHolder.java │ │ │ └── SearchExhaustedViewHolder.java │ │ │ ├── models │ │ │ └── Recipe.java │ │ │ ├── repositories │ │ │ └── RecipeRepository.java │ │ │ ├── requests │ │ │ ├── RecipeApi.java │ │ │ ├── RecipeApiClient.java │ │ │ ├── ServiceGenerator.java │ │ │ └── responses │ │ │ │ ├── RecipeResponse.java │ │ │ │ └── RecipeSearchResponse.java │ │ │ ├── util │ │ │ ├── Constants.java │ │ │ ├── HorizontalDottedProgress.java │ │ │ ├── Testing.java │ │ │ └── VerticalSpacingItemDecorator.java │ │ │ └── viewmodels │ │ │ ├── RecipeListViewModel.java │ │ │ └── RecipeViewModel.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ ├── barbeque.jpg │ │ ├── beef.jpg │ │ ├── breakfast.jpg │ │ ├── brunch.jpg │ │ ├── chicken.jpg │ │ ├── dinner.jpg │ │ ├── ic_launcher_background.xml │ │ ├── ic_mood_bad_black_24dp.xml │ │ ├── italian.jpg │ │ └── wine.jpg │ │ ├── layout │ │ ├── activity_base.xml │ │ ├── activity_recipe.xml │ │ ├── activity_recipe_list.xml │ │ ├── layout_category_list_item.xml │ │ ├── layout_loading_list_item.xml │ │ ├── layout_recipe_list_item.xml │ │ └── layout_search_exhausted.xml │ │ ├── menu │ │ └── recipe_search_menu.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── values │ │ ├── colors.xml │ │ ├── dimen.xml │ │ ├── strings.xml │ │ └── styles.xml │ │ └── xml │ │ └── network_security_config.xml │ └── test │ └── java │ └── com │ └── codingwithmitch │ └── foodrecipes │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | -------------------------------------------------------------------------------- /.idea/caches/gradle_models.ser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mitchtabian/RestApiMVVM/18d0ca15e989b361e3b29b3b894776c2c0d4d2f8/.idea/caches/gradle_models.ser -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 |
7 | 8 | 9 | 10 | xmlns:android 11 | 12 | ^$ 13 | 14 | 15 | 16 |
17 |
18 | 19 | 20 | 21 | xmlns:.* 22 | 23 | ^$ 24 | 25 | 26 | BY_NAME 27 | 28 |
29 |
30 | 31 | 32 | 33 | .*:id 34 | 35 | http://schemas.android.com/apk/res/android 36 | 37 | 38 | 39 |
40 |
41 | 42 | 43 | 44 | .*:name 45 | 46 | http://schemas.android.com/apk/res/android 47 | 48 | 49 | 50 |
51 |
52 | 53 | 54 | 55 | name 56 | 57 | ^$ 58 | 59 | 60 | 61 |
62 |
63 | 64 | 65 | 66 | style 67 | 68 | ^$ 69 | 70 | 71 | 72 |
73 |
74 | 75 | 76 | 77 | .* 78 | 79 | ^$ 80 | 81 | 82 | BY_NAME 83 | 84 |
85 |
86 | 87 | 88 | 89 | .* 90 | 91 | http://schemas.android.com/apk/res/android 92 | 93 | 94 | ANDROID_ATTRIBUTE_ORDER 95 | 96 |
97 |
98 | 99 | 100 | 101 | .* 102 | 103 | .* 104 | 105 | 106 | BY_NAME 107 | 108 |
109 |
110 |
111 |
112 |
113 |
-------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 14 | 15 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 14 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 |

REST API with MVVM and Retrofit2

4 |

Watch the app demo.

5 | 6 |
7 | In this course you'll learn how to interact with a REST API from Food2Fork.com. The app will retrieve information from the website and display it in various view types. 8 |

9 | Here's the specifics of what you will see in the course: 10 |

11 |
    12 |
  1. Communicating with a webservice (rest api) using Retrofit2
  2. 13 |
  3. MVVM Architecture: ViewModel, Repository, Client structure
  4. 14 |
  5. How to design an architecture
  6. 15 |
  7. Singletons
  8. 16 |
  9. Custom Loading Animation ProgressBar in Recyclerview
  10. 17 |
  11. ViewModels and AndroidViewModels
  12. 18 |
  13. Multiple View Types in a Recyclerview
  14. 19 |
  15. RecyclerView Pagination
  16. 20 |
  17. Building Custom Toolbars
  18. 21 |
  19. Customizing Toolbar Behaviors with CoordinatorLayout and AppBarLayout
  20. 22 |
  21. Observables, LiveData, MutableLiveData and MediatorLiveData
  22. 23 |
  23. Displaying Images using Glide
  24. 24 |
  25. CardViews
  26. 25 |
  27. SearchViews
  28. 26 |
  29. Menus
  30. 27 |
  31. Passing data between activities using intent extras
  32. 28 |
  33. Executors and Background Threads
  34. 29 |
  35. ThreadPools
  36. 30 |
  37. Network Security Config for HTTP (API 28+)
  38. 31 |
  39. And much more...
  40. 32 |
33 |
34 | 35 | Architecture Diagram 36 |

37 |
38 | 39 |
40 |

41 | 42 | -------------------------------------------------------------------------------- /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.codingwithmitch.foodrecipes" 7 | minSdkVersion 21 8 | targetSdkVersion 28 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | def retrofitVersion = '2.4.0' 23 | def lifecycle_version = "1.1.1" 24 | def supportVersion = "28.0.0" 25 | def glideVersion = "4.8.0" 26 | 27 | implementation fileTree(dir: 'libs', include: ['*.jar']) 28 | implementation 'com.android.support:appcompat-v7:28.0.0' 29 | implementation 'com.android.support.constraint:constraint-layout:1.1.3' 30 | testImplementation 'junit:junit:4.12' 31 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 32 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 33 | 34 | 35 | //retrofit 36 | implementation "com.squareup.retrofit2:retrofit:$retrofitVersion" 37 | 38 | // Retrofit gson converter 39 | implementation "com.squareup.retrofit2:converter-gson:$retrofitVersion" 40 | 41 | // ViewModel and LiveData 42 | implementation "android.arch.lifecycle:extensions:$lifecycle_version" 43 | 44 | // CardViews 45 | implementation "com.android.support:cardview-v7:$supportVersion" 46 | 47 | // RecyclerViews 48 | implementation "com.android.support:recyclerview-v7:$supportVersion" 49 | 50 | // Design Support 51 | implementation "com.android.support:design:$supportVersion" 52 | 53 | // Glide 54 | implementation "com.github.bumptech.glide:glide:$glideVersion" 55 | annotationProcessor "com.github.bumptech.glide:compiler:$glideVersion" 56 | 57 | // Circle ImageView 58 | implementation 'de.hdodenhof:circleimageview:3.0.0' 59 | } 60 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/codingwithmitch/foodrecipes/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.codingwithmitch.foodrecipes; 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.codingwithmitch.foodrecipes", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /app/src/main/java/com/codingwithmitch/foodrecipes/AppExecutors.java: -------------------------------------------------------------------------------- 1 | package com.codingwithmitch.foodrecipes; 2 | 3 | import java.util.concurrent.Executors; 4 | import java.util.concurrent.ScheduledExecutorService; 5 | 6 | public class AppExecutors { 7 | 8 | private static AppExecutors instance; 9 | 10 | public static AppExecutors getInstance(){ 11 | if(instance == null){ 12 | instance = new AppExecutors(); 13 | } 14 | return instance; 15 | } 16 | 17 | private final ScheduledExecutorService mNetworkIO = Executors.newScheduledThreadPool(3); 18 | 19 | public ScheduledExecutorService networkIO(){ 20 | return mNetworkIO; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /app/src/main/java/com/codingwithmitch/foodrecipes/BaseActivity.java: -------------------------------------------------------------------------------- 1 | package com.codingwithmitch.foodrecipes; 2 | 3 | import android.support.constraint.ConstraintLayout; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.view.View; 6 | import android.widget.FrameLayout; 7 | import android.widget.ProgressBar; 8 | 9 | public abstract class BaseActivity extends AppCompatActivity { 10 | 11 | public ProgressBar mProgressBar; 12 | 13 | @Override 14 | public void setContentView(int layoutResID) { 15 | 16 | ConstraintLayout constraintLayout = (ConstraintLayout) getLayoutInflater().inflate(R.layout.activity_base, null); 17 | FrameLayout frameLayout = constraintLayout.findViewById(R.id.activity_content); 18 | mProgressBar = constraintLayout.findViewById(R.id.progress_bar); 19 | 20 | getLayoutInflater().inflate(layoutResID, frameLayout, true); 21 | super.setContentView(constraintLayout); 22 | } 23 | 24 | public void showProgressBar(boolean visibility){ 25 | mProgressBar.setVisibility(visibility ? View.VISIBLE : View.INVISIBLE); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /app/src/main/java/com/codingwithmitch/foodrecipes/RecipeActivity.java: -------------------------------------------------------------------------------- 1 | package com.codingwithmitch.foodrecipes; 2 | 3 | import android.arch.lifecycle.Observer; 4 | import android.arch.lifecycle.ViewModelProviders; 5 | import android.os.Bundle; 6 | import android.support.annotation.Nullable; 7 | import android.support.v7.widget.AppCompatImageView; 8 | import android.util.Log; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | import android.widget.LinearLayout; 12 | import android.widget.ScrollView; 13 | import android.widget.TextView; 14 | 15 | import com.bumptech.glide.Glide; 16 | import com.bumptech.glide.request.RequestOptions; 17 | import com.codingwithmitch.foodrecipes.models.Recipe; 18 | import com.codingwithmitch.foodrecipes.viewmodels.RecipeViewModel; 19 | 20 | public class RecipeActivity extends BaseActivity { 21 | 22 | private static final String TAG = "RecipeActivity"; 23 | 24 | // UI components 25 | private AppCompatImageView mRecipeImage; 26 | private TextView mRecipeTitle, mRecipeRank; 27 | private LinearLayout mRecipeIngredientsContainer; 28 | private ScrollView mScrollView; 29 | 30 | private RecipeViewModel mRecipeViewModel; 31 | 32 | 33 | @Override 34 | protected void onCreate(@Nullable Bundle savedInstanceState) { 35 | super.onCreate(savedInstanceState); 36 | setContentView(R.layout.activity_recipe); 37 | mRecipeImage = findViewById(R.id.recipe_image); 38 | mRecipeTitle = findViewById(R.id.recipe_title); 39 | mRecipeRank = findViewById(R.id.recipe_social_score); 40 | mRecipeIngredientsContainer = findViewById(R.id.ingredients_container); 41 | mScrollView = findViewById(R.id.parent); 42 | 43 | mRecipeViewModel = ViewModelProviders.of(this).get(RecipeViewModel.class); 44 | 45 | showProgressBar(true); 46 | subscribeObservers(); 47 | getIncomingIntent(); 48 | } 49 | 50 | private void getIncomingIntent(){ 51 | if(getIntent().hasExtra("recipe")){ 52 | Recipe recipe = getIntent().getParcelableExtra("recipe"); 53 | Log.d(TAG, "getIncomingIntent: " + recipe.getTitle()); 54 | mRecipeViewModel.searchRecipeById(recipe.getRecipe_id()); 55 | } 56 | } 57 | 58 | private void subscribeObservers(){ 59 | mRecipeViewModel.getRecipe().observe(this, new Observer() { 60 | @Override 61 | public void onChanged(@Nullable Recipe recipe) { 62 | if(recipe != null){ 63 | if(recipe.getRecipe_id().equals(mRecipeViewModel.getRecipeId())){ 64 | setRecipeProperties(recipe); 65 | mRecipeViewModel.setRetrievedRecipe(true); 66 | } 67 | } 68 | } 69 | }); 70 | 71 | mRecipeViewModel.isRecipeRequestTimedOut().observe(this, new Observer() { 72 | @Override 73 | public void onChanged(@Nullable Boolean aBoolean) { 74 | if(aBoolean && !mRecipeViewModel.didRetrieveRecipe()){ 75 | Log.d(TAG, "onChanged: timed out.."); 76 | displayErrorScreen("Error retrieving data. Check network connection."); 77 | } 78 | } 79 | }); 80 | } 81 | 82 | private void displayErrorScreen(String errorMessage){ 83 | mRecipeTitle.setText("Error retrieveing recipe..."); 84 | mRecipeRank.setText(""); 85 | TextView textView = new TextView(this); 86 | if(!errorMessage.equals("")){ 87 | textView.setText(errorMessage); 88 | } 89 | else{ 90 | textView.setText("Error"); 91 | } 92 | textView.setTextSize(15); 93 | textView.setLayoutParams(new LinearLayout.LayoutParams( 94 | ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT 95 | )); 96 | mRecipeIngredientsContainer.addView(textView); 97 | 98 | RequestOptions requestOptions = new RequestOptions() 99 | .placeholder(R.drawable.ic_launcher_background); 100 | 101 | Glide.with(this) 102 | .setDefaultRequestOptions(requestOptions) 103 | .load(R.drawable.ic_launcher_background) 104 | .into(mRecipeImage); 105 | 106 | showParent(); 107 | showProgressBar(false); 108 | } 109 | 110 | private void setRecipeProperties(Recipe recipe){ 111 | if(recipe != null){ 112 | RequestOptions requestOptions = new RequestOptions() 113 | .placeholder(R.drawable.ic_launcher_background); 114 | 115 | Glide.with(this) 116 | .setDefaultRequestOptions(requestOptions) 117 | .load(recipe.getImage_url()) 118 | .into(mRecipeImage); 119 | 120 | mRecipeTitle.setText(recipe.getTitle()); 121 | mRecipeRank.setText(String.valueOf(Math.round(recipe.getSocial_rank()))); 122 | 123 | mRecipeIngredientsContainer.removeAllViews(); 124 | for(String ingredient: recipe.getIngredients()){ 125 | TextView textView = new TextView(this); 126 | textView.setText(ingredient); 127 | textView.setTextSize(15); 128 | textView.setLayoutParams(new LinearLayout.LayoutParams( 129 | ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT 130 | )); 131 | mRecipeIngredientsContainer.addView(textView); 132 | } 133 | } 134 | 135 | showParent(); 136 | showProgressBar(false); 137 | } 138 | 139 | private void showParent(){ 140 | mScrollView.setVisibility(View.VISIBLE); 141 | } 142 | } 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | -------------------------------------------------------------------------------- /app/src/main/java/com/codingwithmitch/foodrecipes/RecipeListActivity.java: -------------------------------------------------------------------------------- 1 | package com.codingwithmitch.foodrecipes; 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.os.Parcelable; 8 | import android.support.annotation.NonNull; 9 | import android.support.annotation.Nullable; 10 | import android.support.v7.widget.LinearLayoutManager; 11 | import android.support.v7.widget.RecyclerView; 12 | import android.support.v7.widget.SearchView; 13 | import android.support.v7.widget.Toolbar; 14 | import android.util.Log; 15 | import android.view.Menu; 16 | import android.view.MenuItem; 17 | 18 | 19 | import com.codingwithmitch.foodrecipes.adapters.OnRecipeListener; 20 | import com.codingwithmitch.foodrecipes.adapters.RecipeRecyclerAdapter; 21 | import com.codingwithmitch.foodrecipes.models.Recipe; 22 | import com.codingwithmitch.foodrecipes.util.Testing; 23 | import com.codingwithmitch.foodrecipes.util.VerticalSpacingItemDecorator; 24 | import com.codingwithmitch.foodrecipes.viewmodels.RecipeListViewModel; 25 | 26 | 27 | import java.util.List; 28 | 29 | 30 | public class RecipeListActivity extends BaseActivity implements OnRecipeListener { 31 | 32 | private static final String TAG = "RecipeListActivity"; 33 | 34 | private RecipeListViewModel mRecipeListViewModel; 35 | private RecyclerView mRecyclerView; 36 | private RecipeRecyclerAdapter mAdapter; 37 | private SearchView mSearchView; 38 | 39 | @Override 40 | protected void onCreate(Bundle savedInstanceState) { 41 | super.onCreate(savedInstanceState); 42 | setContentView(R.layout.activity_recipe_list); 43 | mRecyclerView = findViewById(R.id.recipe_list); 44 | mSearchView = findViewById(R.id.search_view); 45 | 46 | mRecipeListViewModel = ViewModelProviders.of(this).get(RecipeListViewModel.class); 47 | 48 | initRecyclerView(); 49 | subscribeObservers(); 50 | initSearchView(); 51 | if(!mRecipeListViewModel.isViewingRecipes()){ 52 | // display search categories 53 | displaySearchCategories(); 54 | } 55 | setSupportActionBar((Toolbar)findViewById(R.id.toolbar)); 56 | } 57 | 58 | private void subscribeObservers(){ 59 | mRecipeListViewModel.getRecipes().observe(this, new Observer>() { 60 | @Override 61 | public void onChanged(@Nullable List recipes) { 62 | if(recipes != null){ 63 | if(mRecipeListViewModel.isViewingRecipes()){ 64 | Testing.printRecipes(recipes, "recipes test"); 65 | mRecipeListViewModel.setIsPerformingQuery(false); 66 | mAdapter.setRecipes(recipes); 67 | } 68 | } 69 | } 70 | }); 71 | 72 | mRecipeListViewModel.isQueryExhausted().observe(this, new Observer() { 73 | @Override 74 | public void onChanged(@Nullable Boolean aBoolean) { 75 | Log.d(TAG, "onChanged: the query is exhausted..." + aBoolean); 76 | if(aBoolean) { 77 | mAdapter.setQueryExhausted(); 78 | } 79 | } 80 | }); 81 | } 82 | 83 | private void initRecyclerView(){ 84 | mAdapter = new RecipeRecyclerAdapter(this); 85 | VerticalSpacingItemDecorator itemDecorator = new VerticalSpacingItemDecorator(30); 86 | mRecyclerView.addItemDecoration(itemDecorator); 87 | mRecyclerView.setAdapter(mAdapter); 88 | mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); 89 | 90 | mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { 91 | @Override 92 | public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) { 93 | 94 | if(!mRecyclerView.canScrollVertically(1)){ 95 | // search the next page 96 | mRecipeListViewModel.searchNextPage(); 97 | } 98 | } 99 | }); 100 | } 101 | 102 | private void initSearchView(){ 103 | mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { 104 | @Override 105 | public boolean onQueryTextSubmit(String s) { 106 | 107 | mAdapter.displayLoading(); 108 | mRecipeListViewModel.searchRecipesApi(s, 1); 109 | mSearchView.clearFocus(); 110 | 111 | return false; 112 | } 113 | 114 | @Override 115 | public boolean onQueryTextChange(String s) { 116 | return false; 117 | } 118 | }); 119 | } 120 | 121 | @Override 122 | public void onRecipeClick(int position) { 123 | Intent intent = new Intent(this, RecipeActivity.class); 124 | intent.putExtra("recipe", mAdapter.getSelectedRecipe(position)); 125 | startActivity(intent); 126 | } 127 | 128 | @Override 129 | public void onCategoryClick(String category) { 130 | mAdapter.displayLoading(); 131 | mRecipeListViewModel.searchRecipesApi(category, 1); 132 | mSearchView.clearFocus(); 133 | } 134 | 135 | private void displaySearchCategories(){ 136 | mRecipeListViewModel.setIsViewingRecipes(false); 137 | mAdapter.displaySearchCategories(); 138 | } 139 | 140 | @Override 141 | public void onBackPressed() { 142 | if(mRecipeListViewModel.onBackPressed()){ 143 | super.onBackPressed(); 144 | } 145 | else{ 146 | displaySearchCategories(); 147 | } 148 | } 149 | 150 | @Override 151 | public boolean onOptionsItemSelected(MenuItem item) { 152 | 153 | if(item.getItemId() == R.id.action_categories){ 154 | displaySearchCategories(); 155 | } 156 | return super.onOptionsItemSelected(item); 157 | } 158 | 159 | @Override 160 | public boolean onCreateOptionsMenu(Menu menu) { 161 | getMenuInflater().inflate(R.menu.recipe_search_menu, menu); 162 | return super.onCreateOptionsMenu(menu); 163 | } 164 | } 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | -------------------------------------------------------------------------------- /app/src/main/java/com/codingwithmitch/foodrecipes/adapters/CategoryViewHolder.java: -------------------------------------------------------------------------------- 1 | package com.codingwithmitch.foodrecipes.adapters; 2 | 3 | import android.support.annotation.NonNull; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.view.View; 6 | import android.widget.TextView; 7 | 8 | import com.codingwithmitch.foodrecipes.R; 9 | 10 | import de.hdodenhof.circleimageview.CircleImageView; 11 | 12 | public class CategoryViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 13 | 14 | CircleImageView categoryImage; 15 | TextView categoryTitle; 16 | OnRecipeListener listener; 17 | 18 | public CategoryViewHolder(@NonNull View itemView, OnRecipeListener listener) { 19 | super(itemView); 20 | 21 | this.listener = listener; 22 | categoryImage = itemView.findViewById(R.id.category_image); 23 | categoryTitle = itemView.findViewById(R.id.category_title); 24 | 25 | itemView.setOnClickListener(this); 26 | } 27 | 28 | 29 | @Override 30 | public void onClick(View v) { 31 | listener.onCategoryClick(categoryTitle.getText().toString()); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /app/src/main/java/com/codingwithmitch/foodrecipes/adapters/LoadingViewHolder.java: -------------------------------------------------------------------------------- 1 | package com.codingwithmitch.foodrecipes.adapters; 2 | 3 | import android.support.annotation.NonNull; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.view.View; 6 | 7 | public class LoadingViewHolder extends RecyclerView.ViewHolder { 8 | 9 | public LoadingViewHolder(@NonNull View itemView) { 10 | super(itemView); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /app/src/main/java/com/codingwithmitch/foodrecipes/adapters/OnRecipeListener.java: -------------------------------------------------------------------------------- 1 | package com.codingwithmitch.foodrecipes.adapters; 2 | 3 | public interface OnRecipeListener { 4 | 5 | void onRecipeClick(int position); 6 | 7 | void onCategoryClick(String category); 8 | } 9 | -------------------------------------------------------------------------------- /app/src/main/java/com/codingwithmitch/foodrecipes/adapters/RecipeRecyclerAdapter.java: -------------------------------------------------------------------------------- 1 | package com.codingwithmitch.foodrecipes.adapters; 2 | 3 | import android.net.Uri; 4 | import android.support.annotation.NonNull; 5 | import android.support.v7.widget.RecyclerView; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | 10 | import com.bumptech.glide.Glide; 11 | import com.bumptech.glide.request.RequestOptions; 12 | import com.codingwithmitch.foodrecipes.R; 13 | import com.codingwithmitch.foodrecipes.models.Recipe; 14 | import com.codingwithmitch.foodrecipes.util.Constants; 15 | 16 | import java.util.ArrayList; 17 | import java.util.List; 18 | 19 | public class RecipeRecyclerAdapter extends RecyclerView.Adapter { 20 | 21 | private static final int RECIPE_TYPE = 1; 22 | private static final int LOADING_TYPE = 2; 23 | private static final int CATEGORY_TYPE = 3; 24 | private static final int EXHAUSTED_TYPE = 4; 25 | 26 | private List mRecipes; 27 | private OnRecipeListener mOnRecipeListener; 28 | 29 | public RecipeRecyclerAdapter(OnRecipeListener mOnRecipeListener) { 30 | this.mOnRecipeListener = mOnRecipeListener; 31 | } 32 | 33 | @NonNull 34 | @Override 35 | public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { 36 | 37 | View view = null; 38 | switch (i){ 39 | 40 | case RECIPE_TYPE:{ 41 | view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_recipe_list_item, viewGroup, false); 42 | return new RecipeViewHolder(view, mOnRecipeListener); 43 | } 44 | 45 | case LOADING_TYPE:{ 46 | view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_loading_list_item, viewGroup, false); 47 | return new LoadingViewHolder(view); 48 | } 49 | 50 | case EXHAUSTED_TYPE:{ 51 | view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_search_exhausted, viewGroup, false); 52 | return new SearchExhaustedViewHolder(view); 53 | } 54 | 55 | case CATEGORY_TYPE:{ 56 | view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_category_list_item, viewGroup, false); 57 | return new CategoryViewHolder(view, mOnRecipeListener); 58 | } 59 | 60 | default:{ 61 | view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_recipe_list_item, viewGroup, false); 62 | return new RecipeViewHolder(view, mOnRecipeListener); 63 | } 64 | } 65 | 66 | 67 | } 68 | 69 | @Override 70 | public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) { 71 | 72 | int itemViewType = getItemViewType(i); 73 | if(itemViewType == RECIPE_TYPE){ 74 | RequestOptions requestOptions = new RequestOptions() 75 | .placeholder(R.drawable.ic_launcher_background); 76 | 77 | Glide.with(viewHolder.itemView.getContext()) 78 | .setDefaultRequestOptions(requestOptions) 79 | .load(mRecipes.get(i).getImage_url()) 80 | .into(((RecipeViewHolder)viewHolder).image); 81 | 82 | ((RecipeViewHolder)viewHolder).title.setText(mRecipes.get(i).getTitle()); 83 | ((RecipeViewHolder)viewHolder).publisher.setText(mRecipes.get(i).getPublisher()); 84 | ((RecipeViewHolder)viewHolder).socialScore.setText(String.valueOf(Math.round(mRecipes.get(i).getSocial_rank()))); 85 | } 86 | else if(itemViewType == CATEGORY_TYPE){ 87 | 88 | RequestOptions requestOptions = new RequestOptions() 89 | .placeholder(R.drawable.ic_launcher_background); 90 | 91 | Uri path = Uri.parse("android.resource://com.codingwithmitch.foodrecipes/drawable/" + mRecipes.get(i).getImage_url()); 92 | Glide.with(viewHolder.itemView.getContext()) 93 | .setDefaultRequestOptions(requestOptions) 94 | .load(path) 95 | .into(((CategoryViewHolder)viewHolder).categoryImage); 96 | 97 | ((CategoryViewHolder)viewHolder).categoryTitle.setText(mRecipes.get(i).getTitle()); 98 | 99 | } 100 | 101 | } 102 | 103 | @Override 104 | public int getItemViewType(int position) { 105 | if(mRecipes.get(position).getSocial_rank() == -1){ 106 | return CATEGORY_TYPE; 107 | } 108 | else if(mRecipes.get(position).getTitle().equals("LOADING...")){ 109 | return LOADING_TYPE; 110 | } 111 | else if(mRecipes.get(position).getTitle().equals("EXHAUSTED...")){ 112 | return EXHAUSTED_TYPE; 113 | } 114 | else if(position == mRecipes.size() - 1 115 | && position != 0 116 | && !mRecipes.get(position).getTitle().equals("EXHAUSTED...")){ 117 | return LOADING_TYPE; 118 | } 119 | else{ 120 | return RECIPE_TYPE; 121 | } 122 | } 123 | 124 | public void setQueryExhausted(){ 125 | hideLoading(); 126 | Recipe exhaustedRecipe = new Recipe(); 127 | exhaustedRecipe.setTitle("EXHAUSTED..."); 128 | mRecipes.add(exhaustedRecipe); 129 | notifyDataSetChanged(); 130 | } 131 | 132 | private void hideLoading(){ 133 | if(isLoading()){ 134 | for(Recipe recipe: mRecipes){ 135 | if(recipe.getTitle().equals("LOADING...")){ 136 | mRecipes.remove(recipe); 137 | } 138 | } 139 | notifyDataSetChanged(); 140 | } 141 | } 142 | 143 | public void displayLoading(){ 144 | if(!isLoading()){ 145 | Recipe recipe = new Recipe(); 146 | recipe.setTitle("LOADING..."); 147 | List loadingList = new ArrayList<>(); 148 | loadingList.add(recipe); 149 | mRecipes = loadingList; 150 | notifyDataSetChanged(); 151 | } 152 | } 153 | 154 | private boolean isLoading(){ 155 | if(mRecipes != null){ 156 | if(mRecipes.size() > 0){ 157 | if(mRecipes.get(mRecipes.size() - 1).getTitle().equals("LOADING...")){ 158 | return true; 159 | } 160 | } 161 | } 162 | return false; 163 | } 164 | 165 | public void displaySearchCategories(){ 166 | List categories = new ArrayList<>(); 167 | for(int i = 0; i< Constants.DEFAULT_SEARCH_CATEGORIES.length; i++){ 168 | Recipe recipe = new Recipe(); 169 | recipe.setTitle(Constants.DEFAULT_SEARCH_CATEGORIES[i]); 170 | recipe.setImage_url(Constants.DEFAULT_SEARCH_CATEGORY_IMAGES[i]); 171 | recipe.setSocial_rank(-1); 172 | categories.add(recipe); 173 | } 174 | mRecipes = categories; 175 | notifyDataSetChanged(); 176 | } 177 | 178 | @Override 179 | public int getItemCount() { 180 | if(mRecipes != null){ 181 | return mRecipes.size(); 182 | } 183 | return 0; 184 | } 185 | 186 | public void setRecipes(List recipes){ 187 | mRecipes = recipes; 188 | notifyDataSetChanged(); 189 | } 190 | 191 | public Recipe getSelectedRecipe(int position){ 192 | if(mRecipes != null){ 193 | if(mRecipes.size() > 0){ 194 | return mRecipes.get(position); 195 | } 196 | } 197 | return null; 198 | } 199 | 200 | } 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | -------------------------------------------------------------------------------- /app/src/main/java/com/codingwithmitch/foodrecipes/adapters/RecipeViewHolder.java: -------------------------------------------------------------------------------- 1 | package com.codingwithmitch.foodrecipes.adapters; 2 | 3 | import android.support.annotation.NonNull; 4 | import android.support.v7.widget.AppCompatImageView; 5 | import android.support.v7.widget.RecyclerView; 6 | import android.view.View; 7 | import android.widget.TextView; 8 | 9 | import com.codingwithmitch.foodrecipes.R; 10 | 11 | public class RecipeViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 12 | 13 | TextView title, publisher, socialScore; 14 | AppCompatImageView image; 15 | OnRecipeListener onRecipeListener; 16 | 17 | public RecipeViewHolder(@NonNull View itemView, OnRecipeListener onRecipeListener) { 18 | super(itemView); 19 | 20 | this.onRecipeListener = onRecipeListener; 21 | 22 | title = itemView.findViewById(R.id.recipe_title); 23 | publisher = itemView.findViewById(R.id.recipe_publisher); 24 | socialScore = itemView.findViewById(R.id.recipe_social_score); 25 | image = itemView.findViewById(R.id.recipe_image); 26 | 27 | itemView.setOnClickListener(this); 28 | } 29 | 30 | @Override 31 | public void onClick(View v) { 32 | onRecipeListener.onRecipeClick(getAdapterPosition()); 33 | } 34 | } 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /app/src/main/java/com/codingwithmitch/foodrecipes/adapters/SearchExhaustedViewHolder.java: -------------------------------------------------------------------------------- 1 | package com.codingwithmitch.foodrecipes.adapters; 2 | 3 | import android.support.annotation.NonNull; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.view.View; 6 | 7 | public class SearchExhaustedViewHolder extends RecyclerView.ViewHolder { 8 | 9 | public SearchExhaustedViewHolder(@NonNull View itemView) { 10 | super(itemView); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /app/src/main/java/com/codingwithmitch/foodrecipes/models/Recipe.java: -------------------------------------------------------------------------------- 1 | package com.codingwithmitch.foodrecipes.models; 2 | 3 | import android.os.Parcel; 4 | import android.os.Parcelable; 5 | 6 | import java.util.Arrays; 7 | 8 | public class Recipe implements Parcelable { 9 | 10 | private String title; 11 | private String publisher; 12 | private String[] ingredients; 13 | private String recipe_id; 14 | private String image_url; 15 | private float social_rank; 16 | 17 | public Recipe(String title, String publisher, String[] ingredients, String recipe_id, 18 | String image_url, float social_rank) { 19 | this.title = title; 20 | this.publisher = publisher; 21 | this.ingredients = ingredients; 22 | this.recipe_id = recipe_id; 23 | this.image_url = image_url; 24 | this.social_rank = social_rank; 25 | } 26 | 27 | public Recipe() { 28 | } 29 | 30 | protected Recipe(Parcel in) { 31 | title = in.readString(); 32 | publisher = in.readString(); 33 | ingredients = in.createStringArray(); 34 | recipe_id = in.readString(); 35 | image_url = in.readString(); 36 | social_rank = in.readFloat(); 37 | } 38 | 39 | public static final Creator CREATOR = new Creator() { 40 | @Override 41 | public Recipe createFromParcel(Parcel in) { 42 | return new Recipe(in); 43 | } 44 | 45 | @Override 46 | public Recipe[] newArray(int size) { 47 | return new Recipe[size]; 48 | } 49 | }; 50 | 51 | public String getTitle() { 52 | return title; 53 | } 54 | 55 | public void setTitle(String title) { 56 | this.title = title; 57 | } 58 | 59 | public String getPublisher() { 60 | return publisher; 61 | } 62 | 63 | public void setPublisher(String publisher) { 64 | this.publisher = publisher; 65 | } 66 | 67 | public String[] getIngredients() { 68 | return ingredients; 69 | } 70 | 71 | public void setIngredients(String[] ingredients) { 72 | this.ingredients = ingredients; 73 | } 74 | 75 | public String getRecipe_id() { 76 | return recipe_id; 77 | } 78 | 79 | public void setRecipe_id(String recipe_id) { 80 | this.recipe_id = recipe_id; 81 | } 82 | 83 | public String getImage_url() { 84 | return image_url; 85 | } 86 | 87 | public void setImage_url(String image_url) { 88 | this.image_url = image_url; 89 | } 90 | 91 | public float getSocial_rank() { 92 | return social_rank; 93 | } 94 | 95 | public void setSocial_rank(float social_rank) { 96 | this.social_rank = social_rank; 97 | } 98 | 99 | @Override 100 | public String toString() { 101 | return "Recipe{" + 102 | "title='" + title + '\'' + 103 | ", publisher='" + publisher + '\'' + 104 | ", ingredients=" + Arrays.toString(ingredients) + 105 | ", recipe_id='" + recipe_id + '\'' + 106 | ", image_url='" + image_url + '\'' + 107 | ", social_rank=" + social_rank + 108 | '}'; 109 | } 110 | 111 | @Override 112 | public int describeContents() { 113 | return 0; 114 | } 115 | 116 | @Override 117 | public void writeToParcel(Parcel dest, int flags) { 118 | dest.writeString(title); 119 | dest.writeString(publisher); 120 | dest.writeStringArray(ingredients); 121 | dest.writeString(recipe_id); 122 | dest.writeString(image_url); 123 | dest.writeFloat(social_rank); 124 | } 125 | } 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | -------------------------------------------------------------------------------- /app/src/main/java/com/codingwithmitch/foodrecipes/repositories/RecipeRepository.java: -------------------------------------------------------------------------------- 1 | package com.codingwithmitch.foodrecipes.repositories; 2 | 3 | import android.arch.lifecycle.LiveData; 4 | import android.arch.lifecycle.MediatorLiveData; 5 | import android.arch.lifecycle.MutableLiveData; 6 | import android.arch.lifecycle.Observer; 7 | import android.support.annotation.Nullable; 8 | import android.util.Log; 9 | 10 | import com.codingwithmitch.foodrecipes.models.Recipe; 11 | import com.codingwithmitch.foodrecipes.requests.RecipeApiClient; 12 | 13 | import java.util.List; 14 | 15 | public class RecipeRepository { 16 | 17 | private static RecipeRepository instance; 18 | private RecipeApiClient mRecipeApiClient; 19 | private String mQuery; 20 | private int mPageNumber; 21 | private MutableLiveData mIsQueryExhausted = new MutableLiveData<>(); 22 | private MediatorLiveData> mRecipes = new MediatorLiveData<>(); 23 | 24 | public static RecipeRepository getInstance(){ 25 | if(instance == null){ 26 | instance = new RecipeRepository(); 27 | } 28 | return instance; 29 | } 30 | 31 | private RecipeRepository(){ 32 | mRecipeApiClient = RecipeApiClient.getInstance(); 33 | initMediators(); 34 | } 35 | 36 | private void initMediators(){ 37 | LiveData> recipeListApiSource = mRecipeApiClient.getRecipes(); 38 | mRecipes.addSource(recipeListApiSource, new Observer>() { 39 | @Override 40 | public void onChanged(@Nullable List recipes) { 41 | 42 | if(recipes != null){ 43 | mRecipes.setValue(recipes); 44 | doneQuery(recipes); 45 | } 46 | else{ 47 | // search database cache 48 | doneQuery(null); 49 | } 50 | } 51 | }); 52 | } 53 | 54 | private void doneQuery(List list){ 55 | if(list != null){ 56 | if (list.size() % 30 != 0) { 57 | mIsQueryExhausted.setValue(true); 58 | } 59 | } 60 | else{ 61 | mIsQueryExhausted.setValue(true); 62 | } 63 | } 64 | 65 | public LiveData isQueryExhausted(){ 66 | return mIsQueryExhausted; 67 | } 68 | 69 | public LiveData> getRecipes(){ 70 | return mRecipes; 71 | } 72 | 73 | public LiveData getRecipe(){ 74 | return mRecipeApiClient.getRecipe(); 75 | } 76 | 77 | public void searchRecipeById(String recipeId){ 78 | mRecipeApiClient.searchRecipeById(recipeId); 79 | } 80 | 81 | 82 | public void searchRecipesApi(String query, int pageNumber){ 83 | if(pageNumber == 0){ 84 | pageNumber = 1; 85 | } 86 | mQuery = query; 87 | mPageNumber = pageNumber; 88 | mIsQueryExhausted.setValue(false); 89 | mRecipeApiClient.searchRecipesApi(query, pageNumber); 90 | } 91 | 92 | public void searchNextPage(){ 93 | searchRecipesApi(mQuery, mPageNumber + 1); 94 | } 95 | 96 | public void cancelRequest(){ 97 | mRecipeApiClient.cancelRequest(); 98 | } 99 | 100 | public LiveData isRecipeRequestTimedOut(){ 101 | return mRecipeApiClient.isRecipeRequestTimedOut(); 102 | } 103 | } 104 | 105 | 106 | 107 | 108 | -------------------------------------------------------------------------------- /app/src/main/java/com/codingwithmitch/foodrecipes/requests/RecipeApi.java: -------------------------------------------------------------------------------- 1 | package com.codingwithmitch.foodrecipes.requests; 2 | 3 | import com.codingwithmitch.foodrecipes.requests.responses.RecipeResponse; 4 | import com.codingwithmitch.foodrecipes.requests.responses.RecipeSearchResponse; 5 | 6 | import retrofit2.Call; 7 | import retrofit2.http.GET; 8 | import retrofit2.http.Query; 9 | 10 | public interface RecipeApi { 11 | 12 | // SEARCH 13 | @GET("api/search") 14 | Call searchRecipe( 15 | @Query("key") String key, 16 | @Query("q") String query, 17 | @Query("page") String page 18 | ); 19 | 20 | // GET RECIPE REQUEST 21 | @GET("api/get") 22 | Call getRecipe( 23 | @Query("key") String key, 24 | @Query("rId") String recipe_id 25 | ); 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/java/com/codingwithmitch/foodrecipes/requests/RecipeApiClient.java: -------------------------------------------------------------------------------- 1 | package com.codingwithmitch.foodrecipes.requests; 2 | 3 | import android.arch.lifecycle.LiveData; 4 | import android.arch.lifecycle.MutableLiveData; 5 | import android.util.Log; 6 | 7 | import com.codingwithmitch.foodrecipes.AppExecutors; 8 | import com.codingwithmitch.foodrecipes.models.Recipe; 9 | import com.codingwithmitch.foodrecipes.requests.responses.RecipeResponse; 10 | import com.codingwithmitch.foodrecipes.requests.responses.RecipeSearchResponse; 11 | import com.codingwithmitch.foodrecipes.util.Constants; 12 | 13 | import java.io.IOException; 14 | import java.util.ArrayList; 15 | import java.util.List; 16 | import java.util.concurrent.Future; 17 | import java.util.concurrent.TimeUnit; 18 | 19 | import retrofit2.Call; 20 | import retrofit2.Response; 21 | 22 | import static com.codingwithmitch.foodrecipes.util.Constants.NETWORK_TIMEOUT; 23 | 24 | public class RecipeApiClient { 25 | 26 | private static final String TAG = "RecipeApiClient"; 27 | 28 | private static RecipeApiClient instance; 29 | private MutableLiveData> mRecipes; 30 | private RetrieveRecipesRunnable mRetrieveRecipesRunnable; 31 | private MutableLiveData mRecipe; 32 | private RetrieveRecipeRunnable mRetrieveRecipeRunnable; 33 | private MutableLiveData mRecipeRequestTimeout = new MutableLiveData<>(); 34 | 35 | public static RecipeApiClient getInstance(){ 36 | if(instance == null){ 37 | instance = new RecipeApiClient(); 38 | } 39 | return instance; 40 | } 41 | 42 | private RecipeApiClient(){ 43 | mRecipes = new MutableLiveData<>(); 44 | mRecipe = new MutableLiveData<>(); 45 | } 46 | 47 | public LiveData> getRecipes(){ 48 | return mRecipes; 49 | } 50 | 51 | public LiveData getRecipe(){ 52 | return mRecipe; 53 | } 54 | 55 | public LiveData isRecipeRequestTimedOut(){ 56 | return mRecipeRequestTimeout; 57 | } 58 | 59 | public void searchRecipesApi(String query, int pageNumber){ 60 | if(mRetrieveRecipesRunnable != null){ 61 | mRetrieveRecipesRunnable = null; 62 | } 63 | mRetrieveRecipesRunnable = new RetrieveRecipesRunnable(query, pageNumber); 64 | final Future handler = AppExecutors.getInstance().networkIO().submit(mRetrieveRecipesRunnable); 65 | 66 | AppExecutors.getInstance().networkIO().schedule(new Runnable() { 67 | @Override 68 | public void run() { 69 | // let the user know its timed out 70 | handler.cancel(true); 71 | } 72 | }, NETWORK_TIMEOUT, TimeUnit.MILLISECONDS); 73 | } 74 | 75 | public void searchRecipeById(String recipeId){ 76 | if(mRetrieveRecipeRunnable != null){ 77 | mRetrieveRecipeRunnable = null; 78 | } 79 | mRetrieveRecipeRunnable = new RetrieveRecipeRunnable(recipeId); 80 | 81 | final Future handler = AppExecutors.getInstance().networkIO().submit(mRetrieveRecipeRunnable); 82 | 83 | mRecipeRequestTimeout.setValue(false); 84 | AppExecutors.getInstance().networkIO().schedule(new Runnable() { 85 | @Override 86 | public void run() { 87 | // let the user know it's timed out 88 | mRecipeRequestTimeout.postValue(true); 89 | handler.cancel(true); 90 | } 91 | }, NETWORK_TIMEOUT, TimeUnit.MILLISECONDS); 92 | 93 | } 94 | 95 | private class RetrieveRecipesRunnable implements Runnable{ 96 | 97 | private String query; 98 | private int pageNumber; 99 | boolean cancelRequest; 100 | 101 | public RetrieveRecipesRunnable(String query, int pageNumber) { 102 | this.query = query; 103 | this.pageNumber = pageNumber; 104 | cancelRequest = false; 105 | } 106 | 107 | @Override 108 | public void run() { 109 | try { 110 | Response response = getRecipes(query, pageNumber).execute(); 111 | if(cancelRequest){ 112 | return; 113 | } 114 | if(response.code() == 200){ 115 | List list = new ArrayList<>(((RecipeSearchResponse)response.body()).getRecipes()); 116 | if(pageNumber == 1){ 117 | mRecipes.postValue(list); 118 | } 119 | else{ 120 | List currentRecipes = mRecipes.getValue(); 121 | currentRecipes.addAll(list); 122 | mRecipes.postValue(currentRecipes); 123 | } 124 | } 125 | else{ 126 | String error = response.errorBody().string(); 127 | Log.e(TAG, "run: " + error ); 128 | mRecipes.postValue(null); 129 | } 130 | } catch (IOException e) { 131 | e.printStackTrace(); 132 | mRecipes.postValue(null); 133 | } 134 | 135 | } 136 | 137 | private Call getRecipes(String query, int pageNumber){ 138 | return ServiceGenerator.getRecipeApi().searchRecipe( 139 | Constants.API_KEY, 140 | query, 141 | String.valueOf(pageNumber) 142 | ); 143 | } 144 | 145 | private void cancelRequest(){ 146 | Log.d(TAG, "cancelRequest: canceling the search request."); 147 | cancelRequest = true; 148 | } 149 | } 150 | 151 | private class RetrieveRecipeRunnable implements Runnable{ 152 | 153 | private String recipeId; 154 | boolean cancelRequest; 155 | 156 | public RetrieveRecipeRunnable(String recipeId) { 157 | this.recipeId = recipeId; 158 | cancelRequest = false; 159 | } 160 | 161 | @Override 162 | public void run() { 163 | try { 164 | Response response = getRecipe(recipeId).execute(); 165 | if(cancelRequest){ 166 | return; 167 | } 168 | if(response.code() == 200){ 169 | Recipe recipe = ((RecipeResponse)response.body()).getRecipe(); 170 | mRecipe.postValue(recipe); 171 | } 172 | else{ 173 | String error = response.errorBody().string(); 174 | Log.e(TAG, "run: " + error ); 175 | mRecipe.postValue(null); 176 | } 177 | } catch (IOException e) { 178 | e.printStackTrace(); 179 | mRecipe.postValue(null); 180 | } 181 | 182 | } 183 | 184 | private Call getRecipe(String recipeId){ 185 | return ServiceGenerator.getRecipeApi().getRecipe( 186 | Constants.API_KEY, 187 | recipeId 188 | ); 189 | } 190 | 191 | private void cancelRequest(){ 192 | Log.d(TAG, "cancelRequest: canceling the search request."); 193 | cancelRequest = true; 194 | } 195 | } 196 | 197 | public void cancelRequest(){ 198 | if(mRetrieveRecipesRunnable != null){ 199 | mRetrieveRecipesRunnable.cancelRequest(); 200 | } 201 | if(mRetrieveRecipeRunnable != null){ 202 | mRetrieveRecipeRunnable.cancelRequest(); 203 | } 204 | } 205 | } 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | -------------------------------------------------------------------------------- /app/src/main/java/com/codingwithmitch/foodrecipes/requests/ServiceGenerator.java: -------------------------------------------------------------------------------- 1 | package com.codingwithmitch.foodrecipes.requests; 2 | 3 | import com.codingwithmitch.foodrecipes.util.Constants; 4 | 5 | import retrofit2.Retrofit; 6 | import retrofit2.converter.gson.GsonConverterFactory; 7 | 8 | /** 9 | * Modeled after @brittbarak's example on Github 10 | * https://github.com/brittBarak/NetworkingDemo 11 | * https://twitter.com/brittbarak 12 | */ 13 | public class ServiceGenerator { 14 | 15 | private static Retrofit.Builder retrofitBuilder = 16 | new Retrofit.Builder() 17 | .baseUrl(Constants.BASE_URL) 18 | .addConverterFactory(GsonConverterFactory.create()); 19 | 20 | private static Retrofit retrofit = retrofitBuilder.build(); 21 | 22 | private static RecipeApi recipeApi = retrofit.create(RecipeApi.class); 23 | 24 | public static RecipeApi getRecipeApi(){ 25 | return recipeApi; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /app/src/main/java/com/codingwithmitch/foodrecipes/requests/responses/RecipeResponse.java: -------------------------------------------------------------------------------- 1 | package com.codingwithmitch.foodrecipes.requests.responses; 2 | 3 | import com.codingwithmitch.foodrecipes.models.Recipe; 4 | import com.google.gson.annotations.Expose; 5 | import com.google.gson.annotations.SerializedName; 6 | 7 | public class RecipeResponse { 8 | 9 | @SerializedName("recipe") 10 | @Expose() 11 | private Recipe recipe; 12 | 13 | public Recipe getRecipe(){ 14 | return recipe; 15 | } 16 | 17 | @Override 18 | public String toString() { 19 | return "RecipeResponse{" + 20 | "recipe=" + recipe + 21 | '}'; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/codingwithmitch/foodrecipes/requests/responses/RecipeSearchResponse.java: -------------------------------------------------------------------------------- 1 | package com.codingwithmitch.foodrecipes.requests.responses; 2 | 3 | import com.codingwithmitch.foodrecipes.models.Recipe; 4 | import com.google.gson.annotations.Expose; 5 | import com.google.gson.annotations.SerializedName; 6 | 7 | import java.util.List; 8 | 9 | public class RecipeSearchResponse { 10 | 11 | @SerializedName("count") 12 | @Expose() 13 | private int count; 14 | 15 | @SerializedName("recipes") 16 | @Expose() 17 | private List recipes; 18 | 19 | public int getCount() { 20 | return count; 21 | } 22 | 23 | public List getRecipes() { 24 | return recipes; 25 | } 26 | 27 | @Override 28 | public String toString() { 29 | return "RecipeSearchResponse{" + 30 | "count=" + count + 31 | ", recipes=" + recipes + 32 | '}'; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /app/src/main/java/com/codingwithmitch/foodrecipes/util/Constants.java: -------------------------------------------------------------------------------- 1 | package com.codingwithmitch.foodrecipes.util; 2 | 3 | public class Constants { 4 | 5 | // public static final String BASE_URL = "https://www.food2fork.com"; 6 | public static final String BASE_URL = "https://recipesapi.herokuapp.com"; 7 | 8 | // API_KEY is no longer necessary since food2fork has shutdown. This can be empty it doesn't matter. 9 | // public static final String API_KEY = "453556cb475252e7e42d65aa11912447"; 10 | public static final String API_KEY = ""; 11 | 12 | public static final int NETWORK_TIMEOUT = 3000; 13 | 14 | public static final String[] DEFAULT_SEARCH_CATEGORIES = 15 | {"Barbeque", "Breakfast", "Chicken", "Beef", "Brunch", "Dinner", "Wine", "Italian"}; 16 | 17 | public static final String[] DEFAULT_SEARCH_CATEGORY_IMAGES = 18 | { 19 | "barbeque", 20 | "breakfast", 21 | "chicken", 22 | "beef", 23 | "brunch", 24 | "dinner", 25 | "wine", 26 | "italian" 27 | }; 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/java/com/codingwithmitch/foodrecipes/util/HorizontalDottedProgress.java: -------------------------------------------------------------------------------- 1 | package com.codingwithmitch.foodrecipes.util; 2 | 3 | import android.content.Context; 4 | import android.graphics.Canvas; 5 | import android.graphics.Color; 6 | import android.graphics.Paint; 7 | import android.util.AttributeSet; 8 | import android.util.Log; 9 | import android.view.View; 10 | import android.view.animation.Animation; 11 | import android.view.animation.LinearInterpolator; 12 | import android.view.animation.Transformation; 13 | 14 | public class HorizontalDottedProgress extends View { 15 | 16 | //actual dot radius 17 | private int mDotRadius = 5; 18 | 19 | //Bounced Dot Radius 20 | private int mBounceDotRadius = 8; 21 | 22 | //to get identified in which position dot has to bounce 23 | private int mDotPosition; 24 | 25 | //specify how many dots you need in a progressbar 26 | private int mDotAmount = 10; 27 | 28 | public HorizontalDottedProgress(Context context) { 29 | super(context); 30 | } 31 | 32 | public HorizontalDottedProgress(Context context, AttributeSet attrs) { 33 | super(context, attrs); 34 | } 35 | 36 | public HorizontalDottedProgress(Context context, AttributeSet attrs, int defStyleAttr) { 37 | super(context, attrs, defStyleAttr); 38 | } 39 | 40 | //Method to draw your customized dot on the canvas 41 | @Override 42 | protected void onDraw(Canvas canvas) { 43 | super.onDraw(canvas); 44 | 45 | Paint paint = new Paint(); 46 | 47 | //set the color for the dot that you want to draw 48 | paint.setColor(Color.parseColor("#fd583f")); 49 | 50 | //function to create dot 51 | createDot(canvas,paint); 52 | } 53 | 54 | @Override 55 | protected void onAttachedToWindow() { 56 | super.onAttachedToWindow(); 57 | //Animation called when attaching to the window, i.e to your screen 58 | startAnimation(); 59 | } 60 | 61 | private void createDot(Canvas canvas, Paint paint) { 62 | 63 | //here i have setted progress bar with 10 dots , so repeat and wnen i = mDotPosition then increase the radius of dot i.e mBounceDotRadius 64 | for(int i = 0; i < mDotAmount; i++ ){ 65 | if(i == mDotPosition){ 66 | canvas.drawCircle(10+(i*20), mBounceDotRadius, mBounceDotRadius, paint); 67 | }else { 68 | canvas.drawCircle(10+(i*20), mBounceDotRadius, mDotRadius, paint); 69 | } 70 | } 71 | 72 | 73 | } 74 | 75 | @Override 76 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 77 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 78 | int width; 79 | int height; 80 | 81 | //calculate the view width 82 | int calculatedWidth = (20*9); 83 | 84 | width = calculatedWidth; 85 | height = (mBounceDotRadius*2); 86 | 87 | 88 | 89 | //MUST CALL THIS 90 | setMeasuredDimension(width, height); 91 | } 92 | 93 | private void startAnimation() { 94 | BounceAnimation bounceAnimation = new BounceAnimation(); 95 | bounceAnimation.setDuration(100); 96 | bounceAnimation.setRepeatCount(Animation.INFINITE); 97 | bounceAnimation.setInterpolator(new LinearInterpolator()); 98 | bounceAnimation.setAnimationListener(new Animation.AnimationListener() { 99 | @Override 100 | public void onAnimationStart(Animation animation) { 101 | 102 | } 103 | 104 | @Override 105 | public void onAnimationEnd(Animation animation) { 106 | 107 | } 108 | 109 | @Override 110 | public void onAnimationRepeat(Animation animation) { 111 | mDotPosition++; 112 | //when mDotPosition == mDotAmount , then start again applying animation from 0th positon , i.e mDotPosition = 0; 113 | if (mDotPosition == mDotAmount) { 114 | mDotPosition = 0; 115 | } 116 | Log.d("INFOMETHOD","----On Animation Repeat----"); 117 | 118 | } 119 | }); 120 | startAnimation(bounceAnimation); 121 | } 122 | 123 | 124 | private class BounceAnimation extends Animation { 125 | @Override 126 | protected void applyTransformation(float interpolatedTime, Transformation t) { 127 | super.applyTransformation(interpolatedTime, t); 128 | //call invalidate to redraw your view againg. 129 | invalidate(); 130 | } 131 | } 132 | } -------------------------------------------------------------------------------- /app/src/main/java/com/codingwithmitch/foodrecipes/util/Testing.java: -------------------------------------------------------------------------------- 1 | package com.codingwithmitch.foodrecipes.util; 2 | 3 | import android.util.Log; 4 | 5 | import com.codingwithmitch.foodrecipes.models.Recipe; 6 | 7 | import java.util.List; 8 | 9 | public class Testing { 10 | 11 | public static void printRecipes(Listlist, String tag){ 12 | for(Recipe recipe: list){ 13 | Log.d(tag, "onChanged: " + recipe.getTitle()); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /app/src/main/java/com/codingwithmitch/foodrecipes/util/VerticalSpacingItemDecorator.java: -------------------------------------------------------------------------------- 1 | package com.codingwithmitch.foodrecipes.util; 2 | 3 | import android.graphics.Rect; 4 | import android.support.annotation.NonNull; 5 | import android.support.v7.widget.RecyclerView; 6 | import android.view.View; 7 | 8 | public class VerticalSpacingItemDecorator extends RecyclerView.ItemDecoration { 9 | 10 | private final int verticalSpaceHeight; 11 | 12 | public VerticalSpacingItemDecorator(int verticalSpaceHeight) { 13 | this.verticalSpaceHeight = verticalSpaceHeight; 14 | } 15 | 16 | @Override 17 | public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) { 18 | 19 | outRect.top = verticalSpaceHeight; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /app/src/main/java/com/codingwithmitch/foodrecipes/viewmodels/RecipeListViewModel.java: -------------------------------------------------------------------------------- 1 | package com.codingwithmitch.foodrecipes.viewmodels; 2 | 3 | import android.arch.lifecycle.LiveData; 4 | import android.arch.lifecycle.MutableLiveData; 5 | import android.arch.lifecycle.ViewModel; 6 | import android.util.Log; 7 | 8 | import com.codingwithmitch.foodrecipes.models.Recipe; 9 | import com.codingwithmitch.foodrecipes.repositories.RecipeRepository; 10 | 11 | import java.util.List; 12 | 13 | public class RecipeListViewModel extends ViewModel { 14 | 15 | private RecipeRepository mRecipeRepository; 16 | private boolean mIsViewingRecipes; 17 | private boolean mIsPerformingQuery; 18 | 19 | public RecipeListViewModel() { 20 | mRecipeRepository = RecipeRepository.getInstance(); 21 | mIsPerformingQuery = false; 22 | } 23 | 24 | public LiveData> getRecipes(){ 25 | return mRecipeRepository.getRecipes(); 26 | } 27 | 28 | public LiveData isQueryExhausted(){ 29 | return mRecipeRepository.isQueryExhausted(); 30 | } 31 | 32 | public void searchRecipesApi(String query, int pageNumber){ 33 | mIsViewingRecipes = true; 34 | mIsPerformingQuery = true; 35 | mRecipeRepository.searchRecipesApi(query, pageNumber); 36 | } 37 | 38 | public void searchNextPage(){ 39 | if(!mIsPerformingQuery 40 | && mIsViewingRecipes 41 | && !isQueryExhausted().getValue()){ 42 | mRecipeRepository.searchNextPage(); 43 | } 44 | } 45 | 46 | public boolean isViewingRecipes(){ 47 | return mIsViewingRecipes; 48 | } 49 | 50 | public void setIsViewingRecipes(boolean isViewingRecipes){ 51 | mIsViewingRecipes = isViewingRecipes; 52 | } 53 | 54 | public void setIsPerformingQuery(Boolean isPerformingQuery){ 55 | mIsPerformingQuery = isPerformingQuery; 56 | } 57 | 58 | public boolean isPerformingQuery(){ 59 | return mIsPerformingQuery; 60 | } 61 | 62 | public boolean onBackPressed(){ 63 | if(mIsPerformingQuery){ 64 | // cancel the query 65 | mRecipeRepository.cancelRequest(); 66 | mIsPerformingQuery = false; 67 | } 68 | if(mIsViewingRecipes){ 69 | mIsViewingRecipes = false; 70 | return false; 71 | } 72 | return true; 73 | } 74 | } 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /app/src/main/java/com/codingwithmitch/foodrecipes/viewmodels/RecipeViewModel.java: -------------------------------------------------------------------------------- 1 | package com.codingwithmitch.foodrecipes.viewmodels; 2 | 3 | import android.arch.lifecycle.LiveData; 4 | import android.arch.lifecycle.ViewModel; 5 | 6 | import com.codingwithmitch.foodrecipes.models.Recipe; 7 | import com.codingwithmitch.foodrecipes.repositories.RecipeRepository; 8 | 9 | public class RecipeViewModel extends ViewModel { 10 | 11 | private RecipeRepository mRecipeRepository; 12 | private String mRecipeId; 13 | private boolean mDidRetrieveRecipe; 14 | 15 | public RecipeViewModel() { 16 | mRecipeRepository = RecipeRepository.getInstance(); 17 | mDidRetrieveRecipe = false; 18 | } 19 | 20 | public LiveData getRecipe(){ 21 | return mRecipeRepository.getRecipe(); 22 | } 23 | 24 | public LiveData isRecipeRequestTimedOut(){ 25 | return mRecipeRepository.isRecipeRequestTimedOut(); 26 | } 27 | 28 | public void searchRecipeById(String recipeId){ 29 | mRecipeId = recipeId; 30 | mRecipeRepository.searchRecipeById(recipeId); 31 | } 32 | 33 | public String getRecipeId() { 34 | return mRecipeId; 35 | } 36 | 37 | public void setRetrievedRecipe(boolean retrievedRecipe){ 38 | mDidRetrieveRecipe = retrievedRecipe; 39 | } 40 | 41 | public boolean didRetrieveRecipe(){ 42 | return mDidRetrieveRecipe; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /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/barbeque.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mitchtabian/RestApiMVVM/18d0ca15e989b361e3b29b3b894776c2c0d4d2f8/app/src/main/res/drawable/barbeque.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/beef.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mitchtabian/RestApiMVVM/18d0ca15e989b361e3b29b3b894776c2c0d4d2f8/app/src/main/res/drawable/beef.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/breakfast.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mitchtabian/RestApiMVVM/18d0ca15e989b361e3b29b3b894776c2c0d4d2f8/app/src/main/res/drawable/breakfast.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/brunch.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mitchtabian/RestApiMVVM/18d0ca15e989b361e3b29b3b894776c2c0d4d2f8/app/src/main/res/drawable/brunch.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/chicken.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mitchtabian/RestApiMVVM/18d0ca15e989b361e3b29b3b894776c2c0d4d2f8/app/src/main/res/drawable/chicken.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/dinner.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mitchtabian/RestApiMVVM/18d0ca15e989b361e3b29b3b894776c2c0d4d2f8/app/src/main/res/drawable/dinner.jpg -------------------------------------------------------------------------------- /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_mood_bad_black_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/italian.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mitchtabian/RestApiMVVM/18d0ca15e989b361e3b29b3b894776c2c0d4d2f8/app/src/main/res/drawable/italian.jpg -------------------------------------------------------------------------------- /app/src/main/res/drawable/wine.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mitchtabian/RestApiMVVM/18d0ca15e989b361e3b29b3b894776c2c0d4d2f8/app/src/main/res/drawable/wine.jpg -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_base.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 12 | 13 | 14 | 15 | 25 | 26 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_recipe.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 13 | 14 | 15 | 20 | 21 | 30 | 31 | 32 | 40 | 41 | 50 | 51 | 60 | 61 | 62 | 63 | 64 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_recipe_list.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 13 | 14 | 19 | 20 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /app/src/main/res/layout/layout_category_list_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 15 | 16 | 21 | 22 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /app/src/main/res/layout/layout_loading_list_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/res/layout/layout_recipe_list_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 13 | 14 | 18 | 19 | 25 | 26 | 27 | 32 | 33 | 39 | 40 | 45 | 46 | 53 | 54 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /app/src/main/res/layout/layout_search_exhausted.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 15 | 16 | -------------------------------------------------------------------------------- /app/src/main/res/menu/recipe_search_menu.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | -------------------------------------------------------------------------------- /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/mitchtabian/RestApiMVVM/18d0ca15e989b361e3b29b3b894776c2c0d4d2f8/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mitchtabian/RestApiMVVM/18d0ca15e989b361e3b29b3b894776c2c0d4d2f8/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mitchtabian/RestApiMVVM/18d0ca15e989b361e3b29b3b894776c2c0d4d2f8/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mitchtabian/RestApiMVVM/18d0ca15e989b361e3b29b3b894776c2c0d4d2f8/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mitchtabian/RestApiMVVM/18d0ca15e989b361e3b29b3b894776c2c0d4d2f8/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mitchtabian/RestApiMVVM/18d0ca15e989b361e3b29b3b894776c2c0d4d2f8/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mitchtabian/RestApiMVVM/18d0ca15e989b361e3b29b3b894776c2c0d4d2f8/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mitchtabian/RestApiMVVM/18d0ca15e989b361e3b29b3b894776c2c0d4d2f8/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mitchtabian/RestApiMVVM/18d0ca15e989b361e3b29b3b894776c2c0d4d2f8/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mitchtabian/RestApiMVVM/18d0ca15e989b361e3b29b3b894776c2c0d4d2f8/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #fff 4 | #b3b3b3 5 | #FF4081 6 | 7 | #e22b2b 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimen.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 250dp 4 | 20sp 5 | 16sp 6 | 80dp 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | FoodRecipes 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/res/xml/network_security_config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | static.food2fork.com 5 | 6 | -------------------------------------------------------------------------------- /app/src/test/java/com/codingwithmitch/foodrecipes/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.codingwithmitch.foodrecipes; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | google() 6 | jcenter() 7 | 8 | } 9 | dependencies { 10 | classpath 'com.android.tools.build:gradle:3.3.0' 11 | 12 | // NOTE: Do not place your application dependencies here; they belong 13 | // in the individual module build.gradle files 14 | } 15 | } 16 | 17 | allprojects { 18 | repositories { 19 | google() 20 | jcenter() 21 | 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 | 15 | 16 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mitchtabian/RestApiMVVM/18d0ca15e989b361e3b29b3b894776c2c0d4d2f8/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Jan 15 11:33:08 PST 2019 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.10.1-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 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | --------------------------------------------------------------------------------