Search in sources :

Example 1 with DownloadHelper

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);
    }
}
Also used : Locale(java.util.Locale) Configuration(android.content.res.Configuration) ArrayList(java.util.ArrayList) Fragment(androidx.fragment.app.Fragment) NavHostFragment(androidx.navigation.fragment.NavHostFragment) BaseFragment(xyz.zedler.patrick.grocy.fragment.BaseFragment) BottomSheetDialogFragment(com.google.android.material.bottomsheet.BottomSheetDialogFragment) CompatibilityBottomSheet(xyz.zedler.patrick.grocy.fragment.bottomSheetDialog.CompatibilityBottomSheet) Language(xyz.zedler.patrick.grocy.model.Language) NetUtil(xyz.zedler.patrick.grocy.util.NetUtil) MainRepository(xyz.zedler.patrick.grocy.repository.MainRepository) ClickUtil(xyz.zedler.patrick.grocy.util.ClickUtil) NavHostFragment(androidx.navigation.fragment.NavHostFragment) SSLContext(javax.net.ssl.SSLContext) Context(android.content.Context) IntentFilter(android.content.IntentFilter) Bundle(android.os.Bundle) Handler(android.os.Handler) Intent(android.content.Intent) BroadcastReceiver(android.content.BroadcastReceiver) DownloadHelper(xyz.zedler.patrick.grocy.helper.DownloadHelper) BottomAppBarRefreshScrollBehavior(xyz.zedler.patrick.grocy.behavior.BottomAppBarRefreshScrollBehavior) Resources(android.content.res.Resources)

Example 2 with DownloadHelper

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);
}
Also used : SharedPreferences(android.content.SharedPreferences) Gson(com.google.gson.Gson) Handler(android.os.Handler) DownloadHelper(xyz.zedler.patrick.grocy.helper.DownloadHelper) View(android.view.View)

Example 3 with DownloadHelper

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);
}
Also used : PluralUtil(xyz.zedler.patrick.grocy.util.PluralUtil) SharedPreferences(android.content.SharedPreferences) Gson(com.google.gson.Gson) Handler(android.os.Handler) DownloadHelper(xyz.zedler.patrick.grocy.helper.DownloadHelper) View(android.view.View)

Example 4 with DownloadHelper

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();
}
Also used : BottomSheetDialog(com.google.android.material.bottomsheet.BottomSheetDialog) Chip(com.google.android.material.chip.Chip) LinearLayout(android.widget.LinearLayout) Bundle(android.os.Bundle) Spanned(android.text.Spanned) TypeToken(com.google.gson.reflect.TypeToken) FragmentBottomsheetProductOverviewBinding(xyz.zedler.patrick.grocy.databinding.FragmentBottomsheetProductOverviewBinding) NonNull(androidx.annotation.NonNull) PriceHistoryEntry(xyz.zedler.patrick.grocy.model.PriceHistoryEntry) NumUtil(xyz.zedler.patrick.grocy.util.NumUtil) MainActivity(xyz.zedler.patrick.grocy.activity.MainActivity) Dialog(android.app.Dialog) HashMap(java.util.HashMap) TextUtil(xyz.zedler.patrick.grocy.util.TextUtil) MasterProductFragmentArgs(xyz.zedler.patrick.grocy.fragment.MasterProductFragmentArgs) ProductDetails(xyz.zedler.patrick.grocy.model.ProductDetails) DownloadHelper(xyz.zedler.patrick.grocy.helper.DownloadHelper) ArrayList(java.util.ArrayList) AmountUtil(xyz.zedler.patrick.grocy.util.AmountUtil) QuantityUnit(xyz.zedler.patrick.grocy.model.QuantityUnit) R(xyz.zedler.patrick.grocy.R) Gson(com.google.gson.Gson) View(android.view.View) Build(android.os.Build) StockItem(xyz.zedler.patrick.grocy.model.StockItem) MenuCompat(androidx.core.view.MenuCompat) NavHostFragment(androidx.navigation.fragment.NavHostFragment) PREF(xyz.zedler.patrick.grocy.util.Constants.PREF) PluralUtil(xyz.zedler.patrick.grocy.util.PluralUtil) BezierCurveChart(xyz.zedler.patrick.grocy.view.BezierCurveChart) LayoutInflater(android.view.LayoutInflater) Constants(xyz.zedler.patrick.grocy.util.Constants) Location(xyz.zedler.patrick.grocy.model.Location) ViewGroup(android.view.ViewGroup) Nullable(androidx.annotation.Nullable) SharedPreferences(android.content.SharedPreferences) Product(xyz.zedler.patrick.grocy.model.Product) Type(java.lang.reflect.Type) DateUtil(xyz.zedler.patrick.grocy.util.DateUtil) Html(android.text.Html) ViewTreeObserver(android.view.ViewTreeObserver) PreferenceManager(androidx.preference.PreferenceManager) ShoppingListItemEditFragmentArgs(xyz.zedler.patrick.grocy.fragment.ShoppingListItemEditFragmentArgs) Store(xyz.zedler.patrick.grocy.model.Store) Collections(java.util.Collections) DecelerateInterpolator(android.view.animation.DecelerateInterpolator) ValueAnimator(android.animation.ValueAnimator) PluralUtil(xyz.zedler.patrick.grocy.util.PluralUtil) MasterProductFragmentArgs(xyz.zedler.patrick.grocy.fragment.MasterProductFragmentArgs) StockItem(xyz.zedler.patrick.grocy.model.StockItem) DownloadHelper(xyz.zedler.patrick.grocy.helper.DownloadHelper) Chip(com.google.android.material.chip.Chip) Spanned(android.text.Spanned) LinearLayout(android.widget.LinearLayout)

Example 5 with DownloadHelper

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);
}
Also used : SharedPreferences(android.content.SharedPreferences) Gson(com.google.gson.Gson) Handler(android.os.Handler) DownloadHelper(xyz.zedler.patrick.grocy.helper.DownloadHelper) View(android.view.View)

Aggregations

DownloadHelper (xyz.zedler.patrick.grocy.helper.DownloadHelper)6 Handler (android.os.Handler)5 View (android.view.View)5 Gson (com.google.gson.Gson)5 SharedPreferences (android.content.SharedPreferences)4 Bundle (android.os.Bundle)2 NavHostFragment (androidx.navigation.fragment.NavHostFragment)2 ArrayList (java.util.ArrayList)2 PluralUtil (xyz.zedler.patrick.grocy.util.PluralUtil)2 ValueAnimator (android.animation.ValueAnimator)1 Dialog (android.app.Dialog)1 BroadcastReceiver (android.content.BroadcastReceiver)1 Context (android.content.Context)1 Intent (android.content.Intent)1 IntentFilter (android.content.IntentFilter)1 Configuration (android.content.res.Configuration)1 Resources (android.content.res.Resources)1 Build (android.os.Build)1 Html (android.text.Html)1 Spanned (android.text.Spanned)1