Skip to content

Commit

Permalink
do not request for calendar permissions on opening config or when NON…
Browse files Browse the repository at this point in the history
…E symbol set
  • Loading branch information
mvmike committed Dec 12, 2023
1 parent 131c88b commit bb73d21
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import android.content.Context
import android.content.Intent
import cat.mvmike.minimalcalendarwidget.application.RedrawWidgetUseCase
import cat.mvmike.minimalcalendarwidget.domain.configuration.BooleanConfigurationItem
import cat.mvmike.minimalcalendarwidget.domain.configuration.EnumConfigurationItem.InstancesSymbolSet
import cat.mvmike.minimalcalendarwidget.domain.configuration.item.SymbolSet
import cat.mvmike.minimalcalendarwidget.domain.intent.ActionableView.CellDay
import cat.mvmike.minimalcalendarwidget.domain.intent.ActionableView.CellDay.getExtraInstant
import cat.mvmike.minimalcalendarwidget.domain.intent.ActionableView.ConfigurationIcon
Expand All @@ -15,7 +17,7 @@ import cat.mvmike.minimalcalendarwidget.domain.intent.toActionableView
import cat.mvmike.minimalcalendarwidget.infrastructure.activity.CalendarActivity
import cat.mvmike.minimalcalendarwidget.infrastructure.activity.ConfigurationActivity
import cat.mvmike.minimalcalendarwidget.infrastructure.activity.PermissionsActivity
import cat.mvmike.minimalcalendarwidget.infrastructure.resolver.CalendarResolver
import cat.mvmike.minimalcalendarwidget.infrastructure.resolver.CalendarResolver.isReadCalendarPermitted
import cat.mvmike.minimalcalendarwidget.infrastructure.resolver.SystemResolver

object ProcessIntentUseCase {
Expand All @@ -24,40 +26,34 @@ object ProcessIntentUseCase {
context: Context,
intent: Intent
) = when (intent.toActionableView()) {
ConfigurationIcon -> startConfigurationActivity(context)
ConfigurationIcon -> ConfigurationActivity.start(context)
MonthAndYearHeader,
RowHeader -> startCalendarActivity(context)
CellDay -> startCalendarActivity(context, intent)
RowHeader -> context.askForPermissionsIfNeededOrExecuteAndRedraw { startCalendarActivity(context) }
CellDay -> context.askForPermissionsIfNeededOrExecuteAndRedraw { startCalendarActivity(context, intent) }
else -> null
}?.let { context.executeAndRedrawOrAskForPermissions(it) }

private fun startConfigurationActivity(context: Context): () -> Unit = {
ConfigurationActivity.start(context)
}

private fun startCalendarActivity(context: Context): () -> Unit = {
CalendarActivity.start(context, SystemResolver.getSystemInstant())
}
private fun startCalendarActivity(context: Context) = CalendarActivity.start(
context = context,
startTime = SystemResolver.getSystemInstant()
)

private fun startCalendarActivity(
context: Context,
intent: Intent
): () -> Unit = {
CalendarActivity.start(
context,
when {
BooleanConfigurationItem.OpenCalendarOnClickedDay.get(context) -> intent.getExtraInstant()
else -> SystemResolver.getSystemInstant()
}
)
}
) = CalendarActivity.start(
context = context,
startTime = when {
BooleanConfigurationItem.OpenCalendarOnClickedDay.get(context) -> intent.getExtraInstant()
else -> SystemResolver.getSystemInstant()
}
)

