Search in sources :

Example 1 with ClickUtil

use of xyz.zedler.patrick.grocy.util.ClickUtil 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 ClickUtil

use of xyz.zedler.patrick.grocy.util.ClickUtil in project grocy-android by patzly.

the class LoginApiFormFragment method onViewCreated.

@Override
public void onViewCreated(@Nullable View view, @Nullable Bundle savedInstanceState) {
    activity = (MainActivity) requireActivity();
    LoginApiFormFragmentArgs args = LoginApiFormFragmentArgs.fromBundle(requireArguments());
    viewModel = new ViewModelProvider(this, new LoginApiFormViewModel.LoginApiFormViewModelFactory(activity.getApplication(), args)).get(LoginApiFormViewModel.class);
    binding.setFragment(this);
    binding.setClickUtil(new ClickUtil());
    binding.setViewModel(viewModel);
    binding.setFormData(viewModel.getFormData());
    binding.setLifecycleOwner(getViewLifecycleOwner());
    binding.setActivity(activity);
}
Also used : LoginApiFormViewModel(xyz.zedler.patrick.grocy.viewmodel.LoginApiFormViewModel) ClickUtil(xyz.zedler.patrick.grocy.util.ClickUtil) ViewModelProvider(androidx.lifecycle.ViewModelProvider)

Example 3 with ClickUtil

use of xyz.zedler.patrick.grocy.util.ClickUtil in project grocy-android by patzly.

the class LoginIntroFragment method onViewCreated.

@Override
public void onViewCreated(@Nullable View view, @Nullable Bundle savedInstanceState) {
    activity = (MainActivity) requireActivity();
    binding.setFragment(this);
    binding.setClickUtil(new ClickUtil());
}
Also used : ClickUtil(xyz.zedler.patrick.grocy.util.ClickUtil)

Example 4 with ClickUtil

use of xyz.zedler.patrick.grocy.util.ClickUtil in project grocy-android by patzly.

the class LoginApiQrCodeFragment method onViewCreated.

@Override
public void onViewCreated(@Nullable View view, @Nullable Bundle savedInstanceState) {
    activity = (MainActivity) requireActivity();
    args = LoginApiQrCodeFragmentArgs.fromBundle(requireArguments());
    pageStatus = args.getGrocyApiKey() == null ? SCAN_GROCY_KEY : SCAN_HASS_TOKEN;
    binding.setFragment(this);
    binding.setClickUtil(new ClickUtil());
    embeddedFragmentScanner.setScannerVisibilityLive(new MutableLiveData<>(true));
}
Also used : ClickUtil(xyz.zedler.patrick.grocy.util.ClickUtil)

Example 5 with ClickUtil

use of xyz.zedler.patrick.grocy.util.ClickUtil in project grocy-android by patzly.

the class MasterProductCatBarcodesFragment method onViewCreated.

