Skip to content

Commit

Permalink
closes #70
Browse files Browse the repository at this point in the history
fixed some recompositions
  • Loading branch information
yamin8000 committed Feb 9, 2024
1 parent bcbce76 commit c7f3272
Show file tree
Hide file tree
Showing 17 changed files with 219 additions and 226 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ android {
applicationId = appId
minSdk = 21
targetSdk = 34
versionCode = 37
versionName = "1.6.0"
versionCode = 38
versionName = "1.6.1"
vectorDrawables.useSupportLibrary = true
ksp.arg("room.schemaLocation", "$projectDir/schemas")
archivesName = "$applicationId-v$versionCode($versionName)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@

package io.github.yamin8000.owl.data.model

import android.os.Parcelable
import androidx.compose.runtime.Immutable
import kotlinx.parcelize.Parcelize
import androidx.compose.runtime.Stable

@Immutable
@Parcelize
@Stable
data class Definition(
val definition: String,
val example: String?,
val synonyms: List<String>,
val antonyms: List<String>
) : Parcelable
)
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,10 @@

package io.github.yamin8000.owl.data.model

import android.os.Parcelable
import kotlinx.parcelize.Parcelize

@Parcelize
data class Entry(
val word: String,
val phonetics: List<Phonetic>,
val meanings: List<Meaning>,
val license: License,
val sourceUrls: List<String>
) : Parcelable
)
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@

package io.github.yamin8000.owl.data.model

import android.os.Parcelable
import kotlinx.parcelize.Parcelize

@Parcelize
data class License(
val name: String,
val url: String
) : Parcelable
)
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@

package io.github.yamin8000.owl.data.model

import android.os.Parcelable
import kotlinx.parcelize.Parcelize
import androidx.compose.runtime.Stable

@Parcelize
@Stable
data class Meaning(
val partOfSpeech: String,
val definitions: List<Definition>,
val synonyms: List<String>,
val antonyms: List<String>
) : Parcelable
)
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,9 @@

package io.github.yamin8000.owl.data.model

import android.os.Parcelable
import kotlinx.parcelize.Parcelize

@Parcelize
data class Phonetic(
val text: String? = null,
val audio: String? = null,
val sourceUrl: String? = null,
val license: License? = null
) : Parcelable
)
5 changes: 5 additions & 0 deletions app/src/main/java/io/github/yamin8000/owl/ui/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ package io.github.yamin8000.owl.ui

import android.app.Application
import android.content.Context
import android.speech.tts.TextToSpeech
import androidx.compose.runtime.ProvidableCompositionLocal
import androidx.compose.runtime.compositionLocalOf
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.preferencesDataStore
Expand All @@ -31,4 +34,6 @@ internal val Context.settingsDataStore: DataStore<Preferences> by preferencesDat
internal val Context.historyDataStore: DataStore<Preferences> by preferencesDataStore(name = "history")
internal val Context.favouritesDataStore: DataStore<Preferences> by preferencesDataStore(name = "favourites")

internal val LocalTTS: ProvidableCompositionLocal<TextToSpeech?> = compositionLocalOf { null }

internal class App : Application()
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

package io.github.yamin8000.owl.ui.composable

import android.content.Context
import android.speech.tts.TextToSpeech
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.interaction.MutableInteractionSource
Expand Down Expand Up @@ -54,7 +52,6 @@ import androidx.compose.material3.TopAppBarScrollBehavior
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -79,12 +76,10 @@ import com.airbnb.lottie.compose.rememberLottieComposition
import io.github.yamin8000.owl.R
import io.github.yamin8000.owl.util.Constants.DNS_SERVERS
import io.github.yamin8000.owl.util.Constants.INTERNET_CHECK_DELAY
import io.github.yamin8000.owl.util.TTS
import io.github.yamin8000.owl.util.findActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext
import java.util.Locale

