/*
 * This file provided by Facebook is for non-commercial testing and evaluation
 * purposes only.  Facebook reserves all rights not expressly granted.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.facebook.samples.litho.errors;

import com.facebook.litho.Column;
import com.facebook.litho.Component;
import com.facebook.litho.ComponentContext;
import com.facebook.litho.StateValue;
import com.facebook.litho.annotations.LayoutSpec;
import com.facebook.litho.annotations.OnCreateInitialState;
import com.facebook.litho.annotations.OnCreateLayout;
import com.facebook.litho.annotations.OnError;
import com.facebook.litho.annotations.OnUpdateState;
import com.facebook.litho.annotations.Param;
import com.facebook.litho.annotations.Prop;
import com.facebook.litho.annotations.State;
import com.facebook.yoga.YogaEdge;
import java.util.Optional;

@LayoutSpec
public class ErrorBoundarySpec {

  @OnCreateLayout
  static Component onCreateLayout(
      ComponentContext c, @Prop Component child, @State Optional<Exception> error) {

    if (error.isPresent()) {
      return Column.create(c)
          .marginDip(YogaEdge.ALL, 16)
          .child(
              DebugErrorComponent.create(c)
                  .message("Error Boundary")
                  .throwable(error.get())
                  .build())
          .build();
    }

    return child;
  }

  @OnCreateInitialState
  static void createInitialState(ComponentContext c, StateValue<Optional<Exception>> error) {

    error.set(Optional.<Exception>empty());
  }

  @OnUpdateState
  static void updateError(StateValue<Optional<Exception>> error, @Param Exception e) {
    error.set(Optional.of(e));
  }

  @OnError
  static void onError(ComponentContext c, Exception error) {
    ErrorBoundary.updateErrorSync(c, error);
  }
}

@@ -664,33 +664,36 @@
 ort 
-com.facebook.litho.Column
+androidx.annotation.Nullable
 ;%0Aim
@@ -863,68 +863,8 @@
 ec;%0A
-import com.facebook.litho.annotations.OnCreateInitialState;%0A
 impo
@@ -1150,70 +1150,8 @@
 ate;
-%0Aimport com.facebook.yoga.YogaEdge;%0Aimport java.util.Optional;
 %0A%0A@L
@@ -1303,25 +1303,26 @@
  @State 
-Optional%3C
+@Nullable 
 Exceptio
@@ -1322,17 +1322,16 @@
 xception
-%3E
  error) 
@@ -1350,20 +1350,16 @@
 rror
-.isPresent()
+ != null
 ) %7B%0A
@@ -1375,96 +1375,8 @@
 urn 
-Column.create(c)%0A          .marginDip(YogaEdge.ALL, 16)%0A          .child(%0A              
 Debu
@@ -1400,35 +1400,16 @@
 reate(c)
-%0A                  
 .message
@@ -1430,290 +1430,58 @@
 ry%22)
-%0A                  .throwable(error.get())%0A                  .build())%0A          .build();%0A    %7D%0A%0A    return child;%0A  %7D%0A%0A  @OnCreateInitialState%0A  static void createInitialState(ComponentContext c, StateValue%3COptional%3CException%3E%3E error) %7B%0A%0A    error.set(Optional.%3CException%3Eempty())
+.throwable(error).build();%0A    %7D%0A%0A    return child
 ;%0A  
@@ -1541,17 +1541,8 @@
 lue%3C
-Optional%3C
 Exce
@@ -1547,17 +1547,16 @@
 ception%3E
-%3E
  error, 
@@ -1595,22 +1595,9 @@
 set(
-Optional.of(e)
+e
 );%0A 

Simplify OnError sample in the sample app