Skip to content

Commit

Permalink
Some refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
T8RIN committed Jan 11, 2025
1 parent 8da932e commit 7c14ead
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import ru.tech.imageresizershrinker.core.data.utils.executorDispatcher
import ru.tech.imageresizershrinker.core.di.DecodingDispatcher
Expand All @@ -31,6 +30,7 @@ import ru.tech.imageresizershrinker.core.di.IoDispatcher
import ru.tech.imageresizershrinker.core.di.UiDispatcher
import java.util.concurrent.Executors
import javax.inject.Singleton
import kotlin.coroutines.CoroutineContext


@Module
Expand All @@ -40,14 +40,14 @@ internal object CoroutinesModule {
@DefaultDispatcher
@Singleton
@Provides
fun defaultDispatcher(): CoroutineDispatcher = executorDispatcher {
fun defaultDispatcher(): CoroutineContext = executorDispatcher {
Executors.newCachedThreadPool()
}

@DecodingDispatcher
@Singleton
@Provides
fun decodingDispatcher(): CoroutineDispatcher = executorDispatcher {
fun decodingDispatcher(): CoroutineContext = executorDispatcher {
Executors.newFixedThreadPool(
2 * Runtime.getRuntime().availableProcessors() + 1
)
Expand All @@ -56,18 +56,18 @@ internal object CoroutinesModule {
@EncodingDispatcher
@Singleton
@Provides
fun encodingDispatcher(): CoroutineDispatcher = executorDispatcher {
fun encodingDispatcher(): CoroutineContext = executorDispatcher {
Executors.newSingleThreadExecutor()
}

@IoDispatcher
@Singleton
@Provides
fun ioDispatcher(): CoroutineDispatcher = Dispatchers.IO
fun ioDispatcher(): CoroutineContext = Dispatchers.IO

@UiDispatcher
@Singleton
@Provides
fun uiDispatcher(): CoroutineDispatcher = Dispatchers.Main.immediate
fun uiDispatcher(): CoroutineContext = Dispatchers.Main.immediate

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@

package ru.tech.imageresizershrinker.core.data.dispatchers

import kotlinx.coroutines.CoroutineDispatcher
import ru.tech.imageresizershrinker.core.di.DecodingDispatcher
import ru.tech.imageresizershrinker.core.di.DefaultDispatcher
import ru.tech.imageresizershrinker.core.di.EncodingDispatcher
import ru.tech.imageresizershrinker.core.di.IoDispatcher
import ru.tech.imageresizershrinker.core.di.UiDispatcher
import ru.tech.imageresizershrinker.core.domain.dispatchers.DispatchersHolder
import javax.inject.Inject
import kotlin.coroutines.CoroutineContext

internal data class AndroidDispatchersHolder @Inject constructor(
@UiDispatcher override val uiDispatcher: CoroutineDispatcher,
@IoDispatcher override val ioDispatcher: CoroutineDispatcher,
@EncodingDispatcher override val encodingDispatcher: CoroutineDispatcher,
@DecodingDispatcher override val decodingDispatcher: CoroutineDispatcher,
@DefaultDispatcher override val defaultDispatcher: CoroutineDispatcher
@UiDispatcher override val uiDispatcher: CoroutineContext,
@IoDispatcher override val ioDispatcher: CoroutineContext,
@EncodingDispatcher override val encodingDispatcher: CoroutineContext,
@DecodingDispatcher override val decodingDispatcher: CoroutineContext,
@DefaultDispatcher override val defaultDispatcher: CoroutineContext
) : DispatchersHolder
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okio.use
import ru.tech.imageresizershrinker.core.data.saving.io.StreamWriteable
Expand Down Expand Up @@ -303,10 +304,12 @@ internal class AndroidFileController @Inject constructor(

override fun clearCache(
onComplete: (String) -> Unit,
) = context.clearCache(
dispatcher = ioDispatcher,
onComplete = onComplete
)
) {
CoroutineScope(ioDispatcher).launch {
context.clearCache()
onComplete(getReadableCacheSize())
}
}

override fun getReadableCacheSize(): String = context.cacheSize()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ import android.os.Build
import androidx.compose.ui.unit.Density
import androidx.core.content.ContextCompat
import androidx.exifinterface.media.ExifInterface
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import ru.tech.imageresizershrinker.core.domain.image.model.MetadataTag
import ru.tech.imageresizershrinker.core.domain.utils.readableByteCount
import java.io.OutputStream
Expand Down Expand Up @@ -98,22 +95,14 @@ fun Context.openWriteableStream(
}
}

internal fun Context.clearCache(
dispatcher: CoroutineDispatcher,
onComplete: (cache: String) -> Unit = {}
) {
CoroutineScope(dispatcher).launch {
coroutineScope {
runCatching {
cacheDir?.deleteRecursively()
codeCacheDir?.deleteRecursively()
externalCacheDir?.deleteRecursively()
externalCacheDirs?.forEach {
it.deleteRecursively()
}
}
internal suspend fun Context.clearCache() = coroutineScope {
runCatching {
cacheDir?.deleteRecursively()
codeCacheDir?.deleteRecursively()
externalCacheDir?.deleteRecursively()
externalCacheDirs?.forEach {
it.deleteRecursively()
}
onComplete(cacheSize())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

package ru.tech.imageresizershrinker.core.data.utils

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.asCoroutineDispatcher
import java.util.concurrent.ExecutorService
import kotlin.coroutines.CoroutineContext

inline fun executorDispatcher(
executor: () -> ExecutorService
): CoroutineDispatcher = executor().asCoroutineDispatcher()
): CoroutineContext = executor().asCoroutineDispatcher()
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

package ru.tech.imageresizershrinker.core.domain.dispatchers

import kotlinx.coroutines.CoroutineDispatcher
import kotlin.coroutines.CoroutineContext

interface DispatchersHolder {
val uiDispatcher: CoroutineDispatcher
val ioDispatcher: CoroutineDispatcher
val encodingDispatcher: CoroutineDispatcher
val decodingDispatcher: CoroutineDispatcher
val defaultDispatcher: CoroutineDispatcher
val uiDispatcher: CoroutineContext
val ioDispatcher: CoroutineContext
val encodingDispatcher: CoroutineContext
val decodingDispatcher: CoroutineContext
val defaultDispatcher: CoroutineContext
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* You should have received a copy of the Apache License
* along with this program. If not, see <http://www.apache.org/licenses/LICENSE-2.0>.
*/
@file:Suppress("NOTHING_TO_INLINE")

package ru.tech.imageresizershrinker.core.domain.utils

Expand All @@ -33,7 +34,7 @@ private val BASE64_PATTERN = Regex(

inline fun <reified T, reified R> T.cast(): R = this as R

@Suppress("NOTHING_TO_INLINE")

inline operator fun CharSequence.times(
count: Int
): CharSequence = repeat(count.coerceAtLeast(0))

0 comments on commit 7c14ead

Please sign in to comment.