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);
}
}
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);
}
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());
}
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));
}
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);
}
Aggregations