Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bundler that modifies object in get method #119

Open
snorkel123 opened this issue Apr 8, 2018 · 0 comments
Open

Bundler that modifies object in get method #119

snorkel123 opened this issue Apr 8, 2018 · 0 comments

Comments

@snorkel123
Copy link

snorkel123 commented Apr 8, 2018

Problem
Some objects whose state should be saved are constructed by other library.
For instance, GoogleMap.
Thus Bundler's get method's return value is not applicable here.

Proposal
Alternative Bundler (MutatingBundler) that:

  • receives object to restore state of in get method and modifies it
  • returns void

Proposal example

class MyMap extends MapFragment implements OnMapReadyCallback {

   @State(MapBundler.class) GoogleMap map;

   Bundle savedInstanceState;

   void onViewCreated(Bundle savedInstanceState) {
      this.savedInstanceState = savedInstanceState;
      getMapAsync(this);
   }

   void onMapReady(GoogleMap map) {
      this.map = map;
      IcePick.restoreInstanceState(this, this.savedInstanceState);
   }

   void onSaveInstanceState(Bundle outState) {
      IcePick.saveInstanceState(this, outState);
      super.onSaveInstanceState(outState);
   }

}

// MutatingBundler!
class MapBundler implements MutatingBundler<GoogleMap> {
   void put(String key, GoogleMap map, Bundle bundle) {

      CameraPosition position = map.getCameraPosition();

      bundle.putFloat(key + "zoom", position.zoom);
      bundle.putDouble(key + "lat", position.target.latitude);
      bundle.putDouble(key + "lon", position.target.longitude);
      bundle.putFloat(key + "tilt", position.tilt);
      bundle.putFloat(key + "bearing", position.bearing);

   }

   // <<<<<<<<<<<<<<<<<<< Here is the difference >>>>>>>>>>>>>>>>>>>
   // GoogleMap get(String key, Bundler bundle) {...} <--------- This is previous state
   void get(String key, @NonNull GoogleMap map, Bundle bundle) {

      float zoom = bundle.getFloat(key + "zoom");
      double lat = bundle.getDouble(key + "lat");
      double lon = bundle.getDouble(key + "lon");
      float tilt = bundle.getFloat(key + "tilt");
      float bearing = bundle.getFloat(key + "bearing");

      LatLng target = new LatLng(lat, lon);
      CameraPosition restoreCamPosition = new CameraPosition(target, zoom, tilt, bearing);
      map.moveCamera(CameraUpdateFactory.newCameraPosition(restoreCamPosition));
   }

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant