From 6dc77bf5eb937ba75ed1b2c16fea8ac463b49e0f Mon Sep 17 00:00:00 2001 From: Suhas Dissanayake Date: Wed, 2 Aug 2023 12:32:42 +0530 Subject: [PATCH] Improved copying of additive details --- .../backend/viewmodels/AdditiveDetailViewModel.kt | 15 +++++++++++---- .../foode/ui/screens/AdditiveDetailScreen.kt | 6 +----- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/app/suhasdissa/foode/backend/viewmodels/AdditiveDetailViewModel.kt b/app/src/main/java/app/suhasdissa/foode/backend/viewmodels/AdditiveDetailViewModel.kt index 8bb5eb0..300ed61 100644 --- a/app/src/main/java/app/suhasdissa/foode/backend/viewmodels/AdditiveDetailViewModel.kt +++ b/app/src/main/java/app/suhasdissa/foode/backend/viewmodels/AdditiveDetailViewModel.kt @@ -1,5 +1,6 @@ package app.suhasdissa.foode.backend.viewmodels +import android.content.ClipData import android.content.ClipboardManager import android.content.SharedPreferences import android.util.Log @@ -72,10 +73,6 @@ class AdditiveDetailViewModel( } } - fun getClipboard(): ClipboardManager { - return clipboard - } - fun getAdditive(id: Int) { viewModelScope.launch { additive = additivesRepository.getAdditive(id) @@ -91,6 +88,16 @@ class AdditiveDetailViewModel( } } + fun copyToClipboard() { + val copyText = with(additive ?: return) { + "$eCode : $title\n${if (halalStatus == 1) "Halal Certified\n" else "\n"}$info" + } + viewModelScope.launch { + val clip: ClipData = ClipData.newPlainText("Additive Details", copyText) + clipboard.setPrimaryClip(clip) + } + } + companion object { val Factory: ViewModelProvider.Factory = viewModelFactory { initializer { diff --git a/app/src/main/java/app/suhasdissa/foode/ui/screens/AdditiveDetailScreen.kt b/app/src/main/java/app/suhasdissa/foode/ui/screens/AdditiveDetailScreen.kt index a74abbb..71fdf69 100644 --- a/app/src/main/java/app/suhasdissa/foode/ui/screens/AdditiveDetailScreen.kt +++ b/app/src/main/java/app/suhasdissa/foode/ui/screens/AdditiveDetailScreen.kt @@ -1,6 +1,5 @@ package app.suhasdissa.foode.ui.screens -import android.content.ClipData import android.view.SoundEffectConstants import androidx.compose.animation.AnimatedVisibility import androidx.compose.foundation.Image @@ -101,10 +100,7 @@ fun AdditiveDetailScreen( } FloatingActionButton(onClick = { view.playSoundEffect(SoundEffectConstants.CLICK) - val copyText = - "${additive.eCode} : ${additive.title}\n\nHalal Status : ${additive.halalStatus}\n\n${additive.info}" - val clip: ClipData = ClipData.newPlainText("Additive Details", copyText) - additiveViewModel.getClipboard().setPrimaryClip(clip) + additiveViewModel.copyToClipboard() }) { Icon( imageVector = Icons.Default.ContentCopy,