mirror of https://github.com/M66B/FairEmail.git
parent
be1865387d
commit
c15943144b
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,267 @@
|
||||
package eu.faircode.email;
|
||||
|
||||
/*
|
||||
This file is part of FairEmail.
|
||||
|
||||
FairEmail 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.
|
||||
|
||||
FairEmail 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.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2018-2021 by Marcel Bokhorst (M66B)
|
||||
*/
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
import androidx.lifecycle.Lifecycle;
|
||||
import androidx.lifecycle.LifecycleObserver;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.lifecycle.OnLifecycleEvent;
|
||||
import androidx.recyclerview.widget.DiffUtil;
|
||||
import androidx.recyclerview.widget.ListUpdateCallback;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class AdapterNavSearch extends RecyclerView.Adapter<AdapterNavSearch.ViewHolder> {
|
||||
private Context context;
|
||||
private LifecycleOwner owner;
|
||||
private FragmentManager manager;
|
||||
private LayoutInflater inflater;
|
||||
|
||||
private boolean expanded = true;
|
||||
private List<EntitySearch> items = new ArrayList<>();
|
||||
|
||||
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
|
||||
private View view;
|
||||
private ImageView ivItem;
|
||||
private ImageView ivBadge;
|
||||
private TextView tvItem;
|
||||
private TextView tvItemExtra;
|
||||
private ImageView ivExtra;
|
||||
private ImageView ivWarning;
|
||||
|
||||
ViewHolder(View itemView) {
|
||||
super(itemView);
|
||||
|
||||
view = itemView.findViewById(R.id.clItem);
|
||||
ivItem = itemView.findViewById(R.id.ivItem);
|
||||
ivBadge = itemView.findViewById(R.id.ivBadge);
|
||||
tvItem = itemView.findViewById(R.id.tvItem);
|
||||
tvItemExtra = itemView.findViewById(R.id.tvItemExtra);
|
||||
ivExtra = itemView.findViewById(R.id.ivExtra);
|
||||
ivWarning = itemView.findViewById(R.id.ivWarning);
|
||||
}
|
||||
|
||||
private void wire() {
|
||||
view.setOnClickListener(this);
|
||||
view.setOnLongClickListener(this);
|
||||
}
|
||||
|
||||
private void unwire() {
|
||||
view.setOnClickListener(null);
|
||||
view.setOnLongClickListener(null);
|
||||
}
|
||||
|
||||
private void bindTo(EntitySearch search) {
|
||||
ivItem.setImageResource(R.drawable.twotone_search_24);
|
||||
ivBadge.setVisibility(View.GONE);
|
||||
tvItem.setText(search.name);
|
||||
|
||||
|
||||
tvItemExtra.setVisibility(View.GONE);
|
||||
ivExtra.setVisibility(View.GONE);
|
||||
ivWarning.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
int pos = getAdapterPosition();
|
||||
if (pos == RecyclerView.NO_POSITION)
|
||||
return;
|
||||
|
||||
EntitySearch search = items.get(pos);
|
||||
if (search == null)
|
||||
return;
|
||||
|
||||
try {
|
||||
JSONObject json = new JSONObject(search.data);
|
||||
BoundaryCallbackMessages.SearchCriteria criteria =
|
||||
BoundaryCallbackMessages.SearchCriteria.fromJSON(json);
|
||||
FragmentMessages.search(
|
||||
context, owner, manager,
|
||||
-1L, -1L, false, criteria);
|
||||
} catch (Throwable ex) {
|
||||
Log.e(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onLongClick(View v) {
|
||||
int pos = getAdapterPosition();
|
||||
if (pos == RecyclerView.NO_POSITION)
|
||||
return false;
|
||||
|
||||
EntitySearch search = items.get(pos);
|
||||
if (search == null)
|
||||
return false;
|
||||
|
||||
Bundle args = new Bundle();
|
||||
args.putLong("id", search.id);
|
||||
|
||||
new SimpleTask<Void>() {
|
||||
@Override
|
||||
protected Void onExecute(Context context, Bundle args) throws Throwable {
|
||||
long id = args.getLong("id");
|
||||
|
||||
DB db = DB.getInstance(context);
|
||||
db.search().deleteSearch(id);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onException(Bundle args, Throwable ex) {
|
||||
Log.unexpectedError(manager, ex);
|
||||
}
|
||||
}.execute(context, owner, args, "search:delete");
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
AdapterNavSearch(Context context, LifecycleOwner owner, FragmentManager manager) {
|
||||
this.context = context;
|
||||
this.owner = owner;
|
||||
this.manager = manager;
|
||||
this.inflater = LayoutInflater.from(context);
|
||||
|
||||
setHasStableIds(true);
|
||||
|
||||
owner.getLifecycle().addObserver(new LifecycleObserver() {
|
||||
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
|
||||
public void onDestroyed() {
|
||||
Log.d(AdapterNavSearch.this + " parent destroyed");
|
||||
AdapterNavSearch.this.manager = null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void set(@NonNull List<EntitySearch> search, boolean expanded) {
|
||||
Log.i("Set nav search=" + search.size() + " expanded=" + expanded);
|
||||
|
||||
DiffUtil.DiffResult diff = DiffUtil.calculateDiff(new DiffCallback(items, search), false);
|
||||
|
||||
this.expanded = expanded;
|
||||
this.items = search;
|
||||
|
||||
diff.dispatchUpdatesTo(new ListUpdateCallback() {
|
||||
@Override
|
||||
public void onInserted(int position, int count) {
|
||||
Log.d("Inserted @" + position + " #" + count);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRemoved(int position, int count) {
|
||||
Log.d("Removed @" + position + " #" + count);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMoved(int fromPosition, int toPosition) {
|
||||
Log.d("Moved " + fromPosition + ">" + toPosition);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChanged(int position, int count, Object payload) {
|
||||
Log.d("Changed @" + position + " #" + count);
|
||||
}
|
||||
});
|
||||
diff.dispatchUpdatesTo(this);
|
||||
}
|
||||
|
||||
public void setExpanded(boolean expanded) {
|
||||
this.expanded = expanded;
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
EntitySearch get(int pos) {
|
||||
return items.get(pos);
|
||||
}
|
||||
|
||||
private static class DiffCallback extends DiffUtil.Callback {
|
||||
private List<EntitySearch> prev = new ArrayList<>();
|
||||
private List<EntitySearch> next = new ArrayList<>();
|
||||
|
||||
DiffCallback(List<EntitySearch> prev, List<EntitySearch> next) {
|
||||
this.prev.addAll(prev);
|
||||
this.next.addAll(next);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOldListSize() {
|
||||
return prev.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNewListSize() {
|
||||
return next.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
|
||||
EntitySearch s1 = prev.get(oldItemPosition);
|
||||
EntitySearch s2 = next.get(newItemPosition);
|
||||
return s1.id.equals(s2.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
|
||||
EntitySearch s1 = prev.get(oldItemPosition);
|
||||
EntitySearch s2 = next.get(newItemPosition);
|
||||
return s1.equals(s2);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getItemId(int position) {
|
||||
return items.get(position).id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return items.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
return new ViewHolder(inflater.inflate(R.layout.item_nav, parent, false));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
|
||||
holder.unwire();
|
||||
EntitySearch search = items.get(position);
|
||||
holder.bindTo(search);
|
||||
holder.wire();
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package eu.faircode.email;
|
||||
|
||||
/*
|
||||
This file is part of FairEmail.
|
||||
|
||||
FairEmail 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.
|
||||
|
||||
FairEmail 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.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2018-2021 by Marcel Bokhorst (M66B)
|
||||
*/
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.room.Dao;
|
||||
import androidx.room.Insert;
|
||||
import androidx.room.Query;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Dao
|
||||
public interface DaoSearch {
|
||||
@Query("SELECT * FROM search" +
|
||||
" ORDER BY name COLLATE NOCASE")
|
||||
LiveData<List<EntitySearch>> liveSearch();
|
||||
|
||||
@Insert
|
||||
long insertSearch(EntitySearch search);
|
||||
|
||||
@Query("DELETE FROM search" +
|
||||
" WHERE id = :id")
|
||||
int deleteSearch(long id);
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package eu.faircode.email;
|
||||
|
||||
/*
|
||||
This file is part of FairEmail.
|
||||
|
||||
FairEmail 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.
|
||||
|
||||
FairEmail 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.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2018-2021 by Marcel Bokhorst (M66B)
|
||||
*/
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.room.Entity;
|
||||
import androidx.room.PrimaryKey;
|
||||
|
||||
@Entity(
|
||||
tableName = EntitySearch.TABLE_NAME,
|
||||
foreignKeys = {
|
||||
},
|
||||
indices = {
|
||||
}
|
||||
)
|
||||
public class EntitySearch {
|
||||
static final String TABLE_NAME = "search";
|
||||
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
public Long id;
|
||||
@NonNull
|
||||
public String name;
|
||||
@NonNull
|
||||
public String data;
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj instanceof EntitySearch) {
|
||||
EntitySearch other = (EntitySearch) obj;
|
||||
return (this.id.equals(other.id) &&
|
||||
this.name.equals(other.name) &&
|
||||
this.data.equals(other.data));
|
||||
} else
|
||||
return false;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue