-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Advances tour stop when it's audio playback ends (AIC-555) #317
Changes from all commits
74376ee
68602a1
bf48d23
43a385f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,25 @@ | ||
package edu.artic.tours.manager | ||
|
||
import android.annotation.SuppressLint | ||
import android.support.annotation.WorkerThread | ||
import com.fuzz.rx.Optional | ||
import com.fuzz.rx.filterValue | ||
import edu.artic.db.INTRO_TOUR_STOP_OBJECT_ID | ||
import edu.artic.db.daos.ArticAudioFileDao | ||
import edu.artic.db.models.ArticTour | ||
import edu.artic.db.models.AudioFileModel | ||
import io.reactivex.rxkotlin.Observables | ||
import io.reactivex.rxkotlin.subscribeBy | ||
import io.reactivex.subjects.BehaviorSubject | ||
import io.reactivex.subjects.PublishSubject | ||
import io.reactivex.subjects.Subject | ||
|
||
|
||
import timber.log.Timber | ||
|
||
/** | ||
* Responsible for managing the communication between tour carousel and the map. | ||
* @author Sameer Dhakal (Fuzz) | ||
*/ | ||
class TourProgressManager { | ||
class TourProgressManager(val audioFileDao: ArticAudioFileDao) { | ||
/** | ||
* Selected ArticObject | ||
*/ | ||
|
@@ -23,4 +30,61 @@ class TourProgressManager { | |
val selectedTour: Subject<Optional<ArticTour>> = BehaviorSubject.createDefault(Optional(null)) | ||
val proposedTour: Subject<Optional<Pair<ArticTour, ArticTour.TourStop>>> = BehaviorSubject.createDefault(Optional(null)) | ||
val leaveTourRequest: Subject<Boolean> = PublishSubject.create() | ||
|
||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Method There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Method There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Method There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Method |
||
* If [audioFileModel] belongs to currently selected tour i.e. [selectedTour], | ||
* this method advances [selectedStop] to next tour stop. | ||
*/ | ||
@WorkerThread | ||
@SuppressLint("CheckResult") | ||
fun playBackEnded(audioFileModel: AudioFileModel) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea. Updated. |
||
Observables.combineLatest(selectedTour.filterValue(), selectedStop) | ||
.take(1) | ||
.subscribe { (tour, currentStopID) -> | ||
|
||
/** | ||
* Get audio file id | ||
*/ | ||
val audioFileID: String? = if (currentStopID == INTRO_TOUR_STOP_OBJECT_ID) { | ||
tour.tourAudio | ||
} else { | ||
tour.tourStops.find { it.objectId == currentStopID }?.audioId | ||
} | ||
|
||
audioFileID?.let { audioID -> | ||
audioFileDao | ||
.getAudioByIdAsync(audioID) | ||
Cliabhach marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.toObservable() | ||
.subscribeBy( | ||
onNext = { audioFile -> | ||
|
||
/** | ||
* Check if currently ended audio playback session belongs to | ||
* current tour stop. | ||
*/ | ||
val isCurrentStopsAudioTranslation = audioFile | ||
.allTranslations() | ||
.contains(audioFileModel) | ||
|
||
/** | ||
* Advance [selectedStop] if current audio translation playback | ||
* completed. | ||
*/ | ||
if (isCurrentStopsAudioTranslation) { | ||
val indexOfCurrentTourStop = tour.tourStops.indexOfFirst { it.objectId == currentStopID } | ||
val nextStopIndex = Math.min(indexOfCurrentTourStop + 1, tour.tourStops.size) | ||
tour.tourStops[nextStopIndex].objectId?.let { | ||
selectedStop.onNext(it) | ||
} | ||
} | ||
}, | ||
onError = { t -> | ||
Timber.e(t, "Couldn't fetch audio by $audioID") | ||
}) | ||
} | ||
} | ||
|
||
} | ||
|
||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Method
changeAudio
has 28 lines of code (exceeds 25 allowed). Consider refactoring.