/* * Twidere - Twitter client for Android * * Copyright (C) 2012-2014 Mariotaku Lee * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ package org.mariotaku.twidere.util import android.content.Context import android.content.SharedPreferences import com.bumptech.glide.Glide import org.mariotaku.kpreferences.get import org.mariotaku.twidere.constant.mediaPreloadKey import org.mariotaku.twidere.constant.mediaPreloadOnWifiOnlyKey import org.mariotaku.twidere.extension.loadProfileImage import org.mariotaku.twidere.model.ParcelableActivity import org.mariotaku.twidere.model.ParcelableMedia import org.mariotaku.twidere.model.ParcelableStatus import org.mariotaku.twidere.model.util.getActivityStatus class MediaPreloader(val context: Context) { var isNetworkMetered: Boolean = true private var preloadEnabled: Boolean = false private var preloadOnWifiOnly: Boolean = true private val shouldPreload: Boolean get() = preloadEnabled && (!preloadOnWifiOnly || !isNetworkMetered) fun preloadStatus(status: ParcelableStatus) { if (!shouldPreload) return Glide.with(context).loadProfileImage(context, status, 0).preload() preloadMedia(status.media) preloadMedia(status.quoted_media) } fun preloadActivity(activity: ParcelableActivity) { if (!shouldPreload) return activity.getActivityStatus()?.let { preloadStatus(it) } } fun reloadOptions(preferences: SharedPreferences) { preloadEnabled = preferences[mediaPreloadKey] preloadOnWifiOnly = preferences[mediaPreloadOnWifiOnlyKey] } private fun preloadMedia(media: Array?) { media?.forEach { item -> val url = item.preview_url ?: run { if (item.type != ParcelableMedia.Type.IMAGE) return@run null return@run item.media_url } ?: return@forEach preloadPreviewImage(url) } } private fun preloadPreviewImage(url: String?) { Glide.with(context).load(url).preload() } }