Created
June 8, 2016 04:43
-
-
Save AAverin/ab6f057999fe88814489de4c449c16be to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LI01Fragment extends BaseFragment { | |
public final static int DISCOUNTED_TAB = 99; | |
SlidingTabLayout slidingTabs; | |
ViewPager viewPager; | |
ArrayList<CarsPagerItem> mTabs; | |
LinearLayout blankSlate; | |
DrawerLayout filterDrawer; | |
ActionBarDrawerToggle drawerToggle; | |
TextView pickCity, pickDate, dropCity, dropDate; | |
LinearLayout datePicker; | |
boolean loadRequested = false; | |
boolean isFilterApplied = false; | |
boolean isFilterOpened = false; | |
boolean isInited = false; | |
private CarsListActivity activity; | |
private int[] TABS = new int[]{CarTypes.COMPACT, CarTypes.MIDDLE, CarTypes.LUX, CarTypes.CROSSOVER}; | |
private GenericCallback<CarsTotal> carsTotalCallback = new GenericCallback<CarsTotal>() { | |
@Override | |
public void success(CarsTotal carsTotal, Response response) { | |
if (activity == null || !activity.isActive()) | |
return; | |
loadRequested = false; | |
Logger.log(this, " carsTotalCallback was successful "); | |
DataManager.addCarsToList_myRentACar(baseContext, carsTotal.cars); | |
HashMap<String, String> json = new HashMap<String, String>(); | |
json.put(BaseActivity.EVENT_ENTRIES.EVENT_PARAM_CARS_SEARCH_PICKUP_CITY, baseContext.searchFilter.pickupCity.name); | |
json.put(BaseActivity.EVENT_ENTRIES.EVENT_PARAM_CARS_SEARCH_RETURN_CITY, baseContext.searchFilter.dropoffCity.name); | |
json.put(BaseActivity.EVENT_ENTRIES.EVENT_PARAM_CARS_SEARCH_RANGE, String.valueOf(TimeUtils.daysBetween(baseContext.searchFilter.pickupDate, baseContext.searchFilter.dropoffDate))); | |
activity.trackGlobalEvent(BaseActivity.EVENT_ENTRIES.EVENT_NAME_CARS_SEARCHED, json); | |
init(); | |
activity.hideLoading(); | |
showRateDialog(); | |
} | |
@Override | |
public void _failure(boolean genericHandeled, RetrofitError error) { | |
if (activity == null || !activity.isActive()) | |
return; | |
loadRequested = false; | |
activity.hideLoading(); | |
} | |
}; | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
contentView = (ViewGroup) inflater.inflate(R.layout.f_li01, container, false); | |
return contentView; | |
} | |
@Override | |
public String getFragmentName() { | |
return "LI01Fragment"; | |
} | |
@Override | |
public void onActivityCreated(Bundle savedInstanceState) { | |
super.onActivityCreated(savedInstanceState); | |
activity = (CarsListActivity) getBaseActivity(); | |
carsTotalCallback.setActivity(activity); | |
setHasOptionsMenu(true); | |
viewPager = (ViewPager) contentView.findViewById(R.id.li01_pager); | |
slidingTabs = (SlidingTabLayout) contentView.findViewById(R.id.li01_slidingtabs); | |
blankSlate = (LinearLayout) contentView.findViewById(R.id.li01_blank_slate); | |
pickCity = (TextView) contentView.findViewById(R.id.opt_city_pick); | |
dropCity = (TextView) contentView.findViewById(R.id.opt_city_drop); | |
pickDate = (TextView) contentView.findViewById(R.id.opt_date_pick); | |
dropDate = (TextView) contentView.findViewById(R.id.opt_date_drop); | |
activity.cityDateSelector = new CityDateSelector(activity); | |
datePicker = (LinearLayout) contentView.findViewById(R.id.li01_datepicker); | |
datePicker.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
activity.cityDateSelector.setDates(baseContext.searchFilter.pickupDate, baseContext.searchFilter.dropoffDate); | |
activity.cityDateSelector.setCities(baseContext.searchFilter.pickupCity, baseContext.searchFilter.dropoffCity); | |
DialogFragment dialog = activity.cityDateSelector.getAsDialog(new DialogInterface.OnDismissListener() { | |
@Override | |
public void onDismiss(DialogInterface dialog) { | |
if (baseContext.searchFilter.isDirty) { | |
initOpts(); | |
loadList(); | |
} | |
City pickupCity = activity.cityDateSelector.getPickupCity(); | |
City dropOffCity = activity.cityDateSelector.getDropoffCity(); | |
Date pickDate = activity.cityDateSelector.getPickDate(); | |
Date dropDate = activity.cityDateSelector.getDropDate(); | |
HashMap<String, String> data = new HashMap<String, String>(); | |
data.put(BaseActivity.EVENT_ENTRIES.EVENT_PARAM_DATES_CITIES_DIALOG_PICKUP_CITY, pickupCity.name); | |
data.put(BaseActivity.EVENT_ENTRIES.EVENT_PARAM_DATES_CITIES_DIALOG_DROPOFF_CITY, dropOffCity.name); | |
data.put(BaseActivity.EVENT_ENTRIES.EVENT_PARAM_DATES_CITIES_DIALOG_PICKUP_DATE, baseContext.timeUtils.format_dateApi(pickDate)); | |
data.put(BaseActivity.EVENT_ENTRIES.EVENT_PARAM_DATES_CITIES_DIALOG_DROPOFF_DATE, baseContext.timeUtils.format_dateApi(dropDate)); | |
data.put(BaseActivity.EVENT_ENTRIES.EVENT_PARAM_DATES_CITIES_DIALOG_CITIES_DIFFER, String.valueOf(pickupCity.id == dropOffCity.id)); | |
long deep = TimeUtils.daysBetween(new Date(), pickDate); | |
long duration = TimeUtils.daysBetween(pickDate, dropDate); | |
if (duration == 0) { | |
duration = 1; | |
} | |
data.put(BaseActivity.EVENT_ENTRIES.EVENT_PARAM_DATES_CITIES_DIALOG_DEEP, String.valueOf(deep)); | |
data.put(BaseActivity.EVENT_ENTRIES.EVENT_PARAM_DATES_CITIES_DIALOG_DURATION, String.valueOf(duration)); | |
activity.trackGlobalEvent(BaseActivity.EVENT_ENTRIES.EVENT_NAME_DATES_CITIES_DIALOG_CLOSED, data); | |
} | |
}); | |
dialog.show(getActivity().getSupportFragmentManager(), "DATES"); | |
} | |
}); | |
initOpts(); | |
activity.carFilter = CarFilter.getInstance(baseContext, contentView, new CarFilter.CarFilterCallback() { | |
@Override | |
public void onFilterApplied() { | |
applyFilter(); | |
closeFilter(); | |
} | |
}); | |
filterDrawer = (DrawerLayout) contentView.findViewById(R.id.li01_drawer); | |
filterDrawer.setFocusableInTouchMode(false); | |
drawerToggle = new ActionBarDrawerToggle(getActivity(), filterDrawer, android.R.drawable.ic_menu_agenda, R.string.label_opened, R.string.label_closed) { | |
@Override | |
public void onDrawerClosed(View drawerView) { | |
// getActivity().getActionBar().setTitle(null); | |
isFilterOpened = false; | |
activity.invalidateOptionsMenu(); | |
super.onDrawerClosed(drawerView); | |
} | |
@Override | |
public void onDrawerOpened(View drawerView) { | |
// getActivity().getActionBar().setTitle(R.string.label_filter); | |
activity.carFilter.onFilterOpen(); | |
activity.invalidateOptionsMenu(); | |
super.onDrawerOpened(drawerView); | |
} | |
}; | |
filterDrawer.setDrawerListener(drawerToggle); | |
filterDrawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); | |
loadList(); | |
} | |
private void showRateDialog() { | |
if (!baseContext.permanentCache.rate_rated && !baseContext.permanentCache.rate_never) { | |
boolean shouldDisplayRate; | |
if (baseContext.permanentCache.rate_later) { | |
shouldDisplayRate = baseContext.permanentCache.applicationLaunchesCount % 5 == 0; | |
} else if (baseContext.permanentCache.wasCardOrdered) { | |
baseContext.permanentCache.wasCardOrdered = false; | |
shouldDisplayRate = true; | |
} else { | |
shouldDisplayRate = baseContext.permanentCache.applicationLaunchesCount == 3; | |
} | |
if (shouldDisplayRate) { | |
RateDialog dialog = RateDialog.getInstance(new RateDialog.RateDialogCallbacks() { | |
@Override | |
public void onRate() { | |
final String appPackageName = activity.getPackageName(); // getPackageName() from Context or Activity object | |
try { | |
activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName))); | |
} catch (android.content.ActivityNotFoundException anfe) { | |
activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName))); | |
} | |
baseContext.permanentCache.rate_rated = true; | |
baseContext.permanentCache.save(); | |
} | |
@Override | |
public void onLater() { | |
baseContext.permanentCache.rate_later = true; | |
baseContext.permanentCache.save(); | |
} | |
@Override | |
public void onNever() { | |
baseContext.permanentCache.rate_never = true; | |
baseContext.permanentCache.save(); | |
} | |
}); | |
dialog.displayDialog(getBaseActivity()); | |
} | |
} | |
} | |
@Override | |
public void onPause() { | |
super.onPause(); | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
if (loadRequested) { | |
load(); | |
} | |
} | |
private void loadList() { | |
if (activity != null && activity.isActive()) { | |
activity.uiHandler.post(new Runnable() { | |
@Override | |
public void run() { | |
load(); | |
} | |
}); | |
} | |
} | |
private void initOpts() { | |
if (BuildSettings.isCrashlyticsEnabled) | |
Crashlytics.log("initOpts searchFilter=" + baseContext.searchFilter + "; pickupCity=" + baseContext.searchFilter.pickupCity); | |
if (baseContext.searchFilter.pickupCity == null || | |
baseContext.searchFilter.dropoffCity == null) { | |
BaseActivity.restart(baseContext, 100); | |
return; | |
} | |
pickCity.setText(baseContext.isRULocale() ? baseContext.searchFilter.pickupCity.name : baseContext.searchFilter.pickupCity.name_en); | |
dropCity.setText(baseContext.isRULocale() ? baseContext.searchFilter.dropoffCity.name : baseContext.searchFilter.dropoffCity.name_en); | |
pickDate.setText(baseContext.timeUtils.format_dateShort(baseContext.searchFilter.pickupDate)); | |
dropDate.setText(baseContext.timeUtils.format_dateShort(baseContext.searchFilter.dropoffDate)); | |
baseContext.searchFilter.isDirty = false; | |
} | |
@Override | |
public void onConfigurationChanged(Configuration newConfig) { | |
super.onConfigurationChanged(newConfig); | |
drawerToggle.onConfigurationChanged(newConfig); | |
} | |
@Override | |
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { | |
inflater.inflate(R.menu.filter, menu); | |
super.onCreateOptionsMenu(menu, inflater); | |
} | |
@Override | |
public void onPrepareOptionsMenu(Menu menu) { | |
super.onPrepareOptionsMenu(menu); | |
MenuItem filter = menu.findItem(R.id.menu_filter); | |
MenuItem reset = menu.findItem(R.id.menu_reset); | |
MenuItem locale = menu.findItem(R.id.menu_locale); | |
MenuItem about = menu.findItem(R.id.menu_dealer_message); | |
MenuItem report = menu.findItem(R.id.menu_report); | |
// MenuItem supportChat = menu.findItem(R.id.menu_support_chat); | |
if (isFilterOpened) { | |
filter.setVisible(false); | |
locale.setVisible(false); | |
about.setVisible(false); | |
report.setVisible(false); | |
// supportChat.setVisible(false); | |
reset.setVisible(true); | |
} else { | |
reset.setVisible(false); | |
filter.setVisible(true); | |
locale.setVisible(true); | |
about.setVisible(true); | |
report.setVisible(true); | |
// supportChat.setVisible(Config.isTaifunoEnabled); | |
if (isFilterApplied) { | |
filter.setIcon(R.drawable.ic_filter_active); | |
} else { | |
filter.setIcon(R.drawable.ic_filter); | |
} | |
if (baseContext.isRULocale()) { | |
locale.setTitle(R.string.label_english); | |
} else { | |
locale.setTitle(R.string.label_russian); | |
} | |
} | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
if (item.getItemId() == R.id.menu_filter) { | |
if (filterDrawer.isDrawerOpen(Gravity.RIGHT)) { | |
closeFilter(); | |
} else { | |
openFilter(); | |
} | |
} | |
if (item.getItemId() == R.id.menu_dealer_message) { | |
Dealers.Dealer dealer = Dealers.getById(Dealers.MyRentACarMe.id); | |
String dealerName = getResources().getString(dealer.nameStringId); | |
String dealerInfo = getResources().getString(dealer.infoStringId); | |
String dealerMessage = getResources().getString(dealer.messageStringId); | |
DealerInfoDialog dialog = DealerInfoDialog.newInstance(dealerName, dealerInfo, dealerMessage); | |
dialog.displayDialog(activity); | |
} | |
if (item.getItemId() == R.id.menu_report) { | |
activity.shareLogcat(); | |
} | |
if (item.getItemId() == R.id.menu_locale) { | |
if (baseContext.isRULocale()) { | |
baseContext.changeLocale("en", "US"); | |
} else { | |
baseContext.changeLocale("ru", "RUS"); | |
} | |
BaseActivity.restart(baseContext); | |
} | |
if (item.getItemId() == R.id.menu_reset) { | |
if (isFilterOpened) { | |
activity.carFilter.reset(); | |
} | |
} | |
// if (item.getItemId() == R.id.menu_support_chat) { | |
// AlertDialog.Builder builder = new AlertDialog.Builder(activity) | |
// .setTitle(R.string.label_start_support_chat) | |
// .setPositiveButton(R.string.label_yes, new DialogInterface.OnClickListener() { | |
// @Override | |
// public void onClick(DialogInterface dialog, int which) { | |
// Taifuno.getInstance().showChat(); | |
// } | |
// }) | |
// .setNegativeButton(R.string.label_no, new DialogInterface.OnClickListener() { | |
// @Override | |
// public void onClick(DialogInterface dialog, int which) { | |
// | |
// } | |
// }); | |
// | |
// builder.create().show(); | |
// } | |
return super.onOptionsItemSelected(item); | |
} | |
void load() { | |
if (activity != null && activity.isActive()) { | |
loadRequested = true; | |
activity.showLoading(); | |
baseContext.restService.search( | |
baseContext.searchFilter.utf8, | |
baseContext.searchFilter.van ? "1" : "", | |
baseContext.searchFilter.pickupCity.id, | |
baseContext.searchFilter.dropoffCity.id, | |
baseContext.searchFilter.getPickupDateString(baseContext.timeUtils), | |
baseContext.searchFilter.getDropoffDateString(baseContext.timeUtils), | |
baseContext.searchFilter.gearBox > 0 ? String.valueOf(baseContext.searchFilter.gearBox) : "", | |
baseContext.searchFilter.costMin, | |
baseContext.searchFilter.costMax, | |
baseContext.searchFilter.engineMin, | |
baseContext.searchFilter.engineMax, | |
baseContext.searchFilter.drives, | |
baseContext.searchFilter.insurances, | |
baseContext.searchFilter.consumptionMin, | |
baseContext.searchFilter.consumptionMax, | |
baseContext.searchFilter.year, | |
baseContext.searchFilter.colors, | |
baseContext.searchFilter.cabrio ? "1" : "", | |
baseContext.searchFilter.eco ? "1" : "", | |
baseContext.searchFilter.discountedOnly ? "1": "", | |
baseContext.searchFilter.services, | |
baseContext.searchFilter.limit, | |
carsTotalCallback | |
); | |
} | |
} | |
void init() { | |
isInited = true; | |
Resources res = baseContext.getResources(); | |
String title; | |
int color; | |
int dividerColor; | |
mTabs = new ArrayList<CarsPagerItem>(); | |
for (Integer tabType : TABS) { | |
dividerColor = res.getColor(R.color.Divider_Default); | |
switch (tabType) { | |
case DISCOUNTED_TAB: | |
if (baseContext.db.hasDiscountedCars()) { | |
title = res.getString(R.string.car_discounts); | |
color = res.getColor(R.color.TabIndicator_Discounts); | |
mTabs.add(new CarsPagerItem(tabType, title, color, dividerColor)); | |
} | |
break; | |
case CarTypes.COMPACT: | |
if (baseContext.db.hasCarsOfType(tabType)) { | |
title = res.getString(R.string.car_compact); | |
color = res.getColor(R.color.TabIndicator_Compact); | |
mTabs.add(new CarsPagerItem(tabType, title, color, dividerColor)); | |
} | |
break; | |
case CarTypes.MIDDLE: | |
if (baseContext.db.hasCarsOfType(tabType)) { | |
title = res.getString(R.string.car_middle); | |
color = res.getColor(R.color.TabIndicator_Middle); | |
mTabs.add(new CarsPagerItem(tabType, title, color, dividerColor)); | |
} | |
break; | |
case CarTypes.LUX: | |
if (baseContext.db.hasCarsOfType(tabType)) { | |
title = res.getString(R.string.car_lux); | |
color = res.getColor(R.color.TabIndicator_Lux); | |
mTabs.add(new CarsPagerItem(tabType, title, color, dividerColor)); | |
} | |
break; | |
case CarTypes.CROSSOVER: | |
if (baseContext.db.hasCarsOfType(tabType)) { | |
title = res.getString(R.string.car_crossover); | |
color = res.getColor(R.color.TabIndicator_Crossover); | |
mTabs.add(new CarsPagerItem(tabType, title, color, dividerColor)); | |
} | |
break; | |
} | |
} | |
if (mTabs.size() == 0) { | |
viewPager.setVisibility(View.GONE); | |
blankSlate.setVisibility(View.VISIBLE); | |
} else { | |
viewPager.setVisibility(View.VISIBLE); | |
blankSlate.setVisibility(View.GONE); | |
} | |
viewPager.setAdapter(new CarsPageAdapter(getChildFragmentManager(), activity.isTablet(), mTabs)); | |
viewPager.setOffscreenPageLimit(6); | |
slidingTabs.setCustomTabView(R.layout.i_carslist_tab, R.id.i_carslist_tab_text); | |
slidingTabs.setViewPager(viewPager); | |
slidingTabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() { | |
@Override | |
public int getIndicatorColor(int position) { | |
return mTabs.get(position).getIndicatorColor(); | |
} | |
@Override | |
public int getDividerColor(int position) { | |
return mTabs.get(position).getDividerColor(); | |
} | |
}); | |
} | |
@Override | |
public boolean handleBackPress() { | |
if (filterDrawer.isDrawerOpen(Gravity.RIGHT)) { | |
activity.carFilter.onFilterCancel(); | |
closeFilter(); | |
return true; | |
} | |
return false; | |
} | |
private void applyFilter() { | |
activity.carFilter.onFilterApply(); | |
if (activity.carFilter.isDirty) { | |
isFilterApplied = !baseContext.searchFilter.isDefault(); | |
activity.carFilter.isDirty = false; | |
loadList(); | |
} | |
} | |
private void openFilter() { | |
isFilterOpened = true; | |
activity.invalidateOptionsMenu(); | |
filterDrawer.openDrawer(Gravity.RIGHT); | |
} | |
private void closeFilter() { | |
isFilterOpened = false; | |
activity.invalidateOptionsMenu(); | |
filterDrawer.closeDrawer(Gravity.RIGHT); | |
activity.trackGlobalEvent(BaseActivity.EVENT_ENTRIES.EVENT_NAME_FILTER_CLOSED, null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment