Skip to content

Commit

Permalink
Merge pull request #215 from terrakok/dev
Browse files Browse the repository at this point in the history
Implement media upload on iOS.
  • Loading branch information
Hiebeler authored Feb 17, 2025
2 parents ad30f56 + 4c8308e commit 9aae1a6
Show file tree
Hide file tree
Showing 29 changed files with 232 additions and 144 deletions.
15 changes: 15 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ kotlin {
//shared preferences
implementation(libs.multiplatform.settings)

//file picker
implementation(libs.filekit.compose)

//lifecycle
implementation(libs.androidx.lifecycle.runtime.compose)
implementation(libs.androidx.lifecycle.viewmodel)
Expand Down Expand Up @@ -139,6 +142,10 @@ android {
}
}

buildFeatures {
buildConfig = true
}

buildTypes {
release {
isMinifyEnabled = false
Expand All @@ -148,6 +155,14 @@ android {
getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
)
}

create("demo") {
initWith(getByName("debug"))
isMinifyEnabled = true
isDebuggable = false
isProfileable = false
isShrinkResources = true
}
}
packaging.resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import coil3.SingletonImageLoader
import com.daniebeler.pfpixelix.di.AppComponent
import com.daniebeler.pfpixelix.di.WorkerComponent
import com.daniebeler.pfpixelix.di.create
import com.daniebeler.pfpixelix.utils.configureLogger
import com.daniebeler.pfpixelix.widget.notifications.work_manager.LatestImageTask
import com.daniebeler.pfpixelix.widget.notifications.work_manager.NotificationsTask

Expand All @@ -25,6 +26,7 @@ class MyApplication : Application(), Configuration.Provider {
SingletonImageLoader.setSafe {
appComponent.provideImageLoader()
}
configureLogger(BuildConfig.DEBUG)
super.onCreate()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ actual class UpdateAccountUseCase actual constructor(
append(HttpHeaders.ContentDisposition, fileName)
})
} catch (e: Exception) {
Logger.e("UpdateAccountUseCase") { e.message!! }
Logger.e("UpdateAccountUseCase", e)
}
}

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ import com.daniebeler.pfpixelix.utils.ThemePrefUtil.LIGHT
import com.daniebeler.pfpixelix.widget.notifications.NotificationWidgetReceiver
import com.russhwolf.settings.Settings
import com.russhwolf.settings.SharedPreferencesSettings
import io.github.vinceglb.filekit.core.PlatformFile
import okio.Path
import okio.Path.Companion.toPath
import java.io.File

actual typealias KmpUri = Uri
actual fun String.toKmpUri(): KmpUri = this.toUri()
actual val EmptyKmpUri = Uri.EMPTY
actual fun PlatformFile.toKmpUri(): KmpUri = this.uri

actual typealias KmpContext = Context

Expand Down Expand Up @@ -144,4 +146,6 @@ actual fun KmpContext.pinWidget() {
}

actual fun isAbleToDownloadImage(): Boolean =
Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q
Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q

