-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from odaridavid/request-rating
show prompt for rating
- Loading branch information
Showing
7 changed files
with
189 additions
and
3 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
54
app/src/main/java/com/github/odaridavid/designpatterns/RatingManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* | ||
* Copyright 2020 David Odari | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
* | ||
**/ | ||
package com.github.odaridavid.designpatterns | ||
|
||
import android.content.SharedPreferences | ||
|
||
internal interface IRatingManager { | ||
fun hasGivenRating(): Boolean | ||
fun shouldPromptForRating(): Boolean | ||
fun updatePromptForRatingCounter() | ||
fun giveRating() | ||
} | ||
|
||
internal class RatingManager(private val sharedPreferences: SharedPreferences) : | ||
IRatingManager { | ||
|
||
override fun hasGivenRating(): Boolean = sharedPreferences.getBoolean(RATING_PREF_KEY, false) | ||
|
||
override fun giveRating() { | ||
with(sharedPreferences.edit()) { | ||
putBoolean(RATING_PREF_KEY, true) | ||
apply() | ||
} | ||
} | ||
|
||
override fun shouldPromptForRating(): Boolean { | ||
val counter = sharedPreferences.getInt(PROMPT_COUNTER_PREF_KEY, 0) | ||
return counter % 5 == 0 | ||
} | ||
|
||
override fun updatePromptForRatingCounter() { | ||
val counter = sharedPreferences.getInt(PROMPT_COUNTER_PREF_KEY, 0) + 1 | ||
with(sharedPreferences.edit()) { | ||
putInt(PROMPT_COUNTER_PREF_KEY, counter) | ||
apply() | ||
} | ||
} | ||
|
||
companion object { | ||
private const val RATING_PREF_KEY = "rating" | ||
private const val PROMPT_COUNTER_PREF_KEY = "rating_prompt" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
app/src/main/java/com/github/odaridavid/designpatterns/helpers/InjectorUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* | ||
* Copyright 2020 David Odari | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
* | ||
**/ | ||
package com.github.odaridavid.designpatterns.helpers | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
|
||
|
||
internal object InjectorUtils { | ||
private const val DESIGN_PATTERNS_SP_NAME = "design_patterns_pref" | ||
fun provideSharedPreferences(context: Context): SharedPreferences { | ||
return context.getSharedPreferences(DESIGN_PATTERNS_SP_NAME, Context.MODE_PRIVATE) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters