/*
 * Copyright (C) 2016 grandcentrix GmbH
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.grandcentrix.thirtyinch.rx2;

import net.grandcentrix.thirtyinch.Removable;
import net.grandcentrix.thirtyinch.TiLifecycleObserver;
import net.grandcentrix.thirtyinch.TiPresenter;
import net.grandcentrix.thirtyinch.TiView;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.disposables.Disposable;

public class RxTiPresenterUtils {

    /**
     * Observable of the view state. The View is ready to receive calls after calling {@link
     * TiPresenter#attachView(TiView)} and before calling {@link TiPresenter#detachView()}.
     */
    public static Observable<Boolean> isViewReady(final TiPresenter presenter) {
        return Observable.create(
                new ObservableOnSubscribe<Boolean>() {
                    @Override
                    public void subscribe(final ObservableEmitter<Boolean> emitter)
                            throws Exception {
                        if (!emitter.isDisposed()) {
                            emitter.onNext(presenter.getState()
                                    == TiPresenter.State.VIEW_ATTACHED);
                        }

                        final Removable removable = presenter
                                .addLifecycleObserver(new TiLifecycleObserver() {
                                    @Override
                                    public void onChange(final TiPresenter.State state,
                                            final boolean hasLifecycleMethodBeenCalled) {
                                        if (!emitter.isDisposed()) {
                                            emitter.onNext(state ==
                                                    TiPresenter.State.VIEW_ATTACHED);
                                        }
                                    }
                                });

                        emitter.setDisposable(new Disposable() {
                            @Override
                            public void dispose() {
                                removable.remove();
                            }

                            @Override
                            public boolean isDisposed() {
                                return removable.isRemoved();
                            }
                        });
                    }
                })
                .distinctUntilChanged();
    }

}

@@ -1339,33 +1339,16 @@
 .create(
-%0A                
 new Obse
@@ -1386,32 +1386,24 @@
             
-       
- 
 @Override%0A  
@@ -1396,32 +1396,24 @@
   @Override%0A
-        
             
@@ -1496,24 +1496,16 @@
         
-        
 throws E
@@ -1527,32 +1527,24 @@
             
-      
-  
 if (!emitter
@@ -1556,32 +1556,24 @@
 sposed()) %7B%0A
-        
             
@@ -1619,44 +1619,8 @@
 te()
-%0A                                   
  == 
@@ -1669,35 +1669,19 @@
         
- 
-       %7D%0A%0A        
+%7D%0A%0A
         
@@ -1750,24 +1750,16 @@
         
-        
 .addLife
@@ -1824,32 +1824,24 @@
             
- 
-       
 @Override%0A  
@@ -1834,32 +1834,24 @@
   @Override%0A
-        
             
@@ -1918,24 +1918,16 @@
  state,%0A
-        
         
@@ -2028,32 +2028,24 @@
             
-        
 if (!emitter
@@ -2093,32 +2093,24 @@
             
-        
 emitter.onNe
@@ -2124,60 +2124,8 @@
 e ==
-%0A                                                   
  TiP
@@ -2144,34 +2144,32 @@
 te.VIEW_ATTACHED
-);
 %0A               
@@ -2185,38 +2185,70 @@
                 
-%7D%0A
     
+&& hasLifecycleMethodBeenCalled);%0A
                 
@@ -2275,32 +2275,44 @@
                 
+          %7D%0A
               %7D)
@@ -2313,21 +2313,18 @@
     
-%7D);%0A%0A
+  
         
     
@@ -2311,32 +2311,37 @@
                 
+%7D);%0A%0A
                 
@@ -2397,32 +2397,24 @@
             
-        
 @Override%0A  
@@ -2427,32 +2427,24 @@
             
-   
-     
 public void 
@@ -2451,32 +2451,24 @@
 dispose() %7B%0A
-        
             
@@ -2515,38 +2515,27 @@
             
-        %7D%0A%0A   
+%7D%0A%0A
             
@@ -2534,37 +2534,32 @@
                 
-     
 @Override%0A      
@@ -2548,32 +2548,24 @@
   @Override%0A
-        
             
@@ -2626,24 +2626,16 @@
         
-        
 return r
@@ -2676,34 +2676,18 @@
         
-  
-      %7D%0A        
+%7D%0A
         
@@ -2714,53 +2714,20 @@
     
-        %7D%0A                %7D)%0A                
+%7D%0A        %7D)
 .dis

Update isViewReady in Rx2