Skip to content

Commit

Permalink
feat [onboarding]: add onboarding functionality (cont.)
Browse files Browse the repository at this point in the history
  • Loading branch information
kabirnayeem99 committed Feb 12, 2024
1 parent fcd355a commit b5eac58
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 69 deletions.
1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,18 @@ dependencies {

// Jetpack Compose - for UI building
implementation 'androidx.activity:activity-compose:1.8.2'
implementation 'androidx.compose.material:material:1.6.0'
implementation 'androidx.compose.animation:animation:1.6.0'
implementation 'androidx.compose.ui:ui-tooling:1.6.0'
implementation 'androidx.compose.material:material:1.6.1'
implementation 'androidx.compose.animation:animation:1.6.1'
implementation 'androidx.compose.ui:ui-tooling:1.6.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.7.0'
implementation 'androidx.core:core-ktx:1.12.0'
def material_3_version = '1.2.0-alpha03'
implementation "androidx.compose.material3:material3:$material_3_version"
implementation "androidx.compose.material3:material3-window-size-class:$material_3_version"
implementation 'com.google.accompanist:accompanist-systemuicontroller:0.31.5-beta'
implementation 'com.google.accompanist:accompanist-systemuicontroller:0.32.0'

// Navigation
def nav_version = '2.7.6'
def nav_version = '2.7.7'
implementation "androidx.navigation:navigation-compose:$nav_version"
implementation 'androidx.hilt:hilt-navigation-compose:1.1.0'
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
Expand Down Expand Up @@ -127,7 +127,7 @@ dependencies {
// Room - for storing data locally
def room_version = '2.6.1'
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
ksp "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-ktx:$room_version"

// Kotlin JSON Serializer -> for serialization and deserialization
Expand All @@ -149,8 +149,8 @@ dependencies {
// Testing
testImplementation 'junit:junit:4.13.2'
testImplementation 'androidx.test:core-ktx:1.5.0'
testImplementation 'androidx.test.ext:truth:1.6.0-alpha02'
testImplementation 'androidx.test.ext:truth:1.6.0-alpha02'
testImplementation 'androidx.test.ext:truth:1.6.0-alpha03'
testImplementation 'androidx.test.ext:truth:1.6.0-alpha03'
testImplementation 'com.google.truth:truth:1.1.5'
testImplementation 'org.mockito:mockito-core:5.4.0'
testImplementation 'org.robolectric:robolectric:4.10.3'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,75 +9,18 @@ import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import timber.log.Timber
import java.util.Date
import javax.inject.Inject

private const val FETCH_COUNT = "fetch_count"
private const val PREFERRED_FIQH = "preferred_fiqh"
private const val FIQH_LAST_PAGE = "preferred_fiqh"
private const val LAST_SYNC_TIME = "last_sync_time"
private const val SYNC_INTERVAL_TIME_IN_MILLIS = 604_800_000 // 7 days
private const val FIRST_TIME_OPEN = "first_time_open"

class PreferenceDataSource @Inject constructor(private val context: Context) {


private val defaultPrefs: SharedPreferences by lazy {
PreferenceManager.getDefaultSharedPreferences(context)
}

/**
* Notifies whether the data needs to update or not
*/
suspend fun updateNeedingToRefresh() {
withContext(Dispatchers.IO) {
try {
val currentFetchCount = defaultPrefs.getInt(FETCH_COUNT, 1)
defaultPrefs.edit { it.putInt(FETCH_COUNT, currentFetchCount + 1) }
} catch (e: Exception) {
Timber.e(e, "Failed to update needing to refresh -> ${e.message}")
}
}
}


/**
* Checks if the last sync time is greater than the sync interval time
*
* @return A Boolean value, whether needs syncing or not
*/
suspend fun checkIfNeedsSyncing(): Boolean {
val doesNeedSyncing = withContext(Dispatchers.IO) {
try {
val lastSyncTimeInMillis = defaultPrefs.getLong(LAST_SYNC_TIME, 0)
Timber.d("Last sync time in millis -> $lastSyncTimeInMillis")
if (lastSyncTimeInMillis == 0L) true
else {
val currentTimeInMillis = Date().time
val interval = currentTimeInMillis - lastSyncTimeInMillis
interval > SYNC_INTERVAL_TIME_IN_MILLIS
}
} catch (e: Exception) {
true
}
}
Timber.d("Needs syncing or not -> $doesNeedSyncing")
return doesNeedSyncing
}

/**
* Updates the last sync time in the preference data storage
*/
suspend fun updateSyncingStatus() {
withContext(Dispatchers.IO) {
try {
val date = Date()
val dateInMillis = date.time
defaultPrefs.edit().putLong(LAST_SYNC_TIME, dateInMillis).apply()
} catch (e: Exception) {
Timber.e(e, "Failed to update syncing status -> ${e.message}")
}
}
}

private val lastSyncedLock = Mutex()

Expand Down Expand Up @@ -143,6 +86,25 @@ class PreferenceDataSource @Inject constructor(private val context: Context) {
}
}

suspend fun markAsOpened() {
withContext(Dispatchers.IO) {
defaultPrefs.edit { dp -> dp.putBoolean(FIRST_TIME_OPEN, false) }
}
}

suspend fun determineIfFirstTime(): Boolean {
return withContext(Dispatchers.IO) {
try {
val isFirstTime = defaultPrefs.getBoolean(FIRST_TIME_OPEN, true)
val selectedFiqh = defaultPrefs.getString(PREFERRED_FIQH, "") ?: ""

isFirstTime || selectedFiqh.isBlank()
} catch (e: Exception) {
true
}
}
}


/**
* Takes a lambda as a parameter, and calls it with a SharedPreferences.Editor as a parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package io.github.kabirnayeem99.islamqaorg.data.dataSource.localDb
import androidx.room.Database
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import io.github.kabirnayeem99.islamqaorg.BuildConfig
import io.github.kabirnayeem99.islamqaorg.data.dto.room.Converter
import io.github.kabirnayeem99.islamqaorg.data.dto.room.QuestionEntity

@Database(
entities = [QuestionEntity::class], version = BuildConfig.VERSION_CODE + 5, exportSchema = false
entities = [QuestionEntity::class],
version = 5, exportSchema = false
)
@TypeConverters(Converter::class)
abstract class IslamQaDatabase : RoomDatabase() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ class SettingsRepositoryImpl @Inject constructor(

override suspend fun getPreferredFiqh() = preferenceDataSource.getPreferredFiqh()

override suspend fun determineIfFirstTime() = preferenceDataSource.determineIfFirstTime()

override suspend fun markAsOpened() = preferenceDataSource.markAsOpened()

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ interface SettingsRepository {
suspend fun savePreferredFiqh(fiqh: Fiqh)

suspend fun getPreferredFiqh(): Fiqh

suspend fun determineIfFirstTime(): Boolean

suspend fun markAsOpened()
}

0 comments on commit b5eac58

Please sign in to comment.