mirror of
https://github.com/Nezumi-2711/Foodify.git
synced 2026-09-22 13:38:41 +00:00
Merge API Slider to Home Fragment
This commit is contained in:
@@ -6,6 +6,7 @@ import com.capstone.foodify.Model.Food.Food;
|
||||
import com.capstone.foodify.Model.Response.Foods;
|
||||
import com.capstone.foodify.Model.Response.Shops;
|
||||
import com.capstone.foodify.Model.Shop.Shop;
|
||||
import com.capstone.foodify.Model.Slider.Slider;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
@@ -65,4 +66,7 @@ public interface FoodApi {
|
||||
|
||||
@GET("district")
|
||||
Call<List<DistrictWardResponse>> districtResponse();
|
||||
|
||||
@GET("sliders")
|
||||
Call<List<Slider>> listSlider();
|
||||
}
|
||||
|
||||
@@ -46,8 +46,8 @@ public interface FoodApiToken {
|
||||
@GET("wishlist")
|
||||
Call<List<Food>> getListFavoriteFood();
|
||||
|
||||
@POST("wishlist")
|
||||
Call<Food> addFoodToFavorite(@Body Food food);
|
||||
@POST("users/{userId}/loves/{productId}")
|
||||
Call<Food> addFoodToFavorite(@Path("userId") String userId, @Path("productId") String productId);
|
||||
|
||||
@DELETE("wishlist/{id}")
|
||||
Call<Food> removeFoodFromFavorite(@Path("id") int id);
|
||||
|
||||
@@ -267,9 +267,7 @@ public class FoodDetailActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<Food> call, Throwable t) {
|
||||
Common.showNotificationError(t, getBaseContext(), FoodDetailActivity.this);
|
||||
|
||||
Log.e("ERROR", "Get Detail Food Error!");
|
||||
Common.showNotificationError(getBaseContext(), FoodDetailActivity.this);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -135,9 +135,7 @@ public class ShopDetailActivity extends AppCompatActivity {
|
||||
@Override
|
||||
public void onFailure(Call<Foods> call, Throwable t) {
|
||||
//Check internet connection
|
||||
Common.showNotificationError(t, getBaseContext(), ShopDetailActivity.this);
|
||||
|
||||
Log.e("ERROR", "List Food By Shop Id Error!");
|
||||
Common.showNotificationError(getBaseContext(), ShopDetailActivity.this);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -157,9 +155,7 @@ public class ShopDetailActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<Shop> call, Throwable t) {
|
||||
Common.showNotificationError(t, getBaseContext(), ShopDetailActivity.this);
|
||||
|
||||
Log.e("ERROR", "Get Detail Shop Error!");
|
||||
Common.showNotificationError(getBaseContext(), ShopDetailActivity.this);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -142,6 +142,7 @@ public class SignInActivity extends AppCompatActivity {
|
||||
password = (TextInputEditText) findViewById(R.id.edt_password);
|
||||
|
||||
signInButton = (MaterialButton) findViewById(R.id.sign_in_button);
|
||||
|
||||
}
|
||||
private void setFontUI() {
|
||||
textInput_account.setTypeface(Common.setFontBebas(getAssets()));
|
||||
|
||||
@@ -99,16 +99,16 @@ public class FoodFavoriteAdapter extends RecyclerView.Adapter<FoodFavoriteAdapt
|
||||
listFavoriteFood.add(index, food);
|
||||
notifyItemInserted(index);
|
||||
|
||||
FoodApiToken.apiService.addFoodToFavorite(food).enqueue(new Callback<Food>() {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<Food> call, @NonNull Response<Food> response) {
|
||||
Toast.makeText(context, "Add successful!", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(@NonNull Call<Food> call, @NonNull Throwable t) {
|
||||
Toast.makeText(context, "Error: " + t, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
// FoodApiToken.apiService.addFoodToFavorite(food.getId()).enqueue(new Callback<Food>() {
|
||||
// @Override
|
||||
// public void onResponse(@NonNull Call<Food> call, @NonNull Response<Food> response) {
|
||||
// Toast.makeText(context, "Add successful!", Toast.LENGTH_SHORT).show();
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onFailure(@NonNull Call<Food> call, @NonNull Throwable t) {
|
||||
// Toast.makeText(context, "Error: " + t, Toast.LENGTH_SHORT).show();
|
||||
// }
|
||||
// });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.capstone.foodify;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.content.res.AssetManager;
|
||||
import android.graphics.Typeface;
|
||||
@@ -14,6 +15,9 @@ import android.widget.Toast;
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.capstone.foodify.Model.Basket.Basket;
|
||||
import com.saadahmedsoft.popupdialog.PopupDialog;
|
||||
import com.saadahmedsoft.popupdialog.Styles;
|
||||
import com.saadahmedsoft.popupdialog.listener.OnDialogButtonClickListener;
|
||||
import com.thecode.aestheticdialogs.AestheticDialog;
|
||||
import com.thecode.aestheticdialogs.DialogStyle;
|
||||
import com.thecode.aestheticdialogs.DialogType;
|
||||
@@ -73,16 +77,32 @@ public class Common {
|
||||
.show();
|
||||
}
|
||||
|
||||
public static void showNotificationError(Throwable t, Context context, Activity activity) {
|
||||
public static void showNotificationError(Context context, Activity activity) {
|
||||
if(checkInternetConnection(context)){
|
||||
//Has internet connection
|
||||
Toast.makeText(context, "Error: " + t, Toast.LENGTH_SHORT).show();
|
||||
showErrorServerNotification(activity);
|
||||
} else {
|
||||
//No internet, show notification
|
||||
showErrorInternetConnectionNotification(activity);
|
||||
}
|
||||
}
|
||||
|
||||
private static void showErrorServerNotification(Activity activity){
|
||||
PopupDialog.getInstance(activity)
|
||||
.setStyle(Styles.FAILED)
|
||||
.setHeading("Lỗi")
|
||||
.setDescription("Đã xuất hiện lỗi từ hệ thống. Vui lòng thử lại sau!")
|
||||
.setCancelable(false)
|
||||
.setDismissButtonText("Thoát")
|
||||
.showDialog(new OnDialogButtonClickListener() {
|
||||
@Override
|
||||
public void onDismissClicked(Dialog dialog) {
|
||||
activity.finishAffinity();
|
||||
System.exit(0);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static Basket getFoodExistInBasket(String foodId) {
|
||||
return Common.LIST_BASKET_FOOD.stream().filter(food -> foodId.equals(food.getId())).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.capstone.foodify.API.FoodApi;
|
||||
import com.capstone.foodify.Activity.MainActivity;
|
||||
import com.capstone.foodify.Common;
|
||||
import com.capstone.foodify.Model.Food.Food;
|
||||
import com.capstone.foodify.Model.Menu.Menu;
|
||||
@@ -20,10 +21,13 @@ import com.capstone.foodify.Model.Response.Foods;
|
||||
import com.capstone.foodify.Model.Response.Shops;
|
||||
import com.capstone.foodify.Model.Shop.Shop;
|
||||
import com.capstone.foodify.Adapter.ShopAdapter;
|
||||
import com.capstone.foodify.Model.Slider.Slider;
|
||||
import com.capstone.foodify.R;
|
||||
import com.denzcoskun.imageslider.ImageSlider;
|
||||
import com.denzcoskun.imageslider.constants.ScaleTypes;
|
||||
import com.denzcoskun.imageslider.models.SlideModel;
|
||||
import com.saadahmedsoft.popupdialog.PopupDialog;
|
||||
import com.saadahmedsoft.popupdialog.Styles;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -44,6 +48,7 @@ public class HomeFragment extends Fragment {
|
||||
private RecyclerView rcvMenu, recyclerView_restaurant;
|
||||
private MenuAdapter menuAdapter;
|
||||
private ShopAdapter shopAdapter;
|
||||
private PopupDialog popupDialog;
|
||||
|
||||
|
||||
@Override
|
||||
@@ -58,6 +63,12 @@ public class HomeFragment extends Fragment {
|
||||
recyclerView_restaurant = view.findViewById(R.id.recycler_view_restaurant);
|
||||
shopAdapter = new ShopAdapter(getContext());
|
||||
|
||||
popupDialog = PopupDialog.getInstance(getContext());
|
||||
|
||||
//Show progress bar
|
||||
popupDialog.setStyle(Styles.PROGRESS).setProgressDialogTint(getResources().getColor(R.color.primaryColor, null))
|
||||
.setCancelable(false).showDialog();
|
||||
|
||||
|
||||
if(listFood.isEmpty() || recentFood.isEmpty()){
|
||||
getListFood();
|
||||
@@ -70,14 +81,7 @@ public class HomeFragment extends Fragment {
|
||||
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, false);
|
||||
rcvMenu.setLayoutManager(linearLayoutManager);
|
||||
|
||||
List<SlideModel> slideModels = new ArrayList<>();
|
||||
slideModels.add(new SlideModel("https://res.cloudinary.com/dnkmxpujh/image/upload/v1676023773/banner1_dandvd.png", null));
|
||||
slideModels.add(new SlideModel("https://res.cloudinary.com/dnkmxpujh/image/upload/v1676023777/banner2_okkqac.png", null));
|
||||
slideModels.add(new SlideModel("https://res.cloudinary.com/dnkmxpujh/image/upload/v1676023775/banner3_rklfi6.png", null));
|
||||
slideModels.add(new SlideModel("https://res.cloudinary.com/dnkmxpujh/image/upload/v1676023852/banner4_ufsaps.png", null));
|
||||
slideModels.add(new SlideModel("https://res.cloudinary.com/dnkmxpujh/image/upload/v1676023778/banner5_b4d1b8.png", null));
|
||||
|
||||
imageSlider.setImageList(slideModels, ScaleTypes.FIT);
|
||||
showSlider();
|
||||
|
||||
//Get list shop
|
||||
getListShop();
|
||||
@@ -86,6 +90,30 @@ public class HomeFragment extends Fragment {
|
||||
return view;
|
||||
}
|
||||
|
||||
private void showSlider(){
|
||||
FoodApi.apiService.listSlider().enqueue(new Callback<List<Slider>>() {
|
||||
@Override
|
||||
public void onResponse(Call<List<Slider>> call, Response<List<Slider>> response) {
|
||||
List<Slider> sliders = response.body();
|
||||
|
||||
List<SlideModel> slideModels = new ArrayList<>();
|
||||
for(Slider slider: sliders){
|
||||
slideModels.add(new SlideModel(slider.getImageUrl(), null));
|
||||
}
|
||||
|
||||
imageSlider.setImageList(slideModels, ScaleTypes.FIT);
|
||||
|
||||
popupDialog.dismissDialog();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<List<Slider>> call, Throwable t) {
|
||||
popupDialog.dismissDialog();
|
||||
Common.showNotificationError(getContext(), getActivity());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private List<Menu> getListMenu() {
|
||||
List<Menu> listMenu = new ArrayList<>();
|
||||
|
||||
@@ -106,18 +134,13 @@ public class HomeFragment extends Fragment {
|
||||
return;
|
||||
|
||||
listFood = foodResponse.getProducts();
|
||||
|
||||
|
||||
recentFood();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<Foods> call, Throwable t) {
|
||||
//Check internet connection
|
||||
Common.showNotificationError(t, getContext(), getActivity());
|
||||
|
||||
Log.e("ERROR", "Get recommend food error!");
|
||||
|
||||
Common.showNotificationError(getContext(), getActivity());
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -141,10 +164,7 @@ public class HomeFragment extends Fragment {
|
||||
@Override
|
||||
public void onFailure(Call<Foods> call, Throwable t) {
|
||||
//Check internet connection
|
||||
Common.showNotificationError(t,getContext(), getActivity());
|
||||
|
||||
Log.e("ERROR", "Get recent food error!");
|
||||
|
||||
Common.showNotificationError(getContext(), getActivity());
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -167,9 +187,7 @@ public class HomeFragment extends Fragment {
|
||||
@Override
|
||||
public void onFailure(Call<Shops> call, Throwable t) {
|
||||
//Check internet connection
|
||||
Common.showNotificationError(t, getContext(), getActivity());
|
||||
|
||||
Log.e("ERROR", "Get all shop error!");
|
||||
Common.showNotificationError(getContext(), getActivity());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -160,9 +160,7 @@ public class SearchFragment extends Fragment {
|
||||
@Override
|
||||
public void onFailure(Call<Foods> call, Throwable t) {
|
||||
//Check internet connection
|
||||
Common.showNotificationError(t, getContext(), getActivity());
|
||||
|
||||
Log.e("ERROR", "Get search food by name error!");
|
||||
Common.showNotificationError(getContext(), getActivity());
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -240,9 +238,7 @@ public class SearchFragment extends Fragment {
|
||||
@Override
|
||||
public void onFailure(Call<List<Category>> call, Throwable t) {
|
||||
//Check internet connection
|
||||
Common.showNotificationError(t, getContext(), getActivity());
|
||||
|
||||
Log.e("ERROR", "Get list category Error!");
|
||||
Common.showNotificationError(getContext(), getActivity());
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -259,9 +255,7 @@ public class SearchFragment extends Fragment {
|
||||
@Override
|
||||
public void onFailure(Call<Foods> call, Throwable t) {
|
||||
//Check internet connection
|
||||
Common.showNotificationError(t, getContext(), getActivity());
|
||||
|
||||
Log.e("ERROR", "Get List Food By Category Error!");
|
||||
Common.showNotificationError(getContext(), getActivity());
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -277,9 +271,7 @@ public class SearchFragment extends Fragment {
|
||||
@Override
|
||||
public void onFailure(Call<Foods> call, Throwable t) {
|
||||
//Check internet connection
|
||||
Common.showNotificationError(t, getContext(), getActivity());
|
||||
|
||||
Log.e("ERROR", "Get list food error!");
|
||||
Common.showNotificationError(getContext(), getActivity());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.capstone.foodify.Model.Slider;
|
||||
|
||||
public class Slider {
|
||||
private int id;
|
||||
private String imageUrl;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getImageUrl() {
|
||||
return imageUrl;
|
||||
}
|
||||
|
||||
public void setImageUrl(String imageUrl) {
|
||||
this.imageUrl = imageUrl;
|
||||
}
|
||||
}
|
||||
@@ -124,7 +124,7 @@
|
||||
app:backGroundColor="@color/white"
|
||||
app:textColor="@color/primaryColor"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:layout_weight="0.4"
|
||||
android:layout_weight="0.43"
|
||||
/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
@@ -82,6 +82,7 @@
|
||||
android:textColor="@color/red"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="match_parent"
|
||||
android:visibility="gone"
|
||||
/>
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user