@OptIn(ExperimentalFoundationApi::class)
@Composable
Expand Down Expand Up @@ -193,7 +188,7 @@ fun ClickableIcon(
onClick: () -> Unit
) {
val haptic = LocalHapticFeedback.current
val clickWithFeedback = remember {
val clickWithFeedback = remember(onClick) {
{
haptic.performHapticFeedback(HapticFeedbackType.LongPress)
onClick()
Expand All @@ -212,18 +207,6 @@ fun ClickableIcon(
)
}

@Composable
fun TtsAwareContent(
ttsLanguageLocaleTag: String = Locale.US.toLanguageTag(),
content: @Composable (TextToSpeech) -> Unit
) {
val context = LocalContext.current
val ttsHelper = remember { TTS(context, Locale.forLanguageTag(ttsLanguageLocaleTag)) }
val tts: MutableState<TextToSpeech?> = remember { mutableStateOf(null) }
LaunchedEffect(Unit) { tts.value = ttsHelper.getTts() }
if (tts.value != null) tts.value?.let { content(it) }
}

@Composable
fun InternetAwareComposable(
dnsServers: List<String> = DNS_SERVERS,
Expand Down
43 changes: 20 additions & 23 deletions app/src/main/java/io/github/yamin8000/owl/ui/composable/Texts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Dialog
import io.github.yamin8000.owl.R
import io.github.yamin8000.owl.ui.LocalTTS
import io.github.yamin8000.owl.ui.theme.DefaultCutShape
import io.github.yamin8000.owl.ui.theme.Samim
import io.github.yamin8000.owl.ui.theme.defaultGradientBorder
Expand Down Expand Up @@ -131,9 +132,9 @@ fun PersianText(
style: TextStyle = LocalTextStyle.current
) {
val context = LocalContext.current
var localStyle by remember { mutableStateOf(style) }
var localFontFamily by remember { mutableStateOf(fontFamily) }
val currentLocale = remember { getCurrentLocale(context) }
var localStyle by remember(style) { mutableStateOf(style) }
var localFontFamily by remember(fontFamily) { mutableStateOf(fontFamily) }
val currentLocale = remember(context) { getCurrentLocale(context) }
if (currentLocale.language == Locale("fa").language) {
localFontFamily = Samim
localStyle = LocalTextStyle.current.copy(textDirection = TextDirection.Rtl)
Expand Down Expand Up @@ -230,7 +231,7 @@ fun CopyAbleRippleText(
content = {
val words = sanitizeWords(text.split(regex).toSet()).toList()
items(words) { item ->
val onItemClick = remember(onDoubleClick, isDialogShown) {
val onItemClick = remember(onDoubleClick, item) {
{
onDoubleClick?.invoke(item)
isDialogShown = false
Expand Down Expand Up @@ -310,7 +311,6 @@ fun CopyAbleRippleTextWithIcon(
fun SpeakableRippleTextWithIcon(
text: String,
imageVector: ImageVector,
localeTag: String,
title: String? = null,
content: @Composable (() -> Unit)? = null,
onDoubleClick: ((String) -> Unit)? = null
Expand All @@ -321,24 +321,21 @@ fun SpeakableRippleTextWithIcon(
context.findActivity()?.getSystemService(Context.AUDIO_SERVICE) as AudioManager
}

TtsAwareContent(
ttsLanguageLocaleTag = localeTag,
content = {
val onClick = remember(it, audio, content, increaseVolumeText) {
{
if (audio.getStreamVolume(AudioManager.STREAM_MUSIC) == 0)
Toast.makeText(context, increaseVolumeText, Toast.LENGTH_SHORT).show()
it.speak(text)
}
}
CopyAbleRippleTextWithIcon(
text = text,
content = content,
title = title,
imageVector = imageVector,
onDoubleClick = onDoubleClick,
onClick = onClick
)
val tts = LocalTTS.current
val onClick = remember(tts, audio, text) {
{
if (audio.getStreamVolume(AudioManager.STREAM_MUSIC) == 0)
Toast.makeText(context, increaseVolumeText, Toast.LENGTH_SHORT).show()
tts?.speak(text)
Unit
}
}
CopyAbleRippleTextWithIcon(
text = text,
content = content,
title = title,
imageVector = imageVector,
onDoubleClick = onDoubleClick,
onClick = onClick
)
}
Loading

0 comments on commit c7f3272

Please sign in to comment.