Skip to content

Commit

Permalink
- adds shortcuts to the app
Browse files Browse the repository at this point in the history
still needs icons and the actual action doesn't work
  • Loading branch information
pablo03v committed Oct 15, 2023
1 parent a65e03b commit 7c5c4e8
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 34 deletions.
9 changes: 8 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,18 @@
<data android:mimeType="text/comma-separated-values" />
</intent-filter>
</activity>
<activity
android:name=".ui.ShortcutActivity"
android:theme="@android:style/Theme.NoDisplay"
android:exported="false"
android:excludeFromRecents="true">
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="cloud.pablos.overload.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
android:grantUriPermissions="true"
android:excludeFromRecents="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/cloud/pablos/overload/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class MainActivity : ComponentActivity() {
private val viewModel by viewModels<ItemViewModel>(
factoryProducer = {
object : ViewModelProvider.Factory {
@RequiresApi(Build.VERSION_CODES.O)
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return ItemViewModel(db.itemDao()) as T
}
Expand Down
55 changes: 55 additions & 0 deletions app/src/main/java/cloud/pablos/overload/ui/ShortcutActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cloud.pablos.overload.ui

import android.os.Build
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.annotation.RequiresApi
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.room.Room
import cloud.pablos.overload.data.item.ItemDatabase
import cloud.pablos.overload.data.item.ItemViewModel
import cloud.pablos.overload.ui.tabs.home.startOrStopPause
import cloud.pablos.overload.ui.theme.OverloadTheme

class ShortcutActivity : ComponentActivity() {
private val db by lazy {
Room.databaseBuilder(
applicationContext,
ItemDatabase::class.java,
"items",
).build()
}

private val viewModel by viewModels<ItemViewModel>(
factoryProducer = {
object : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return ItemViewModel(db.itemDao()) as T
}
}
},
)

@RequiresApi(Build.VERSION_CODES.S)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setContent {
OverloadTheme {
val state by viewModel.state.collectAsState()
val onEvent = viewModel::onEvent

val context = LocalContext.current

startOrStopPause(state, onEvent, context)
}
}
finish()
}
}
86 changes: 54 additions & 32 deletions app/src/main/java/cloud/pablos/overload/ui/tabs/home/HomeTabFab.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cloud.pablos.overload.ui.tabs.home

import android.content.Context
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.foundation.layout.Row
Expand All @@ -13,11 +14,13 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import cloud.pablos.overload.R
import cloud.pablos.overload.data.item.ItemEvent
import cloud.pablos.overload.data.item.ItemState
import cloud.pablos.overload.ui.utils.ShortcutUtil
import cloud.pablos.overload.ui.views.TextView
import cloud.pablos.overload.ui.views.extractDate
import cloud.pablos.overload.ui.views.parseToLocalDateTime
Expand All @@ -37,40 +40,11 @@ fun HomeTabFab(
extractDate(startTime) == date
}

val context = LocalContext.current

FloatingActionButton(
onClick = {
val isFirst = itemsForToday.isEmpty()
val isNotOngoing = itemsForToday.isEmpty() || !state.items.last().ongoing

if (isFirst) {
onEvent(ItemEvent.SetStart(start = LocalDateTime.now().toString()))
onEvent(ItemEvent.SetOngoing(ongoing = true))
onEvent(ItemEvent.SetPause(pause = false))
onEvent(ItemEvent.SaveItem)

onEvent(ItemEvent.SetIsOngoing(isOngoing = true))
} else if (isNotOngoing) {
onEvent(ItemEvent.SetStart(start = itemsForToday.last().endTime))
onEvent(ItemEvent.SetEnd(end = LocalDateTime.now().toString()))
onEvent(ItemEvent.SetOngoing(ongoing = false))
onEvent(ItemEvent.SetPause(pause = true))
onEvent(ItemEvent.SaveItem)

onEvent(ItemEvent.SetStart(start = LocalDateTime.now().toString()))
onEvent(ItemEvent.SetOngoing(ongoing = true))
onEvent(ItemEvent.SetPause(pause = false))
onEvent(ItemEvent.SaveItem)

onEvent(ItemEvent.SetIsOngoing(isOngoing = true))
} else {
onEvent(ItemEvent.SetId(id = itemsForToday.last().id))
onEvent(ItemEvent.SetStart(start = itemsForToday.last().startTime))
onEvent(ItemEvent.SetEnd(end = LocalDateTime.now().toString()))
onEvent(ItemEvent.SetOngoing(ongoing = false))
onEvent(ItemEvent.SaveItem)

onEvent(ItemEvent.SetIsOngoing(isOngoing = false))
}
startOrStopPause(state, onEvent, context)
},
modifier = Modifier
.padding(10.dp),
Expand Down Expand Up @@ -106,6 +80,54 @@ fun HomeTabFab(
}
}

