From ef1353e82b6f10f0c9ec04de0eace54f15c2c316 Mon Sep 17 00:00:00 2001 From: Wynd Date: Sun, 22 Feb 2026 17:57:55 +0200 Subject: [PATCH] Import menu composable for both kinds of imports --- .../ui/components/ImportDropdownMenu.kt | 53 +++++++++++++++++++ .../recipe/ui/components/MainScreen.kt | 13 +---- .../pixelatedw/recipe/utils/SyncRecipes.kt | 32 +++++------ 3 files changed, 72 insertions(+), 26 deletions(-) create mode 100644 app/src/main/java/xyz/pixelatedw/recipe/ui/components/ImportDropdownMenu.kt diff --git a/app/src/main/java/xyz/pixelatedw/recipe/ui/components/ImportDropdownMenu.kt b/app/src/main/java/xyz/pixelatedw/recipe/ui/components/ImportDropdownMenu.kt new file mode 100644 index 0000000..f6440f4 --- /dev/null +++ b/app/src/main/java/xyz/pixelatedw/recipe/ui/components/ImportDropdownMenu.kt @@ -0,0 +1,53 @@ +package xyz.pixelatedw.recipe.ui.components + +import android.content.Intent +import androidx.compose.foundation.layout.Box +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.Add +import androidx.compose.material3.DropdownMenu +import androidx.compose.material3.DropdownMenuItem +import androidx.compose.material3.Icon +import androidx.compose.material3.IconButton +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import xyz.pixelatedw.recipe.MainActivity +import xyz.pixelatedw.recipe.utils.sync + +@Composable +fun ImportDropdownMenu(ctx: MainActivity) { + var expanded by remember { mutableStateOf(false) } + Box( + ) { + IconButton( + onClick = { expanded = !expanded } + ) { + Icon( + imageVector = Icons.Default.Add, + contentDescription = "Import recipes" + ) + } + DropdownMenu( + expanded = expanded, + onDismissRequest = { expanded = false } + ) { + DropdownMenuItem( + text = { Text("Import") }, + onClick = { + ctx.sourceChooser.launch(Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)) + expanded = false + } + ) + DropdownMenuItem( + text = { Text("Sync") }, + onClick = { + sync(ctx) + expanded = false + } + ) + } + } +} diff --git a/app/src/main/java/xyz/pixelatedw/recipe/ui/components/MainScreen.kt b/app/src/main/java/xyz/pixelatedw/recipe/ui/components/MainScreen.kt index f891783..e162ce2 100644 --- a/app/src/main/java/xyz/pixelatedw/recipe/ui/components/MainScreen.kt +++ b/app/src/main/java/xyz/pixelatedw/recipe/ui/components/MainScreen.kt @@ -115,7 +115,7 @@ fun MainScreen(ctx: MainActivity, padding: PaddingValues, view: RecipesView) { contentDescription = "Toggle tags filtering menu" ) } - // Load / Delete + // Import / Delete if (selectedEntries.isNotEmpty()) { IconButton( modifier = Modifier.weight(0.15f), @@ -130,16 +130,7 @@ fun MainScreen(ctx: MainActivity, padding: PaddingValues, view: RecipesView) { ) } } else { - IconButton( - modifier = Modifier.weight(0.15f), - onClick = { sync(ctx) }, -// onClick = { ctx.sourceChooser.launch(Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)) }, - ) { - Icon( - imageVector = Icons.Default.Add, - contentDescription = "Load recipes from filesystem" - ) - } + ImportDropdownMenu(ctx) } } LazyColumn { diff --git a/app/src/main/java/xyz/pixelatedw/recipe/utils/SyncRecipes.kt b/app/src/main/java/xyz/pixelatedw/recipe/utils/SyncRecipes.kt index c9358d1..8cd1c51 100644 --- a/app/src/main/java/xyz/pixelatedw/recipe/utils/SyncRecipes.kt +++ b/app/src/main/java/xyz/pixelatedw/recipe/utils/SyncRecipes.kt @@ -1,13 +1,10 @@ package xyz.pixelatedw.recipe.utils -import android.os.Build -import android.os.FileUtils +import android.content.Context import android.os.Handler import android.os.Looper -import androidx.annotation.RequiresApi import androidx.documentfile.provider.DocumentFile import xyz.pixelatedw.recipe.MainActivity -import java.io.BufferedOutputStream import java.io.DataInputStream import java.io.File import java.io.FileOutputStream @@ -16,14 +13,20 @@ import java.nio.ByteBuffer import java.nio.charset.StandardCharsets import java.util.concurrent.Executors +const val DEFAULT_SYNC_SERVER_IP = "192.168.0.100" +const val DEFAULT_SYNC_SERVER_PORT = 9696 fun sync(ctx: MainActivity) { val executor = Executors.newSingleThreadExecutor() val handler = Handler(Looper.getMainLooper()) - executor.execute(Runnable() { + executor.execute { try { - val conn = Socket("192.168.0.100", 9696) + val prefs = ctx.getPreferences(Context.MODE_PRIVATE) + val syncServerIp = prefs.getString("syncServerIp", DEFAULT_SYNC_SERVER_IP) + val syncServerPort = prefs.getInt("syncServerPort", DEFAULT_SYNC_SERVER_PORT) + + val conn = Socket(syncServerIp, syncServerPort) val stream = conn.getInputStream() val inputStream = DataInputStream(stream) @@ -31,16 +34,16 @@ fun sync(ctx: MainActivity) { var buffer = ByteArray(8) inputStream.read(buffer) val filesSent = ByteBuffer.wrap(buffer).getLong().toInt() -// println("files sent: $filesSent") - for(f in 0.. contentBufLen) { blockSize = contentBufLen - usedBytes - } - else if (blockSize > contentBufLen) { + } else if (blockSize > contentBufLen) { blockSize = contentBufLen } @@ -87,10 +89,10 @@ fun sync(ctx: MainActivity) { e.printStackTrace() } - handler.post(Runnable { + handler.post { val recipes = ctx.db.recipeWithTagsDao().getAll() ctx.recipeView.setRecipes(recipes) - println("syncing complete") - }) - }) +// println("syncing complete") + } + } }