Skip to content

Commit

Permalink
some refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
yamin8000 committed Feb 7, 2025
1 parent 0568a87 commit 1249851
Show file tree
Hide file tree
Showing 35 changed files with 1,688 additions and 886 deletions.
5 changes: 5 additions & 0 deletions app/src/main/java/io/github/yamin8000/owl/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@
package io.github.yamin8000.owl.ui

import android.annotation.SuppressLint
import android.bluetooth.BluetoothAdapter
import android.content.Intent
import android.os.Bundle
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.hilt.navigation.compose.hiltViewModel
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* freeDictionaryApp/freeDictionaryApp.common.main
* ClickableIcon.kt Copyrighted by Yamin Siahmargooei at 2025/2/7
* ClickableIcon.kt Last modified at 2025/2/7
* This file is part of freeDictionaryApp/freeDictionaryApp.common.main.
* Copyright (C) 2025 Yamin Siahmargooei
*
* freeDictionaryApp/freeDictionaryApp.common.main is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* freeDictionaryApp/freeDictionaryApp.common.main is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with freeDictionaryApp. If not, see <https://www.gnu.org/licenses/>.
*/

package io.github.yamin8000.owl.common.ui.components

import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
import androidx.compose.ui.platform.LocalHapticFeedback

@Composable
fun ClickableIcon(
modifier: Modifier = Modifier,
enabled: Boolean = true,
imageVector: ImageVector,
contentDescription: String,
onClick: () -> Unit
) {
val haptic = LocalHapticFeedback.current
IconButton(
modifier = modifier,
enabled = enabled,
onClick = {
haptic.performHapticFeedback(HapticFeedbackType.LongPress)
onClick()
},
content = {
Icon(
imageVector = imageVector,
contentDescription = contentDescription,
)
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,183 +21,10 @@

package io.github.yamin8000.owl.common.ui.components

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.asPaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.ime
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.twotone.ArrowBack
import androidx.compose.material.icons.twotone.Delete
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Snackbar
import androidx.compose.material3.SnackbarDefaults
import androidx.compose.material3.Surface
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.ripple
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.airbnb.lottie.compose.LottieAnimation
import com.airbnb.lottie.compose.LottieCompositionSpec
import com.airbnb.lottie.compose.LottieConstants
import com.airbnb.lottie.compose.animateLottieCompositionAsState
import com.airbnb.lottie.compose.rememberLottieComposition
import io.github.yamin8000.owl.common.ui.util.ContextUtils.findActivity
import io.github.yamin8000.owl.strings.R

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun Ripple(
modifier: Modifier = Modifier,
content: @Composable BoxScope.() -> Unit,
onClick: () -> Unit,
onLongClick: () -> Unit = {}
) {
val interactionSource = remember { MutableInteractionSource() }
Box(
content = content,
modifier = modifier
.combinedClickable(
interactionSource = interactionSource,
indication = ripple(),
onClick = onClick,
onLongClick = onLongClick
)
)
}

@Composable
fun MySnackbar(
modifier: Modifier = Modifier,
action: @Composable (() -> Unit)? = null,
dismissAction: @Composable (() -> Unit)? = null,
actionOnNewLine: Boolean = false,
containerColor: Color = SnackbarDefaults.color,
contentColor: Color = SnackbarDefaults.contentColor,
actionContentColor: Color = SnackbarDefaults.actionContentColor,
dismissActionContentColor: Color = SnackbarDefaults.dismissActionContentColor,
content: @Composable () -> Unit
) {
Snackbar(
modifier = modifier
.padding(vertical = 16.dp, horizontal = 16.dp)
.padding(WindowInsets.ime.asPaddingValues()),
action = action,
dismissAction = dismissAction,
actionOnNewLine = actionOnNewLine,
shape = RoundedCornerShape(10.dp),
containerColor = containerColor,
contentColor = contentColor,
actionContentColor = actionContentColor,
dismissActionContentColor = dismissActionContentColor,
content = content
)
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ScaffoldWithTitle(
title: String,
modifier: Modifier = Modifier,
snackbarHost: @Composable () -> Unit = {},
onBackClick: () -> Unit,
content: @Composable BoxScope.() -> Unit
) {
val scrollBehavior = TopAppBarDefaults.enterAlwaysScrollBehavior()
val connection by remember { mutableStateOf(scrollBehavior.nestedScrollConnection) }
Scaffold(
modifier = modifier
.fillMaxSize()
.nestedScroll(connection),
snackbarHost = snackbarHost,
topBar = {
Surface(
shadowElevation = 8.dp,
content = {
TopAppBar(
scrollBehavior = scrollBehavior,
title = {
PersianText(
text = title,
fontSize = 20.sp,
textAlign = TextAlign.Center
)
},
actions = {
ClickableIcon(
imageVector = Icons.AutoMirrored.TwoTone.ArrowBack,
contentDescription = stringResource(R.string.back),
onClick = onBackClick
)
}
)
}
)
},
content = {
Box(
content = content,
modifier = Modifier
.padding(it)
.padding(start = 16.dp, end = 16.dp, bottom = 0.dp, top = 4.dp)
)
}
)
}

@Composable
fun ClickableIcon(
modifier: Modifier = Modifier,
enabled: Boolean = true,
imageVector: ImageVector,
contentDescription: String,
onClick: () -> Unit
) {
val haptic = LocalHapticFeedback.current
val clickWithFeedback = remember(onClick) {
{
haptic.performHapticFeedback(HapticFeedbackType.LongPress)
onClick()
}
}
IconButton(
modifier = modifier,
enabled = enabled,
onClick = clickWithFeedback,
content = {
Icon(
imageVector = imageVector,
contentDescription = contentDescription,
)
}
)
}

@Composable
fun LockScreenOrientation(
Expand All @@ -213,47 +40,4 @@ fun LockScreenOrientation(
activity.requestedOrientation = originalOrientation
}
}
}

@Composable
internal fun DeleteMenu(
modifier: Modifier = Modifier,
expanded: Boolean,
onDismiss: () -> Unit,
onDelete: () -> Unit
) {
val delete = stringResource(R.string.delete)
DropdownMenu(
modifier = modifier,
expanded = expanded,
onDismissRequest = onDismiss,
content = {
DropdownMenuItem(
onClick = onDelete,
text = { PersianText(delete) },
leadingIcon = {
Icon(
imageVector = Icons.TwoTone.Delete,
contentDescription = delete
)
}
)
}
)
}

@Composable
fun EmptyList(
modifier: Modifier = Modifier,
) {
val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(io.github.yamin8000.owl.core.R.raw.empty_list))
val progress by animateLottieCompositionAsState(
composition = composition,
iterations = LottieConstants.IterateForever
)
LottieAnimation(
modifier = modifier,
composition = composition,
progress = { progress },
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* freeDictionaryApp/freeDictionaryApp.common.main
* DeleteMenu.kt Copyrighted by Yamin Siahmargooei at 2025/2/7
* DeleteMenu.kt Last modified at 2025/2/7
* This file is part of freeDictionaryApp/freeDictionaryApp.common.main.
* Copyright (C) 2025 Yamin Siahmargooei
*
* freeDictionaryApp/freeDictionaryApp.common.main is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* freeDictionaryApp/freeDictionaryApp.common.main is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with freeDictionaryApp. If not, see <https://www.gnu.org/licenses/>.
*/

package io.github.yamin8000.owl.common.ui.components

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.twotone.Delete
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import io.github.yamin8000.owl.strings.R

@Composable
internal fun DeleteMenu(
modifier: Modifier = Modifier,
expanded: Boolean,
onDismiss: () -> Unit,
onDelete: () -> Unit
) {
val delete = stringResource(R.string.delete)
DropdownMenu(
modifier = modifier,
expanded = expanded,
onDismissRequest = onDismiss,
content = {
DropdownMenuItem(
onClick = onDelete,
text = { PersianText(delete) },
leadingIcon = {
Icon(
imageVector = Icons.TwoTone.Delete,
contentDescription = delete
)
}
)
}
)
}
Loading

0 comments on commit 1249851

Please sign in to comment.