Skip to content

Commit

Permalink
Improve loading wallpapers on first run
Browse files Browse the repository at this point in the history
  • Loading branch information
jahirfiquitiva committed Nov 8, 2020
1 parent 2948e44 commit 3554402
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 5 deletions.
7 changes: 6 additions & 1 deletion library/src/main/kotlin/dev/jahir/frames/data/Preferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ open class Preferences(private val context: Context) {
@SuppressLint("CommitPrefEdits")
val prefsEditor: SharedPreferences.Editor = prefs.edit()

var isFirstRun: Boolean
get() = prefs.getBoolean(FIRST_RUN, true)
set(_) = prefsEditor.putBoolean(FIRST_RUN, false).apply()

var lastVersion: Long
get() = prefs.getLong(LAST_VERSION, -1L)
set(value) = prefsEditor.putLong(LAST_VERSION, value).apply()
Expand Down Expand Up @@ -116,6 +120,7 @@ open class Preferences(private val context: Context) {

companion object {
private const val PREFS_NAME = "jfdb_confs"
private const val FIRST_RUN = "first_run"
private const val LAST_VERSION = "last_version"
internal const val CURRENT_THEME = "current_theme"
internal const val USES_AMOLED_THEME = "uses_amoled_theme"
Expand All @@ -129,4 +134,4 @@ open class Preferences(private val context: Context) {
private const val REFRESH_MUZEI_ON_WIFI_ONLY = "refresh_muzei_on_wifi_only"
private const val MUZEI_COLLECTIONS = "muzei_collections"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,15 @@ fun Context.getAppName(): String {
return if (def.hasContent()) def else "Unknown"
}

val Context.isFirstRun: Boolean
get() {
return try {
preferences.isFirstRun
} catch (e: Exception) {
true
}
}

val Context.isUpdate: Boolean
get() {
val prevVersion = preferences.lastVersion
Expand Down Expand Up @@ -336,4 +345,4 @@ internal fun Context.setDefaultDashboardTheme() {
(this as? AppCompatActivity)?.delegate?.applyDayNight()
} catch (e: Exception) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import dev.jahir.frames.data.listeners.BillingProcessesListener
import dev.jahir.frames.data.models.CleanSkuDetails
import dev.jahir.frames.data.models.DetailedPurchaseRecord
import dev.jahir.frames.data.viewmodels.BillingViewModel
import dev.jahir.frames.extensions.context.firstInstallTime
import dev.jahir.frames.extensions.context.getAppName
import dev.jahir.frames.extensions.context.string
import dev.jahir.frames.extensions.context.stringArray
Expand Down Expand Up @@ -45,6 +46,13 @@ abstract class BaseBillingActivity<out P : Preferences> : BaseLicenseCheckerActi
}
}

override fun onResume() {
super.onResume()
if (preferences.isFirstRun && firstInstallTime > 10000) {
preferences.isFirstRun = false
}
}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
val created = super.onCreateOptionsMenu(menu)
menu?.findItem(R.id.donate)?.isVisible =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ open class CollectionsFragment : BaseFramesFragment<Collection>() {
Intent(activity, CollectionActivity::class.java)

override fun getEmptyText(): Int = R.string.no_collections_found
override fun allowCheckingFirstRun(): Boolean = true

companion object {
const val TAG = "collections_fragment"
Expand All @@ -71,4 +72,4 @@ open class CollectionsFragment : BaseFramesFragment<Collection>() {
fun create(list: ArrayList<Collection> = ArrayList()) =
CollectionsFragment().apply { updateItemsInAdapter(list) }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ open class WallpapersFragment : BaseFramesFragment<Wallpaper>() {
if (isForFavs) R.drawable.ic_empty_favorites else super.getEmptyDrawable()

open fun canToggleSystemUIVisibility(): Boolean = true
override fun allowCheckingFirstRun(): Boolean = true

companion object {
const val TAG = "wallpapers_fragment"
Expand Down Expand Up @@ -190,4 +191,4 @@ open class WallpapersFragment : BaseFramesFragment<Wallpaper>() {
updateItemsInAdapter(list)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ abstract class BaseFramesFragment<T> : Fragment(R.layout.fragment_stateful_recyc
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
recyclerView = view.findViewById(R.id.recycler_view)
recyclerView?.allowFirstRunCheck = allowCheckingFirstRun()
setupContentBottomOffset(view)
recyclerView?.stateDrawableModifier = this

Expand Down Expand Up @@ -155,4 +156,6 @@ abstract class BaseFramesFragment<T> : Fragment(R.layout.fragment_stateful_recyc

@DrawableRes
open fun getNoSearchResultsDrawable(): Int = R.drawable.ic_empty_results

open fun allowCheckingFirstRun(): Boolean = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import androidx.appcompat.widget.AppCompatImageView
import com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView
import dev.jahir.frames.R
import dev.jahir.frames.extensions.context.drawable
import dev.jahir.frames.extensions.context.isFirstRun
import dev.jahir.frames.extensions.context.preferences
import dev.jahir.frames.extensions.context.string
import dev.jahir.frames.extensions.views.gone
Expand All @@ -32,6 +33,8 @@ open class StatefulRecyclerView @JvmOverloads constructor(

var stateDrawableModifier: StateDrawableModifier? = null

var allowFirstRunCheck: Boolean = false

var loading: Boolean = true
set(value) {
field = value
Expand Down Expand Up @@ -161,7 +164,7 @@ open class StatefulRecyclerView @JvmOverloads constructor(
state = when {
loading -> State.LOADING
(adapter?.itemCount ?: 0) > 0 -> State.NORMAL
else -> State.EMPTY
else -> if (context.isFirstRun && allowFirstRunCheck) State.LOADING else State.EMPTY
}
}

Expand Down

0 comments on commit 3554402

Please sign in to comment.