// Path: src/main/java/com/xemantic/ankh/shared/event/Sink.java
// public interface Sink<T> {
// 
//   void publish(T event);
// 
//   /**
//    * Exposes an {@link Observer} as a {@link Sink}.
//    *
//    * @param observer the observer.
//    * @param <T> the event type.
//    * @return the event sink.
//    */
//   static  <T> Sink<T> of(Observer<T> observer) {
//     Objects.requireNonNull(observer, "observer cannot be null");
//     return observer::onNext;
//   }
// 
// }
// 
// Path: src/main/java/com/xemantic/ankh/shared/event/SnackbarMessageEvent.java
// @Event
// public class SnackbarMessageEvent {
// 
//   private final String message;
// 
//   public SnackbarMessageEvent(String message) {
//     this.message = Objects.requireNonNull(message);
//   }
// 
//   public String getMessage() {
//     return message;
//   }
// 
// }
// 
// Path: src/main/java/com/xemantic/ankh/shared/snackbar/SnackbarView.java
// public interface SnackbarView {
// 
//   void show(String message);
// 
// }


import com.xemantic.ankh.shared.event.Sink;
import com.xemantic.ankh.shared.event.SnackbarMessageEvent;
import com.xemantic.ankh.shared.snackbar.SnackbarView;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.logging.Level;
import java.util.logging.Logger;
/*
 * github-users - lists GitHub users. Minimal app demonstrating
 * cross-platform app development (Web, Android, iOS) where core
 * logic is shared and transpiled from Java to JavaScript and
 * Objective-C. This project delivers core application logic.
 *
 * Copyright (C) 2017  Kazimierz Pogoda
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.xemantic.ankh.shared.error;



/**
 * Standardized exception handler. It should be set globally with
 * {@link Thread#setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler)}.
 * The handler will log the exception, check if there is a user friendly message
 * for the exception, and if so it will display it on the {@link SnackbarView}.
 * The logger can be configured to log thrown exception remotely.
 *
 * @author morisil
 */
@Singleton
public class AnkhExceptionHandler implements Thread.UncaughtExceptionHandler {

  private final Logger logger;

  private final ErrorMessageProvider errorMessageProvider;

  private final Sink<SnackbarMessageEvent> snackbarMessageSink;

  @Inject
  public AnkhExceptionHandler(
      Logger logger,
      ErrorMessageProvider errorMessageProvider,
      Sink<SnackbarMessageEvent> snackbarMessageSink
  ) {
    this.logger = logger;
    this.snackbarMessageSink = snackbarMessageSink;
    this.errorMessageProvider = errorMessageProvider;
  }

  @Override
  public void uncaughtException(Thread thread, Throwable throwable) {
    logger.log(Level.SEVERE, "Uncaught Exception", throwable);
    errorMessageProvider.getMessage(throwable)
// Path: src/main/java/com/xemantic/ankh/shared/event/Sink.java
// public interface Sink<T> {
// 
//   void publish(T event);
// 
//   /**
//    * Exposes an {@link Observer} as a {@link Sink}.
//    *
//    * @param observer the observer.
//    * @param <T> the event type.
//    * @return the event sink.
//    */
//   static  <T> Sink<T> of(Observer<T> observer) {
//     Objects.requireNonNull(observer, "observer cannot be null");
//     return observer::onNext;
//   }
// 
// }
// 
// Path: src/main/java/com/xemantic/ankh/shared/event/SnackbarMessageEvent.java
// @Event
// public class SnackbarMessageEvent {
// 
//   private final String message;
// 
//   public SnackbarMessageEvent(String message) {
//     this.message = Objects.requireNonNull(message);
//   }
// 
//   public String getMessage() {
//     return message;
//   }
// 
// }
// 
// Path: src/main/java/com/xemantic/ankh/shared/snackbar/SnackbarView.java
// public interface SnackbarView {
// 
//   void show(String message);
// 
// }


// Path: src/main/java/com/xemantic/ankh/shared/error/AnkhExceptionHandler.java
import com.xemantic.ankh.shared.event.Sink;
import com.xemantic.ankh.shared.event.SnackbarMessageEvent;
import com.xemantic.ankh.shared.snackbar.SnackbarView;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * github-users - lists GitHub users. Minimal app demonstrating
 * cross-platform app development (Web, Android, iOS) where core
 * logic is shared and transpiled from Java to JavaScript and
 * Objective-C. This project delivers core application logic.
 *
 * Copyright (C) 2017  Kazimierz Pogoda
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.xemantic.ankh.shared.error;



/**
 * Standardized exception handler. It should be set globally with
 * {@link Thread#setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler)}.
 * The handler will log the exception, check if there is a user friendly message
 * for the exception, and if so it will display it on the {@link SnackbarView}.
 * The logger can be configured to log thrown exception remotely.
 *
 * @author morisil
 */
@Singleton
public class AnkhExceptionHandler implements Thread.UncaughtExceptionHandler {

  private final Logger logger;

  private final ErrorMessageProvider errorMessageProvider;

  private final Sink<SnackbarMessageEvent> snackbarMessageSink;

  @Inject
  public AnkhExceptionHandler(
      Logger logger,
      ErrorMessageProvider errorMessageProvider,
      Sink<SnackbarMessageEvent> snackbarMessageSink
  ) {
    this.logger = logger;
    this.snackbarMessageSink = snackbarMessageSink;
    this.errorMessageProvider = errorMessageProvider;
  }

  @Override
  public void uncaughtException(Thread thread, Throwable throwable) {
    logger.log(Level.SEVERE, "Uncaught Exception", throwable);
    errorMessageProvider.getMessage(throwable)
        .map(SnackbarMessageEvent::new)