Skip to content

Commit

Permalink
Migrate additional fragments and activities to jetpack bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
mesinger authored and Altonss committed Nov 7, 2024
1 parent 015b7e7 commit ce3b10a
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 32 deletions.
16 changes: 12 additions & 4 deletions app/src/main/java/de/grobox/transportr/about/AboutFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ import android.widget.Button
import android.widget.TextView
import de.grobox.transportr.R
import de.grobox.transportr.TransportrFragment
import de.grobox.transportr.databinding.FragmentAboutBinding


class AboutFragment : TransportrFragment() {

private var _binding: FragmentAboutBinding? = null
private val binding get() = _binding!!

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val v = inflater.inflate(R.layout.fragment_about, container, false)
_binding = FragmentAboutBinding.inflate(inflater, container, false)

// add app name and version
val versionName = try {
Expand All @@ -44,17 +48,21 @@ class AboutFragment : TransportrFragment() {
} catch (e: NameNotFoundException) {
"?.?"
}
val appNameVersion = v.findViewById<TextView>(R.id.appNameVersion)
val appNameVersion = binding.appNameVersion
appNameVersion.text = "${getString(R.string.app_name)} $versionName"

// website button
val websiteButton = v.findViewById<Button>(R.id.websiteButton)
val websiteButton = binding.websiteButton
websiteButton.setOnClickListener {
val launchBrowser = Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.website) + getString(R.string.website_source_app)))
startActivity(launchBrowser)
}

return v
return binding.root
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,47 @@ import de.grobox.transportr.R
import de.grobox.transportr.TransportrFragment
import de.grobox.transportr.about.ContributorAdapter.ContributorViewHolder
import de.grobox.transportr.about.ContributorGroupAdapter.ContributorGroupViewHolder
import de.grobox.transportr.databinding.FragmentContributorsBinding
import de.grobox.transportr.databinding.FragmentTranslatorsBinding

class ContributorFragment : TransportrFragment() {
private var _binding: FragmentContributorsBinding? = null
private val binding get() = _binding!!

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val v = inflater.inflate(R.layout.fragment_contributors, container, false)
_binding = FragmentContributorsBinding.inflate(inflater, container, false)

val list = v.findViewById<RecyclerView>(R.id.list)
val list = binding.list
list.layoutManager = LinearLayoutManager(context)
list.adapter = ContributorGroupAdapter(CONTRIBUTORS)

return v
return binding.root
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}

