-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
still needs icons and the actual action doesn't work
- Loading branch information
Showing
5 changed files
with
179 additions
and
34 deletions.
There are no files selected for viewing
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
55 changes: 55 additions & 0 deletions
55
app/src/main/java/cloud/pablos/overload/ui/ShortcutActivity.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,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() | ||
} | ||
} |
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
62 changes: 62 additions & 0 deletions
62
app/src/main/java/cloud/pablos/overload/ui/utils/ShortcutUtil.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,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) | ||
} | ||
} |