Skip to content

Commit

Permalink
Apply various suggestions and clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
niknetniko committed Oct 26, 2023
1 parent 58af57f commit fad11f4
Show file tree
Hide file tree
Showing 71 changed files with 344 additions and 517 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public Association() {
// Moshi uses this!
}

/** @noinspection ProtectedMemberInFinalClass*/
protected Association(Parcel in) {
abbreviation = in.readString();
name = in.readString();
Expand Down Expand Up @@ -86,7 +87,7 @@ public int describeContents() {
return 0;
}

public static final Creator<Association> CREATOR = new Creator<Association>() {
public static final Creator<Association> CREATOR = new Creator<>() {
@Override
public Association createFromParcel(Parcel in) {
return new Association(in);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,14 @@ public Duration getCacheDuration() {
}
}

@NonNull
public static Request<AssociationList> createRawAssociationRequest(@NonNull Context context) {
return new RawRequest(context);
}

@NonNull
public static Request<List<Association>> createListRequest(@NonNull Context context) {
return new RawRequest(context)
.map(AssociationList::getAssociations);
}

public static Request<Pair<AssociationMap, List<EventItem>>> createItemFilteredEventRequest(@NonNull Context context, EventFilter filter) {
return createRawAssociationRequest(context)
return new RawRequest(context)
.andThen((Function<AssociationList, Request<Pair<List<EventItem>, Set<String>>>>) data -> {
EventRequest.Filter newFilter = filter.toRequestFilter(context, data.getAssociations());
Set<String> requestedAssociations = newFilter.getRequestedAssociations();
Expand All @@ -144,7 +139,7 @@ public static Request<Pair<AssociationMap, List<EventItem>>> createItemFilteredE
}

public static Request<Pair<AssociationMap, List<Event>>> createFilteredEventRequest(@NonNull Context context) {
return createRawAssociationRequest(context)
return new RawRequest(context)
.andThen((Function<AssociationList, Request<Pair<List<Event>, Set<String>>>>) data -> {
EventFilter eventFilter = new EventFilter();
EventRequest.Filter newFilter = eventFilter.toRequestFilter(context, data.getAssociations());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,6 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(event, header, isLastOfSection);
}

void markAsLastOfSection() {
this.isLastOfSection = true;
}

public OffsetDateTime getDate() {
if (isItem()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@

/**
* Get the events for all associations.
*
* The general event flow it like this:
*
* <p>
* The general event flow is like this:
* <p>
* 1. We get the list of associations from the server.
* 2. Use the filter to update the blacklist.
* 3. Use the blacklist to get a whitelist.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.IntentCompat;
import androidx.core.text.util.LinkifyCompat;
import androidx.core.view.WindowCompat;

Expand Down Expand Up @@ -80,8 +81,8 @@ protected void onCreate(Bundle savedInstanceState) {
boolean hasDescription = true;

// Get data from saved instance, or from intent.
event = getIntent().getParcelableExtra(PARCEL_EVENT);
Association association = getIntent().getParcelableExtra(PARCEL_ASSOCIATION);
event = IntentCompat.getParcelableExtra(getIntent(), PARCEL_EVENT, Event.class);
Association association = IntentCompat.getParcelableExtra(getIntent(), PARCEL_ASSOCIATION, Association.class);
assert event != null;
assert association != null;

Expand All @@ -95,7 +96,7 @@ protected void onCreate(Bundle savedInstanceState) {

if (event.getDescription() != null && !event.getDescription().trim().isEmpty()) {
binding.description.setText(event.getDescription());
LinkifyCompat.addLinks(binding.description, Linkify.ALL);
LinkifyCompat.addLinks(binding.description, Linkify.EMAIL_ADDRESSES | Linkify.WEB_URLS);
} else {
hasDescription = false;
binding.eventDescriptionBlock.setVisibility(View.GONE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@

package be.ugent.zeus.hydra.association.event;

import java.util.Map;
import java.util.Objects;

/**
* @author Niko Strijbol
*/
public final class EventList {

/** @noinspection unused*/
private EventPage page;

public EventList() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
*/
public final class EventPage {

/** @noinspection unused*/
private List<Event> entries;

public List<Event> getEntries() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.MenuProvider;
import androidx.core.view.ViewCompat;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
Expand All @@ -59,6 +61,7 @@
import com.google.android.material.datepicker.MaterialDatePicker;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.textfield.TextInputLayout;
import org.jetbrains.annotations.NotNull;

import static be.ugent.zeus.hydra.common.utils.FragmentUtils.requireBaseActivity;

Expand All @@ -83,12 +86,6 @@ public class EventFragment extends Fragment implements MainActivity.ScheduledRem
private TextInputLayout startTime;
private TextInputLayout endTime;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Expand All @@ -99,6 +96,27 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

requireActivity().addMenuProvider(new MenuProvider() {
@Override
public void onCreateMenu(@NonNull @NotNull Menu menu, @NonNull @NotNull MenuInflater menuInflater) {
menuInflater.inflate(R.menu.menu_main_events, menu);
requireBaseActivity(EventFragment.this).tintToolbarIcons(menu, R.id.action_refresh, R.id.action_search);
}

@Override
public boolean onMenuItemSelected(@NonNull @NotNull MenuItem menuItem) {
int itemId = menuItem.getItemId();
if (itemId == R.id.action_refresh) {
viewModel.onRefresh();
return true;
} else if (itemId == R.id.action_search) {
showSheet();
return true;
}
return false;
}
}, getViewLifecycleOwner(), Lifecycle.State.RESUMED);

bottomSheet = requireActivity().findViewById(R.id.bottom_sheet);
// Load the options.
getLayoutInflater().inflate(R.layout.item_search_filter, bottomSheet, true);
Expand Down Expand Up @@ -221,26 +239,6 @@ private void onError(Throwable throwable) {
.show();
}

@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_main_events, menu);
requireBaseActivity(this).tintToolbarIcons(menu, R.id.action_refresh, R.id.action_search);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int itemId = item.getItemId();
if (itemId == R.id.action_refresh) {
viewModel.onRefresh();
return true;
} else if (itemId == R.id.action_search) {
showSheet();
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
public void onRemovalScheduled() {
hideExternalViews();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SearchView;
import androidx.core.view.MenuProvider;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
Expand All @@ -43,6 +44,7 @@
import be.ugent.zeus.hydra.common.arch.observers.ProgressObserver;
import be.ugent.zeus.hydra.common.arch.observers.SuccessObserver;
import com.google.android.material.snackbar.Snackbar;
import org.jetbrains.annotations.NotNull;

import static be.ugent.zeus.hydra.common.utils.FragmentUtils.requireBaseActivity;

Expand All @@ -57,12 +59,6 @@ public class AssociationSelectionPreferenceFragment extends Fragment {

private final SearchableAssociationsAdapter adapter = new SearchableAssociationsAdapter();

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Expand All @@ -73,6 +69,27 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

requireActivity().addMenuProvider(new MenuProvider() {
@Override
public void onCreateMenu(@NonNull @NotNull Menu menu, @NonNull @NotNull MenuInflater menuInflater) {
menuInflater.inflate(R.menu.menu_pref_selectors, menu);
requireBaseActivity(AssociationSelectionPreferenceFragment.this).tintToolbarIcons(menu, R.id.action_select_all, R.id.action_select_none);
}

@Override
public boolean onMenuItemSelected(@NonNull @NotNull MenuItem menuItem) {
int itemId = menuItem.getItemId();
if (itemId == R.id.action_select_all) {
adapter.setAllChecked(true);
return true;
} else if (itemId == R.id.action_select_none) {
adapter.setAllChecked(false);
return true;
}
return false;
}
}, getViewLifecycleOwner());

RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
SearchView searchView = view.findViewById(R.id.search_view);
Expand All @@ -89,26 +106,6 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
model.getData().observe(getViewLifecycleOwner(), new ProgressObserver<>(view.findViewById(R.id.progress_bar)));
}

@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
inflater.inflate(R.menu.menu_pref_selectors, menu);
requireBaseActivity(this).tintToolbarIcons(menu, R.id.action_select_all, R.id.action_select_none);
super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int itemId = item.getItemId();
if (itemId == R.id.action_select_all) {
adapter.setAllChecked(true);
return true;
} else if (itemId == R.id.action_select_none) {
adapter.setAllChecked(false);
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
public void onPause() {
super.onPause();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class ExtendedSparseArray<E> extends SparseArray<E> implements Iterable<E
@NonNull
@Override
public Iterator<E> iterator() {
return new Iterator<E>() {
return new Iterator<>() {
private int current;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,4 @@ protected void onActive() {
protected void loadData() {
this.loadData(Bundle.EMPTY);
}

@FunctionalInterface
public interface OnRefreshStartListener {

/**
* Starts when the refresh begins.
*/
void onRefreshStart();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
public abstract class ErrorObserver<D> implements Observer<Result<D>> {

public static <D> ErrorObserver<D> with(Consumer<RequestException> consumer) {
return new ErrorObserver<D>() {
return new ErrorObserver<>() {
@Override
protected void onError(RequestException throwable) {
consumer.accept(throwable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
public abstract class EventObserver<D> implements Observer<Event<D>> {

public static <D> EventObserver<D> with(Consumer<D> consumer) {
return new EventObserver<D>() {
return new EventObserver<>() {
@Override
protected void onUnhandled(D data) {
consumer.accept(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
public abstract class PartialErrorObserver<D> implements Observer<Result<D>> {

public static <D> PartialErrorObserver<D> with(Consumer<RequestException> consumer) {
return new PartialErrorObserver<D>() {
return new PartialErrorObserver<>() {
@Override
protected void onPartialError(RequestException throwable) {
consumer.accept(throwable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
public abstract class SuccessObserver<D> implements Observer<Result<D>> {

public static <D> SuccessObserver<D> with(Consumer<D> onSuccess) {
return new SuccessObserver<D>() {
return new SuccessObserver<>() {
@Override
protected void onSuccess(@NonNull D data) {
onSuccess.accept(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,17 @@
package be.ugent.zeus.hydra.common.converter;

import android.util.Pair;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.squareup.moshi.*;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.squareup.moshi.*;

/**
* @author Niko Strijbol
*/
Expand Down
Loading

0 comments on commit fad11f4

Please sign in to comment.