Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package codelabs.ambarrukmo.fragment;
- import android.app.Dialog;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.annotation.NonNull;
- import android.support.annotation.Nullable;
- import android.support.v4.app.DialogFragment;
- import android.support.v4.view.ViewPager;
- import android.support.v7.widget.LinearLayoutManager;
- import android.support.v7.widget.RecyclerView;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.FrameLayout;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.google.gson.Gson;
- import com.google.gson.GsonBuilder;
- import com.squareup.picasso.Picasso;
- import java.util.ArrayList;
- import java.util.List;
- import butterknife.BindView;
- import butterknife.ButterKnife;
- import butterknife.Unbinder;
- import codelabs.ambarrukmo.R;
- import codelabs.ambarrukmo.activity.ViewAllMerchantActivity;
- import codelabs.ambarrukmo.adapter.CategoryPromoAdapter;
- import codelabs.ambarrukmo.adapter.MultipleCategoryVerticalAdapter;
- import codelabs.ambarrukmo.adapter.PromoEventListAdapter;
- import codelabs.ambarrukmo.adapter.PromoVoucherHomeAdapter;
- import codelabs.ambarrukmo.connection.ApiUtils;
- import codelabs.ambarrukmo.connection.AppConstant;
- import codelabs.ambarrukmo.connection.DataManager;
- import codelabs.ambarrukmo.connection.RetrofitInterface;
- import codelabs.ambarrukmo.custom.HorizontalItemDecoration;
- import codelabs.ambarrukmo.dialog.DialogProgress;
- import codelabs.ambarrukmo.model.CategoryListPromo;
- import codelabs.ambarrukmo.model.GetPromoList;
- import codelabs.ambarrukmo.model.GettingBannerStatic;
- import codelabs.ambarrukmo.model.GettingPromoEventList;
- import codelabs.ambarrukmo.model.GettingVoucherList;
- import codelabs.ambarrukmo.utils.LoadingDialog;
- import codelabs.ambarrukmo.utils.RecentUtils;
- import retrofit2.Call;
- import retrofit2.Callback;
- import retrofit2.Response;
- public class HomeFragment extends DialogFragment implements ViewPager.OnPageChangeListener, View.OnClickListener {
- @BindView(R.id.tv_error)
- TextView tvError;
- @BindView(R.id.tv_error_desc)
- TextView tvErrorDesc;
- @BindView(R.id.container_error)
- LinearLayout containerError;
- @BindView(R.id.tv_loading)
- TextView tvLoading;
- @BindView(R.id.tv_loading_desc)
- TextView tvLoadingDesc;
- @BindView(R.id.container_loading)
- LinearLayout containerLoading;
- @BindView(R.id.container_content)
- FrameLayout frameLayout;
- @BindView(R.id.viewPager)
- ViewPager viewPagerSlider;
- @BindView(R.id.tv_points)
- TextView tvPoints;
- @BindView(R.id.rv_list_promo_category)
- RecyclerView rvListPromoCategory;
- @BindView(R.id.rv_list_merchant)
- RecyclerView rvListMerchant;
- @BindView(R.id.tv_view_all_merch)
- TextView tvViewAllMerch;
- @BindView(R.id.img_top_promo)
- ImageView imgTopPromo;
- @BindView(R.id.indicator)
- LinearLayout markIndicatorLinear;
- @BindView(R.id.rv_multi_category)
- RecyclerView rvMultiCategory;
- private static final String ARG_TEXT = "param1";
- private static final int SELECT_DETAIL = 111;
- protected DialogProgress dialogProgress;
- private PromoEventListAdapter mAdapterEvent;
- private CategoryPromoAdapter mAdapterPromo;
- private MultipleCategoryVerticalAdapter mAdapterMultiCategory;
- private PromoVoucherHomeAdapter mAdapterPromoVoucher;
- private LinearLayoutManager layoutManager;
- private int currentCount = 0;
- private List<GetPromoList.DATABean> responseData = new ArrayList<>();
- private GettingBannerStatic.DATABean responseDataImage;
- private Call<CategoryListPromo> call;
- Gson gson;
- Dialog myDialog;
- Unbinder unbinder;
- private Toast mToast;
- int markIndicator;
- @Override
- public void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- }
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // Inflate the layout for this fragment
- View view = inflater.inflate(R.layout.fragment_home, container, false);
- return view;
- }
- @Override
- public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- unbinder = ButterKnife.bind(this, view);
- gson = new GsonBuilder().serializeNulls().create();
- myDialog = new Dialog(getContext());
- initVariables();
- initListeners();
- callFunctions();
- }
- private void initVariables(){
- }
- private void initListeners(){
- tvPoints.setText(DataManager.getInstance().getMbr_point()+ " " +"Points");
- tvViewAllMerch.setOnClickListener(this);
- }
- private void callFunctions(){
- initRecyclerView();
- loadDataCategory();
- loadBannerBottom();
- loadEventSlider();
- loadDataVoucherHome();
- loadDataMultiCategory();
- }
- /**
- * Setup Recyclerview
- */
- public void initRecyclerView() {
- mAdapterPromo = new CategoryPromoAdapter(getContext());
- rvListPromoCategory.setHasFixedSize(false);
- rvListPromoCategory.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
- rvListPromoCategory.setAdapter(mAdapterPromo);
- HorizontalItemDecoration itemDecorationCat = new HorizontalItemDecoration(RecentUtils.ConvertDpToPx(getContext(), 2));
- rvListPromoCategory.addItemDecoration(itemDecorationCat);
- mAdapterMultiCategory = new MultipleCategoryVerticalAdapter(getContext());
- rvMultiCategory.setHasFixedSize(false);
- rvMultiCategory.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
- rvMultiCategory.setAdapter(mAdapterMultiCategory);
- mAdapterPromoVoucher = new PromoVoucherHomeAdapter(getContext());
- rvListMerchant.setHasFixedSize(false);
- rvListMerchant.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
- rvListMerchant.setAdapter(mAdapterPromoVoucher);
- HorizontalItemDecoration itemDecorationMer = new HorizontalItemDecoration(RecentUtils.ConvertDpToPx(getContext(), 10));
- rvListMerchant.addItemDecoration(itemDecorationMer);
- mAdapterEvent = new PromoEventListAdapter(getContext());
- viewPagerSlider.setAdapter(mAdapterEvent);
- markIndicator = mAdapterEvent.getCount();
- autoPlay(viewPagerSlider);
- }
- @Override
- public void onClick(View view) {
- if (view == tvViewAllMerch){
- Intent intent = new Intent(getContext(), ViewAllMerchantActivity.class);
- startActivity(intent);
- }
- // } else if (view == btnCheckin) {
- // FragmentTransaction ft = getFragmentManager().beginTransaction();
- // Fragment prev = getFragmentManager().findFragmentByTag("dialog");
- // if (prev !=null) {
- // ft.remove(prev);
- // }
- // ft.addToBackStack(null);
- //
- // DialogFragment dialogFragment= new MapDialogFragment();
- // dialogFragment.show(ft, "dialog");
- // }
- }
- public void loadDataCategory() {
- tvLoading.setText("Getting category");
- tvLoadingDesc.setText("Please wait fot getting about");
- containerLoading.setVisibility(View.VISIBLE);
- showDialogProgress("Getting data");
- RetrofitInterface apiService = ApiUtils.getAPIService();
- String auth = AppConstant.AuthValue + " " +DataManager.getInstance().getToken();
- Call<CategoryListPromo> call = apiService.getPromoCategories(auth);
- call.enqueue(new Callback<CategoryListPromo>() {
- @Override
- public void onResponse(@NonNull Call<CategoryListPromo> call, @NonNull Response<CategoryListPromo> data) {
- hideDialogProgress();
- if (data.isSuccessful()) {
- CategoryListPromo response = data.body();
- if (response != null) {
- if (response.getSTATUS() == 200) {
- // Log.d("hasilcateg", new Gson().toJson(data.body()));
- mAdapterPromo.setData(data.body().getDATA());
- } else {
- containerError.setVisibility(View.VISIBLE);
- tvError.setText(response.getMESSAGE());
- }
- } else {
- containerError.setVisibility(View.VISIBLE);
- tvError.setText(getString(R.string.no_response));
- }
- } else {
- containerError.setVisibility(View.VISIBLE);
- RecentUtils.handleRetrofitError(data.code(), tvError);
- }
- }
- @Override
- public void onFailure(@NonNull Call<CategoryListPromo> call, @NonNull Throwable t) {
- if (!call.isCanceled()){
- hideDialogProgress();
- containerLoading.setVisibility(View.GONE);
- containerError.setVisibility(View.VISIBLE);
- tvError.setText(getString(R.string.network_error));
- }
- }
- });
- }
- public void loadDataMultiCategory(){
- RetrofitInterface apiService = ApiUtils.getAPIService();
- String auth = AppConstant.AuthValue + " "+DataManager.getInstance().getToken();
- Call<CategoryListPromo> call = apiService.getPromoCategories(auth);
- call.enqueue(new Callback<CategoryListPromo>() {
- @Override
- public void onResponse(@NonNull Call<CategoryListPromo> call, @NonNull Response<CategoryListPromo> data) {
- if (data.isSuccessful()) {
- CategoryListPromo response = data.body();
- if (response != null) {
- if (response.getSTATUS() == 200) {
- mAdapterMultiCategory.setData(data.body().getDATA());
- }
- }
- }
- }
- @Override
- public void onFailure(@NonNull Call<CategoryListPromo> call, @NonNull Throwable t) {
- }
- });
- }
- public void loadDataVoucherHome() {
- tvLoading.setText("Getting Data Promo");
- tvLoadingDesc.setText("Please wait for getting promo");
- containerLoading.setVisibility(View.VISIBLE);
- // showDialogProgress("Getting data");
- RetrofitInterface apiService = ApiUtils.getAPIService();
- String auth = AppConstant.AuthValue+" "+ DataManager.getInstance().getToken();
- Call<GettingVoucherList> call = apiService.getVoucherList(auth);
- call.enqueue(new Callback<GettingVoucherList>() {
- @Override
- public void onResponse(@NonNull Call<GettingVoucherList> call,@NonNull Response<GettingVoucherList> data) {
- // hideDialogProgress();
- containerLoading.setVisibility(View.GONE);
- if (data.isSuccessful()) {
- GettingVoucherList response = data.body();
- if (response != null) {
- if (response.getSTATUS() == 200) {
- mAdapterPromoVoucher.setData(data.body().getDATA());
- }else {
- containerError.setVisibility(View.VISIBLE);
- tvError.setText(response.getMESSAGE());
- }
- }else {
- containerError.setVisibility(View.VISIBLE);
- tvError.setText(getString(R.string.no_response));
- }
- }else {
- containerError.setVisibility(View.VISIBLE);
- RecentUtils.handleRetrofitError(data.code(), tvError);
- }
- }
- @Override
- public void onFailure(@NonNull Call<GettingVoucherList> call, @NonNull Throwable t) {
- if (!call.isCanceled()) {
- // hideDialogProgress();
- containerLoading.setVisibility(View.GONE);
- containerError.setVisibility(View.VISIBLE);
- tvError.setText(getString(R.string.network_error));
- }
- }
- });
- }
- public void loadBannerBottom() {
- tvLoading.setText("Getting image");
- tvLoadingDesc.setText("Pleasae wait fot getting image");
- containerLoading.setVisibility(View.VISIBLE);
- // showDialogProgress("Getting data");
- RetrofitInterface apiService = ApiUtils.getAPIService();
- String auth = AppConstant.AuthValue + " " + DataManager.getInstance().getToken();
- Call<GettingBannerStatic> call = apiService.getBannerStatic(auth);
- call.enqueue(new Callback<GettingBannerStatic>() {
- @Override
- public void onResponse(@NonNull Call<GettingBannerStatic> call, @NonNull Response<GettingBannerStatic> data) {
- // hideDialogProgress();
- if (data.isSuccessful()) {
- GettingBannerStatic response = data.body();
- if (response != null) {
- if (response.getSTATUS() == 200) {
- // Log.d("hasilcateg", new Gson().toJson(data.body()));
- responseDataImage = response.getDATA();
- processImageBanner();
- } else {
- containerError.setVisibility(View.VISIBLE);
- tvError.setText(response.getMESSAGE());
- }
- } else {
- containerError.setVisibility(View.VISIBLE);
- tvError.setText(getString(R.string.no_response));
- }
- } else {
- containerError.setVisibility(View.VISIBLE);
- RecentUtils.handleRetrofitError(data.code(), tvError);
- }
- }
- @Override
- public void onFailure(@NonNull Call<GettingBannerStatic> call, @NonNull Throwable t) {
- if (!call.isCanceled()){
- // hideDialogProgress();
- containerLoading.setVisibility(View.GONE);
- containerError.setVisibility(View.VISIBLE);
- tvError.setText(getString(R.string.network_error));
- }
- }
- });
- }
- public void loadEventSlider() {
- tvLoading.setText("Getting Image Promo");
- tvLoadingDesc.setText("Please wait for getting about");
- containerLoading.setVisibility(View.VISIBLE);
- // showDialogProgress("Getting data");
- RetrofitInterface apiService = ApiUtils.getAPIService();
- String auth = AppConstant.AuthValue + DataManager.getInstance().getToken();
- Call<GettingPromoEventList> call = apiService.getPromoEventList(auth);
- call.enqueue(new Callback<GettingPromoEventList>() {
- @Override
- public void onResponse(@NonNull Call<GettingPromoEventList> call, @NonNull Response<GettingPromoEventList> data) {
- // hideDialogProgress();
- containerLoading.setVisibility(View.GONE);
- if (data.isSuccessful()) {
- GettingPromoEventList response = data.body();
- if (response != null) {
- if (response.getSTATUS() == 200) {
- // containerContent.setVisibility(View.VISIBLE);
- Log.d("hasil", new Gson().toJson(data.body()));
- mAdapterEvent.setData(data.body().getDATA());
- } else {
- containerError.setVisibility(View.VISIBLE);
- tvError.setText(response.getMESSAGE());
- }
- } else {
- containerError.setVisibility(View.VISIBLE);
- tvError.setText(getString(R.string.no_response));
- }
- } else {
- containerError.setVisibility(View.VISIBLE);
- RecentUtils.handleRetrofitError(data.code(), tvError);
- }
- }
- @Override
- public void onFailure(@NonNull Call<GettingPromoEventList> call, @NonNull Throwable t) {
- if (!call.isCanceled()) {
- // hideDialogProgress();
- containerLoading.setVisibility(View.GONE);
- containerError.setVisibility(View.VISIBLE);
- tvError.setText(getString(R.string.network_error));
- }
- }
- });
- }
- private void autoPlay(final ViewPager viewPager) {
- viewPager.postDelayed(new Runnable() {
- @Override
- public void run() {
- try {
- if (mAdapterEvent != null && mAdapterEvent.getCount() > 0) {
- int position = currentCount % mAdapterEvent.getCount();
- currentCount++;
- viewPagerSlider.setCurrentItem(position);
- autoPlay(viewPagerSlider);
- }
- } catch (Exception e) {
- Log.e("TAG", "auto scroll pager error.", e);
- }
- }
- }, 2000);
- }
- private void processImageBanner() {
- Picasso.get()
- .load(responseDataImage.getPromo_image())
- .placeholder(R.drawable.placeholder220)
- .error(R.drawable.placeholder220)
- .into(imgTopPromo);
- }
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- // if (resultCode == Activity.RESULT_OK) {
- // if (requestCode == SELECT_DETAIL) {
- //
- // String result = data.getStringExtra("result");
- // mAdapterList.getItems().get(adapterPosition).setStatus(result);
- // mAdapterList.notifyItemChanged(adapterPosition);
- // }
- // }
- }
- public void showDialogProgress(String message) {
- if (message != null) {
- dialogProgress = new DialogProgress(getContext(), message, true);
- dialogProgress.setCancelable(false);
- dialogProgress.show();
- } else {
- dialogProgress = new DialogProgress(getContext(), "Loading ...", false);
- dialogProgress.setCancelable(false);
- dialogProgress.show();
- }
- }
- public void hideDialogProgress() {
- if (dialogProgress != null) {
- if (dialogProgress.isShowing()) {
- dialogProgress.dismiss();
- }
- }
- }
- public void showToast(String val){
- if (mToast != null) mToast.cancel();
- mToast = Toast.makeText(getContext(), val, Toast.LENGTH_SHORT);
- mToast.show();
- }
- public void hideContainer() {
- // containerContent.setVisibility(View.GONE);
- // containerError.setVisibility(View.GONE);
- // containerLoading.setVisibility(View.GONE);
- }
- @Override
- public void onPageScrolled(int i, float v, int i1) {
- }
- @Override
- public void onPageSelected(int i) {
- // if (getActivity() != null) {
- // for (int i1 = 0; i1 < markIndicator; i1++) {
- // imageViewMarkIndicator.get(i1).setImageDrawable(ContextCompat.getDrawable(this.getActivity(), R.drawable. nonselecteditem_dot));
- // }
- // imageViewMarkIndicator.get(i).setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.selecteditem_dot));
- // }
- }
- @Override
- public void onPageScrollStateChanged(int i) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement