Advertisement
ricky_yulianto

Untitled

Jul 11th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.28 KB | None | 0 0
  1. package codelabs.ambarrukmo.fragment;
  2.  
  3. import android.app.Dialog;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.support.annotation.NonNull;
  7. import android.support.annotation.Nullable;
  8. import android.support.v4.app.DialogFragment;
  9. import android.support.v4.view.ViewPager;
  10. import android.support.v7.widget.LinearLayoutManager;
  11. import android.support.v7.widget.RecyclerView;
  12. import android.util.Log;
  13. import android.view.LayoutInflater;
  14. import android.view.View;
  15. import android.view.ViewGroup;
  16. import android.widget.FrameLayout;
  17. import android.widget.ImageView;
  18. import android.widget.LinearLayout;
  19. import android.widget.TextView;
  20. import android.widget.Toast;
  21. import com.google.gson.Gson;
  22. import com.google.gson.GsonBuilder;
  23. import com.squareup.picasso.Picasso;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import butterknife.BindView;
  27. import butterknife.ButterKnife;
  28. import butterknife.Unbinder;
  29. import codelabs.ambarrukmo.R;
  30. import codelabs.ambarrukmo.activity.ViewAllMerchantActivity;
  31. import codelabs.ambarrukmo.adapter.CategoryPromoAdapter;
  32. import codelabs.ambarrukmo.adapter.MultipleCategoryVerticalAdapter;
  33. import codelabs.ambarrukmo.adapter.PromoEventListAdapter;
  34. import codelabs.ambarrukmo.adapter.PromoVoucherHomeAdapter;
  35. import codelabs.ambarrukmo.connection.ApiUtils;
  36. import codelabs.ambarrukmo.connection.AppConstant;
  37. import codelabs.ambarrukmo.connection.DataManager;
  38. import codelabs.ambarrukmo.connection.RetrofitInterface;
  39. import codelabs.ambarrukmo.custom.HorizontalItemDecoration;
  40. import codelabs.ambarrukmo.dialog.DialogProgress;
  41. import codelabs.ambarrukmo.model.CategoryListPromo;
  42. import codelabs.ambarrukmo.model.GetPromoList;
  43. import codelabs.ambarrukmo.model.GettingBannerStatic;
  44. import codelabs.ambarrukmo.model.GettingPromoEventList;
  45. import codelabs.ambarrukmo.model.GettingVoucherList;
  46. import codelabs.ambarrukmo.utils.LoadingDialog;
  47. import codelabs.ambarrukmo.utils.RecentUtils;
  48. import retrofit2.Call;
  49. import retrofit2.Callback;
  50. import retrofit2.Response;
  51.  
  52.  
  53. public class HomeFragment extends DialogFragment implements ViewPager.OnPageChangeListener, View.OnClickListener {
  54.  
  55. @BindView(R.id.tv_error)
  56. TextView tvError;
  57. @BindView(R.id.tv_error_desc)
  58. TextView tvErrorDesc;
  59. @BindView(R.id.container_error)
  60. LinearLayout containerError;
  61. @BindView(R.id.tv_loading)
  62. TextView tvLoading;
  63. @BindView(R.id.tv_loading_desc)
  64. TextView tvLoadingDesc;
  65. @BindView(R.id.container_loading)
  66. LinearLayout containerLoading;
  67. @BindView(R.id.container_content)
  68. FrameLayout frameLayout;
  69. @BindView(R.id.viewPager)
  70. ViewPager viewPagerSlider;
  71. @BindView(R.id.tv_points)
  72. TextView tvPoints;
  73. @BindView(R.id.rv_list_promo_category)
  74. RecyclerView rvListPromoCategory;
  75. @BindView(R.id.rv_list_merchant)
  76. RecyclerView rvListMerchant;
  77. @BindView(R.id.tv_view_all_merch)
  78. TextView tvViewAllMerch;
  79. @BindView(R.id.img_top_promo)
  80. ImageView imgTopPromo;
  81. @BindView(R.id.indicator)
  82. LinearLayout markIndicatorLinear;
  83. @BindView(R.id.rv_multi_category)
  84. RecyclerView rvMultiCategory;
  85.  
  86.  
  87.  
  88.  
  89. private static final String ARG_TEXT = "param1";
  90. private static final int SELECT_DETAIL = 111;
  91. protected DialogProgress dialogProgress;
  92. private PromoEventListAdapter mAdapterEvent;
  93. private CategoryPromoAdapter mAdapterPromo;
  94. private MultipleCategoryVerticalAdapter mAdapterMultiCategory;
  95. private PromoVoucherHomeAdapter mAdapterPromoVoucher;
  96. private LinearLayoutManager layoutManager;
  97. private int currentCount = 0;
  98. private List<GetPromoList.DATABean> responseData = new ArrayList<>();
  99. private GettingBannerStatic.DATABean responseDataImage;
  100. private Call<CategoryListPromo> call;
  101.  
  102.  
  103. Gson gson;
  104. Dialog myDialog;
  105. Unbinder unbinder;
  106. private Toast mToast;
  107. int markIndicator;
  108.  
  109.  
  110.  
  111.  
  112. @Override
  113. public void onCreate(@Nullable Bundle savedInstanceState) {
  114. super.onCreate(savedInstanceState);
  115. }
  116.  
  117.  
  118. @Nullable
  119. @Override
  120. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  121. Bundle savedInstanceState) {
  122. // Inflate the layout for this fragment
  123. View view = inflater.inflate(R.layout.fragment_home, container, false);
  124.  
  125.  
  126. return view;
  127. }
  128.  
  129.  
  130. @Override
  131. public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  132. super.onViewCreated(view, savedInstanceState);
  133. unbinder = ButterKnife.bind(this, view);
  134. gson = new GsonBuilder().serializeNulls().create();
  135. myDialog = new Dialog(getContext());
  136. initVariables();
  137. initListeners();
  138. callFunctions();
  139.  
  140.  
  141. }
  142.  
  143.  
  144. private void initVariables(){
  145.  
  146. }
  147.  
  148. private void initListeners(){
  149.  
  150. tvPoints.setText(DataManager.getInstance().getMbr_point()+ " " +"Points");
  151. tvViewAllMerch.setOnClickListener(this);
  152.  
  153. }
  154.  
  155. private void callFunctions(){
  156. initRecyclerView();
  157. loadDataCategory();
  158. loadBannerBottom();
  159. loadEventSlider();
  160. loadDataVoucherHome();
  161. loadDataMultiCategory();
  162.  
  163. }
  164.  
  165.  
  166.  
  167. /**
  168. * Setup Recyclerview
  169. */
  170. public void initRecyclerView() {
  171.  
  172.  
  173. mAdapterPromo = new CategoryPromoAdapter(getContext());
  174. rvListPromoCategory.setHasFixedSize(false);
  175. rvListPromoCategory.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
  176. rvListPromoCategory.setAdapter(mAdapterPromo);
  177.  
  178. HorizontalItemDecoration itemDecorationCat = new HorizontalItemDecoration(RecentUtils.ConvertDpToPx(getContext(), 2));
  179. rvListPromoCategory.addItemDecoration(itemDecorationCat);
  180.  
  181.  
  182. mAdapterMultiCategory = new MultipleCategoryVerticalAdapter(getContext());
  183. rvMultiCategory.setHasFixedSize(false);
  184. rvMultiCategory.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
  185. rvMultiCategory.setAdapter(mAdapterMultiCategory);
  186.  
  187.  
  188.  
  189. mAdapterPromoVoucher = new PromoVoucherHomeAdapter(getContext());
  190. rvListMerchant.setHasFixedSize(false);
  191. rvListMerchant.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
  192. rvListMerchant.setAdapter(mAdapterPromoVoucher);
  193.  
  194. HorizontalItemDecoration itemDecorationMer = new HorizontalItemDecoration(RecentUtils.ConvertDpToPx(getContext(), 10));
  195. rvListMerchant.addItemDecoration(itemDecorationMer);
  196.  
  197.  
  198.  
  199. mAdapterEvent = new PromoEventListAdapter(getContext());
  200. viewPagerSlider.setAdapter(mAdapterEvent);
  201. markIndicator = mAdapterEvent.getCount();
  202. autoPlay(viewPagerSlider);
  203.  
  204. }
  205.  
  206.  
  207.  
  208. @Override
  209. public void onClick(View view) {
  210. if (view == tvViewAllMerch){
  211. Intent intent = new Intent(getContext(), ViewAllMerchantActivity.class);
  212. startActivity(intent);
  213. }
  214. // } else if (view == btnCheckin) {
  215. // FragmentTransaction ft = getFragmentManager().beginTransaction();
  216. // Fragment prev = getFragmentManager().findFragmentByTag("dialog");
  217. // if (prev !=null) {
  218. // ft.remove(prev);
  219. // }
  220. // ft.addToBackStack(null);
  221. //
  222. // DialogFragment dialogFragment= new MapDialogFragment();
  223. // dialogFragment.show(ft, "dialog");
  224.  
  225. // }
  226.  
  227. }
  228.  
  229. public void loadDataCategory() {
  230. tvLoading.setText("Getting category");
  231. tvLoadingDesc.setText("Please wait fot getting about");
  232. containerLoading.setVisibility(View.VISIBLE);
  233.  
  234. showDialogProgress("Getting data");
  235.  
  236.  
  237. RetrofitInterface apiService = ApiUtils.getAPIService();
  238. String auth = AppConstant.AuthValue + " " +DataManager.getInstance().getToken();
  239. Call<CategoryListPromo> call = apiService.getPromoCategories(auth);
  240. call.enqueue(new Callback<CategoryListPromo>() {
  241. @Override
  242. public void onResponse(@NonNull Call<CategoryListPromo> call, @NonNull Response<CategoryListPromo> data) {
  243. hideDialogProgress();
  244. if (data.isSuccessful()) {
  245. CategoryListPromo response = data.body();
  246. if (response != null) {
  247. if (response.getSTATUS() == 200) {
  248. // Log.d("hasilcateg", new Gson().toJson(data.body()));
  249. mAdapterPromo.setData(data.body().getDATA());
  250.  
  251. } else {
  252. containerError.setVisibility(View.VISIBLE);
  253. tvError.setText(response.getMESSAGE());
  254. }
  255. } else {
  256. containerError.setVisibility(View.VISIBLE);
  257. tvError.setText(getString(R.string.no_response));
  258.  
  259. }
  260. } else {
  261. containerError.setVisibility(View.VISIBLE);
  262. RecentUtils.handleRetrofitError(data.code(), tvError);
  263.  
  264. }
  265.  
  266. }
  267.  
  268. @Override
  269. public void onFailure(@NonNull Call<CategoryListPromo> call, @NonNull Throwable t) {
  270.  
  271. if (!call.isCanceled()){
  272. hideDialogProgress();
  273. containerLoading.setVisibility(View.GONE);
  274. containerError.setVisibility(View.VISIBLE);
  275. tvError.setText(getString(R.string.network_error));
  276.  
  277. }
  278. }
  279. });
  280.  
  281. }
  282.  
  283. public void loadDataMultiCategory(){
  284.  
  285.  
  286. RetrofitInterface apiService = ApiUtils.getAPIService();
  287. String auth = AppConstant.AuthValue + " "+DataManager.getInstance().getToken();
  288. Call<CategoryListPromo> call = apiService.getPromoCategories(auth);
  289. call.enqueue(new Callback<CategoryListPromo>() {
  290. @Override
  291. public void onResponse(@NonNull Call<CategoryListPromo> call, @NonNull Response<CategoryListPromo> data) {
  292. if (data.isSuccessful()) {
  293. CategoryListPromo response = data.body();
  294. if (response != null) {
  295. if (response.getSTATUS() == 200) {
  296. mAdapterMultiCategory.setData(data.body().getDATA());
  297. }
  298. }
  299. }
  300. }
  301.  
  302. @Override
  303. public void onFailure(@NonNull Call<CategoryListPromo> call, @NonNull Throwable t) {
  304.  
  305. }
  306. });
  307. }
  308.  
  309. public void loadDataVoucherHome() {
  310. tvLoading.setText("Getting Data Promo");
  311. tvLoadingDesc.setText("Please wait for getting promo");
  312. containerLoading.setVisibility(View.VISIBLE);
  313.  
  314. // showDialogProgress("Getting data");
  315.  
  316. RetrofitInterface apiService = ApiUtils.getAPIService();
  317. String auth = AppConstant.AuthValue+" "+ DataManager.getInstance().getToken();
  318. Call<GettingVoucherList> call = apiService.getVoucherList(auth);
  319. call.enqueue(new Callback<GettingVoucherList>() {
  320. @Override
  321. public void onResponse(@NonNull Call<GettingVoucherList> call,@NonNull Response<GettingVoucherList> data) {
  322. // hideDialogProgress();
  323. containerLoading.setVisibility(View.GONE);
  324. if (data.isSuccessful()) {
  325. GettingVoucherList response = data.body();
  326. if (response != null) {
  327. if (response.getSTATUS() == 200) {
  328. mAdapterPromoVoucher.setData(data.body().getDATA());
  329.  
  330. }else {
  331. containerError.setVisibility(View.VISIBLE);
  332. tvError.setText(response.getMESSAGE());
  333. }
  334. }else {
  335. containerError.setVisibility(View.VISIBLE);
  336. tvError.setText(getString(R.string.no_response));
  337. }
  338. }else {
  339. containerError.setVisibility(View.VISIBLE);
  340. RecentUtils.handleRetrofitError(data.code(), tvError);
  341. }
  342. }
  343.  
  344. @Override
  345. public void onFailure(@NonNull Call<GettingVoucherList> call, @NonNull Throwable t) {
  346. if (!call.isCanceled()) {
  347. // hideDialogProgress();
  348. containerLoading.setVisibility(View.GONE);
  349. containerError.setVisibility(View.VISIBLE);
  350. tvError.setText(getString(R.string.network_error));
  351. }
  352. }
  353. });
  354.  
  355. }
  356.  
  357. public void loadBannerBottom() {
  358. tvLoading.setText("Getting image");
  359. tvLoadingDesc.setText("Pleasae wait fot getting image");
  360. containerLoading.setVisibility(View.VISIBLE);
  361.  
  362. // showDialogProgress("Getting data");
  363.  
  364. RetrofitInterface apiService = ApiUtils.getAPIService();
  365. String auth = AppConstant.AuthValue + " " + DataManager.getInstance().getToken();
  366. Call<GettingBannerStatic> call = apiService.getBannerStatic(auth);
  367. call.enqueue(new Callback<GettingBannerStatic>() {
  368. @Override
  369. public void onResponse(@NonNull Call<GettingBannerStatic> call, @NonNull Response<GettingBannerStatic> data) {
  370. // hideDialogProgress();
  371. if (data.isSuccessful()) {
  372. GettingBannerStatic response = data.body();
  373. if (response != null) {
  374. if (response.getSTATUS() == 200) {
  375. // Log.d("hasilcateg", new Gson().toJson(data.body()));
  376. responseDataImage = response.getDATA();
  377. processImageBanner();
  378. } else {
  379. containerError.setVisibility(View.VISIBLE);
  380. tvError.setText(response.getMESSAGE());
  381.  
  382. }
  383. } else {
  384. containerError.setVisibility(View.VISIBLE);
  385. tvError.setText(getString(R.string.no_response));
  386.  
  387. }
  388. } else {
  389. containerError.setVisibility(View.VISIBLE);
  390. RecentUtils.handleRetrofitError(data.code(), tvError);
  391.  
  392. }
  393.  
  394. }
  395.  
  396. @Override
  397. public void onFailure(@NonNull Call<GettingBannerStatic> call, @NonNull Throwable t) {
  398.  
  399. if (!call.isCanceled()){
  400. // hideDialogProgress();
  401. containerLoading.setVisibility(View.GONE);
  402. containerError.setVisibility(View.VISIBLE);
  403. tvError.setText(getString(R.string.network_error));
  404.  
  405. }
  406. }
  407. });
  408.  
  409. }
  410.  
  411.  
  412.  
  413. public void loadEventSlider() {
  414. tvLoading.setText("Getting Image Promo");
  415. tvLoadingDesc.setText("Please wait for getting about");
  416. containerLoading.setVisibility(View.VISIBLE);
  417.  
  418. // showDialogProgress("Getting data");
  419.  
  420.  
  421. RetrofitInterface apiService = ApiUtils.getAPIService();
  422. String auth = AppConstant.AuthValue + DataManager.getInstance().getToken();
  423. Call<GettingPromoEventList> call = apiService.getPromoEventList(auth);
  424. call.enqueue(new Callback<GettingPromoEventList>() {
  425. @Override
  426. public void onResponse(@NonNull Call<GettingPromoEventList> call, @NonNull Response<GettingPromoEventList> data) {
  427. // hideDialogProgress();
  428. containerLoading.setVisibility(View.GONE);
  429. if (data.isSuccessful()) {
  430. GettingPromoEventList response = data.body();
  431. if (response != null) {
  432. if (response.getSTATUS() == 200) {
  433. // containerContent.setVisibility(View.VISIBLE);
  434. Log.d("hasil", new Gson().toJson(data.body()));
  435. mAdapterEvent.setData(data.body().getDATA());
  436.  
  437. } else {
  438. containerError.setVisibility(View.VISIBLE);
  439. tvError.setText(response.getMESSAGE());
  440. }
  441. } else {
  442. containerError.setVisibility(View.VISIBLE);
  443. tvError.setText(getString(R.string.no_response));
  444. }
  445. } else {
  446. containerError.setVisibility(View.VISIBLE);
  447. RecentUtils.handleRetrofitError(data.code(), tvError);
  448. }
  449. }
  450.  
  451. @Override
  452. public void onFailure(@NonNull Call<GettingPromoEventList> call, @NonNull Throwable t) {
  453. if (!call.isCanceled()) {
  454. // hideDialogProgress();
  455. containerLoading.setVisibility(View.GONE);
  456. containerError.setVisibility(View.VISIBLE);
  457. tvError.setText(getString(R.string.network_error));
  458. }
  459. }
  460. });
  461. }
  462.  
  463.  
  464. private void autoPlay(final ViewPager viewPager) {
  465.  
  466. viewPager.postDelayed(new Runnable() {
  467. @Override
  468. public void run() {
  469. try {
  470. if (mAdapterEvent != null && mAdapterEvent.getCount() > 0) {
  471. int position = currentCount % mAdapterEvent.getCount();
  472. currentCount++;
  473. viewPagerSlider.setCurrentItem(position);
  474. autoPlay(viewPagerSlider);
  475. }
  476. } catch (Exception e) {
  477. Log.e("TAG", "auto scroll pager error.", e);
  478. }
  479. }
  480. }, 2000);
  481. }
  482.  
  483. private void processImageBanner() {
  484. Picasso.get()
  485. .load(responseDataImage.getPromo_image())
  486. .placeholder(R.drawable.placeholder220)
  487. .error(R.drawable.placeholder220)
  488. .into(imgTopPromo);
  489. }
  490.  
  491.  
  492. @Override
  493. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  494. super.onActivityResult(requestCode, resultCode, data);
  495.  
  496. // if (resultCode == Activity.RESULT_OK) {
  497. // if (requestCode == SELECT_DETAIL) {
  498. //
  499. // String result = data.getStringExtra("result");
  500. // mAdapterList.getItems().get(adapterPosition).setStatus(result);
  501. // mAdapterList.notifyItemChanged(adapterPosition);
  502. // }
  503. // }
  504.  
  505.  
  506. }
  507.  
  508. public void showDialogProgress(String message) {
  509. if (message != null) {
  510. dialogProgress = new DialogProgress(getContext(), message, true);
  511. dialogProgress.setCancelable(false);
  512. dialogProgress.show();
  513. } else {
  514. dialogProgress = new DialogProgress(getContext(), "Loading ...", false);
  515. dialogProgress.setCancelable(false);
  516. dialogProgress.show();
  517. }
  518. }
  519.  
  520. public void hideDialogProgress() {
  521. if (dialogProgress != null) {
  522. if (dialogProgress.isShowing()) {
  523. dialogProgress.dismiss();
  524. }
  525. }
  526. }
  527. public void showToast(String val){
  528. if (mToast != null) mToast.cancel();
  529. mToast = Toast.makeText(getContext(), val, Toast.LENGTH_SHORT);
  530. mToast.show();
  531. }
  532.  
  533. public void hideContainer() {
  534. // containerContent.setVisibility(View.GONE);
  535. // containerError.setVisibility(View.GONE);
  536. // containerLoading.setVisibility(View.GONE);
  537. }
  538.  
  539. @Override
  540. public void onPageScrolled(int i, float v, int i1) {
  541.  
  542. }
  543.  
  544. @Override
  545. public void onPageSelected(int i) {
  546. // if (getActivity() != null) {
  547. // for (int i1 = 0; i1 < markIndicator; i1++) {
  548. // imageViewMarkIndicator.get(i1).setImageDrawable(ContextCompat.getDrawable(this.getActivity(), R.drawable. nonselecteditem_dot));
  549. // }
  550. // imageViewMarkIndicator.get(i).setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.selecteditem_dot));
  551. // }
  552.  
  553. }
  554.  
  555. @Override
  556. public void onPageScrollStateChanged(int i) {
  557.  
  558. }
  559. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement