Skip to content

Commit

Permalink
Merge pull request #16 from SuddenH4X/feature/2.1.1
Browse files Browse the repository at this point in the history
Feature/2.1.1
  • Loading branch information
SuddenH4X authored Jul 11, 2020
2 parents fa9c7cc + af3bc26 commit 6ab51c2
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ The library supports API level 14 and higher. You can simply include it in your
```groovy
dependencies {
...
implementation 'com.suddenh4x.ratingdialog:awesome-app-rating:2.0.0'
implementation 'com.suddenh4x.ratingdialog:awesome-app-rating:2.1.1'
}
```

Expand Down
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apply plugin: 'kotlin-android-extensions'
apply plugin: "de.mannodermaus.android-junit5"
apply plugin: 'com.novoda.bintray-release'

def version = "2.1.0"
def version = "2.1.1"

android {
compileSdkVersion 29
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ internal object DialogManager {
) {
dialogBuilder.setNeutralButton(rateLaterButton.textId) { _, _ ->
RatingLogger.info("Rate later button clicked.")
PreferenceUtil.updateRemindTimestamp(context)
PreferenceUtil.onLaterButtonClicked(context)
rateLaterButton.rateDialogClickListener?.onClick()
?: RatingLogger.info("Rate later button has no click listener.")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ internal object ConditionsChecker {

RatingLogger.verbose("Is dialog agreed: $isDialogAgreed.")
RatingLogger.verbose("Do not show again: $isDoNotShowAgain.")
RatingLogger.verbose("Days between later button click and now: $daysBetween.")

if (!checkCustomCondition(dialogOptions)) return false

if (wasLaterButtonClicked) {
RatingLogger.debug("Show later button has already been clicked.")
RatingLogger.verbose("Days between later button click and now: $daysBetween.")
if (!checkCustomConditionToShowAgain(dialogOptions)) return false

return (!isDialogAgreed &&
Expand All @@ -34,6 +34,7 @@ internal object ConditionsChecker {
PreferenceUtil.getMinimumLaunchTimesToShowAgain(context)))
}

RatingLogger.verbose("Days between first app start and now: $daysBetween.")
RatingLogger.debug("Show later button hasn't been clicked until now.")
return (!isDialogAgreed &&
!isDoNotShowAgain &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import androidx.core.content.edit
import com.suddenh4x.ratingdialog.logging.RatingLogger

internal object PreferenceUtil {
const val PREF_FILE_NAME = "awesome_app_rate"
internal const val PREF_FILE_NAME = "awesome_app_rate"

const val PREF_KEY_LAUNCH_TIMES = "launch_times"
private const val PREF_KEY_REMIND_TIMESTAMP = "timestamp"
internal const val PREF_KEY_LAUNCH_TIMES = "launch_times"
internal const val PREF_KEY_REMIND_TIMESTAMP = "timestamp"
private const val PREF_KEY_MINIMUM_LAUNCH_TIMES = "minimum_launch_times"
private const val PREF_KEY_MINIMUM_LAUNCH_TIMES_TO_SHOW_AGAIN =
"minimum_launch_times_to_show_again"
Expand All @@ -18,7 +18,7 @@ internal object PreferenceUtil {
private const val PREF_KEY_DIALOG_AGREED = "dialog_agreed"
private const val PREF_KEY_DIALOG_SHOW_LATER = "dialog_show_later"
private const val PREF_KEY_DIALOG_DO_NOT_SHOW_AGAIN = "dialog_do_not_show_again"
const val PREF_KEY_NUMBER_OF_LATER_BUTTON_CLICKS = "number_of_later_button_clicks"
internal const val PREF_KEY_NUMBER_OF_LATER_BUTTON_CLICKS = "number_of_later_button_clicks"

fun getPreferences(context: Context): SharedPreferences =
context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE)
Expand Down Expand Up @@ -72,8 +72,8 @@ internal object PreferenceUtil {
fun getMinimumDaysToShowAgain(context: Context) =
getPreferences(context).getInt(PREF_KEY_MINIMUM_DAYS_TO_SHOW_AGAIN, 14)

fun updateRemindTimestamp(context: Context) {
RatingLogger.verbose("Update remind timestamp. Set launch times to 0.")
fun onLaterButtonClicked(context: Context) {
RatingLogger.verbose("Later button was clicked. Update remind timestamp and set launch times to 0.")
getPreferences(context).edit {
putLong(PREF_KEY_REMIND_TIMESTAMP, System.currentTimeMillis())
putInt(PREF_KEY_LAUNCH_TIMES, 0)
Expand All @@ -82,8 +82,22 @@ internal object PreferenceUtil {
increaseNumberOfLaterButtonClicks(context)
}

fun getRemindTimestamp(context: Context) =
getPreferences(context).getLong(PREF_KEY_REMIND_TIMESTAMP, System.currentTimeMillis())
fun getRemindTimestamp(context: Context): Long {
val remindTimestamp = getPreferences(context).getLong(PREF_KEY_REMIND_TIMESTAMP, -1)
if (remindTimestamp == -1L) {
return setInitialRemindTimestamp(context)
}
return remindTimestamp
}

private fun setInitialRemindTimestamp(context: Context): Long {
RatingLogger.debug("First app start. Set initial remind timestamp.")
val currentTime = System.currentTimeMillis()
getPreferences(context).edit {
putLong(PREF_KEY_REMIND_TIMESTAMP, currentTime)
}
return currentTime
}

fun setDialogAgreed(context: Context) {
RatingLogger.debug("Set dialog agreed.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,32 @@ import com.suddenh4x.ratingdialog.logging.RatingLogger
import com.suddenh4x.ratingdialog.preferences.PreferenceUtil.PREF_FILE_NAME
import com.suddenh4x.ratingdialog.preferences.PreferenceUtil.PREF_KEY_LAUNCH_TIMES
import com.suddenh4x.ratingdialog.preferences.PreferenceUtil.PREF_KEY_NUMBER_OF_LATER_BUTTON_CLICKS
import com.suddenh4x.ratingdialog.preferences.PreferenceUtil.PREF_KEY_REMIND_TIMESTAMP
import io.mockk.clearAllMocks
import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.impl.annotations.RelaxedMockK
import io.mockk.junit5.MockKExtension
import io.mockk.mockkObject
import io.mockk.mockkStatic
import io.mockk.unmockkAll
import io.mockk.verify
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(MockKExtension::class)
class PreferenceUtilTest {

@MockK
lateinit var context: Context

@MockK
lateinit var sharedPreferences: SharedPreferences

@RelaxedMockK
lateinit var editor: SharedPreferences.Editor

Expand All @@ -39,6 +48,12 @@ class PreferenceUtilTest {
every { sharedPreferences.edit() } returns editor
}

@AfterEach
fun shutdown() {
unmockkAll()
clearAllMocks()
}

@Test
fun `correct preferences are used`() {
assertEquals(PreferenceUtil.getPreferences(context), sharedPreferences)
Expand All @@ -62,9 +77,31 @@ class PreferenceUtilTest {
verify(exactly = 1) { editor.putInt(PREF_KEY_NUMBER_OF_LATER_BUTTON_CLICKS, 2) }
}

@Test
@Disabled
// fixme: The System class can't be mocked at the moment: https://github.com/mockk/mockk/issues/98
fun `get remind timestamp returns current time if not set`() {
mockkStatic(System::class)
every { sharedPreferences.getLong(any(), any()) } returns -1L
every { System.currentTimeMillis() } returns CURRENT_TIME_IN_MILLIS

PreferenceUtil.getRemindTimestamp(context)

verify(exactly = 1) {
sharedPreferences.getLong(
PREF_KEY_REMIND_TIMESTAMP,
CURRENT_TIME_IN_MILLIS
)
}
}

@Test
fun `reset works correctly`() {
PreferenceUtil.reset(context)
verify(exactly = 1) { editor.clear() }
}

companion object {
private const val CURRENT_TIME_IN_MILLIS = 123456789L
}
}

0 comments on commit 6ab51c2

Please sign in to comment.