// Path: deferred/src/main/java/com/tuenti/deferred/Promise.java
// enum State {
// 	/**
// 	 * The Promise is still pending - it could be created, submitted for execution,
// 	 * or currently running, but not yet finished.
// 	 */
// 	PENDING,
// 
// 	/**
// 	 * The Promise has finished running and a failure occurred.
// 	 * Thus, the Promise is rejected.
// 	 *
// 	 * @see Deferred#reject(Object)
// 	 */
// 	REJECTED,
// 
// 	/**
// 	 * The Promise has finished running successfully.
// 	 * Thus the Promise is resolved.
// 	 *
// 	 * @see Deferred#resolve(Object)
// 	 */
// 	RESOLVED
// }
// 
// Path: deferred/src/main/java/com/tuenti/deferred/context/ViewContextHolder.java
// public class ViewContextHolder<V> {
// 
// 	public static final ViewContextHolder NOP = new ViewContextHolder<>(new Object());
// 
// 	private final WeakReference<V> reference;
// 
// 	@SuppressWarnings("unchecked")
// 	public static <V> ViewContextHolder<V> of(V viewContext) {
// 		ViewContextHolder viewContextHolder;
// 		if (viewContext instanceof Activity) {
// 			viewContextHolder = new ActivityContextHolder<>((AppCompatActivity) viewContext);
// 		} else if (viewContext instanceof Fragment) {
// 			viewContextHolder = new FragmentContextHolder<>((Fragment) viewContext);
// 		} else {
// 			viewContextHolder = new ViewContextHolder(viewContext);
// 		}
// 		return viewContextHolder;
// 	}
// 
// 	protected ViewContextHolder(V viewContext) {
// 		reference = new WeakReference<>(viewContext);
// 	}
// 
// 	public boolean isValid() {
// 		return reference.get() != null;
// 	}
// 
// 	public V get() {
// 		return reference.get();
// 	}
// 
// }


import com.tuenti.deferred.Promise.State;
import com.tuenti.deferred.context.ViewContextHolder;
/*
 * Copyright (c) Tuenti Technologies S.L. All rights reserved.
 *
 * 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 com.tuenti.deferred;


/**
 * @author Rayco Araña <rayco@tuenti.com>
 */
public interface Always {


	interface Immediately<D, R> extends AlwaysCallback<D, R> {

	}

	/**
	 * Please use UIAlwaysContextualCallback if your callback depends on a view
	 */
	interface UI<D, R> extends AlwaysCallback<D, R>, UICallback {

	}

	abstract class UIContextual<D, R, V> implements Always.UI<D, R> {

		private final ViewContextHolder<V> viewContextHolder;

		public UIContextual(V view) {
			this.viewContextHolder = ViewContextHolder.of(view);
		}

		public final void onAlways(final State state, final D resolved, final R rejected) {
			if (viewContextHolder.isValid()) {
				onAlwaysWithContext(state, resolved, rejected);
			}
		}

		public V view() {
			return viewContextHolder.get();
		}

// Path: deferred/src/main/java/com/tuenti/deferred/Promise.java
// enum State {
// 	/**
// 	 * The Promise is still pending - it could be created, submitted for execution,
// 	 * or currently running, but not yet finished.
// 	 */
// 	PENDING,
// 
// 	/**
// 	 * The Promise has finished running and a failure occurred.
// 	 * Thus, the Promise is rejected.
// 	 *
// 	 * @see Deferred#reject(Object)
// 	 */
// 	REJECTED,
// 
// 	/**
// 	 * The Promise has finished running successfully.
// 	 * Thus the Promise is resolved.
// 	 *
// 	 * @see Deferred#resolve(Object)
// 	 */
// 	RESOLVED
// }
// 
// Path: deferred/src/main/java/com/tuenti/deferred/context/ViewContextHolder.java
// public class ViewContextHolder<V> {
// 
// 	public static final ViewContextHolder NOP = new ViewContextHolder<>(new Object());
// 
// 	private final WeakReference<V> reference;
// 
// 	@SuppressWarnings("unchecked")
// 	public static <V> ViewContextHolder<V> of(V viewContext) {
// 		ViewContextHolder viewContextHolder;
// 		if (viewContext instanceof Activity) {
// 			viewContextHolder = new ActivityContextHolder<>((AppCompatActivity) viewContext);
// 		} else if (viewContext instanceof Fragment) {
// 			viewContextHolder = new FragmentContextHolder<>((Fragment) viewContext);
// 		} else {
// 			viewContextHolder = new ViewContextHolder(viewContext);
// 		}
// 		return viewContextHolder;
// 	}
// 
// 	protected ViewContextHolder(V viewContext) {
// 		reference = new WeakReference<>(viewContext);
// 	}
// 
// 	public boolean isValid() {
// 		return reference.get() != null;
// 	}
// 
// 	public V get() {
// 		return reference.get();
// 	}
// 
// }


// Path: deferred/src/main/java/com/tuenti/deferred/Always.java
import com.tuenti.deferred.Promise.State;
import com.tuenti.deferred.context.ViewContextHolder;

/*
 * Copyright (c) Tuenti Technologies S.L. All rights reserved.
 *
 * 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 com.tuenti.deferred;


/**
 * @author Rayco Araña <rayco@tuenti.com>
 */
public interface Always {


	interface Immediately<D, R> extends AlwaysCallback<D, R> {

	}

	/**
	 * Please use UIAlwaysContextualCallback if your callback depends on a view
	 */
	interface UI<D, R> extends AlwaysCallback<D, R>, UICallback {

	}

	abstract class UIContextual<D, R, V> implements Always.UI<D, R> {

		private final ViewContextHolder<V> viewContextHolder;

		public UIContextual(V view) {
			this.viewContextHolder = ViewContextHolder.of(view);
		}

		public final void onAlways(final State state, final D resolved, final R rejected) {
			if (viewContextHolder.isValid()) {
				onAlwaysWithContext(state, resolved, rejected);
			}
		}

		public V view() {
			return viewContextHolder.get();
		}

		public abstract void onAlwaysWithContext(final State state, final D resolved, final R rejected);