TunnelListFragment: show selected tunnel

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2018-07-29 05:44:04 +02:00
parent 1b10e75168
commit b960b4a6cd

View File

@ -21,6 +21,7 @@ import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.ActionMode; import android.support.v7.view.ActionMode;
import android.support.v7.widget.RecyclerView;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
@ -38,6 +39,7 @@ import com.wireguard.android.databinding.TunnelListFragmentBinding;
import com.wireguard.android.databinding.TunnelListItemBinding; import com.wireguard.android.databinding.TunnelListItemBinding;
import com.wireguard.android.model.Tunnel; import com.wireguard.android.model.Tunnel;
import com.wireguard.android.util.ExceptionLoggers; import com.wireguard.android.util.ExceptionLoggers;
import com.wireguard.android.util.ObservableSortedKeyedList;
import com.wireguard.android.widget.fab.FloatingActionsMenuRecyclerViewScrollListener; import com.wireguard.android.widget.fab.FloatingActionsMenuRecyclerViewScrollListener;
import com.wireguard.config.Config; import com.wireguard.config.Config;
@ -262,7 +264,28 @@ public class TunnelListFragment extends BaseFragment {
@Override @Override
public void onSelectedTunnelChanged(@Nullable final Tunnel oldTunnel, @Nullable final Tunnel newTunnel) { public void onSelectedTunnelChanged(@Nullable final Tunnel oldTunnel, @Nullable final Tunnel newTunnel) {
// Do nothing. if (binding == null)
return;
Application.getTunnelManager().getTunnels().thenAccept(tunnels -> {
if (newTunnel != null)
binding.tunnelList.findViewHolderForAdapterPosition(tunnels.indexOf(newTunnel)).itemView.setActivated(true);
if (oldTunnel != null)
binding.tunnelList.findViewHolderForAdapterPosition(tunnels.indexOf(oldTunnel)).itemView.setActivated(false);
});
/* Alternative 1: results in sluggish change:
if (binding.tunnelList.getAdapter() == null)
return;
* Alternative 2: results in overly quick change:
binding.tunnelList.getAdapter().notifyDataSetChanged();
* Hence, we go with the above.
*/
} }
private void onTunnelDeletionFinished(final Integer count, @Nullable final Throwable throwable) { private void onTunnelDeletionFinished(final Integer count, @Nullable final Throwable throwable) {
@ -348,7 +371,11 @@ public class TunnelListFragment extends BaseFragment {
return true; return true;
}); });
binding.getRoot().setActivated(actionModeListener.checkedItems.contains(position)); if (actionMode != null)
binding.getRoot().setActivated(actionModeListener.checkedItems.contains(position));
else
binding.getRoot().setActivated(getSelectedTunnel() == tunnel);
}); });
} }
@ -397,6 +424,7 @@ public class TunnelListFragment extends BaseFragment {
resources = getActivity().getResources(); resources = getActivity().getResources();
} }
mode.getMenuInflater().inflate(R.menu.tunnel_list_action_mode, menu); mode.getMenuInflater().inflate(R.menu.tunnel_list_action_mode, menu);
binding.tunnelList.getAdapter().notifyDataSetChanged();
return true; return true;
} }
@ -424,15 +452,16 @@ public class TunnelListFragment extends BaseFragment {
checkedItems.remove(position); checkedItems.remove(position);
} }
final RecyclerView.Adapter adapter = binding == null ? null : binding.tunnelList.getAdapter();
if (actionMode == null && !checkedItems.isEmpty() && getActivity() != null) { if (actionMode == null && !checkedItems.isEmpty() && getActivity() != null) {
((AppCompatActivity) getActivity()).startSupportActionMode(this); ((AppCompatActivity) getActivity()).startSupportActionMode(this);
} else if (actionMode != null && checkedItems.isEmpty()) { } else if (actionMode != null && checkedItems.isEmpty()) {
actionMode.finish(); actionMode.finish();
} }
if (binding != null && binding.tunnelList.getAdapter() != null) { if (adapter != null)
binding.tunnelList.getAdapter().notifyItemChanged(position); adapter.notifyItemChanged(position);
}
updateTitle(actionMode); updateTitle(actionMode);
} }