actual fun KmpUri.getPlatformUriObject(): Any = this
4 changes: 2 additions & 2 deletions app/src/commonMain/kotlin/com/daniebeler/pfpixelix/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,8 @@ private fun NavGraphBuilder.navigationGraph(
val uId = navBackStackEntry.arguments?.getString("postid")
val refresh = navBackStackEntry.arguments?.getBoolean("refresh")!!
val openReplies = navBackStackEntry.arguments?.getBoolean("openReplies")!!
Logger.d("refresh") { refresh.toString() }
Logger.d("openReplies") { openReplies.toString() }
Logger.d { "refresh $refresh" }
Logger.d { "openReplies $openReplies" }
uId?.let { id ->
SinglePostComposable(navController, postId = id, refresh, openReplies)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,9 @@ interface PixelfedApi {
@Query("q") searchText: String
): Call<List<PlaceDto>>

@Headers("Content-Type: application/json")
@POST("api/v2/media")
fun uploadMedia(
@Body body: String
@Body body: MultiPartFormDataContent
): Call<MediaAttachmentDto>

@FormUrlEncoded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ data class SoftwareSmallDto(
@SerialName("version") val version: String
): DtoInterface<SoftwareSmall> {
override fun toModel(): SoftwareSmall {
Logger.d("SoftwareSmallDto") { "Converting SoftwareSmallDto to SoftwareSmall: $this" }
Logger.d { "SoftwareSmallDto: Converting SoftwareSmallDto to SoftwareSmall: $this" }
return SoftwareSmall(
id = id,
name = name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class PostEditorRepositoryImpl @Inject constructor(
)

try {
val res = pixelfedApi.uploadMedia(json.encodeToString(data)).execute().toModel()
val res = pixelfedApi.uploadMedia(data).execute().toModel()
emit(Resource.Success(res))
} catch (e: Exception) {
Logger.d(e.message.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ abstract class AppComponent(
install(Logging) {
logger = object : io.ktor.client.plugins.logging.Logger {
override fun log(message: String) {
Logger.v("HttpClient") {
Logger.v("Pixelix HttpClient") {
message.lines().joinToString { "\n\t\t$it"}
}
}
}
level = LogLevel.BODY
level = LogLevel.INFO
}
install(HttpTimeout) {
requestTimeoutMillis = 60000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class AuthService(
install(Logging) {
logger = object : io.ktor.client.plugins.logging.Logger {
override fun log(message: String) {
Logger.v("HttpAuth") {
Logger.v("Pixelix HttpAuth") {
message.lines().joinToString { "\n\t\t$it" }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ import kotlinx.coroutines.flow.Flow
import me.tatarka.inject.annotations.Inject

@Inject
expect class UploadMediaUseCase(
postEditorRepository: PostEditorRepository
class UploadMediaUseCase(
private val postEditorRepository: PostEditorRepository
) {
operator fun invoke(
url: KmpUri,
description: String,
context: KmpContext,
): Flow<Resource<MediaAttachment>>
context: KmpContext
): Flow<Resource<MediaAttachment>> {
return postEditorRepository.uploadMedia(
url, description, context
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fun MentionComposable(
LaunchedEffect(viewModel.postState.post, viewModel.postContextState.postContext) {
if (viewModel.postState.post != null && viewModel.postContextState.postContext != null) {
val index = viewModel.postContextState.postContext!!.ancestors.size + 1
Logger.d("index") { index.toString() }
Logger.d { "index $index" }
coroutineScope.launch {
lazyListState.scrollToItem(index)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.daniebeler.pfpixelix.ui.composables.newpost

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
Expand Down Expand Up @@ -64,10 +65,16 @@ import com.daniebeler.pfpixelix.ui.composables.textfield_mentions.TextFieldMenti
import com.daniebeler.pfpixelix.utils.KmpUri
import com.daniebeler.pfpixelix.utils.LocalKmpContext
import com.daniebeler.pfpixelix.utils.MimeType
import com.daniebeler.pfpixelix.utils.getPlatformUriObject
import com.daniebeler.pfpixelix.utils.imeAwareInsets
import com.daniebeler.pfpixelix.utils.toKmpUri
import io.github.vinceglb.filekit.compose.rememberFilePickerLauncher
import io.github.vinceglb.filekit.core.PickerMode
import io.github.vinceglb.filekit.core.PickerType
import org.jetbrains.compose.resources.stringResource
import org.jetbrains.compose.resources.vectorResource
import pixelix.app.generated.resources.Res
import pixelix.app.generated.resources.add_outline
import pixelix.app.generated.resources.alt_text
import pixelix.app.generated.resources.audience
import pixelix.app.generated.resources.audience_public
Expand Down Expand Up @@ -134,13 +141,13 @@ fun NewPostComposable(
if (type != null && type.take(5) == "video") {
//todo KMP video
AsyncImage(
model = image.imageUri,
model = image.imageUri.getPlatformUriObject(),
contentDescription = "video thumbnail",
modifier = Modifier.width(100.dp)
)
} else {
AsyncImage(
model = image.imageUri,
model = image.imageUri.getPlatformUriObject(),
contentDescription = null,
modifier = Modifier.width(100.dp)
)
Expand Down Expand Up @@ -195,11 +202,22 @@ fun NewPostComposable(
}
}
Box(modifier = Modifier.fillMaxWidth(), contentAlignment = Alignment.Center) {
SinglePhotoPickerButton { result ->
result.forEach {
viewModel.addImage(it, context)
val launcher = rememberFilePickerLauncher(
type = PickerType.ImageAndVideo,
mode = PickerMode.Multiple()
) { files ->
files?.forEach { file ->
viewModel.addImage(file.toKmpUri(), context)
}
}
Icon(
modifier = Modifier
.clickable { launcher.launch() }
.height(50.dp)
.width(50.dp),
imageVector = vectorResource(Res.drawable.add_outline),
contentDescription = null,
)
}
Spacer(modifier = Modifier.height(20.dp))
TextFieldMentionsComposable(
Expand Down Expand Up @@ -300,11 +318,10 @@ fun NewPostComposable(
}



}
}
TextFieldLocationsComposable(
submit = {viewModel.setLocation(it)},
submit = { viewModel.setLocation(it) },
submitPlace = {},
initialValue = null,
labelStringId = Res.string.location,
Expand Down Expand Up @@ -359,6 +376,3 @@ fun NewPostComposable(
}
}
}

@Composable
expect fun SinglePhotoPickerButton(selected: (result: List<KmpUri>) -> Unit)
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class OtherProfileViewModel @Inject constructor(
}

fun loadDataByUsername(username: String, refreshing: Boolean) {
Logger.d("byUsername") { "load data by username" }
Logger.d { "byUsername: load data by username" }
getAccountByUsername(username, refreshing)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ object HtmlToText {
val text = document.text().replace("\\n", "\n")
val cleanedText = text.lines().joinToString("\n") { it.trimStart() } // Trim leading spaces

Logger.d("htmlToText") { cleanedText }
Logger.d { cleanedText }
return cleanedText.trim()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import androidx.compose.runtime.staticCompositionLocalOf
import coil3.PlatformContext
import com.daniebeler.pfpixelix.ui.composables.settings.icon_selection.IconWithName
import com.russhwolf.settings.Settings
import io.github.vinceglb.filekit.core.PlatformFile
import okio.Path

expect abstract class KmpUri {
abstract override fun toString(): String
}
expect fun String.toKmpUri(): KmpUri
expect fun PlatformFile.toKmpUri(): KmpUri
expect val EmptyKmpUri: KmpUri
expect fun KmpUri.getPlatformUriObject(): Any

expect abstract class KmpContext
val LocalKmpContext = staticCompositionLocalOf<KmpContext> { error("no KmpContext") }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.daniebeler.pfpixelix.utils

import co.touchlab.kermit.Logger
import co.touchlab.kermit.Severity

fun configureLogger(isDebug: Boolean = false) {
Logger.setTag("Pixelix")
Logger.setMinSeverity(
if (isDebug) Severity.Verbose else Severity.Error
)
}
Loading

0 comments on commit 9aae1a6

Please sign in to comment.