package com.discord.stores; import android.content.Context; import c.d.b.a.a; import com.discord.models.domain.ModelApplicationStream; import com.discord.stores.updates.ObservationDeck; import com.discord.stores.updates.ObservationDeckProvider; import com.discord.utilities.error.Error; import com.discord.utilities.rest.RestAPI; import com.discord.utilities.rx.ObservableExtensionsKt; import com.discord.utilities.streams.StreamContext; import com.discord.utilities.time.Clock; import d0.t.h0; import d0.z.d.m; import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; import kotlin.jvm.functions.Function0; import kotlin.jvm.functions.Function1; import kotlin.jvm.internal.DefaultConstructorMarker; import retrofit2.HttpException; import rx.Observable; import rx.Subscription; /* compiled from: StoreApplicationStreamPreviews.kt */ public final class StoreApplicationStreamPreviews extends StoreV2 { private static final Companion Companion = new Companion(null); @Deprecated private static final long READ_PREVIEW_DEFAULT_RETRY_DELAY_MS = 10000; private final Clock clock; private final Dispatcher dispatcher; private final HashMap fetchAttempts; private final HashMap fetchStreamPreviewSubscriptions; private final ObservationDeck observationDeck; private final RestAPI restAPI; private final Map streamKeyToPreviewMap; private Map streamKeyToPreviewMapSnapshot; /* compiled from: StoreApplicationStreamPreviews.kt */ public static final class Companion { private Companion() { } public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) { this(); } } /* compiled from: StoreApplicationStreamPreviews.kt */ public static abstract class StreamPreview { /* compiled from: StoreApplicationStreamPreviews.kt */ public static final class Fetching extends StreamPreview { public static final Fetching INSTANCE = new Fetching(); private Fetching() { super(null); } } /* compiled from: StoreApplicationStreamPreviews.kt */ public static final class Resolved extends StreamPreview { private final String url; public Resolved(String str) { super(null); this.url = str; } public static /* synthetic */ Resolved copy$default(Resolved resolved, String str, int i, Object obj) { if ((i & 1) != 0) { str = resolved.url; } return resolved.copy(str); } public final String component1() { return this.url; } public final Resolved copy(String str) { return new Resolved(str); } public boolean equals(Object obj) { if (this != obj) { return (obj instanceof Resolved) && m.areEqual(this.url, ((Resolved) obj).url); } return true; } public final String getUrl() { return this.url; } public int hashCode() { String str = this.url; if (str != null) { return str.hashCode(); } return 0; } public String toString() { return a.C(a.K("Resolved(url="), this.url, ")"); } } private StreamPreview() { } public /* synthetic */ StreamPreview(DefaultConstructorMarker defaultConstructorMarker) { this(); } } public StoreApplicationStreamPreviews(Dispatcher dispatcher, Clock clock, RestAPI restAPI, ObservationDeck observationDeck) { m.checkNotNullParameter(dispatcher, "dispatcher"); m.checkNotNullParameter(clock, "clock"); m.checkNotNullParameter(restAPI, "restAPI"); m.checkNotNullParameter(observationDeck, "observationDeck"); this.dispatcher = dispatcher; this.clock = clock; this.restAPI = restAPI; this.observationDeck = observationDeck; this.streamKeyToPreviewMap = new HashMap(); this.streamKeyToPreviewMapSnapshot = h0.emptyMap(); this.fetchAttempts = new HashMap<>(); this.fetchStreamPreviewSubscriptions = new HashMap<>(); } /* JADX INFO: this call moved to the top of the method (can break code semantics) */ public /* synthetic */ StoreApplicationStreamPreviews(Dispatcher dispatcher, Clock clock, RestAPI restAPI, ObservationDeck observationDeck, int i, DefaultConstructorMarker defaultConstructorMarker) { this(dispatcher, clock, (i & 4) != 0 ? RestAPI.Companion.getApi() : restAPI, (i & 8) != 0 ? ObservationDeckProvider.get() : observationDeck); } public static final /* synthetic */ void access$fetchStreamPreviewIfNotFetching(StoreApplicationStreamPreviews storeApplicationStreamPreviews, String str) { storeApplicationStreamPreviews.fetchStreamPreviewIfNotFetching(str); } public static final /* synthetic */ Dispatcher access$getDispatcher$p(StoreApplicationStreamPreviews storeApplicationStreamPreviews) { return storeApplicationStreamPreviews.dispatcher; } public static final /* synthetic */ HashMap access$getFetchStreamPreviewSubscriptions$p(StoreApplicationStreamPreviews storeApplicationStreamPreviews) { return storeApplicationStreamPreviews.fetchStreamPreviewSubscriptions; } private final void fetchStreamPreview(String str) { this.dispatcher.schedule(new StoreApplicationStreamPreviews$fetchStreamPreview$1(this, str)); Subscription subscription = this.fetchStreamPreviewSubscriptions.get(str); if (subscription != null) { subscription.unsubscribe(); } ObservableExtensionsKt.appSubscribe$default(ObservableExtensionsKt.restSubscribeOn(this.restAPI.getStreamPreview(str, this.clock.currentTimeMillis()), false), StoreApplicationStreamPreviews.class, (Context) null, new StoreApplicationStreamPreviews$fetchStreamPreview$2(this, str), new StoreApplicationStreamPreviews$fetchStreamPreview$4(this, str), new StoreApplicationStreamPreviews$fetchStreamPreview$5(this, str), (Function0) null, new StoreApplicationStreamPreviews$fetchStreamPreview$3(this, str), 34, (Object) null); } private final void fetchStreamPreviewIfNotFetching(StreamPreview streamPreview, String str) { if (streamPreview == null || !(streamPreview instanceof StreamPreview.Fetching)) { fetchStreamPreview(str); } } @StoreThread private final void fetchStreamPreviewIfNotFetching(String str) { fetchStreamPreviewIfNotFetching(this.streamKeyToPreviewMap.get(str), str); } public final void fetchStreamPreviewIfNotFetching(StreamContext streamContext) { m.checkNotNullParameter(streamContext, "streamContext"); fetchStreamPreviewIfNotFetching(streamContext.getPreview(), streamContext.getStream().getEncodedStreamKey()); } /* JADX DEBUG: Type inference failed for r0v0. Raw type applied. Possible types: java.util.Map, java.util.Map */ public final Map getStreamKeyToPreviewMap() { return this.streamKeyToPreviewMapSnapshot; } @StoreThread public final void handleFetchFailed(String str, Error error) { m.checkNotNullParameter(str, "streamKey"); m.checkNotNullParameter(error, "error"); Long l = null; this.streamKeyToPreviewMap.put(str, new StreamPreview.Resolved(null)); Throwable throwable = error.getThrowable(); if (!(throwable instanceof HttpException)) { throwable = null; } HttpException httpException = (HttpException) throwable; Integer valueOf = httpException != null ? Integer.valueOf(httpException.a()) : null; Error.Response response = error.getResponse(); m.checkNotNullExpressionValue(response, "error.response"); Long retryAfterMs = response.getRetryAfterMs(); Integer num = this.fetchAttempts.get(str); if (num == null) { num = 0; } m.checkNotNullExpressionValue(num, "fetchAttempts[streamKey] ?: 0"); int intValue = num.intValue() + 1; long j = ((long) intValue) * 10000; if (valueOf == null) { l = Long.valueOf(j); } else if (valueOf.intValue() == 429) { if (retryAfterMs != null) { j = retryAfterMs.longValue(); } l = Long.valueOf(j); } else if (!(valueOf.intValue() == 401 || valueOf.intValue() == 403)) { l = Long.valueOf(j); } if (l != null) { Subscription subscription = this.fetchStreamPreviewSubscriptions.get(str); if (subscription != null) { subscription.unsubscribe(); } Observable d02 = Observable.d0(l.longValue(), TimeUnit.MILLISECONDS); m.checkNotNullExpressionValue(d02, "Observable.timer(retryAf…s, TimeUnit.MILLISECONDS)"); ObservableExtensionsKt.appSubscribe$default(d02, StoreApplicationStreamPreviews.class, (Context) null, new StoreApplicationStreamPreviews$handleFetchFailed$1(this, str), (Function1) null, new StoreApplicationStreamPreviews$handleFetchFailed$2(this, str), (Function0) null, new StoreApplicationStreamPreviews$handleFetchFailed$3(this, str), 42, (Object) null); } this.fetchAttempts.put(str, Integer.valueOf(intValue)); markChanged(); } @StoreThread public final void handleFetchStart(String str) { m.checkNotNullParameter(str, "streamKey"); this.streamKeyToPreviewMap.put(str, StreamPreview.Fetching.INSTANCE); markChanged(); } @StoreThread public final void handleFetchSuccess(String str, String str2) { m.checkNotNullParameter(str, "streamKey"); m.checkNotNullParameter(str2, "url"); this.streamKeyToPreviewMap.put(str, new StreamPreview.Resolved(str2)); this.fetchAttempts.remove(str); markChanged(); } public final Observable observeStreamPreview(ModelApplicationStream modelApplicationStream) { m.checkNotNullParameter(modelApplicationStream, "applicationStream"); return observeStreamPreview(modelApplicationStream.getEncodedStreamKey()); } public final Observable observeStreamPreview(String str) { m.checkNotNullParameter(str, "streamKey"); Observable r = ObservationDeck.connectRx$default(this.observationDeck, new ObservationDeck.UpdateSource[]{this}, false, null, null, new StoreApplicationStreamPreviews$observeStreamPreview$1(this, str), 14, null).r(); m.checkNotNullExpressionValue(r, "observationDeck.connectR… }.distinctUntilChanged()"); return r; } @Override // com.discord.stores.StoreV2 @StoreThread public void snapshotData() { super.snapshotData(); this.streamKeyToPreviewMapSnapshot = new HashMap(this.streamKeyToPreviewMap); } }