content: package com.ulicae.cinelog.android.v2.fragments.wishlist.list;
  
  import android.content.DialogInterface;
  import android.os.Bundle;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.ListView;
  
  import androidx.annotation.NonNull;
  import androidx.annotation.Nullable;
  import androidx.appcompat.app.AlertDialog;
  import androidx.appcompat.widget.SearchView;
  import androidx.fragment.app.Fragment;
  
  import com.google.android.material.floatingactionbutton.FloatingActionButton;
  import com.ulicae.cinelog.KinoApplication;
  import com.ulicae.cinelog.R;
  import com.ulicae.cinelog.android.v2.activities.MainActivity;
  import com.ulicae.cinelog.data.dto.data.WishlistDataDto;
  import com.ulicae.cinelog.data.services.wishlist.SerieWishlistService;
  import com.ulicae.cinelog.data.services.wishlist.WishlistService;
  
  import java.util.ArrayList;
  import java.util.List;
  
  /**
   * CineLog Copyright 2022 Pierre Rognon
   * <p>
   * <p>
   * This file is part of CineLog.
   * CineLog is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   * <p>
   * CineLog is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   * GNU General Public License for more details.
   * <p>
   * You should have received a copy of the GNU General Public License
   * along with CineLog. If not, see <https://www.gnu.org/licenses/>.
   */
  public abstract class WishlistFragment extends Fragment {
  
      WishlistListAdapter listAdapter;
  
      List<WishlistDataDto> dataDtos;
  
      protected WishlistService service;
  
      protected int actionToItem;
  
      @Override
      public void onViewCreated(@NonNull View view,
                                @Nullable Bundle savedInstanceState) {
          createListView(1);
  
          FloatingActionButton fab = ((MainActivity) requireActivity()).getFab();
          fab.setOnClickListener(v -> onFabClick());
          fab.setImageResource(R.drawable.add_kino);
  
          SearchView searchView = ((MainActivity) requireActivity()).getSearchView();
          searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
              @Override
              public boolean onQueryTextSubmit(String query) {
                  return false;
              }
  
              @Override
              public boolean onQueryTextChange(String newText) {
                  listAdapter.getFilter().filter(newText);
                  return false;
              }
          });
          searchView.setVisibility(View.VISIBLE);
      }
  
      protected abstract void onFabClick();
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          if (!item.hasSubMenu()) {
              createListView(item.getItemId());
          }
  
          return super.onOptionsItemSelected(item);
      }
  
      protected void createListView(int orderId) {
          if (getKinoList() != null) {
              //date added
              dataDtos = getResults(orderId);
  
              listAdapter = new WishlistListAdapter(getContext(), dataDtos);
              getKinoList().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                  public boolean onItemLongClick(final AdapterView<?> view, View parent, final int position, long rowId) {
                      AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                      builder.setMessage(R.string.delete_kino_dialog)
                              .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                                  public void onClick(DialogInterface dialog, int id) {
                                      // Delete the kino
                                      WishlistDataDto wishlistDataDto = listAdapter.getItem(position);
                                      listAdapter.remove(wishlistDataDto);
  
                                      //noinspection unchecked
                                      service.delete(wishlistDataDto);
  
                                      listAdapter.notifyDataSetChanged();
                                  }
                              })
                              .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                                  public void onClick(DialogInterface dialog, int id) {
                                      // User cancelled the dialog
                                  }
                              });
                      builder.create().show();
                      return true;
                  }
              });
              getKinoList().setOnItemClickListener((view, parent, position, rowId) -> {
                  ((MainActivity) requireActivity()).navigateToWishlistItem(listAdapter.getItem(position), actionToItem);
              });
  
              getKinoList().setAdapter(listAdapter);
          }
      }
  
      protected abstract ListView getKinoList();
  
      protected List<WishlistDataDto> getResults(int order) {
          List<WishlistDataDto> fetchedDtos;
          fetchedDtos = service.getAll();
  
          return new ArrayList<>(fetchedDtos);
      }
  }
  