class TranslatorsFragment : TransportrFragment() {
private var _binding: FragmentTranslatorsBinding? = null
private val binding get() = _binding!!

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val v = inflater.inflate(R.layout.fragment_translators, container, false)
_binding = FragmentTranslatorsBinding.inflate(inflater, container, false)

val list = v.findViewById<RecyclerView>(R.id.list)
val list = binding.list
list.layoutManager = LinearLayoutManager(context)
list.adapter = ContributorGroupAdapter(LANGUAGES)

return v
return binding.root
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@

import de.grobox.transportr.R;
import de.grobox.transportr.TransportrActivity;
import de.grobox.transportr.databinding.ActivityDeparturesBinding;
import de.grobox.transportr.locations.WrapLocation;
import de.grobox.transportr.ui.LceAnimator;
import de.grobox.transportr.ui.TimeDateFragment;
Expand All @@ -77,6 +78,7 @@ public class DeparturesActivity extends TransportrActivity

private enum SearchState {INITIAL, TOP, BOTTOM}

private ActivityDeparturesBinding binding;
private ProgressBar progressBar;
private View errorLayout;
private TextView errorText;
Expand All @@ -101,16 +103,17 @@ public void onFinish() {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityDeparturesBinding.inflate(getLayoutInflater());

Intent intent = getIntent();
location = (WrapLocation) intent.getSerializableExtra(WRAP_LOCATION);
if (location == null || location.getLocation() == null)
throw new IllegalArgumentException("No Location");

setContentView(R.layout.activity_departures);
setContentView(binding.getRoot());

// Toolbar
Toolbar toolbar = findViewById(R.id.toolbar);
Toolbar toolbar = binding.toolbar;
setSupportActionBar(toolbar);
toolbar.setSubtitle(location.getName());
ActionBar actionBar = getSupportActionBar();
Expand All @@ -120,7 +123,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
}

// Swipe to Refresh
swipe = findViewById(R.id.swipe);
swipe = binding.swipe;
swipe.setColorSchemeResources(R.color.accent);
swipe.setDirection(SwipyRefreshLayoutDirection.BOTH);
swipe.setDistanceToTriggerSync(getDragDistance(this));
Expand All @@ -129,7 +132,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) {

// Departures List
adapter = new DepartureAdapter();
list = findViewById(R.id.list);
list = binding.list;
list.setVisibility(INVISIBLE);
list.setAdapter(adapter);
list.setLayoutManager(new LinearLayoutManager(this));
Expand All @@ -139,8 +142,8 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
Loader<QueryDeparturesResult> loader = getSupportLoaderManager().initLoader(LOADER_DEPARTURES, args, this);

// Progress Bar and Error View
progressBar = findViewById(R.id.progressBar);
errorLayout = findViewById(R.id.errorLayout);
progressBar = binding.progressBar;
errorLayout = binding.errorLayout;
errorText = errorLayout.findViewById(R.id.errorText);
errorLayout.findViewById(R.id.errorButton).setOnClickListener(view -> {
LceAnimator.showLoading(progressBar, list, errorLayout);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import de.grobox.transportr.AppComponent
import de.grobox.transportr.R
import de.grobox.transportr.TransportrApplication
import de.grobox.transportr.data.locations.FavoriteLocation.FavLocationType
import de.grobox.transportr.databinding.FragmentSpecialLocationBinding
import de.grobox.transportr.favorites.trips.FavoriteTripListener
import de.grobox.transportr.locations.LocationView
import de.grobox.transportr.locations.LocationsViewModel
Expand All @@ -54,6 +55,9 @@ abstract class SpecialLocationFragment : DialogFragment(), LocationView.Location
protected lateinit var viewModel: LocationsViewModel
var listener: FavoriteTripListener? = null

private var _binding: FragmentSpecialLocationBinding? = null;
private val binding get() = _binding!!

private lateinit var loc: LocationView

@get:StringRes
Expand All @@ -72,10 +76,11 @@ abstract class SpecialLocationFragment : DialogFragment(), LocationView.Location
protected abstract fun viewModel(): LocationsViewModel // a method to init when activity has been created

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val v = inflater.inflate(R.layout.fragment_special_location, container)
_binding = FragmentSpecialLocationBinding.inflate(inflater, container, false)
val v = binding.root

// Initialize LocationView
loc = v.findViewById(R.id.location_input)
loc = binding.locationInput
loc.setHint(hint)
loc.setLocationViewListener(this)

Expand Down Expand Up @@ -121,4 +126,8 @@ abstract class SpecialLocationFragment : DialogFragment(), LocationView.Location

override fun onLocationCleared(type: FavLocationType) {}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import de.grobox.transportr.TransportrFragment;
import de.grobox.transportr.data.locations.HomeLocation;
import de.grobox.transportr.data.locations.WorkLocation;
import de.grobox.transportr.databinding.FragmentFavoritesBinding;
import de.grobox.transportr.favorites.locations.HomePickerDialogFragment;
import de.grobox.transportr.favorites.locations.WorkPickerDialogFragment;
import de.grobox.transportr.locations.WrapLocation;
Expand All @@ -57,18 +58,19 @@ public abstract class FavoriteTripsFragment<VM extends SavedSearchesViewModel> e
@Inject protected ViewModelProvider.Factory viewModelFactory;
protected VM viewModel;

private FragmentFavoritesBinding binding;
private ProgressBar progressBar;
private RecyclerView list;
private FavoriteTripAdapter adapter;
private boolean listAlreadyUpdated = false;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_favorites, container, false);
binding = FragmentFavoritesBinding.inflate(inflater, container, false);

progressBar = v.findViewById(R.id.progressBar);
progressBar = binding.progressBar;

list = v.findViewById(R.id.favorites);
list = binding.favorites;
adapter = new FavoriteTripAdapter(this);
list.setHasFixedSize(false);
list.setAdapter(adapter);
Expand All @@ -79,7 +81,13 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
viewModel.getWork().observe(getViewLifecycleOwner(), this::onWorkLocationChanged);
viewModel.getFavoriteTrips().observe(getViewLifecycleOwner(), this::onFavoriteTripsChanged);

return v;
return binding.getRoot();
}

@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}

