// Path: app/src/main/java/com/chrynan/android_guitar_tuner/tuner/PitchPlayer.java
// public interface PitchPlayer {
// 
//     /**
//      * Starts playing the provided frequency and returns a {@link Completable} that performs the
//      * playing of the frequency.
//      *
//      * @param frequency The frequency to be played.
//      * @return A {@link Completable} running the playing operation until unsubscribed.
//      */
//     Completable startPlaying(double frequency);
// }
// 
// Path: app/src/main/java/com/chrynan/android_guitar_tuner/tuner/volume/VolumeObserver.java
// public interface VolumeObserver {
// 
//     /**
//      * Subscribes to the volume observer for volume states, either on an interval or changes to the
//      * devices volume state.
//      *
//      * @return An {@link Observable<VolumeState>} providing the volume states as long as subscribed.
//      */
//     Observable<VolumeState> startListening();
// }
// 
// Path: app/src/main/java/com/chrynan/android_guitar_tuner/tuner/volume/VolumeState.java
// public enum VolumeState {
// 
//     MUTED,
//     UNMUTED;
// 
//     /**
//      * Retrieves a {@link VolumeState} for the provided volume.
//      *
//      * @param volume The volume.
//      * @return A {@link VolumeState} representing the provided volume.
//      */
//     public static VolumeState forVolume(final int volume) {
//         if (volume < 1) {
//             return MUTED;
//         }
// 
//         return UNMUTED;
//     }
// }
// 
// Path: app/src/main/java/com/chrynan/android_guitar_tuner/ui/view/PitchView.java
// public interface PitchView {
// 
//     void onUpdateVolumeState(@StringRes int volumeStateText, @ColorRes int textColor);
// 
//     void onErrorPlayingNote(@StringRes int errorDescription, boolean showAction, @StringRes int errorAction, @ColorRes int actionColor);
// }


import com.chrynan.android_guitar_tuner.R;
import com.chrynan.android_guitar_tuner.tuner.PitchPlayer;
import com.chrynan.android_guitar_tuner.tuner.volume.VolumeObserver;
import com.chrynan.android_guitar_tuner.tuner.volume.VolumeState;
import com.chrynan.android_guitar_tuner.ui.view.PitchView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import timber.log.Timber;
package com.chrynan.android_guitar_tuner.presenter;



public class PitchPresenter implements Presenter {

    private static final int RETRY_COUNT_MAX = 3;

    private final PitchView view;
    private final PitchPlayer pitchPlayer;
    private final VolumeObserver volumeObserver;

    private Disposable pitchPlayerDisposable;
    private Disposable volumeDisposable;

    private int retryCount = 0;

    public PitchPresenter(final PitchView view, final PitchPlayer pitchPlayer, final VolumeObserver volumeObserver) {
        this.view = view;
        this.pitchPlayer = pitchPlayer;
        this.volumeObserver = volumeObserver;
    }

    @Override
    public void detachView() {
        // No Operation
    }

    public void startPlayingNote(final double noteFrequency) {
        pitchPlayerDisposable = pitchPlayer.startPlaying(noteFrequency)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(() -> {
                }, error -> {
                    Timber.e(error, "Error Starting Pitch Playback.");

                    boolean showAction = retryCount < RETRY_COUNT_MAX;
// Path: app/src/main/java/com/chrynan/android_guitar_tuner/tuner/PitchPlayer.java
// public interface PitchPlayer {
// 
//     /**
//      * Starts playing the provided frequency and returns a {@link Completable} that performs the
//      * playing of the frequency.
//      *
//      * @param frequency The frequency to be played.
//      * @return A {@link Completable} running the playing operation until unsubscribed.
//      */
//     Completable startPlaying(double frequency);
// }
// 
// Path: app/src/main/java/com/chrynan/android_guitar_tuner/tuner/volume/VolumeObserver.java
// public interface VolumeObserver {
// 
//     /**
//      * Subscribes to the volume observer for volume states, either on an interval or changes to the
//      * devices volume state.
//      *
//      * @return An {@link Observable<VolumeState>} providing the volume states as long as subscribed.
//      */
//     Observable<VolumeState> startListening();
// }
// 
// Path: app/src/main/java/com/chrynan/android_guitar_tuner/tuner/volume/VolumeState.java
// public enum VolumeState {
// 
//     MUTED,
//     UNMUTED;
// 
//     /**
//      * Retrieves a {@link VolumeState} for the provided volume.
//      *
//      * @param volume The volume.
//      * @return A {@link VolumeState} representing the provided volume.
//      */
//     public static VolumeState forVolume(final int volume) {
//         if (volume < 1) {
//             return MUTED;
//         }
// 
//         return UNMUTED;
//     }
// }
// 
// Path: app/src/main/java/com/chrynan/android_guitar_tuner/ui/view/PitchView.java
// public interface PitchView {
// 
//     void onUpdateVolumeState(@StringRes int volumeStateText, @ColorRes int textColor);
// 
//     void onErrorPlayingNote(@StringRes int errorDescription, boolean showAction, @StringRes int errorAction, @ColorRes int actionColor);
// }


// Path: app/src/main/java/com/chrynan/android_guitar_tuner/presenter/PitchPresenter.java
import com.chrynan.android_guitar_tuner.R;
import com.chrynan.android_guitar_tuner.tuner.PitchPlayer;
import com.chrynan.android_guitar_tuner.tuner.volume.VolumeObserver;
import com.chrynan.android_guitar_tuner.tuner.volume.VolumeState;
import com.chrynan.android_guitar_tuner.ui.view.PitchView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import timber.log.Timber;

package com.chrynan.android_guitar_tuner.presenter;



public class PitchPresenter implements Presenter {

    private static final int RETRY_COUNT_MAX = 3;

    private final PitchView view;
    private final PitchPlayer pitchPlayer;
    private final VolumeObserver volumeObserver;

    private Disposable pitchPlayerDisposable;
    private Disposable volumeDisposable;

    private int retryCount = 0;

    public PitchPresenter(final PitchView view, final PitchPlayer pitchPlayer, final VolumeObserver volumeObserver) {
        this.view = view;
        this.pitchPlayer = pitchPlayer;
        this.volumeObserver = volumeObserver;
    }

    @Override
    public void detachView() {
        // No Operation
    }

    public void startPlayingNote(final double noteFrequency) {
        pitchPlayerDisposable = pitchPlayer.startPlaying(noteFrequency)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(() -> {
                }, error -> {
                    Timber.e(error, "Error Starting Pitch Playback.");

                    boolean showAction = retryCount < RETRY_COUNT_MAX;
                    int description = showAction ? R.string.pitch_player_error_playing_note_description_with_action