package com.esdut.ciremember;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.SearchView;
import android.widget.TextView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import java.util.List;
public class WordsFragment extends Fragment {
private WordViewModel wordViewModel;
private RecyclerView recyclerView;
private MyAdapter myAdapter1, myAdapter2;
private FloatingActionButton floatingActionButton;
private LiveData<List<Word>> filteredWords;
private static final String VIEW_TYPE_SHP = "view_type_shp";
private static final String IS_USING_CARD_VIEW = "is_using_card_view";
private List<Word> allWords;
private boolean undoAction;
private DividerItemDecoration dividerItemDecoration;
private TextView view;
public WordsFragment() {
// Required empty public constructor
setHasOptionsMenu(true);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.clearData:
AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
builder.setTitle("清空数据");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
wordViewModel.clearWords();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create();
builder.show();
break;
case R.id.switchViewType:
SharedPreferences shp = requireActivity().getSharedPreferences(VIEW_TYPE_SHP, Context.MODE_PRIVATE);
boolean viewType = shp.getBoolean(IS_USING_CARD_VIEW, false);
SharedPreferences.Editor editor = shp.edit();
if (viewType) {
recyclerView.setAdapter(myAdapter1);
recyclerView.addItemDecoration(dividerItemDecoration);
editor.putBoolean(IS_USING_CARD_VIEW, false);
} else {
recyclerView.setAdapter(myAdapter2);
recyclerView.removeItemDecoration(dividerItemDecoration);
editor.putBoolean(IS_USING_CARD_VIEW, true);
}
editor.apply();
break;
case R.id.exit:
requireActivity().finish();
}
return super.onOptionsItemSelected(item);
}
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.main_menu, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.app_bar_search).getActionView();
searchView.setMaxWidth(500);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
String patten = newText.trim();
//移除! 原有的观察
filteredWords.removeObservers(getViewLifecycleOwner());
filteredWords = wordViewModel.findWordsWithPatten(patten);
filteredWords.observe(getViewLifecycleOwner(), new Observer<List<Word>>() {
@Override
public void onChanged(List<Word> words) {
int temp = myAdapter1.getItemCount();
allWords = words;
// myAdapter1.setAllWords(words);
// myAdapter2.setAllWords(words);
if (temp != words.size()) {
// myAdapter1.notifyDataSetChanged();
// myAdapter2.notifyDataSetChanged();
myAdapter1.submitList(words);
myAdapter2.submitList(words);
}
}
});
return true;
}
});
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_words, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
wordViewModel = new ViewModelProvider(this).get(WordViewModel.class);
recyclerView = requireActivity().findViewById(R.id.WordsFragmentView);
recyclerView.setLayoutManager(new LinearLayoutManager(requireActivity()));
myAdapter1 = new MyAdapter(false, wordViewModel);
myAdapter2 = new MyAdapter(true, wordViewModel);
recyclerView.setItemAnimator(new DefaultItemAnimator() {
@Override
public void onAnimationFinished(@NonNull RecyclerView.ViewHolder viewHolder) {
super.onAnimationFinished(viewHolder);
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
if (linearLayoutManager != null) {
int firstPostion = linearLayoutManager.findFirstVisibleItemPosition();
int lastPositon = linearLayoutManager.findLastVisibleItemPosition();
for (int i = firstPostion; i <= lastPositon; i++) {
MyAdapter.MyViewHolder holder = (MyAdapter.MyViewHolder) recyclerView.findViewHolderForAdapterPosition(i);
if (holder != null) {
holder.textViewNumber.setText(String.valueOf(i + 1));
}
}
}
}
});
///
/// 读取SHP
///////////////////////////////////////////////////////////////////////////////////////////////////////
SharedPreferences shp = requireActivity().getSharedPreferences(VIEW_TYPE_SHP, Context.MODE_PRIVATE);
boolean viewType = shp.getBoolean(IS_USING_CARD_VIEW, false);
//SharedPreferences.Editor editor = shp.edit();
/////////////////////////////////////////////////////////////////////////////////////////////////////////