ProfileFragment: Helper class to remember a fragment's profile

Caching the actual profile object is important when the fragment is on
an activity's back stack: when it is shown again, onCreateView will be
called with profile already set and the service already connected. In
this case, there is no later notification from which to bind the
profile, so the existing object needs to be bound there.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Samuel Holland
2017-08-09 07:18:31 -05:00
parent 39ed03f758
commit 2c4f605b66
2 changed files with 62 additions and 16 deletions
@@ -10,22 +10,15 @@ import android.view.ViewGroup;
import com.wireguard.android.databinding.ProfileDetailFragmentBinding;
/**
* Fragment for viewing and editing a WireGuard profile.
* Fragment for viewing information about a WireGuard profile.
*/
public class ProfileDetailFragment extends ServiceClientFragment<ProfileServiceInterface> {
public class ProfileDetailFragment extends ProfileFragment {
private ProfileDetailFragmentBinding binding;
private String name;
public ProfileDetailFragment() {
super();
setArguments(new Bundle());
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
name = getArguments().getString(ProfileActivity.KEY_PROFILE_NAME);
setHasOptionsMenu(true);
}
@@ -37,20 +30,20 @@ public class ProfileDetailFragment extends ServiceClientFragment<ProfileServiceI
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
binding = ProfileDetailFragmentBinding.inflate(inflater, parent, false);
binding.setProfile(getCachedProfile());
return binding.getRoot();
}
@Override
public void onServiceConnected(ProfileServiceInterface service) {
super.onServiceConnected(service);
binding.setProfile(service.getProfiles().get(name));
binding.setProfile(service.getProfiles().get(getProfile()));
}
public void setProfile(String name) {
this.name = name;
getArguments().putString(ProfileActivity.KEY_PROFILE_NAME, name);
final ProfileServiceInterface service = getService();
if (binding != null && service != null)
binding.setProfile(service.getProfiles().get(name));
@Override
public void setProfile(String profile) {
super.setProfile(profile);
if (binding != null)
binding.setProfile(getCachedProfile());
}
}