Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app-service-hilt/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
31 changes: 31 additions & 0 deletions app-service-hilt/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Service

## Initial Setup

This is a sample project on how to use `compose-floating-window` on a service for long running operations

1. Create a new service. Like [this](src/main/java/com/github/only52607/compose/window/service/MyService.kt) for example

2. Add the permission to the manifest file

```xml
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
```

3. Declare the service in the manifest file

```xml
<service android:name=".MyService" />
```

4. Follow the `MyService` sample on how to use it on a service.

## Usage

Do not follow the `build.gradle.kts` setup as it was done for the sample project

Note:
- Be sure to `AppCompatActivity` instead of `ComponentActivity` in order to change the theme of the app

This is mostly just a sample project on how to incorporate hilt into viewmodels and services.

89 changes: 89 additions & 0 deletions app-service-hilt/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
buildscript {
dependencies {
classpath(libs.ksp.gradle)
classpath(libs.hilt.android.gradle.plugin)
}
}
hilt {
enableAggregatingTask = false
}

plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
alias(libs.plugins.compose.compiler)

alias(libs.plugins.hilt.android)
alias(libs.plugins.ksp)
}

android {
namespace = "com.github.only52607.compose.window.hilt"
compileSdk = libs.versions.compile.sdk.get().toInt()

defaultConfig {
applicationId = "com.github.only52607.compose.window"
minSdk = libs.versions.min.sdk.get().toInt()
targetSdk = libs.versions.target.sdk.get().toInt()
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
}

buildTypes {
release {
isMinifyEnabled = true
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
signingConfig = signingConfigs.getByName("debug")
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = "17"
}
buildFeatures {
compose = true
}
packaging {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
}

dependencies {
implementation(project(":library"))
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.androidx.lifecycle.viewmodel.compose)
implementation(libs.activity.compose)
implementation(platform(libs.compose.bom))
implementation(libs.compose.ui)
implementation(libs.compose.ui.graphics)
implementation(libs.compose.ui.tooling.preview)
implementation(libs.compose.material3)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.test.junit)
androidTestImplementation(libs.androidx.test.espresso)
androidTestImplementation(platform(libs.compose.bom))
androidTestImplementation(libs.compose.ui.test.junit4)
debugImplementation(libs.compose.ui.tooling)
debugImplementation(libs.compose.ui.test.manifest)
debugImplementation(libs.leak.canary)

implementation(libs.dagger.hilt.android)
ksp(libs.dagger.hilt.compiler)

implementation(libs.appcompat)
implementation(libs.bundles.datastore)
}
21 changes: 21 additions & 0 deletions app-service-hilt/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.github.only52607.compose.window

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.github.only52607.compose.window", appContext.packageName)
}
}
33 changes: 33 additions & 0 deletions app-service-hilt/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<application
android:name=".FloatingApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service android:name=".MyService" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.only52607.compose.window.hilt

import android.app.Application
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class FloatingApplication: Application()
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.github.only52607.compose.window.hilt

import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.github.only52607.compose.window.hilt.repository.UserPreferencesRepository
import com.github.only52607.compose.window.hilt.ui.DialogPermission
import com.github.only52607.compose.window.hilt.ui.theme.ComposeFloatingWindowTheme
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.launch
import javax.inject.Inject

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

@Inject
lateinit var userPreferencesRepository: UserPreferencesRepository

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ComposeFloatingWindowTheme {
LaunchedEffect(userPreferencesRepository.darkModeFlow) {
userPreferencesRepository
.darkModeFlow
.distinctUntilChanged()
.collect { darkMode ->
AppCompatDelegate.setDefaultNightMode(
if (darkMode) AppCompatDelegate.MODE_NIGHT_YES
else AppCompatDelegate.MODE_NIGHT_NO
)
}
}

val showDialogPermission = remember { mutableStateOf(false) }

val context = LocalContext.current

val isShowing by MyService.serviceStarted.collectAsStateWithLifecycle(false)

Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Column(
Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Button(
onClick = {
MyService.start(context)
},
enabled = !isShowing
) {
Text("Show")
}
Spacer(modifier = Modifier.height(10.dp))
Button(
onClick = {
MyService.stop(context)
},
enabled = isShowing
) {
Text("Hide")
}
}
DialogPermission(showDialogState = showDialogPermission)
}
}
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.github.only52607.compose.window.hilt

import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.IBinder
import com.github.only52607.compose.window.ComposeFloatingWindow
import com.github.only52607.compose.window.hilt.repository.UserPreferencesRepository
import com.github.only52607.compose.window.hilt.ui.FloatingWindowContent
import com.github.only52607.compose.window.hilt.ui.FloatingWindowViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import javax.inject.Inject

@AndroidEntryPoint
class MyService : Service() {
companion object {
private var _serviceStarted = MutableStateFlow(false)
val serviceStarted: StateFlow<Boolean>
get() = _serviceStarted.asStateFlow()

fun start(context: Context) {
val intent = Intent(context, MyService::class.java)
context.startService(intent)
}

fun stop(context: Context) {
val intent = Intent(context, MyService::class.java)
context.stopService(intent)
}
}

@Inject
lateinit var userPreferencesRepository: UserPreferencesRepository

private val viewModel by lazy {
FloatingWindowViewModel(userPreferencesRepository)
}

private val floatingWindow by lazy {
createFloatingWindow()
}

private fun createFloatingWindow(): ComposeFloatingWindow =
ComposeFloatingWindow(this).apply {
setContent {
FloatingWindowContent(viewModel)
}
}

override fun onCreate() {
super.onCreate()
_serviceStarted.update { true }
floatingWindow.show()
}

override fun onBind(intent: Intent?): IBinder? = null

override fun onDestroy() {
_serviceStarted.update { false }
// Call close for cleanup and it will hide it in the process
floatingWindow.close()
super.onDestroy()
}
}
Loading