Skip to content

Commit

Permalink
Add dialog for donations every 42 uses (#792)
Browse files Browse the repository at this point in the history
Co-authored-by: rahimvirani <[email protected]>
Co-authored-by: Leonardo Colman Lopes <[email protected]>
  • Loading branch information
3 people authored Jan 16, 2025
1 parent 9cfd5d2 commit e946b7e
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 4 deletions.
7 changes: 7 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,11 @@

</application>

<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="https" />
</intent>
</queries>

</manifest>
62 changes: 61 additions & 1 deletion app/src/main/kotlin/br/com/colman/petals/use/AddUseButton.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package br.com.colman.petals.use

import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.compose.foundation.layout.Row
import androidx.compose.material.AlertDialog
import androidx.compose.material.Button
Expand All @@ -14,12 +17,19 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import br.com.colman.petals.R.string.add_use
import br.com.colman.petals.R.string.add_use_during_pause_alert
import br.com.colman.petals.R.string.later
import br.com.colman.petals.R.string.no
import br.com.colman.petals.R.string.ok
import br.com.colman.petals.R.string.support_my_work
import br.com.colman.petals.R.string.support_now
import br.com.colman.petals.R.string.thank_your_for_using_message
import br.com.colman.petals.R.string.yes
import br.com.colman.petals.R.string.yes_timer
import br.com.colman.petals.use.repository.Use
Expand All @@ -38,11 +48,26 @@ fun AddUseButton(
isAnyPauseActive: Boolean
) {
var openAddUseDialog by remember { mutableStateOf(false) }
var openSupportDialog by remember { mutableStateOf(false) }
var openConfirmAddUseDialog by remember { mutableStateOf(false) }
val lastUse by repository.getLastUse().collectAsState(null)
val context = LocalContext.current
val totalUseCount by repository.countAll().collectAsState(0)

if (openAddUseDialog) {
AddUseDialog(lastUse, { repository.upsert(it) }) { openAddUseDialog = false }
AddUseDialog(lastUse, {
repository.upsert(it)
if (totalUseCount > 0 && totalUseCount % 42 == 0) {
openSupportDialog = true
}
}) { openAddUseDialog = false }
}

if (openSupportDialog) {
SupportDeveloperDialog({ openSupportDialog = false }) {
openSupportDialog = false
context.launchKofi()
}
}

if (openConfirmAddUseDialog) {
Expand Down Expand Up @@ -78,6 +103,32 @@ private fun PauseUseButton(onClick: () -> Unit = { }) {
}
}

@Composable
@Preview
private fun SupportDeveloperDialog(
onDismiss: () -> Unit = {},
onConfirm: () -> Unit = {}
) {
AlertDialog(
onDismissRequest = onDismiss,
title = { Text(stringResource(support_my_work)) },
text = { Text(stringResource(thank_your_for_using_message), fontWeight = FontWeight.Bold) },
confirmButton = {
TextButton(onConfirm) {
Text(stringResource(support_now))
}
},
dismissButton = {
TextButton(onDismiss) {
Text(
stringResource(later),
color = Color.LightGray
)
}
}
)
}

@Composable
@Preview
private fun ConfirmAddUseDuringPauseDialog(
Expand Down Expand Up @@ -151,3 +202,12 @@ private fun ConfirmNewUseButton(
Text(stringResource(ok))
}
}

private const val KofiUrl = "https://ko-fi.com/leocolman"

private fun Context.launchKofi() {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(KofiUrl))
intent.resolveActivity(packageManager)?.let {
startActivity(intent)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package br.com.colman.petals.use.io.output

import android.content.res.Resources
import androidx.compose.ui.graphics.Path
import br.com.colman.petals.R
import br.com.colman.petals.R.string.amount_label
import br.com.colman.petals.R.string.cost_per_gram_label
Expand All @@ -29,7 +28,6 @@ import br.com.colman.petals.use.repository.UseRepository
import com.github.doyaaaaaken.kotlincsv.dsl.csvWriter
import kotlinx.coroutines.flow.first
import java.io.ByteArrayOutputStream
import java.util.regex.Matcher
import kotlin.text.Charsets.UTF_8

data class UseCsvHeaders(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class UseRepository(

fun getLastUseDate() = getLastUse().map { it?.date }

fun countAll() = useQueries.countAll().asFlow().mapToOneOrNull(Dispatchers.IO).map { it?.toInt() ?: 0 }

fun all(): Flow<List<Use>> = useQueries.selectAll().asFlow().mapToList(
Dispatchers.IO
).map { it.map(UseEntity::toUse) }
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/values-pt/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@
<string name="delete">Remover</string>
<string name="yes">Sim</string>
<string name="yes_timer">Sim (%ss)</string>
<string name="thank_your_for_using_message">Obrigado por usar o Petals! Este é um lembrete para você colaborar para
manter este projeto gratuito e melhorando!
</string>
<string name="no">Não</string>
<string name="add_use_during_pause_alert">Você marcou este horário como uma pause. Tem certeza que deseja adicionar um uso agora?</string>
<string name="grams_distribution_per_day_of_week">Distribuição de gramas por dia da semana</string>
Expand Down Expand Up @@ -188,4 +191,7 @@
<string name="description_label">descrição</string>
<string name="description">Descrição</string>
<string name="because_i_wanted">Porque eu queria</string>
<string name="support_now">Colaborar Agora</string>
<string name="later">Mais Tarde</string>
<string name="support_my_work">Colabore com o Petals ❤️</string>
</resources>
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@
<string name="delete">Delete</string>
<string name="yes">Yes</string>
<string name="yes_timer">Yes (%ss)</string>
<string name="support_now">Support Now</string>
<string name="later">Later</string>
<string name="support_my_work">Support My Work ❤️</string>
<string name="thank_your_for_using_message">Thank you for using Petals! Just a reminder that you can support my work on improving this free app.</string>
<string name="no">No</string>
<string name="add_use_during_pause_alert">You scheduled this period to be a pause. Are you sure you want to add a use now?</string>
<string name="grams_distribution_per_day_of_week">Grams distribution per day of week</string>
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/sqldelight/br/com/colman/petals/Use.sq
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ selectAll:
SELECT * FROM Use;

delete:
DELETE FROM Use WHERE id IS (?);
DELETE FROM Use WHERE id IS (?);

countAll:
SELECT COUNT(*) FROM Use;

0 comments on commit e946b7e

Please sign in to comment.