ProfileFragment: Make good use of the cached profile

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Samuel Holland 2017-08-10 00:57:44 -05:00
parent d00480a3a5
commit d6d6f34088
2 changed files with 23 additions and 21 deletions

View File

@ -8,6 +8,7 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import com.wireguard.android.databinding.ProfileDetailFragmentBinding; import com.wireguard.android.databinding.ProfileDetailFragmentBinding;
import com.wireguard.config.Profile;
/** /**
* Fragment for viewing information about a WireGuard profile. * Fragment for viewing information about a WireGuard profile.
@ -16,6 +17,12 @@ import com.wireguard.android.databinding.ProfileDetailFragmentBinding;
public class ProfileDetailFragment extends ProfileFragment { public class ProfileDetailFragment extends ProfileFragment {
private ProfileDetailFragmentBinding binding; private ProfileDetailFragmentBinding binding;
@Override
protected void onCachedProfileChanged(Profile cachedProfile) {
if (binding != null)
binding.setProfile(cachedProfile);
}
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -33,17 +40,4 @@ public class ProfileDetailFragment extends ProfileFragment {
binding.setProfile(getCachedProfile()); binding.setProfile(getCachedProfile());
return binding.getRoot(); return binding.getRoot();
} }
@Override
public void onServiceConnected(ProfileServiceInterface service) {
super.onServiceConnected(service);
binding.setProfile(service.getProfiles().get(getProfile()));
}
@Override
public void setProfile(String profile) {
super.setProfile(profile);
if (binding != null)
binding.setProfile(getCachedProfile());
}
} }

View File

@ -20,14 +20,17 @@ abstract class ProfileFragment extends ServiceClientFragment<ProfileServiceInter
return profile; return profile;
} }
protected void onCachedProfileChanged(Profile cachedProfile) {
}
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
// Restore the saved profile if there is one; otherwise grab it from the arguments. // Restore the saved profile if there is one; otherwise grab it from the arguments.
if (savedInstanceState != null) if (savedInstanceState != null)
profile = savedInstanceState.getString(ProfileActivity.KEY_PROFILE_NAME); setProfile(savedInstanceState.getString(ProfileActivity.KEY_PROFILE_NAME));
else if (getArguments() != null) else if (getArguments() != null)
profile = getArguments().getString(ProfileActivity.KEY_PROFILE_NAME); setProfile(getArguments().getString(ProfileActivity.KEY_PROFILE_NAME));
} }
@Override @Override
@ -39,15 +42,20 @@ abstract class ProfileFragment extends ServiceClientFragment<ProfileServiceInter
@Override @Override
public void onServiceConnected(ProfileServiceInterface service) { public void onServiceConnected(ProfileServiceInterface service) {
super.onServiceConnected(service); super.onServiceConnected(service);
cachedProfile = service.getProfiles().get(profile); updateCachedProfile(service);
} }
public void setProfile(String profile) { public void setProfile(String profile) {
this.profile = profile; this.profile = profile;
final ProfileServiceInterface service = getService(); updateCachedProfile(getService());
if (service != null) }
cachedProfile = service.getProfiles().get(profile);
else private void updateCachedProfile(ProfileServiceInterface service) {
cachedProfile = null; final Profile newCachedProfile = service != null
? service.getProfiles().get(profile) : null;
if (newCachedProfile != cachedProfile) {
cachedProfile = newCachedProfile;
onCachedProfileChanged(newCachedProfile);
}
} }
} }