Skip to content

Instantly share code, notes, and snippets.

@esilverberg
Created November 24, 2020 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esilverberg/45d1f5f4da5a5630201bb9f94d347737 to your computer and use it in GitHub Desktop.
Save esilverberg/45d1f5f4da5a5630201bb9f94d347737 to your computer and use it in GitHub Desktop.
// 🍎
final class PSSMatchLogic {
private let accountRepository: PSSAccountRepository
private let matchRepository: PSSMatchRepository
private let locationRepository: PSSLocationRepository
private let featureRepository: PSSFeatureRepository
init(accountRepository: PSSAccountRepository, matchRepository: PSSMatchRepository, locationRepository: PSSLocationRepository, featureRepository: PSSFeatureRepository) {
self.accountRepository = accountRepository
self.matchRepository = matchRepository
self.locationRepository = locationRepository
self.featureRepository = featureRepository
}
var myProfile: Property<PSSProfile> {
return accountRepository.profile
}
private let mutualMatchPipe = Signal<PSSProfile, Never>.pipe()
var mutualMatchEvents: Signal<PSSProfile, Never> {
return mutualMatchPipe.output
}
func matchStack(location: CLLocationCoordinate2D, loadMore: Bool) -> SignalProducer<PSSMatchStack, Error> {
return matchRepository.matchStack(location: location, browseMode: browseMode, loadMore: loadMore).map { [weak self] (matchStack) -> PSSMatchStack in
guard let self = self else { return matchStack }
if self.matchRepository.hasShownInstructions == false {
self.matchRepository.hasShownInstructions = true
var matchStackWithTutorial = matchStack
matchStackWithTutorial.results.insert(.instructions(rating: .askMeTomorrow), at: 0)
matchStackWithTutorial.results.insert(.instructions(rating: .notInterested), at: 0)
matchStackWithTutorial.results.insert(.instructions(rating: .interested), at: 0)
return matchStackWithTutorial
} else {
return matchStack
}
}
}
func rate(profile: PSSProfile, rating: PSSProfileRating) -> SignalProducer<Void, Error> {
if rating == .definitely && profile.hisRating == .definitely {
mutualMatchPipe.input.send(value: profile)
}
return matchRepository.rate(profile: profile, rating: rating)
}
var restartMatch: Signal<Void, Never> {
let isPro = accountRepository.isPro.signal.map { _ in () }
let profile = accountRepository.profile.signal.map { _ in () }
return profile.merge(with: isPro).take(duringLifetimeOf: self)
}
<snip/>
// 🤖
class MatchLogic(private val accountRepository: AccountRepository,
private val matchRepository: MatchRepository,
private val locationRepository: LocationRepository,
private val featureRepository: FeatureRepository) {
val myProfile: Observable<Profile>
get() = accountRepository.profileObservable
private val mutualMatchPubsub: PublishSubject<Profile> = PublishSubject.create()
val mutualMatchEvents: Observable<Profile> = mutualMatchPubsub
// MatchApi calls
fun getMatchStack(location: PSSLocation, loadMore: Boolean): Single<MatchStack> {
return matchRepository.getMatchStack(location, loadMore).doOnSuccess {
// Add instructions cards only if we haven't
// shown them before and the stack is not empty
if (!matchRepository.hasShownInstructions) {
matchRepository.hasShownInstructions = true
it.items.push(InstructionCard(MatchRating.AskMeTomorrow))
it.items.push(InstructionCard(MatchRating.NotForMe))
it.items.push(InstructionCard(MatchRating.Interested))
}
}
}
fun rate(profile: Profile, profileRating: Profile.ProfileRating): Completable {
if (profileRating == Profile.ProfileRating.Definitely && profile.hisRating == Profile.ProfileRating.Definitely.ordinal) {
mutualMatchPubsub.onNext(profile)
}
return matchRepository.rate(profile, profileRating)
}
fun restartMatch(): Observable<Unit> {
val proStatusUpdates = accountRepository.isProObservable.map { Unit }
val profileUpdates = accountRepository.profileObservable.map { Unit }
return profileUpdates.mergeWith(proStatusUpdates)
}
<snip/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment