Skip to content

Commit

Permalink
fix: di stackoverflow
Browse files Browse the repository at this point in the history
unless it got worse..
  • Loading branch information
aliernfrog committed Jan 28, 2024
1 parent 33a8c48 commit 2183266
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 32 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/com/aliernfrog/lactool/di/AppModule.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.aliernfrog.lactool.di

import com.aliernfrog.lactool.impl.ProgressState
import com.aliernfrog.lactool.util.manager.ContextUtils
import com.aliernfrog.lactool.util.manager.PreferenceManager
import com.aliernfrog.toptoast.state.TopToastState
Expand All @@ -9,6 +10,7 @@ import org.koin.dsl.module
val appModule = module {
singleOf(::ContextUtils)
singleOf(::PreferenceManager)
singleOf(::ProgressState)
single {
TopToastState(composeView = null)
}
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/aliernfrog/lactool/di/Util.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.aliernfrog.lactool.di

import org.koin.mp.KoinPlatformTools

inline fun <reified T : Any> get(): T = KoinPlatformTools.defaultContext().get().get<T>()
8 changes: 8 additions & 0 deletions app/src/main/java/com/aliernfrog/lactool/impl/Progress.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package com.aliernfrog.lactool.impl

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue

class ProgressState() {
var currentProgress by mutableStateOf<Progress?>(null)
}

class Progress(
val description: String,
totalProgress: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ fun MainScreen(
mainViewModel.navController = navController
}

mainViewModel.activeProgress?.let {
mainViewModel.progressState.currentProgress?.let {
ProgressDialog(it) {}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import androidx.navigation.NavController
import com.aliernfrog.lactool.R
import com.aliernfrog.lactool.TAG
import com.aliernfrog.lactool.data.ReleaseInfo
import com.aliernfrog.lactool.di.get
import com.aliernfrog.lactool.enum.MapsListSegment
import com.aliernfrog.lactool.githubRepoURL
import com.aliernfrog.lactool.impl.MapFile
import com.aliernfrog.lactool.impl.Progress
import com.aliernfrog.lactool.impl.ProgressState
import com.aliernfrog.lactool.util.Destination
import com.aliernfrog.lactool.util.extension.cacheFile
import com.aliernfrog.lactool.util.extension.showErrorToast
Expand All @@ -45,8 +47,7 @@ import java.net.URL
class MainViewModel(
val prefs: PreferenceManager,
val topToastState: TopToastState,
private val mapsViewModel: MapsViewModel,
private val mapsListViewModel: MapsListViewModel,
val progressState: ProgressState,
context: Context
) : ViewModel() {
lateinit var scope: CoroutineScope
Expand All @@ -58,7 +59,6 @@ class MainViewModel(
val applicationVersionCode = GeneralUtil.getAppVersionCode(context)
private val applicationIsPreRelease = applicationVersionName.contains("-alpha")

var activeProgress by mutableStateOf<Progress?>(null)
var showAlphaWarningDialog by mutableStateOf(
applicationIsPreRelease && prefs.lastAlphaAck != applicationVersionName
)
Expand Down Expand Up @@ -139,6 +139,9 @@ class MainViewModel(
}

fun handleIntent(intent: Intent, context: Context) {
val mapsViewModel = get<MapsViewModel>()
val mapsListViewModel = get<MapsListViewModel>()

try {
val uris: MutableList<Uri> = intent.data?.let {
mutableListOf(it)
Expand All @@ -150,7 +153,7 @@ class MainViewModel(
}
if (uris.isEmpty()) return

activeProgress = Progress(context.getString(R.string.info_pleaseWait))
progressState.currentProgress = Progress(context.getString(R.string.info_pleaseWait))
viewModelScope.launch(Dispatchers.IO) {
val cached = uris.map { uri ->
MapFile(uri.cacheFile(context)!!)
Expand All @@ -162,12 +165,12 @@ class MainViewModel(
mapsViewModel.sharedMaps = cached.toMutableStateList()
mapsListViewModel.chosenSegment = MapsListSegment.SHARED
}
activeProgress = null
progressState.currentProgress = null
}
} catch (e: Exception) {
Log.e(TAG, "handleIntent: $e")
topToastState.showErrorToast()
activeProgress = null
progressState.currentProgress = null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.aliernfrog.laclib.map.LACMapEditor
import com.aliernfrog.lactool.R
import com.aliernfrog.lactool.TAG
import com.aliernfrog.lactool.impl.Progress
import com.aliernfrog.lactool.impl.ProgressState
import com.aliernfrog.lactool.util.Destination
import com.aliernfrog.lactool.util.extension.nameWithoutExtension
import com.aliernfrog.lactool.util.extension.removeHtml
Expand All @@ -44,6 +45,7 @@ import java.io.File
@OptIn(ExperimentalMaterial3Api::class)
class MapsEditViewModel(
val topToastState: TopToastState,
private val progressState: ProgressState,
private val mainViewModel: MainViewModel,
context: Context
) : ViewModel() {
Expand Down Expand Up @@ -75,10 +77,6 @@ class MapsEditViewModel(
var materialSheetChosenMaterial by mutableStateOf<LACMapDownloadableMaterial?>(null)
var materialSheetMaterialFailed by mutableStateOf(false)

private var activeProgress: Progress?
get() = mainViewModel.activeProgress
set(value) { mainViewModel.activeProgress = value }

@SuppressLint("Recycle")
suspend fun openMap(file: Any, context: Context) {
when (file) {
Expand Down Expand Up @@ -183,7 +181,7 @@ class MapsEditViewModel(
@SuppressLint("Recycle")
suspend fun saveAndFinishEditing(onNavigateBackRequest: () -> Unit, context: Context) {
val mapName = mapFile?.nameWithoutExtension ?: mapDocumentFile?.nameWithoutExtension
activeProgress = Progress(
progressState.currentProgress = Progress(
context.getString(R.string.maps_edit_saving).replace("{NAME}", mapName.toString())
)
try { withContext(Dispatchers.IO) {
Expand All @@ -204,7 +202,7 @@ class MapsEditViewModel(
Log.e(TAG, "saveAndFinishEditing: ", e)
}
finishEditingWithoutSaving(onNavigateBackRequest)
activeProgress = null
progressState.currentProgress = null
}

suspend fun finishEditingWithoutSaving(onNavigateBackRequest: () -> Unit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.aliernfrog.lactool.TAG
import com.aliernfrog.lactool.data.MapActionResult
import com.aliernfrog.lactool.impl.MapFile
import com.aliernfrog.lactool.impl.Progress
import com.aliernfrog.lactool.impl.ProgressState
import com.aliernfrog.lactool.util.extension.resolvePath
import com.aliernfrog.lactool.util.extension.showErrorToast
import com.aliernfrog.lactool.util.manager.ContextUtils
Expand All @@ -30,7 +31,7 @@ import kotlinx.coroutines.withContext
@OptIn(ExperimentalMaterial3Api::class)
class MapsViewModel(
val topToastState: TopToastState,
private val mainViewModel: MainViewModel,
private val progressState: ProgressState,
private val contextUtils: ContextUtils,
val prefs: PreferenceManager
) : ViewModel() {
Expand All @@ -52,8 +53,8 @@ class MapsViewModel(
var customDialogTitleAndText: Pair<String, String>? by mutableStateOf(null)

var activeProgress: Progress?
get() = mainViewModel.activeProgress
set(value) { mainViewModel.activeProgress = value }
get() = progressState.currentProgress
set(value) { progressState.currentProgress = value }

val mapListBackButtonShown
get() = chosenMap != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.lifecycle.ViewModel
import com.aliernfrog.lactool.R
import com.aliernfrog.lactool.data.ImageFile
import com.aliernfrog.lactool.impl.Progress
import com.aliernfrog.lactool.impl.ProgressState
import com.aliernfrog.lactool.util.extension.resolvePath
import com.aliernfrog.lactool.util.manager.ContextUtils
import com.aliernfrog.lactool.util.manager.PreferenceManager
Expand All @@ -30,8 +31,8 @@ import kotlinx.coroutines.withContext
class ScreenshotsViewModel(
val prefs: PreferenceManager,
private val topToastState: TopToastState,
private val progressState: ProgressState,
private val contextUtils: ContextUtils,
private val mainViewModel: MainViewModel,
context: Context
) : ViewModel() {
val topAppBarState = TopAppBarState(0F, 0F, 0F)
Expand All @@ -45,10 +46,6 @@ class ScreenshotsViewModel(
var screenshots by mutableStateOf(emptyList<ImageFile>())
var screenshotSheetScreeenshot by mutableStateOf<ImageFile?>(null)

private var activeProgress: Progress?
get() = mainViewModel.activeProgress
set(value) { mainViewModel.activeProgress = value }

fun getScreenshotsFile(context: Context): DocumentFileCompat {
val isUpToDate = if (!::screenshotsFile.isInitialized) false
else {
Expand All @@ -73,15 +70,15 @@ class ScreenshotsViewModel(
}

suspend fun deleteImportedScreenshot(screenshot: ImageFile) {
activeProgress = Progress(
progressState.currentProgress = Progress(
contextUtils.getString(R.string.screenshots_deleting)
)
withContext(Dispatchers.IO) {
screenshotsFile.findFile(screenshot.fileName)?.delete()
fetchScreenshots()
topToastState.showToast(R.string.screenshots_deleted, Icons.Rounded.Delete, TopToastColor.ERROR)
}
activeProgress = null
progressState.currentProgress = null
}

suspend fun shareImportedScreenshot(screenshot: ImageFile, context: Context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import androidx.lifecycle.ViewModel
import com.aliernfrog.lactool.R
import com.aliernfrog.lactool.data.ImageFile
import com.aliernfrog.lactool.impl.Progress
import com.aliernfrog.lactool.impl.ProgressState
import com.aliernfrog.lactool.util.extension.resolvePath
import com.aliernfrog.lactool.util.manager.ContextUtils
import com.aliernfrog.lactool.util.manager.PreferenceManager
Expand All @@ -34,8 +35,8 @@ import java.io.File
class WallpapersViewModel(
val prefs: PreferenceManager,
val topToastState: TopToastState,
private val progressState: ProgressState,
private val contextUtils: ContextUtils,
private val mainViewModel: MainViewModel,
context: Context
) : ViewModel() {
val topAppBarState = TopAppBarState(0F, 0F, 0F)
Expand All @@ -50,10 +51,6 @@ class WallpapersViewModel(
var pickedWallpaper by mutableStateOf<ImageFile?>(null)
var wallpaperSheetWallpaper by mutableStateOf<ImageFile?>(null)

private var activeProgress: Progress?
get() = mainViewModel.activeProgress
set(value) { mainViewModel.activeProgress = value }

suspend fun setPickedWallpaper(uri: Uri, context: Context) {
withContext(Dispatchers.IO) {
val file = UriUtil.cacheFile(
Expand All @@ -75,7 +72,7 @@ class WallpapersViewModel(

suspend fun importPickedWallpaper(context: Context) {
val wallpaper = pickedWallpaper ?: return
activeProgress = Progress(
progressState.currentProgress = Progress(
contextUtils.getString(R.string.wallpapers_chosen_importing)
)
withContext(Dispatchers.IO) {
Expand All @@ -89,7 +86,7 @@ class WallpapersViewModel(
fetchImportedWallpapers()
topToastState.showToast(R.string.wallpapers_chosen_imported, Icons.Rounded.Download)
}
activeProgress = null
progressState.currentProgress = null
}

suspend fun shareImportedWallpaper(wallpaper: ImageFile, context: Context) {
Expand All @@ -99,15 +96,15 @@ class WallpapersViewModel(
}

suspend fun deleteImportedWallpaper(wallpaper: ImageFile) {
activeProgress = Progress(
progressState.currentProgress = Progress(
contextUtils.getString(R.string.wallpapers_deleting)
)
withContext(Dispatchers.IO) {
wallpapersFile.findFile(wallpaper.fileName)?.delete()
fetchImportedWallpapers()
topToastState.showToast(R.string.wallpapers_deleted, Icons.Rounded.Delete, TopToastColor.ERROR)
}
activeProgress = null
progressState.currentProgress = null
}

fun getWallpapersFile(context: Context): DocumentFileCompat {
Expand Down

0 comments on commit 2183266

Please sign in to comment.