diff --git a/presenter/src/main/java/com/foke/together/presenter/screen/ShareScreen.kt b/presenter/src/main/java/com/foke/together/presenter/screen/ShareScreen.kt index 10bf65b..0cea2ac 100644 --- a/presenter/src/main/java/com/foke/together/presenter/screen/ShareScreen.kt +++ b/presenter/src/main/java/com/foke/together/presenter/screen/ShareScreen.kt @@ -1,17 +1,10 @@ package com.foke.together.presenter.screen -import android.graphics.Bitmap -import android.graphics.BitmapFactory -import android.net.Uri import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.aspectRatio import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.foundation.layout.size -import androidx.compose.foundation.layout.wrapContentSize import androidx.compose.material.icons.Icons -import androidx.compose.material.icons.filled.Download import androidx.compose.material.icons.filled.Home import androidx.compose.material.icons.filled.Print import androidx.compose.material.icons.filled.Share @@ -20,27 +13,18 @@ import androidx.compose.material3.IconButton import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface import androidx.compose.runtime.Composable -import androidx.compose.runtime.State -import androidx.compose.runtime.collectAsState import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.tooling.preview.Preview -import androidx.compose.ui.unit.dp -import androidx.constraintlayout.compose.ChainStyle import androidx.constraintlayout.compose.ConstraintLayout import androidx.constraintlayout.compose.Dimension import androidx.core.content.FileProvider import androidx.core.net.toFile -import com.foke.together.presenter.frame.FourCutFrame import com.foke.together.presenter.theme.FourCutTogetherTheme -import com.foke.together.presenter.theme.highContrastDarkColorScheme import androidx.hilt.navigation.compose.hiltViewModel -import androidx.lifecycle.Lifecycle -import androidx.lifecycle.compose.LifecycleEventEffect import coil.compose.AsyncImage import coil.request.ImageRequest -import com.foke.together.presenter.R import com.foke.together.presenter.viewmodel.ShareViewModel import com.foke.together.util.ImageFileUtil @@ -50,8 +34,6 @@ fun ShareScreen( viewModel: ShareViewModel = hiltViewModel() ) { val context = LocalContext.current - val qrCodeBitmap: State = viewModel.qrCodeBitmap.collectAsState(initial = Bitmap.createBitmap( - 1,1,Bitmap.Config.ARGB_8888) ) ConstraintLayout( modifier = Modifier.fillMaxSize() ) { @@ -142,13 +124,14 @@ fun ShareScreen( ){ AsyncImage( model = ImageRequest.Builder(context) - .data(qrCodeBitmap.value) + .data(viewModel.qrCodeBitmap) .build(), contentDescription = "qr code", modifier = Modifier.fillMaxSize() ) } +// TODO: add android native share button // IconButton( // onClick = { // viewModel.downloadImage() diff --git a/presenter/src/main/java/com/foke/together/presenter/viewmodel/ShareViewModel.kt b/presenter/src/main/java/com/foke/together/presenter/viewmodel/ShareViewModel.kt index e3ee0c8..7ef6798 100644 --- a/presenter/src/main/java/com/foke/together/presenter/viewmodel/ShareViewModel.kt +++ b/presenter/src/main/java/com/foke/together/presenter/viewmodel/ShareViewModel.kt @@ -3,6 +3,9 @@ package com.foke.together.presenter.viewmodel import android.content.Context import android.graphics.Bitmap import android.net.Uri +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.setValue import androidx.core.content.FileProvider import androidx.core.net.toFile import androidx.lifecycle.ViewModel @@ -11,13 +14,11 @@ import com.foke.together.domain.interactor.GeneratePhotoFrameUseCase import com.foke.together.domain.interactor.GetQRCodeUseCase import com.foke.together.domain.interactor.web.GetDownloadUrlUseCase import com.foke.together.domain.interactor.web.SessionKeyUseCase +import com.foke.together.util.AppLog import com.foke.together.util.AppPolicy import com.foke.together.util.ImageFileUtil import dagger.hilt.android.lifecycle.HiltViewModel -import dagger.hilt.android.qualifiers.ActivityContext import dagger.hilt.android.qualifiers.ApplicationContext -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.flow import kotlinx.coroutines.launch import javax.inject.Inject @@ -29,15 +30,16 @@ class ShareViewModel @Inject constructor( private val sessionKeyUseCase: SessionKeyUseCase, private val generatePhotoFrameUseCase: GeneratePhotoFrameUseCase ): ViewModel() { - val qrCodeBitmap: Flow = flow{ - val qrCodeBitmap = generateQRcode() - emit(qrCodeBitmap) - } - + var qrCodeBitmap by mutableStateOf(null) val singleImageUri: Uri = generatePhotoFrameUseCase.getFinalSingleImageUri() - val twoImageUri: Uri = generatePhotoFrameUseCase.getFinalTwoImageUri() + init { + viewModelScope.launch { + generateQRcode() + } + } + fun downloadImage() { val imageBitmap = ImageFileUtil.getBitmapFromUri(context, singleImageUri) viewModelScope.launch { @@ -49,19 +51,15 @@ class ShareViewModel @Inject constructor( } } - suspend fun generateQRcode(): Bitmap { + private suspend fun generateQRcode() { val sessionKey = sessionKeyUseCase.getSessionKey() - val downloadUrl:String = getDownloadUrlUseCase(sessionKey).toString() - if(downloadUrl.contains("Failure")){ - return Bitmap.createBitmap( - 1,1,Bitmap.Config.ARGB_8888) + val downloadUrl: String = getDownloadUrlUseCase(sessionKey).getOrElse { "https://4cuts.store" } + + if (AppPolicy.isDebugMode) { + AppLog.e(TAG, "generateQRcode", "sessionKey: $sessionKey") + AppLog.e(TAG, "generateQRcode", "downloadUrl: $downloadUrl") } - return getQRCodeUseCase(sessionKey, downloadUrl) - // return a empty bitmap if the qr code generation fails - .getOrElse( - return Bitmap.createBitmap( - 1,1,Bitmap.Config.ARGB_8888) - ) + qrCodeBitmap = getQRCodeUseCase(sessionKey, downloadUrl).getOrNull() } fun shareImage() { @@ -76,4 +74,8 @@ class ShareViewModel @Inject constructor( fun printImage(activityContext: Context) { ImageFileUtil.printFromUri(activityContext,twoImageUri) } + + companion object { + private val TAG = ShareViewModel::class.java.simpleName + } } \ No newline at end of file