Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DialogのComposable化 #165

Merged
merged 8 commits into from
Jun 5, 2024
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
2 changes: 1 addition & 1 deletion AndroidApp/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
android:theme="@style/AppTheme.SplashScreen">

<activity
android:name="me.nya_n.notificationnotifier.ui.screen.MainActivity"
android:name="me.nya_n.notificationnotifier.ui.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import me.nya_n.notificationnotifier.domain.usecase.impl.PackageVisibilityGrante
import me.nya_n.notificationnotifier.domain.usecase.impl.SaveAddressUseCaseImpl
import me.nya_n.notificationnotifier.domain.usecase.impl.SaveFilterConditionUseCaseImpl
import me.nya_n.notificationnotifier.domain.util.SharedPreferenceProvider
import me.nya_n.notificationnotifier.ui.screen.app.AppViewModel
import me.nya_n.notificationnotifier.ui.screen.detail.DetailViewModel
import me.nya_n.notificationnotifier.ui.screen.selection.SelectionViewModel
import me.nya_n.notificationnotifier.ui.screen.settings.SettingsViewModel
Expand Down Expand Up @@ -71,6 +72,7 @@ class App : Application() {
single<AppRepository> { AppRepositoryImpl(get(), get()) }

// ViewModel
viewModel { AppViewModel(get(), packageName, get(), get()) }
viewModel { SelectionViewModel(get(), get(), get()) }
viewModel { params -> DetailViewModel(get(), get(), get(), params.get()) }
viewModel { TargetViewModel(get(), get()) }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package me.nya_n.notificationnotifier.ui

import android.os.Bundle
import androidx.activity.SystemBarStyle
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.ui.graphics.toArgb
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import me.nya_n.notificationnotifier.ui.screen.app.AppScreen
import me.nya_n.notificationnotifier.ui.theme.AppColors

class MainActivity : AppCompatActivity() {
private val isReady = MutableStateFlow(false)

override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen().setKeepOnScreenCondition { !isReady.value }
super.onCreate(savedInstanceState)
setContent {
enableEdgeToEdge(
statusBarStyle = SystemBarStyle.dark(
AppColors.Primary.toArgb(),
),
navigationBarStyle = SystemBarStyle.dark(
AppColors.Primary.toArgb()
)
)
AppScreen()
}
lifecycleScope.launch {
delay(500)
isReady.update { true }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package me.nya_n.notificationnotifier.ui.common

import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.tooling.preview.Preview
import me.nya_n.notificationnotifier.ui.R
import me.nya_n.notificationnotifier.ui.theme.AppTheme

@Composable
fun CommonDialog(
message: String,
positiveButtonText: String,
negativeButtonText: String,
onPositiveDismissRequest: () -> Unit,
onNegativeDismissRequest: () -> Unit,
) {
AlertDialog(
text = { Text(text = message) },
onDismissRequest = { /* noop */ },
confirmButton = {
TextButton(
onClick = onPositiveDismissRequest
) {
Text(
text = positiveButtonText,
style = TextStyle(
color = Color.White
)
)
}
},
dismissButton = {
TextButton(
onClick = onNegativeDismissRequest
) {
Text(
text = negativeButtonText,
style = TextStyle(
color = Color.White
)
)
}
}
)
}

@Composable
fun RequireNotificationPermissionDialog(
onDismissRequest: (isGranted: Boolean) -> Unit
) {
CommonDialog(
message = stringResource(id = R.string.require_permission),
positiveButtonText = stringResource(id = R.string.next),
negativeButtonText = stringResource(id = R.string.ng),
onPositiveDismissRequest = { onDismissRequest(true) },
onNegativeDismissRequest = { onDismissRequest(false) }
)
}

@Composable
fun RequirePackageVisibilityDialog(
onDismissRequest: (isGranted: Boolean) -> Unit
) {
CommonDialog(
message = stringResource(id = R.string.require_package_visibility),
positiveButtonText = stringResource(id = R.string.approval),
negativeButtonText = stringResource(id = R.string.ng),
onPositiveDismissRequest = { onDismissRequest(true) },
onNegativeDismissRequest = { onDismissRequest(false) }
)
}

@Preview
@Composable
fun CommonDialogPreview() {
AppTheme {
CommonDialog(
message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
positiveButtonText = "YES",
negativeButtonText = "NO",
onPositiveDismissRequest = { },
onNegativeDismissRequest = { }
)
}
}

@Preview
@Composable
fun RequireNotificationPermissionDialogPreview() {
AppTheme {
RequireNotificationPermissionDialog(
onDismissRequest = { }
)
}
}

@Preview
@Composable
fun RequirePackageVisibilityDialogPreview() {
AppTheme {
RequirePackageVisibilityDialog(
onDismissRequest = { }
)
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package me.nya_n.notificationnotifier.ui.screen

// UiEvent消化時にオブジェクトを比較したいのでdata classやobjectではなくclassを使う
// classじゃないと同じイベントを消してしまうことがある
abstract class AppUiEvent {
abstract class BaseUiEvent {
override fun equals(other: Any?): Boolean {
return this === other
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update

abstract class AppViewModel<UiEvent : AppUiEvent> : ViewModel() {
abstract class BaseViewModel<UiEvent : BaseUiEvent> : ViewModel() {
/** 単発のイベント
* - イベント消化後はAppViewModel#consumeEventを呼ぶ
*/
Expand Down
Loading