Skip to content

Commit 0c3cbe9

Browse files
committed
fixup! Added LifecycleObserver and tests
1 parent 047a7e9 commit 0c3cbe9

File tree

1 file changed

+51
-36
lines changed

1 file changed

+51
-36
lines changed

rxandroid/src/main/java/rx/android/lifecycle/LifecycleObservable.java

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -127,22 +127,32 @@ public Boolean call(Boolean shouldComplete) {
127127
new Func1<LifecycleEvent, LifecycleEvent>() {
128128
@Override
129129
public LifecycleEvent call(LifecycleEvent lastEvent) {
130-
if (lastEvent != null) {
131-
switch (lastEvent) {
132-
case CREATE:
133-
return LifecycleEvent.DESTROY;
134-
case START:
135-
return LifecycleEvent.STOP;
136-
case RESUME:
137-
return LifecycleEvent.PAUSE;
138-
case PAUSE:
139-
return LifecycleEvent.STOP;
140-
case STOP:
141-
return LifecycleEvent.DESTROY;
142-
}
130+
if (lastEvent == null) {
131+
throw new NullPointerException("Cannot bind to null LifecycleEvent.");
143132
}
144133

145-
throw new IllegalStateException("Cannot bind to Activity lifecycle when outside of it.");
134+
switch (lastEvent) {
135+
case CREATE:
136+
return LifecycleEvent.DESTROY;
137+
case START:
138+
return LifecycleEvent.STOP;
139+
case RESUME:
140+
return LifecycleEvent.PAUSE;
141+
case PAUSE:
142+
return LifecycleEvent.STOP;
143+
case STOP:
144+
return LifecycleEvent.DESTROY;
145+
case DESTROY:
146+
throw new IllegalStateException("Cannot bind to Activity lifecycle when outside of it.");
147+
case ATTACH:
148+
case CREATE_VIEW:
149+
case DESTROY_VIEW:
150+
case DETACH:
151+
throw new IllegalStateException("Cannot bind to " + lastEvent + " for an Activity.");
152+
default:
153+
throw new UnsupportedOperationException("Binding to LifecycleEvent " + lastEvent
154+
+ " not yet implemented");
155+
}
146156
}
147157
};
148158

@@ -151,30 +161,35 @@ public LifecycleEvent call(LifecycleEvent lastEvent) {
151161
new Func1<LifecycleEvent, LifecycleEvent>() {
152162
@Override
153163
public LifecycleEvent call(LifecycleEvent lastEvent) {
154-
if (lastEvent != null) {
155-
switch (lastEvent) {
156-
case ATTACH:
157-
return LifecycleEvent.DETACH;
158-
case CREATE:
159-
return LifecycleEvent.DESTROY;
160-
case CREATE_VIEW:
161-
return LifecycleEvent.DESTROY_VIEW;
162-
case START:
163-
return LifecycleEvent.STOP;
164-
case RESUME:
165-
return LifecycleEvent.PAUSE;
166-
case PAUSE:
167-
return LifecycleEvent.STOP;
168-
case STOP:
169-
return LifecycleEvent.DESTROY_VIEW;
170-
case DESTROY_VIEW:
171-
return LifecycleEvent.DESTROY;
172-
case DESTROY:
173-
return LifecycleEvent.DETACH;
174-
}
164+
if (lastEvent == null) {
165+
throw new NullPointerException("Cannot bind to null LifecycleEvent.");
175166
}
176167

177-
throw new IllegalStateException("Cannot bind to Fragment lifecycle when outside of it.");
168+
switch (lastEvent) {
169+
case ATTACH:
170+
return LifecycleEvent.DETACH;
171+
case CREATE:
172+
return LifecycleEvent.DESTROY;
173+
case CREATE_VIEW:
174+
return LifecycleEvent.DESTROY_VIEW;
175+
case START:
176+
return LifecycleEvent.STOP;
177+
case RESUME:
178+
return LifecycleEvent.PAUSE;
179+
case PAUSE:
180+
return LifecycleEvent.STOP;
181+
case STOP:
182+
return LifecycleEvent.DESTROY_VIEW;
183+
case DESTROY_VIEW:
184+
return LifecycleEvent.DESTROY;
185+
case DESTROY:
186+
return LifecycleEvent.DETACH;
187+
case DETACH:
188+
throw new IllegalStateException("Cannot bind to Fragment lifecycle when outside of it.");
189+
default:
190+
throw new UnsupportedOperationException("Binding to LifecycleEvent " + lastEvent
191+
+ " not yet implemented");
192+
}
178193
}
179194
};
180195
}

0 commit comments

Comments
 (0)