Skip to content

Commit

Permalink
[Feature] New API readExternal & writeExternal to support sub-class E…
Browse files Browse the repository at this point in the history
…xternalizable implementation
  • Loading branch information
rh-id committed Nov 27, 2021
1 parent d528ce6 commit 4945e08
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,33 @@
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.VisibleForTesting;
import androidx.appcompat.widget.Toolbar;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

import m.co.rh.id.anavigator.StatefulView;
import m.co.rh.id.anavigator.component.INavigator;
import m.co.rh.id.anavigator.component.RequireNavigator;

public class CommonAppBar extends StatefulView<Activity> implements RequireNavigator {
public class CommonAppBar extends StatefulView<Activity> implements RequireNavigator, Externalizable {

private transient INavigator mNavigator;
private String mTitle;
private transient View.OnClickListener mNavigationOnClickListener;
private int mBackgroundColor;
private boolean mIsInitialRoute;
private Integer mBackgroundColor;
private Boolean mIsInitialRoute;

/**
* Used for Externalizable only
*/
@Deprecated
@VisibleForTesting
public CommonAppBar() {
}

public CommonAppBar(INavigator navigator) {
mNavigator = navigator;
Expand Down Expand Up @@ -65,4 +79,20 @@ public void setNavigationOnClickListener(View.OnClickListener navigationOnClickL
public void setBackgroundColor(int backgroundColor) {
mBackgroundColor = backgroundColor;
}

@Override
public void writeExternal(ObjectOutput out) throws IOException {
super.writeExternal(out);
out.writeObject(mTitle);
out.writeObject(mBackgroundColor);
out.writeObject(mIsInitialRoute);
}

@Override
public void readExternal(ObjectInput in) throws ClassNotFoundException, IOException {
super.readExternal(in);
mTitle = (String) in.readObject();
mBackgroundColor = (Integer) in.readObject();
mIsInitialRoute = (Boolean) in.readObject();
}
}
28 changes: 25 additions & 3 deletions navigator/src/main/java/m/co/rh/id/anavigator/StatefulView.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import android.view.View;
import android.view.ViewGroup;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.Serializable;
import java.util.UUID;

Expand All @@ -12,8 +15,8 @@
* NOTE: Always call super implementation when extending this.
*/
public abstract class StatefulView<ACT extends Activity> implements Serializable {
private boolean mIsInitialized;
private final String mKey;
private Boolean mIsInitialized;
private String mKey;

public StatefulView() {
this(null);
Expand All @@ -28,6 +31,7 @@ public StatefulView(String key) {
mKey = key == null ? this.getClass().getName() + "-StatefulViewClassKey-" +
UUID.randomUUID().toString()
: key;
mIsInitialized = false;
}

/**
Expand All @@ -42,7 +46,7 @@ public String getKey() {
/**
* @return true if this has been initialized, false otherwise
*/
public final boolean isInitialized() {
public boolean isInitialized() {
return mIsInitialized;
}

Expand Down Expand Up @@ -85,4 +89,22 @@ public final void initialize(ACT activity) {
initState(activity);
}
}

/**
* Implementation of Externalizable, call this if sub-class implements Externalizable
* to handle this parent class externalization
*/
protected void writeExternal(ObjectOutput objectOutput) throws IOException {
objectOutput.writeObject(mIsInitialized);
objectOutput.writeObject(mKey);
}

/**
* Implementation of Externalizable, call this if sub-class implements Externalizable
* to handle this parent class externalization
*/
protected void readExternal(ObjectInput objectInput) throws ClassNotFoundException, IOException {
mIsInitialized = (Boolean) objectInput.readObject();
mKey = (String) objectInput.readObject();
}
}

0 comments on commit 4945e08

Please sign in to comment.