@Override
public void onViewCreated(@Nullable View view, @Nullable Bundle savedInstanceState) {
    activity = (MainActivity) requireActivity();
    clickUtil = new ClickUtil();
    MasterProductFragmentArgs args = MasterProductFragmentArgs.fromBundle(requireArguments());
    viewModel = new ViewModelProvider(this, new MasterProductCatBarcodesViewModel.MasterProductCatBarcodesViewModelFactory(activity.getApplication(), args)).get(MasterProductCatBarcodesViewModel.class);
    binding.setActivity(activity);
    binding.setViewModel(viewModel);
    binding.setFragment(this);
    binding.setLifecycleOwner(getViewLifecycleOwner());
    viewModel.getEventHandler().observeEvent(getViewLifecycleOwner(), event -> {
        if (event.getType() == Event.SNACKBAR_MESSAGE) {
            SnackbarMessage message = (SnackbarMessage) event;
            Snackbar snack = message.getSnackbar(activity, activity.binding.frameMainContainer);
            activity.showSnackbar(snack);
        } else if (event.getType() == Event.NAVIGATE_UP) {
            activity.navigateUp();
        } else if (event.getType() == Event.BOTTOM_SHEET) {
            BottomSheetEvent bottomSheetEvent = (BottomSheetEvent) event;
            activity.showBottomSheet(bottomSheetEvent.getBottomSheet(), event.getBundle());
        }
    });
    infoFullscreenHelper = new InfoFullscreenHelper(binding.frameContainer);
    viewModel.getInfoFullscreenLive().observe(getViewLifecycleOwner(), infoFullscreen -> infoFullscreenHelper.setInfo(infoFullscreen));
    viewModel.getIsLoadingLive().observe(getViewLifecycleOwner(), isLoading -> {
        if (!isLoading) {
            viewModel.setCurrentQueueLoading(null);
        }
    });
    binding.recycler.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
    binding.recycler.setItemAnimator(new DefaultItemAnimator());
    binding.recycler.setAdapter(new MasterPlaceholderAdapter());
    viewModel.getProductBarcodesLive().observe(getViewLifecycleOwner(), barcodes -> {
        if (barcodes == null) {
            return;
        }
        if (barcodes.isEmpty()) {
            InfoFullscreen info = new InfoFullscreen(InfoFullscreen.INFO_EMPTY_PRODUCT_BARCODES);
            viewModel.getInfoFullscreenLive().setValue(info);
        } else {
            viewModel.getInfoFullscreenLive().setValue(null);
        }
        if (binding.recycler.getAdapter() instanceof ProductBarcodeAdapter) {
            ((ProductBarcodeAdapter) binding.recycler.getAdapter()).updateData(barcodes);
        } else {
            binding.recycler.setAdapter(new ProductBarcodeAdapter(barcodes, this, viewModel.getQuantityUnits(), viewModel.getStores()));
        }
    });
    if (savedInstanceState == null) {
        viewModel.loadFromDatabase(true);
    }
    updateUI(savedInstanceState == null);
}
Also used : BottomSheetEvent(xyz.zedler.patrick.grocy.model.BottomSheetEvent) MasterPlaceholderAdapter(xyz.zedler.patrick.grocy.adapter.MasterPlaceholderAdapter) InfoFullscreenHelper(xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) DefaultItemAnimator(androidx.recyclerview.widget.DefaultItemAnimator) MasterProductCatBarcodesViewModel(xyz.zedler.patrick.grocy.viewmodel.MasterProductCatBarcodesViewModel) SnackbarMessage(xyz.zedler.patrick.grocy.model.SnackbarMessage) InfoFullscreen(xyz.zedler.patrick.grocy.model.InfoFullscreen) ClickUtil(xyz.zedler.patrick.grocy.util.ClickUtil) ViewModelProvider(androidx.lifecycle.ViewModelProvider) Snackbar(com.google.android.material.snackbar.Snackbar) ProductBarcodeAdapter(xyz.zedler.patrick.grocy.adapter.ProductBarcodeAdapter)

Aggregations

ClickUtil (xyz.zedler.patrick.grocy.util.ClickUtil)23 ViewModelProvider (androidx.lifecycle.ViewModelProvider)20 BottomSheetEvent (xyz.zedler.patrick.grocy.model.BottomSheetEvent)12 SettingsViewModel (xyz.zedler.patrick.grocy.viewmodel.SettingsViewModel)9 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)8 InfoFullscreenHelper (xyz.zedler.patrick.grocy.helper.InfoFullscreenHelper)8 SnackbarMessage (xyz.zedler.patrick.grocy.model.SnackbarMessage)7 Snackbar (com.google.android.material.snackbar.Snackbar)6 MasterPlaceholderAdapter (xyz.zedler.patrick.grocy.adapter.MasterPlaceholderAdapter)5 InfoFullscreen (xyz.zedler.patrick.grocy.model.InfoFullscreen)5 Bundle (android.os.Bundle)4 DefaultItemAnimator (androidx.recyclerview.widget.DefaultItemAnimator)4 ArrayList (java.util.ArrayList)4 AppBarBehavior (xyz.zedler.patrick.grocy.behavior.AppBarBehavior)4 LayoutInflater (android.view.LayoutInflater)3 MenuItem (android.view.MenuItem)3 View (android.view.View)3 ViewGroup (android.view.ViewGroup)3 NonNull (androidx.annotation.NonNull)3 Nullable (androidx.annotation.Nullable)3