fun startOrStopPause(state: ItemState, onEvent: (ItemEvent) -> Unit, context: Context) {
val date = LocalDate.now()

val itemsForToday = state.items.filter { item ->
val startTime = parseToLocalDateTime(item.startTime)
extractDate(startTime) == date
}

val isFirst = itemsForToday.isEmpty()
val isNotOngoing = itemsForToday.isEmpty() || !state.items.last().ongoing

if (isFirst) {
onEvent(ItemEvent.SetStart(start = LocalDateTime.now().toString()))
onEvent(ItemEvent.SetOngoing(ongoing = true))
onEvent(ItemEvent.SetPause(pause = false))
onEvent(ItemEvent.SaveItem)

onEvent(ItemEvent.SetIsOngoing(isOngoing = true))
} else if (isNotOngoing) {
onEvent(ItemEvent.SetStart(start = itemsForToday.last().endTime))
onEvent(ItemEvent.SetEnd(end = LocalDateTime.now().toString()))
onEvent(ItemEvent.SetOngoing(ongoing = false))
onEvent(ItemEvent.SetPause(pause = true))
onEvent(ItemEvent.SaveItem)

onEvent(ItemEvent.SetStart(start = LocalDateTime.now().toString()))
onEvent(ItemEvent.SetOngoing(ongoing = true))
onEvent(ItemEvent.SetPause(pause = false))
onEvent(ItemEvent.SaveItem)

onEvent(ItemEvent.SetIsOngoing(isOngoing = true))

ShortcutUtil.removeShortcutStopPause(context)
ShortcutUtil.createShortcutStartPause(context)
} else {
onEvent(ItemEvent.SetId(id = itemsForToday.last().id))
onEvent(ItemEvent.SetStart(start = itemsForToday.last().startTime))
onEvent(ItemEvent.SetEnd(end = LocalDateTime.now().toString()))
onEvent(ItemEvent.SetOngoing(ongoing = false))
onEvent(ItemEvent.SaveItem)

onEvent(ItemEvent.SetIsOngoing(isOngoing = false))

ShortcutUtil.removeShortcutStartPause(context)
ShortcutUtil.addShortcutStopPause(context)
}
}

/*@RequiresApi(Build.VERSION_CODES.O)
@Preview
@Composable
Expand Down
62 changes: 62 additions & 0 deletions app/src/main/java/cloud/pablos/overload/ui/utils/ShortcutUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package cloud.pablos.overload.ui.utils

import android.content.Context
import android.content.Intent
import androidx.core.content.pm.ShortcutInfoCompat
import androidx.core.content.pm.ShortcutManagerCompat
import androidx.core.graphics.drawable.IconCompat
import cloud.pablos.overload.R
import cloud.pablos.overload.ui.ShortcutActivity

object ShortcutUtil {

private const val stopPauseId = "stop_pause"
private const val startPauseId = "stop_pause"
fun addShortcutStopPause(
context: Context,
) {
val shortcut = ShortcutInfoCompat.Builder(context, stopPauseId)
.setShortLabel("stop pause")
.setLongLabel("stop the pause")
.setIcon(IconCompat.createWithResource(context, R.drawable.ic_launcher_background))
.setIntent(
Intent(context, ShortcutActivity::class.java).apply {
action = Intent.ACTION_VIEW
},
)
.build()
ShortcutManagerCompat.pushDynamicShortcut(context, shortcut)
}

fun removeShortcutStopPause(
context: Context,
) {
val shortcuts: MutableList<String> = mutableListOf()
shortcuts.add(stopPauseId)
ShortcutManagerCompat.removeDynamicShortcuts(context, shortcuts)
}

fun createShortcutStartPause(
context: Context,
) {
val shortcut = ShortcutInfoCompat.Builder(context, startPauseId)
.setShortLabel("start pause")
.setLongLabel("start the pause")
.setIcon(IconCompat.createWithResource(context, R.drawable.ic_launcher_background))
.setIntent(
Intent(context, ShortcutActivity::class.java).apply {
action = Intent.ACTION_VIEW
},
)
.build()
ShortcutManagerCompat.pushDynamicShortcut(context, shortcut)
}

fun removeShortcutStartPause(
context: Context,
) {
val shortcuts: MutableList<String> = mutableListOf()
shortcuts.add(startPauseId)
ShortcutManagerCompat.removeDynamicShortcuts(context, shortcuts)
}
}

0 comments on commit 7c5c4e8

Please sign in to comment.