frankiesardo / icepick

Android Instance State made easy
Eclipse Public License 1.0
3.75k stars 208 forks source link

Add support for visibility state #104

Open thekalinga opened 7 years ago

thekalinga commented 7 years ago

Currently if there are views whose visibility changes at runtime, we have to explicitly add member variables that represent the visibility status for each view.

This is tedious & error prone.

It would be much better if we add a new annotation @VisibilityState that can auto save the visibility status of a view.

The annotation processor can add a new variable (whose name can be derived from the name of the variable, for eg., mycustomViewRef$$visibility) in the bundle during saveInstanceState & auto apply the visibility status on restoreInstanceState

Here is an example

class MyActivity {
 @State
 int regularState;

 @VisibilityState
 TextView dynamicVisibilityTv;
 @VisibilityState
 CustomView customView;

 public void onSaveInstanceState(Bundle outState) {
  super.onSaveInstanceState(outState);
  Icepick.saveInstanceState(this, outState); // this should save `regularState`, `dynamicVisibilityTv$$visibility` & `customView$$visibility`
 }

 public void onRestoreInstanceState(Bundle savedInstanceState) {
  // Always call the superclass so it can restore the view hierarchy
  super.onRestoreInstanceState(savedInstanceState);
  Icepick.restoreInstanceState(this, savedInstanceState); // this should auto restore `regularState`, `dynamicVisibilityTv$$visibility` & `customView$$visibility`
 }

}
ghost commented 7 years ago

Yes, it would be excellent.

thekalinga commented 7 years ago

Along with visibility state, we should identify additional common views/components whose state we would want to be restore out of the box such as LinearLayoutManager, e.t.c.

Why does anyone needs to pollute code with most basic things when its apt can do this much better

thekalinga commented 7 years ago

@frankiesardo Any views on this?

FYI: I'm not comfortable clojure, else, I would have submitted a pull request :(