getContext -> requireContext
LocationBasedServices/mowesta/feature%2Fandroid-app%2Fshow-weather-data This commit looks good

This commit is contained in:
Jan Reichl
2020-01-19 23:07:13 +01:00
parent 4e28fa4c45
commit 71d0f4c8a3
2 changed files with 11 additions and 12 deletions
@@ -45,14 +45,14 @@ public class ChooseWeatherFragment extends AbstractObservableFragment {
final AutoCompleteTextView acLocationSearch = view.findViewById(R.id.ac_location_search);
setLoadingSpinner(view.findViewById(R.id.loadingSpinnerChooseWeather));
final LocationAutocompleteAdapter autocompleteAdapter = new LocationAutocompleteAdapter(getContext());
final LocationAutocompleteAdapter autocompleteAdapter = new LocationAutocompleteAdapter(requireContext());
acLocationSearch.setAdapter(autocompleteAdapter);
acLocationSearch.setOnItemClickListener((parent, view1, position, id) -> {
final LocationHierarchy item = autocompleteAdapter.getItem(position);
chooseLocation(item.getId());
InputMethodManager in = (InputMethodManager) Objects.requireNonNull(getContext()).getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(Objects.requireNonNull(Objects.requireNonNull(getActivity()).getCurrentFocus()).getWindowToken(), 0);
InputMethodManager in = (InputMethodManager) requireContext().getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(Objects.requireNonNull(requireActivity().getCurrentFocus()).getWindowToken(), 0);
});
acLocationSearch.setOnFocusChangeListener((v, hasFocus) -> {
final AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) v;
@@ -84,16 +84,16 @@ public class ChooseWeatherFragment extends AbstractObservableFragment {
}
private void useLocation(View view) {
boolean permissionGranted = PermissionChecker.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) == PermissionChecker.PERMISSION_GRANTED &&
PermissionChecker.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PermissionChecker.PERMISSION_GRANTED;
boolean permissionGranted = PermissionChecker.checkSelfPermission(requireContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) == PermissionChecker.PERMISSION_GRANTED &&
PermissionChecker.checkSelfPermission(requireContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PermissionChecker.PERMISSION_GRANTED;
if (!permissionGranted) {
Log.d(TAG, "Requesting permissions.");
ActivityCompat.requestPermissions(getActivity(), new String[] {android.Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 1);
ActivityCompat.requestPermissions(requireActivity(), new String[] {android.Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 1);
return;
}
LocationManager locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
LocationManager locationManager = (LocationManager) requireContext().getSystemService(Context.LOCATION_SERVICE);
addDisposable("location", Observable.just(LocationManager.GPS_PROVIDER, LocationManager.NETWORK_PROVIDER, LocationManager.PASSIVE_PROVIDER)
.doOnNext(s -> startLoading("location"))
.filter(locationManager::isProviderEnabled)
@@ -142,7 +142,7 @@ public class ChooseWeatherFragment extends AbstractObservableFragment {
}
private void chooseLocation(Long locationId) {
FragmentTransaction ft = Objects.requireNonNull(getActivity()).getSupportFragmentManager().beginTransaction();
FragmentTransaction ft = requireActivity().getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
final ShowWeatherFragment showWeatherFragment = new ShowWeatherFragment();
@@ -156,7 +156,7 @@ public class ChooseWeatherFragment extends AbstractObservableFragment {
private void chooseLocation(Location location) {
stopLoading("location");
FragmentTransaction ft = Objects.requireNonNull(getActivity()).getSupportFragmentManager().beginTransaction();
FragmentTransaction ft = requireActivity().getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
final ShowWeatherFragment showWeatherFragment = new ShowWeatherFragment();
@@ -147,8 +147,7 @@ public class ShowWeatherFragment extends AbstractObservableFragment {
}
private void toggleFavorite(View v) {
((ImageView) v).setImageDrawable(ContextCompat.getDrawable(Objects.requireNonNull(getContext()),
(favorite = !favorite) ? R.drawable.ic_favorite_red_24dp : R.drawable.ic_favorite_border_black_24dp));
((ImageView) v).setImageDrawable(ContextCompat.getDrawable(requireContext(), (favorite = !favorite) ? R.drawable.ic_favorite_red_24dp : R.drawable.ic_favorite_border_black_24dp));
}
private String getNameFromLocation(Map<String, String> names) {
@@ -188,7 +187,7 @@ public class ShowWeatherFragment extends AbstractObservableFragment {
calendarTo.add(Calendar.HOUR_OF_DAY, condition.getDuration());
// Set item views based on your views and data model
final Context context = Objects.requireNonNull(getContext());
final Context context = requireContext();
final Resources resources = Objects.requireNonNull(getResources());
viewHolder.teAirTemperature.setText(resources.getString(R.string.weather_unit_celsius, Math.round(condition.getTemperature())));
viewHolder.teSnow.setText(resources.getString(R.string.weather_unit_cm, Math.round(condition.getSnowDepth() * 100)));