use of xyz.zedler.patrick.grocy.helper.DownloadHelper in project grocy-android by patzly.
the class MainActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
debug = PrefsUtil.isDebuggingEnabled(sharedPrefs);
insertConscrypt();
// DARK MODE
// this has to be placed before super.onCreate(savedInstanceState);
// https://stackoverflow.com/a/53356918
int theme = sharedPrefs.getInt(APPEARANCE.THEME, SETTINGS_DEFAULT.APPEARANCE.THEME);
AppCompatDelegate.setDefaultNightMode(theme);
// LANGUAGE
Locale userLocale = LocaleUtil.getUserLocale(this);
Locale.setDefault(userLocale);
// base
Resources resBase = getBaseContext().getResources();
Configuration configBase = resBase.getConfiguration();
configBase.setLocale(userLocale);
resBase.updateConfiguration(configBase, resBase.getDisplayMetrics());
// app
Resources resApp = getApplicationContext().getResources();
Configuration configApp = resApp.getConfiguration();
configApp.setLocale(userLocale);
resApp.updateConfiguration(configApp, getResources().getDisplayMetrics());
// set localized demo instance
String serverUrl = sharedPrefs.getString(Constants.PREF.SERVER_URL, null);
if (serverUrl != null && serverUrl.contains("demo.grocy.info")) {
List<Language> languages = LocaleUtil.getLanguages(this);
String demoDomain = null;
for (Language language : languages) {
if (language.getCode().equals(userLocale.getLanguage())) {
demoDomain = language.getDemoDomain();
}
}
if (demoDomain != null && !serverUrl.contains(demoDomain)) {
serverUrl = serverUrl.replaceAll("[a-z]+-?[a-z]*\\.demo\\.grocy\\.info", demoDomain);
sharedPrefs.edit().putString(Constants.PREF.SERVER_URL, serverUrl).apply();
}
}
super.onCreate(savedInstanceState);
// UTILS
clickUtil = new ClickUtil();
netUtil = new NetUtil(this);
// WEB
networkReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Fragment navHostFragment = fragmentManager.findFragmentById(R.id.nav_host_fragment);
assert navHostFragment != null;
if (navHostFragment.getChildFragmentManager().getFragments().size() == 0) {
return;
}
getCurrentFragment().updateConnectivity(netUtil.isOnline());
}
};
registerReceiver(networkReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
boolean useTor = sharedPrefs.getBoolean(NETWORK.TOR, SETTINGS_DEFAULT.NETWORK.TOR);
if (useTor && !OrbotHelper.get(this).init()) {
OrbotHelper.get(this).installOrbot(this);
}
// API
updateGrocyApi();
repository = new MainRepository(getApplication());
// VIEWS
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
fragmentManager = getSupportFragmentManager();
NavHostFragment navHostFragment = (NavHostFragment) fragmentManager.findFragmentById(R.id.nav_host_fragment);
assert navHostFragment != null;
navController = navHostFragment.getNavController();
updateStartDestination();
navController.addOnDestinationChangedListener((controller, dest, args) -> {
if (isServerUrlEmpty() || dest.getId() == R.id.shoppingModeFragment || dest.getId() == R.id.onboardingFragment) {
binding.bottomAppBar.setVisibility(View.GONE);
binding.fabMain.hide();
new Handler().postDelayed(() -> setNavBarColor(R.color.background), 10);
} else {
binding.bottomAppBar.setVisibility(View.VISIBLE);
setNavBarColor(R.color.primary);
}
setProperNavBarDividerColor(dest);
});
// BOTTOM APP BAR
binding.bottomAppBar.setNavigationOnClickListener(v -> {
if (clickUtil.isDisabled()) {
return;
}
ViewUtil.startIcon(binding.bottomAppBar.getNavigationIcon());
navController.navigate(R.id.action_global_drawerBottomSheetDialogFragment);
});
scrollBehavior = new BottomAppBarRefreshScrollBehavior(this);
scrollBehavior.setUpBottomAppBar(binding.bottomAppBar);
scrollBehavior.setUpTopScroll(R.id.fab_scroll);
scrollBehavior.setHideOnScroll(true);
Runnable onSuccessConfigLoad = () -> {
String version = sharedPrefs.getString(Constants.PREF.GROCY_VERSION, null);
if (version == null || version.isEmpty()) {
return;
}
ArrayList<String> supportedVersions = new ArrayList<>(Arrays.asList(getResources().getStringArray(R.array.compatible_grocy_versions)));
if (supportedVersions.contains(version)) {
return;
}
// If user already ignored warning, do not display again
String ignoredVersion = sharedPrefs.getString(Constants.PREF.VERSION_COMPATIBILITY_IGNORED, null);
if (ignoredVersion != null && ignoredVersion.equals(version)) {
return;
}
Bundle bundle = new Bundle();
bundle.putString(Constants.ARGUMENT.VERSION, version);
bundle.putStringArrayList(Constants.ARGUMENT.SUPPORTED_VERSIONS, supportedVersions);
showBottomSheet(new CompatibilityBottomSheet(), bundle);
};
if (!isServerUrlEmpty()) {
ConfigUtil.loadInfo(new DownloadHelper(this, TAG), grocyApi, sharedPrefs, onSuccessConfigLoad, null);
}
}
use of xyz.zedler.patrick.grocy.helper.DownloadHelper in project grocy-android by patzly.
the class MasterLocationFragment method onViewCreated.
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
if (isHidden()) {
return;
}
activity = (MainActivity) requireActivity();
// PREFERENCES
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(activity);
debug = PrefsUtil.isDebuggingEnabled(sharedPrefs);
// WEB REQUESTS
dlHelper = new DownloadHelper(activity, TAG);
grocyApi = activity.getGrocyApi();
gson = new Gson();
// INITIALIZE VARIABLES
locations = new ArrayList<>();
locationNames = new ArrayList<>();
editLocation = null;
isRefresh = false;
// INITIALIZE VIEWS
binding.frameMasterLocationCancel.setOnClickListener(v -> activity.onBackPressed());
// swipe refresh
binding.swipeMasterLocation.setProgressBackgroundColorSchemeColor(ContextCompat.getColor(activity, R.color.surface));
binding.swipeMasterLocation.setColorSchemeColors(ContextCompat.getColor(activity, R.color.secondary));
binding.swipeMasterLocation.setOnRefreshListener(this::refresh);
// name
binding.editTextMasterLocationName.setOnFocusChangeListener((View v, boolean hasFocus) -> {
if (hasFocus) {
ViewUtil.startIcon(binding.imageMasterLocationName);
}
});
// description
binding.editTextMasterLocationDescription.setOnFocusChangeListener((View v, boolean hasFocus) -> {
if (hasFocus) {
ViewUtil.startIcon(binding.imageMasterLocationDescription);
}
});
// is freezer
binding.checkboxMasterLocationFreezer.setOnCheckedChangeListener((buttonView, isChecked) -> ViewUtil.startIcon(binding.imageMasterLocationFreezer));
binding.linearMasterLocationFreezer.setOnClickListener(v -> {
ViewUtil.startIcon(binding.imageMasterLocationFreezer);
binding.checkboxMasterLocationFreezer.setChecked(!binding.checkboxMasterLocationFreezer.isChecked());
});
MasterLocationFragmentArgs args = MasterLocationFragmentArgs.fromBundle(requireArguments());
editLocation = args.getLocation();
if (editLocation != null) {
fillWithEditReferences();
} else if (savedInstanceState == null) {
resetAll();
new Handler().postDelayed(() -> activity.showKeyboard(binding.editTextMasterLocationName), 50);
}
if (savedInstanceState == null) {
load();
} else {
restoreSavedInstanceState(savedInstanceState);
}
// UPDATE UI
updateUI((getArguments() == null || getArguments().getBoolean(Constants.ARGUMENT.ANIMATED, true)) && savedInstanceState == null);
}
use of xyz.zedler.patrick.grocy.helper.DownloadHelper in project grocy-android by patzly.
the class MasterQuantityUnitFragment method onViewCreated.
@Override
public void onViewCreated(@Nullable View view, @Nullable Bundle savedInstanceState) {
activity = (MainActivity) requireActivity();
// PREFERENCES
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(activity);
debug = PrefsUtil.isDebuggingEnabled(sharedPrefs);
// WEB
dlHelper = new DownloadHelper(activity, TAG);
grocyApi = activity.getGrocyApi();
gson = new Gson();
pluralUtil = new PluralUtil(activity);
// VARIABLES
quantityUnits = new ArrayList<>();
quantityUnitNames = new ArrayList<>();
editQuantityUnit = null;
isRefresh = false;
// VIEWS
binding.frameMasterQuantityUnitCancel.setOnClickListener(v -> activity.onBackPressed());
// swipe refresh
binding.swipeMasterQuantityUnit.setProgressBackgroundColorSchemeColor(ContextCompat.getColor(activity, R.color.surface));
binding.swipeMasterQuantityUnit.setColorSchemeColors(ContextCompat.getColor(activity, R.color.secondary));
binding.swipeMasterQuantityUnit.setOnRefreshListener(this::refresh);
// name
binding.editTextMasterQuantityUnitName.setOnFocusChangeListener((View v, boolean hasFocus) -> {
if (hasFocus) {
ViewUtil.startIcon(binding.imageMasterQuantityUnitName);
}
});
// name plural
binding.editTextMasterQuantityUnitNamePlural.setOnFocusChangeListener((View v, boolean hasFocus) -> {
if (hasFocus) {
ViewUtil.startIcon(binding.imageMasterQuantityUnitNamePlural);
}
});
binding.linearMasterQuantityUnitForms.setVisibility(pluralUtil.isPluralFormsFieldNecessary() ? View.VISIBLE : View.GONE);
if (pluralUtil.languageRulesNotImplemented()) {
binding.cardPluralFormsNotSupportedInfo.setVisibility(View.VISIBLE);
}
// description
binding.editTextMasterQuantityUnitDescription.setOnFocusChangeListener((View v, boolean hasFocus) -> {
if (hasFocus) {
ViewUtil.startIcon(binding.imageMasterQuantityUnitDescription);
}
});
MasterQuantityUnitFragmentArgs args = MasterQuantityUnitFragmentArgs.fromBundle(requireArguments());
editQuantityUnit = args.getQuantityUnit();
if (editQuantityUnit != null) {
fillWithEditReferences();
} else if (savedInstanceState == null) {
resetAll();
new Handler().postDelayed(() -> activity.showKeyboard(binding.editTextMasterQuantityUnitName), 50);
}
if (savedInstanceState == null) {
load();
} else {
restoreSavedInstanceState(savedInstanceState);
}
// UPDATE UI
updateUI((getArguments() == null || getArguments().getBoolean(Constants.ARGUMENT.ANIMATED, true)) && savedInstanceState == null);
}
use of xyz.zedler.patrick.grocy.helper.DownloadHelper in project grocy-android by patzly.
the class ProductOverviewBottomSheet method onViewCreated.
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
activity = (MainActivity) requireActivity();
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(activity);
pluralUtil = new PluralUtil(activity);
ProductOverviewBottomSheetArgs args = ProductOverviewBottomSheetArgs.fromBundle(requireArguments());
boolean showActions = args.getShowActions();
if (args.getProductDetails() != null) {
productDetails = args.getProductDetails();
product = productDetails.getProduct();
stockItem = new StockItem(productDetails);
} else if (args.getStockItem() != null) {
stockItem = args.getStockItem();
quantityUnit = args.getQuantityUnit();
location = args.getLocation();
product = stockItem.getProduct();
}
// WEB REQUESTS
dlHelper = new DownloadHelper(activity, TAG);
// VIEWS
refreshItems();
binding.textName.setText(product.getName());
// TOOLBAR
boolean isInStock = stockItem.getAmountDouble() > 0;
MenuCompat.setGroupDividerEnabled(binding.toolbar.getMenu(), true);
// disable actions if necessary
binding.toolbar.getMenu().findItem(R.id.action_consume_all).setEnabled(isInStock);
binding.toolbar.getMenu().findItem(R.id.action_consume_spoiled).setEnabled(isInStock && product.getEnableTareWeightHandlingInt() == 0);
binding.toolbar.setOnMenuItemClickListener(item -> {
if (item.getItemId() == R.id.action_add_to_shopping_list) {
navigateDeepLink(R.string.deep_link_shoppingListItemEditFragment, new ShoppingListItemEditFragmentArgs.Builder(Constants.ACTION.CREATE).setProductId(String.valueOf(product.getId())).build().toBundle());
dismiss();
return true;
} else if (item.getItemId() == R.id.action_consume_all) {
activity.getCurrentFragment().performAction(Constants.ACTION.CONSUME_ALL, stockItem);
dismiss();
return true;
} else if (item.getItemId() == R.id.action_consume_spoiled) {
activity.getCurrentFragment().performAction(Constants.ACTION.CONSUME_SPOILED, stockItem);
dismiss();
return true;
} else if (item.getItemId() == R.id.action_edit_product) {
String productId = String.valueOf(product.getId());
navigateDeepLink(R.string.deep_link_masterProductFragment, new MasterProductFragmentArgs.Builder(Constants.ACTION.EDIT).setProductId(productId).build().toBundle());
dismiss();
return true;
}
return false;
});
Chip chipConsume = view.findViewById(R.id.chip_consume);
chipConsume.setVisibility(isInStock ? View.VISIBLE : View.GONE);
chipConsume.setOnClickListener(v -> {
NavHostFragment.findNavController(this).navigate(ProductOverviewBottomSheetDirections.actionProductOverviewBottomSheetDialogFragmentToConsumeFragment().setCloseWhenFinished(true).setProductId(String.valueOf(product.getId())));
dismiss();
});
Chip chipPurchase = view.findViewById(R.id.chip_purchase);
chipPurchase.setOnClickListener(v -> {
NavHostFragment.findNavController(this).navigate(ProductOverviewBottomSheetDirections.actionProductOverviewBottomSheetDialogFragmentToPurchaseFragment().setCloseWhenFinished(true).setProductId(String.valueOf(product.getId())));
dismiss();
});
Chip chipTransfer = view.findViewById(R.id.chip_transfer);
chipTransfer.setVisibility(isInStock && product.getEnableTareWeightHandlingInt() == 0 ? View.VISIBLE : View.GONE);
chipTransfer.setOnClickListener(v -> {
NavHostFragment.findNavController(this).navigate(ProductOverviewBottomSheetDirections.actionProductOverviewBottomSheetDialogFragmentToTransferFragment().setCloseWhenFinished(true).setProductId(String.valueOf(product.getId())));
dismiss();
});
Chip chipInventory = view.findViewById(R.id.chip_inventory);
chipInventory.setOnClickListener(v -> {
NavHostFragment.findNavController(this).navigate(ProductOverviewBottomSheetDirections.actionProductOverviewBottomSheetDialogFragmentToInventoryFragment().setCloseWhenFinished(true).setProductId(String.valueOf(product.getId())));
dismiss();
});
if (!showActions) {
view.findViewById(R.id.container_chips).setVisibility(View.GONE);
}
// DESCRIPTION
Spanned description = product.getDescription() != null ? Html.fromHtml(product.getDescription()) : null;
description = (Spanned) TextUtil.trimCharSequence(description);
if (description != null && !description.toString().isEmpty()) {
binding.cardDescription.setText(description.toString());
} else {
binding.cardDescription.setVisibility(View.GONE);
}
if (!showActions) {
// hide actions when set up with productDetails
binding.linearActionContainer.setVisibility(View.GONE);
// set info menu
binding.toolbar.getMenu().clear();
binding.toolbar.inflateMenu(R.menu.menu_actions_product_overview_info);
}
refreshButtonStates(false);
binding.buttonConsume.setOnClickListener(v -> {
disableActions();
activity.getCurrentFragment().performAction(Constants.ACTION.CONSUME, stockItem);
dismiss();
});
binding.buttonOpen.setOnClickListener(v -> {
disableActions();
activity.getCurrentFragment().performAction(Constants.ACTION.OPEN, stockItem);
dismiss();
});
// tooltips
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
binding.buttonConsume.setTooltipText(activity.getString(R.string.action_consume_one, quantityUnit.getName(), product.getName()));
binding.buttonOpen.setTooltipText(activity.getString(R.string.action_open_one, quantityUnit.getName(), product.getName()));
// TODO: tooltip colors
}
// no margin if description is != null
if (description != null) {
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 0, 0, 0);
binding.linearAmount.setLayoutParams(layoutParams);
}
if (activity.isOnline() && !hasDetails()) {
dlHelper.getProductDetails(product.getId(), details -> {
productDetails = details;
stockItem = new StockItem(productDetails);
refreshButtonStates(true);
refreshItems();
loadPriceHistory();
}).perform(dlHelper.getUuid());
} else if (activity.isOnline() && hasDetails()) {
loadPriceHistory();
}
hideDisabledFeatures();
}
use of xyz.zedler.patrick.grocy.helper.DownloadHelper in project grocy-android by patzly.
the class MasterProductGroupFragment method onViewCreated.
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
if (isHidden()) {
return;
}
activity = (MainActivity) requireActivity();
// PREFERENCES
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(activity);
debug = PrefsUtil.isDebuggingEnabled(sharedPrefs);
// WEB
dlHelper = new DownloadHelper(activity, TAG);
grocyApi = activity.getGrocyApi();
gson = new Gson();
// VARIABLES
productGroups = new ArrayList<>();
productGroupNames = new ArrayList<>();
editProductGroup = null;
isRefresh = false;
// VIEWS
binding.frameMasterProductGroupCancel.setOnClickListener(v -> activity.onBackPressed());
// swipe refresh
binding.swipeMasterProductGroup.setProgressBackgroundColorSchemeColor(ContextCompat.getColor(activity, R.color.surface));
binding.swipeMasterProductGroup.setColorSchemeColors(ContextCompat.getColor(activity, R.color.secondary));
binding.swipeMasterProductGroup.setOnRefreshListener(this::refresh);
// name
binding.editTextMasterProductGroupName.setOnFocusChangeListener((View v, boolean hasFocus) -> {
if (hasFocus) {
ViewUtil.startIcon(binding.imageMasterProductGroupName);
}
});
// description
binding.editTextMasterProductGroupDescription.setOnFocusChangeListener((View v, boolean hasFocus) -> {
if (hasFocus) {
ViewUtil.startIcon(binding.imageMasterProductGroupDescription);
}
});
MasterProductGroupFragmentArgs args = MasterProductGroupFragmentArgs.fromBundle(requireArguments());
editProductGroup = args.getProductGroup();
if (editProductGroup != null) {
fillWithEditReferences();
} else if (savedInstanceState == null) {
resetAll();
new Handler().postDelayed(() -> activity.showKeyboard(binding.editTextMasterProductGroupName), 50);
}
if (savedInstanceState == null) {
load();
} else {
restoreSavedInstanceState(savedInstanceState);
}
// UPDATE UI
updateUI((getArguments() == null || getArguments().getBoolean(Constants.ARGUMENT.ANIMATED, true)) && savedInstanceState == null);
}
Aggregations