abstract protected VM getViewModel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@

import de.grobox.transportr.R;
import de.grobox.transportr.TransportrFragment;
import de.grobox.transportr.databinding.FragmentLocationBinding;
import de.grobox.transportr.departures.DeparturesActivity;
import de.grobox.transportr.departures.DeparturesLoader;
import de.grobox.transportr.locations.ReverseGeocoder.ReverseGeocoderCallback;
Expand Down Expand Up @@ -87,6 +88,7 @@ public class LocationFragment extends TransportrFragment
private WrapLocation location;
private final LineAdapter adapter = new LineAdapter();

private FragmentLocationBinding binding;
private ImageView locationIcon;
private TextView locationName;
private TextView locationInfo;
Expand All @@ -113,28 +115,28 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
location = (WrapLocation) args.getSerializable(WRAP_LOCATION);
if (location == null) throw new IllegalArgumentException("No location");

View v = inflater.inflate(R.layout.fragment_location, container, false);
binding = FragmentLocationBinding.inflate(inflater, container, false);
getComponent().inject(this);

if (getActivity() == null) throw new IllegalStateException();
viewModel = new ViewModelProvider(requireActivity(), viewModelFactory).get(MapViewModel.class);
viewModel.nearbyStationsFound().observe(getViewLifecycleOwner(), found -> onNearbyStationsLoaded());

// Location
locationIcon = v.findViewById(R.id.locationIcon);
locationName = v.findViewById(R.id.locationName);
locationIcon = binding.locationIcon;
locationName = binding.locationName;
locationIcon.setOnClickListener(view -> onLocationClicked());
locationName.setOnClickListener(view -> onLocationClicked());

// Lines
linesLayout = v.findViewById(R.id.linesLayout);
linesLayout = binding.linesLayout;
linesLayout.setVisibility(GONE);
linesLayout.setAdapter(adapter);
linesLayout.setLayoutManager(new LinearLayoutManager(getContext(), HORIZONTAL, false));
linesLayout.setOnClickListener(view -> onLocationClicked());

// Location Info
locationInfo = v.findViewById(R.id.locationInfo);
locationInfo = binding.locationInfo;
showLocation();

if (location.getLocation().type == COORD) {
Expand All @@ -143,7 +145,7 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
}

// Departures
Button departuresButton = v.findViewById(R.id.departuresButton);
Button departuresButton = binding.departuresButton;
if (location.hasId()) {
departuresButton.setOnClickListener(view -> {
Intent intent = new Intent(getContext(), DeparturesActivity.class);
Expand All @@ -155,27 +157,34 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
}

// Nearby Stations
nearbyStationsButton = v.findViewById(R.id.nearbyStationsButton);
nearbyStationsProgress = v.findViewById(R.id.nearbyStationsProgress);
nearbyStationsButton = binding.nearbyStationsButton;
nearbyStationsProgress = binding.nearbyStationsProgress;
nearbyStationsButton.setOnClickListener(view -> {
nearbyStationsButton.setVisibility(INVISIBLE);
nearbyStationsProgress.setVisibility(VISIBLE);
IntentUtils.findNearbyStations(getContext(), location);
});

// Share Location
Button shareButton = v.findViewById(R.id.shareButton);
Button shareButton = binding.shareButton;
shareButton.setOnClickListener(view -> startGeoIntent(getActivity(), location));

// Overflow Button
ImageButton overflowButton = v.findViewById(R.id.overflowButton);
ImageButton overflowButton = binding.overflowButton;
overflowButton.setOnClickListener(view -> new LocationPopupMenu(getContext(), view, location).show());

View v = binding.getRoot();
v.getViewTreeObserver().addOnGlobalLayoutListener(this);

return v;
}

@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}

@Override
public void onGlobalLayout() {
// set peek distance to show view header
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class DirectionsActivity : TransportrActivity(), OnOffsetChangedListener {
supportFragmentManager.beginTransaction()
.replace(R.id.fragmentContainer, TripsFragment(), TripsFragment.TAG)
.commit()
binding.fragmentContainer.requestFocus()
}

private fun processIntent(intent: Intent?) {
Expand Down

0 comments on commit ce3b10a

Please sign in to comment.