diff options
author | Samuel Holland <samuel@sholland.org> | 2017-08-10 00:57:44 -0500 |
---|---|---|
committer | Samuel Holland <samuel@sholland.org> | 2017-08-10 00:57:44 -0500 |
commit | d6d6f3408860d4f3abba07b4c749d215edbf7cb7 (patch) | |
tree | 2fceef50902fae269d42320afb44192e4fd9d307 | |
parent | d00480a3a5bbc179117d051180c6ee5316d34544 (diff) |
ProfileFragment: Make good use of the cached profile
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
-rw-r--r-- | app/src/main/java/com/wireguard/android/ProfileDetailFragment.java | 20 | ||||
-rw-r--r-- | app/src/main/java/com/wireguard/android/ProfileFragment.java | 24 |
2 files changed, 23 insertions, 21 deletions
diff --git a/app/src/main/java/com/wireguard/android/ProfileDetailFragment.java b/app/src/main/java/com/wireguard/android/ProfileDetailFragment.java index 2aa40c06..0fed7708 100644 --- a/app/src/main/java/com/wireguard/android/ProfileDetailFragment.java +++ b/app/src/main/java/com/wireguard/android/ProfileDetailFragment.java @@ -8,6 +8,7 @@ import android.view.View; import android.view.ViewGroup; import com.wireguard.android.databinding.ProfileDetailFragmentBinding; +import com.wireguard.config.Profile; /** * Fragment for viewing information about a WireGuard profile. @@ -17,6 +18,12 @@ public class ProfileDetailFragment extends ProfileFragment { private ProfileDetailFragmentBinding binding; @Override + protected void onCachedProfileChanged(Profile cachedProfile) { + if (binding != null) + binding.setProfile(cachedProfile); + } + + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); @@ -33,17 +40,4 @@ public class ProfileDetailFragment extends ProfileFragment { binding.setProfile(getCachedProfile()); 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()); - } } diff --git a/app/src/main/java/com/wireguard/android/ProfileFragment.java b/app/src/main/java/com/wireguard/android/ProfileFragment.java index cf1c690e..0e9092ec 100644 --- a/app/src/main/java/com/wireguard/android/ProfileFragment.java +++ b/app/src/main/java/com/wireguard/android/ProfileFragment.java @@ -20,14 +20,17 @@ abstract class ProfileFragment extends ServiceClientFragment<ProfileServiceInter return profile; } + protected void onCachedProfileChanged(Profile cachedProfile) { + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Restore the saved profile if there is one; otherwise grab it from the arguments. if (savedInstanceState != null) - profile = savedInstanceState.getString(ProfileActivity.KEY_PROFILE_NAME); + setProfile(savedInstanceState.getString(ProfileActivity.KEY_PROFILE_NAME)); else if (getArguments() != null) - profile = getArguments().getString(ProfileActivity.KEY_PROFILE_NAME); + setProfile(getArguments().getString(ProfileActivity.KEY_PROFILE_NAME)); } @Override @@ -39,15 +42,20 @@ abstract class ProfileFragment extends ServiceClientFragment<ProfileServiceInter @Override public void onServiceConnected(ProfileServiceInterface service) { super.onServiceConnected(service); - cachedProfile = service.getProfiles().get(profile); + updateCachedProfile(service); } public void setProfile(String profile) { this.profile = profile; - final ProfileServiceInterface service = getService(); - if (service != null) - cachedProfile = service.getProfiles().get(profile); - else - cachedProfile = null; + updateCachedProfile(getService()); + } + + private void updateCachedProfile(ProfileServiceInterface service) { + final Profile newCachedProfile = service != null + ? service.getProfiles().get(profile) : null; + if (newCachedProfile != cachedProfile) { + cachedProfile = newCachedProfile; + onCachedProfileChanged(newCachedProfile); + } } } |