private fun Context.executeAndRedrawOrAskForPermissions(function: () -> Unit) =
when (CalendarResolver.isReadCalendarPermitted(this)) {
true -> {
function.invoke()
RedrawWidgetUseCase.execute(this)
}
else -> PermissionsActivity.start(this)
private fun Context.askForPermissionsIfNeededOrExecuteAndRedraw(function: () -> Unit) = when {
isReadCalendarPermitted(this) || InstancesSymbolSet.get(this) == SymbolSet.NONE -> {
function.invoke()
RedrawWidgetUseCase.execute(this)
}
else -> PermissionsActivity.start(this)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,92 +5,121 @@ package cat.mvmike.minimalcalendarwidget.application.user
import android.appwidget.AppWidgetManager.ACTION_APPWIDGET_UPDATE
import cat.mvmike.minimalcalendarwidget.BaseTest
import cat.mvmike.minimalcalendarwidget.application.RedrawWidgetUseCase
import cat.mvmike.minimalcalendarwidget.domain.configuration.item.SymbolSet
import cat.mvmike.minimalcalendarwidget.domain.intent.AutoUpdate.ACTION_AUTO_UPDATE
import cat.mvmike.minimalcalendarwidget.infrastructure.activity.CalendarActivity
import cat.mvmike.minimalcalendarwidget.infrastructure.activity.ConfigurationActivity
import cat.mvmike.minimalcalendarwidget.infrastructure.activity.PermissionsActivity
import io.mockk.justRun
import io.mockk.mockkObject
import io.mockk.verify
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.MethodSource
import org.junit.jupiter.params.provider.NullSource
import org.junit.jupiter.params.provider.ValueSource
import java.time.Instant.ofEpochSecond

private const val MONTH_AND_YEAR_HEADER_CLICK_ACTION = "action.mincal.month_and_year_header_click"
private const val ROW_HEADER_CLICK_ACTION = "action.mincal.row_header_click"
private const val CELL_DAY_CLICK_ACTION = "action.mincal.cell_day_click"

internal class ProcessIntentUseCaseTest : BaseTest() {

@ParameterizedTest
@NullSource
@ValueSource(
strings = [
ACTION_AUTO_UPDATE,
ACTION_APPWIDGET_UPDATE,
"some_random_intent_action"
"some_random_intent_action",
""
]
)
fun shouldDoNothing_whenNoActionableViewIntent(action: String) {
mockIntent(action)
fun shouldDoNothing_whenNoActionableViewIntent(intentAction: String?) {
mockIntent(intentAction)

ProcessIntentUseCase.execute(context, intent)

verifyIntentAction()
}

@Test
fun shouldLaunchConfigurationActivity() {
mockIntent("action.mincal.configuration_icon_click")
justRun { ConfigurationActivity.Companion.start(context) }

ProcessIntentUseCase.execute(context, intent)

verifyIntentAction()
verify { ConfigurationActivity.Companion.start(context) }
}

@ParameterizedTest
@ValueSource(
strings = [
"action.mincal.configuration_icon_click",
"action.mincal.month_and_year_header_click",
"action.mincal.row_header_click",
"action.mincal.cell_day_click.1675886154",
"action.mincal.cell_day_click.1671249586",
"action.mincal.cell_day_click.1624398458",
"action.mincal.cell_day_click.1434987405"
MONTH_AND_YEAR_HEADER_CLICK_ACTION,
ROW_HEADER_CLICK_ACTION,
"$CELL_DAY_CLICK_ACTION.1675886154",
"$CELL_DAY_CLICK_ACTION.1671249586",
"$CELL_DAY_CLICK_ACTION.1624398458",
"$CELL_DAY_CLICK_ACTION.1434987405"
]
)
fun shouldLaunchPermissionsActivity_whenActionableViewIntentAndNoPermissionsGiven(action: String) {
fun shouldLaunchPermissionsActivity_whenNoPermissionsGivenAndNoneSymbolSet(action: String) {
mockIntent(action)
mockIsReadCalendarPermitted(false)
mockSharedPreferences()
mockInstancesSymbolSet(SymbolSet.MINIMAL)
justRun { PermissionsActivity.Companion.start(context) }

ProcessIntentUseCase.execute(context, intent)

verifyIntentAction()
verifyIsReadCalendarPermitted()
verifyInstancesSymbolSet()
verify { PermissionsActivity.Companion.start(context) }
}

@ParameterizedTest
@ValueSource(
strings = [
"action.mincal.configuration_icon_click"
MONTH_AND_YEAR_HEADER_CLICK_ACTION,
ROW_HEADER_CLICK_ACTION
]
)
fun shouldLaunchConfigurationActivityAndRedrawWidget(action: String) {
fun shouldLaunchCalendarActivityAndRedrawWidget_whenPermissionsGiven(action: String) {
mockIntent(action)
mockIsReadCalendarPermitted(true)
mockGetSystemInstant()
mockOpenCalendarOnClickedDay(false)
mockkObject(RedrawWidgetUseCase)

justRun { ConfigurationActivity.Companion.start(context) }
justRun { CalendarActivity.start(context, systemInstant) }
justRun { RedrawWidgetUseCase.execute(context) }

ProcessIntentUseCase.execute(context, intent)

verifyIntentAction()
verifyIsReadCalendarPermitted()
verify { ConfigurationActivity.Companion.start(context) }
verifyGetSystemInstant()
verify { CalendarActivity.start(context, systemInstant) }
verify { RedrawWidgetUseCase.execute(context) }
}

@ParameterizedTest
@ValueSource(
strings = [
"action.mincal.month_and_year_header_click",
"action.mincal.row_header_click"
MONTH_AND_YEAR_HEADER_CLICK_ACTION,
ROW_HEADER_CLICK_ACTION
]
)
fun shouldLaunchCalendarActivityAndRedrawWidget_whenIntentAndPermissionsGiven(action: String) {
fun shouldLaunchCalendarActivityAndRedrawWidget_whenNoPermissionsGivenAndNoneSymbolSet(action: String) {
mockIntent(action)
mockIsReadCalendarPermitted(true)
mockIsReadCalendarPermitted(false)
mockSharedPreferences()
mockInstancesSymbolSet(SymbolSet.NONE)
mockGetSystemInstant()
mockOpenCalendarOnClickedDay(false)
mockkObject(RedrawWidgetUseCase)
Expand All @@ -102,6 +131,7 @@ internal class ProcessIntentUseCaseTest : BaseTest() {

verifyIntentAction()
verifyIsReadCalendarPermitted()
verifyInstancesSymbolSet()
verifyGetSystemInstant()
verify { CalendarActivity.start(context, systemInstant) }
verify { RedrawWidgetUseCase.execute(context) }
Expand Down Expand Up @@ -165,9 +195,9 @@ internal class ProcessIntentUseCaseTest : BaseTest() {
}

private fun getMincalCalendarIntentActionAndExpectedExtraInstantAndStartTimeInstant() = listOf(
Arguments.of("action.mincal.cell_day_click.1675886154", 1675886154, 1675863134),
Arguments.of("action.mincal.cell_day_click.1671249586", 1671249586, 1671283934),
Arguments.of("action.mincal.cell_day_click.1624398458", 1624398458, 1624455134),
Arguments.of("action.mincal.cell_day_click.1434987405", 1434987405, 1434979934)
Arguments.of("$CELL_DAY_CLICK_ACTION.1675886154", 1675886154, 1675863134),
Arguments.of("$CELL_DAY_CLICK_ACTION.1671249586", 1671249586, 1671283934),
Arguments.of("$CELL_DAY_CLICK_ACTION.1624398458", 1624398458, 1624455134),
Arguments.of("$CELL_DAY_CLICK_ACTION.1434987405", 1434987405, 1434979934)
)
}

0 comments on commit bb73d21

Please sign in to comment.