Moved toolbar management in common fragment

This commit is contained in:
Daniele Verducci (Slimpenguin) 2024-02-24 16:45:52 +01:00
parent 32b005599a
commit 572095a7c3
3 changed files with 50 additions and 41 deletions

View File

@ -45,43 +45,12 @@ public class GeofavoriteListFragment extends GeofavoritesFragment implements Sor
private SwipeRefreshLayout swipeRefresh;
private GeofavoriteAdapter geofavoriteAdapter;
private Toolbar toolbar;
private MaterialCardView homeToolbar;
private SearchView searchView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_geofavorite_list, container, false);
// Setup toolbar/searchbar
toolbar = v.findViewById(R.id.toolbar);
homeToolbar = v.findViewById(R.id.home_toolbar);
searchView = v.findViewById(R.id.search_view);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String query) {
onSearch(query);
return false;
}
});
searchView.setOnCloseListener(() -> {
if (toolbar.getVisibility() == VISIBLE && TextUtils.isEmpty(searchView.getQuery())) {
updateToolbars(true);
return true;
}
return false;
});
homeToolbar.setOnClickListener(view -> updateToolbars(false));
// Setup list
int sortRule = SettingsManager.getGeofavoriteListSortBy(requireContext());
@ -196,13 +165,4 @@ public class GeofavoriteListFragment extends GeofavoritesFragment implements Sor
}
}
private void updateToolbars(boolean disableSearch) {
homeToolbar.setVisibility(disableSearch ? VISIBLE : GONE);
toolbar.setVisibility(disableSearch ? GONE : VISIBLE);
if (disableSearch) {
searchView.setQuery(null, true);
}
searchView.setIconified(disableSearch);
}
}

View File

@ -1,9 +1,13 @@
package it.danieleverducci.nextcloudmaps.fragments;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
@ -13,10 +17,13 @@ import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.AppCompatImageButton;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import com.google.android.material.card.MaterialCardView;
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException;
import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException;
import com.nextcloud.android.sso.helper.SingleAccountHelper;
@ -40,6 +47,9 @@ public abstract class GeofavoritesFragment extends Fragment {
private final String TAG = "GeofavoritesFragment";
protected GeofavoritesFragmentViewModel mGeofavoritesFragmentViewModel;
private View toolbar;
private View homeToolbar;
private SearchView searchView;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
@ -70,6 +80,33 @@ public abstract class GeofavoritesFragment extends Fragment {
View userBadgeContainer = view.findViewById(R.id.user_badge_container);
userBadgeContainer.setOnClickListener(v -> showSwitchAccountDialog());
// Setup toolbar/searchbar
toolbar = view.findViewById(R.id.toolbar);
homeToolbar = view.findViewById(R.id.home_toolbar);
searchView = view.findViewById(R.id.search_view);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String query) {
onSearch(query);
return false;
}
});
searchView.setOnCloseListener(() -> {
if (toolbar.getVisibility() == VISIBLE && TextUtils.isEmpty(searchView.getQuery())) {
updateToolbars(true);
return true;
}
return false;
});
homeToolbar.setOnClickListener(v -> updateToolbars(false));
// Set user badge (async)
Handler h = new Handler();
h.post(() -> {
@ -154,4 +191,13 @@ public abstract class GeofavoritesFragment extends Fragment {
ad.show();
}
private void updateToolbars(boolean disableSearch) {
homeToolbar.setVisibility(disableSearch ? VISIBLE : GONE);
toolbar.setVisibility(disableSearch ? GONE : VISIBLE);
if (disableSearch) {
searchView.setQuery(null, true);
}
searchView.setIconified(disableSearch);
}
}

View File

@ -19,7 +19,10 @@
<androidx.appcompat.widget.SearchView
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:background="@color/translucent">
</androidx.appcompat.widget.SearchView>
</androidx.appcompat.widget.Toolbar>