wireguard-android/app/src/main/java/com/wireguard/android/fragment/TunnelDetailFragment.java
Jason A. Donenfeld 63071f57b7 Use validation instead of two-way binding
This is insane, but it appears to be working. We essentially store
things in a separate class for editing, and then commit it back at a
given time.

This business with onViewStateRestored in both TunnelEditorFragment and
in TunnelDetailFragment is buggy and likely wrong.

In general TunnelEditorFragment should probably be rewritten. The
relationship with the changed name is not clear.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2018-04-28 04:51:43 +02:00

81 lines
2.5 KiB
Java

package com.wireguard.android.fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import com.wireguard.android.R;
import com.wireguard.android.databinding.TunnelDetailFragmentBinding;
import com.wireguard.android.model.Tunnel;
import com.wireguard.config.Config;
/**
* Fragment that shows details about a specific tunnel.
*/
public class TunnelDetailFragment extends BaseFragment {
private TunnelDetailFragmentBinding binding;
private boolean isViewStateRestored;
private String originalName;
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
inflater.inflate(R.menu.tunnel_detail, menu);
}
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
final Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
binding = TunnelDetailFragmentBinding.inflate(inflater, container, false);
binding.executePendingBindings();
return binding.getRoot();
}
@Override
public void onDestroyView() {
binding = null;
super.onDestroyView();
}
private void onConfigLoaded(Config config) {
binding.setConfig(new Config.Observable(config, originalName));
}
@Override
public void onSelectedTunnelChanged(final Tunnel oldTunnel, final Tunnel newTunnel) {
if (binding != null && isViewStateRestored) {
binding.setTunnel(newTunnel);
if (newTunnel == null)
binding.setConfig(null);
else {
originalName = newTunnel.getName();
newTunnel.getConfigAsync().thenAccept(this::onConfigLoaded);
}
}
}
@Override
public void onViewStateRestored(final Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
Tunnel tunnel = getSelectedTunnel();
binding.setTunnel(tunnel);
if (tunnel == null)
binding.setConfig(null);
else {
originalName = tunnel.getName();
tunnel.getConfigAsync().thenAccept(this::onConfigLoaded);
}
isViewStateRestored = true;
}
}