Skip to content

Commit

Permalink
Merge branch 'trunk' into andy/revert-foreground-service-types
Browse files Browse the repository at this point in the history
  • Loading branch information
notandyvee committed Jun 12, 2024
2 parents 491e138 + 756cbd8 commit 69efddc
Show file tree
Hide file tree
Showing 31 changed files with 3,907 additions and 3,001 deletions.
30 changes: 15 additions & 15 deletions .idea/checkstyle-idea.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.wordpress.android.datasets

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
* Helper class to handle asynchronous I/O tasks using coroutines
* @see <a href="https://github.com/wordpress-mobile/WordPress-Android/pull/20937">Introduction</a>
*/
object AsyncTaskExecutor {
/**
* Execute a data loading task in the IO thread and handle the result on the main thread
*/
@JvmStatic
fun <T> executeIo(scope: CoroutineScope, backgroundTask: () -> T, callback: AsyncTaskCallback<T>) {
execute(scope, Dispatchers.IO, backgroundTask, callback)
}

/**
* Execute a data loading task in the default thread and handle the result on the main thread
*/
@JvmStatic
fun <T> executeDefault(scope: CoroutineScope, backgroundTask: () -> T, callback: AsyncTaskCallback<T>) {
execute(scope, Dispatchers.Default, backgroundTask, callback)
}

private fun <T> execute(
scope: CoroutineScope,
dispatcher: CoroutineDispatcher,
backgroundTask: () -> T,
callback: AsyncTaskCallback<T>
) {
scope.launch(dispatcher) {
// handle the background task
val result = backgroundTask()

withContext(Dispatchers.Main) {
// handle the result on the main thread
callback.onTaskFinished(result)
}
}
}

interface AsyncTaskCallback<T> {
fun onTaskFinished(result: T)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class CardViewModelSlice @Inject constructor(
identifier = buildConfigWrapper.getApplicationId(),
marketingVersion = buildConfigWrapper.getAppVersionName(),
platform = FEATURE_FLAG_PLATFORM_PARAMETER,
osVersion = buildConfigWrapper.androidVersion
)
val result = cardsStore.fetchCards(payload)
val error = result.error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ class EditPostActivity : LocaleAwareActivity(), EditorFragmentActivity, EditorIm
val hasQuickPressBlogId = extras.containsKey(EditPostActivityConstants.EXTRA_QUICKPRESS_BLOG_ID)

// QuickPress might want to use a different blog than the current blog
return if (!isActionSendOrNewMedia && !hasQuickPressFlag && hasQuickPressBlogId) {
return if ((isActionSendOrNewMedia || hasQuickPressFlag) && hasQuickPressBlogId) {
val localSiteId = intent.getIntExtra(EditPostActivityConstants.EXTRA_QUICKPRESS_BLOG_ID, -1)
siteStore.getSiteByLocalId(localSiteId)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,9 @@ class ReaderFragment : Fragment(R.layout.reader_fragment_layout), ScrollableView
}

fun requestBookmarkTab() {
if (!::viewModel.isInitialized) {
viewModel = ViewModelProvider(this@ReaderFragment, viewModelFactory)[ReaderViewModel::class.java]
}
viewModel.bookmarkTabRequested()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@

import javax.inject.Inject;

import static androidx.lifecycle.LifecycleOwnerKt.getLifecycleScope;
import static org.wordpress.android.fluxc.generated.AccountActionBuilder.newUpdateSubscriptionNotificationPostAction;
import static org.wordpress.android.ui.reader.ReaderActivityLauncher.OpenUrlType.INTERNAL;

Expand Down Expand Up @@ -1923,7 +1924,8 @@ private ReaderPostAdapter getPostAdapter() {
mImageManager,
mUiHelpers,
mNetworkUtilsWrapper,
mIsTopLevel
mIsTopLevel,
getLifecycleScope(this)
);
mPostAdapter.setOnFollowListener(this);
mPostAdapter.setOnPostSelectedListener(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleCoroutineScope;
import androidx.recyclerview.widget.RecyclerView;

import org.wordpress.android.R;
import org.wordpress.android.WordPress;
import org.wordpress.android.analytics.AnalyticsTracker;
import org.wordpress.android.datasets.AsyncTaskHandler;
import org.wordpress.android.datasets.AsyncTaskExecutor;
import org.wordpress.android.datasets.ReaderPostTable;
import org.wordpress.android.datasets.ReaderTagTable;
import org.wordpress.android.fluxc.store.AccountStore;
Expand Down Expand Up @@ -82,11 +83,13 @@
import kotlin.jvm.functions.Function1;
import kotlin.jvm.functions.Function2;
import kotlin.jvm.functions.Function3;
import kotlinx.coroutines.CoroutineScope;

public class ReaderPostAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final ImageManager mImageManager;
private final UiHelpers mUiHelpers;
private final NetworkUtilsWrapper mNetworkUtilsWrapper;
private final CoroutineScope mScope;
private ReaderTag mCurrentTag;
private long mCurrentBlogId;
private long mCurrentFeedId;
Expand Down Expand Up @@ -372,7 +375,8 @@ private void toggleFollowButton(
return;
}

AsyncTaskHandler.load(
AsyncTaskExecutor.executeIo(
mScope,
() -> !ReaderTagTable.isFollowedTagName(currentTag.getTagSlug()),
isAskingToFollow -> {
final String slugForTracking = currentTag.getTagSlug();
Expand Down Expand Up @@ -688,7 +692,8 @@ public ReaderPostAdapter(
ImageManager imageManager,
UiHelpers uiHelpers,
@NonNull final NetworkUtilsWrapper networkUtilsWrapper,
boolean isMainReader
boolean isMainReader,
LifecycleCoroutineScope scope
) {
super();
((WordPress) context.getApplicationContext()).component().inject(this);
Expand All @@ -699,6 +704,7 @@ public ReaderPostAdapter(
mNetworkUtilsWrapper = networkUtilsWrapper;
mAvatarSzSmall = context.getResources().getDimensionPixelSize(R.dimen.avatar_sz_small);
mIsMainReader = isMainReader;
mScope = scope;

int displayWidth = DisplayUtils.getWindowPixelWidth(context);
int cardMargin = context.getResources().getDimensionPixelSize(R.dimen.reader_card_margin);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package org.wordpress.android.ui.voicetocontent

import android.content.res.Configuration
import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.with
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.ContentAlpha
import androidx.compose.material.MaterialTheme
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 androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Devices
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import org.wordpress.android.R
import org.wordpress.android.ui.compose.theme.AppTheme

@OptIn(ExperimentalAnimationApi::class)
@Suppress("DEPRECATION")
@Composable
fun MicToStopIcon(model: RecordingPanelUIModel) {
val isEnabled = model.isEnabled
var isMic by remember { mutableStateOf(true) }
val isLight = !isSystemInDarkTheme()

val circleColor by animateColorAsState(
targetValue = if (!isEnabled) MaterialTheme.colors.onSurface.copy(alpha = 0.3f)
else if (isMic) MaterialTheme.colors.primary
else if (isLight) Color.Black
else Color.White, label = ""
)

val iconColor by animateColorAsState(
targetValue = if (!isEnabled) MaterialTheme.colors.onSurface.copy(alpha = ContentAlpha.disabled)
else if (isMic) Color.White
else if (isLight) Color.White
else Color.Black, label = ""
)

val micIcon: Painter = painterResource(id = R.drawable.ic_mic_none_24)
val stopIcon: Painter = painterResource(id = R.drawable.v2c_stop)

Box(
contentAlignment = Alignment.Center,
modifier = Modifier
.size(100.dp)
.background(Color.Transparent) // Ensure transparent background
.clickable(
enabled = isEnabled,
onClick = {
if (model.hasPermission) {
if (isMic) {
model.onMicTap?.invoke()
} else {
model.onStopTap?.invoke()
}
// isMic = !isMic
} else {
model.onRequestPermission?.invoke()
}
isMic = !isMic
}
)
) {
Box(
modifier = Modifier
.size(100.dp)
.background(circleColor, shape = CircleShape)
)
if (model.hasPermission) {
AnimatedContent(
targetState = isMic,
transitionSpec = {
fadeIn(animationSpec = tween(300)) with fadeOut(animationSpec = tween(300))
}, label = ""
) { targetState ->
val icon: Painter = if (targetState) micIcon else stopIcon
val iconSize = if (targetState) 50.dp else 35.dp
Image(
painter = icon,
contentDescription = null,
modifier = Modifier.size(iconSize),
colorFilter = ColorFilter.tint(iconColor)
)
}
} else {
// Display mic icon statically if permission is not granted
Image(
painter = micIcon,
contentDescription = null,
modifier = Modifier.size(50.dp),
colorFilter = ColorFilter.tint(iconColor)
)
}
}
}

@Preview(showBackground = true)
@Preview(showBackground = true, device = Devices.PIXEL_4_XL)
@Preview(showBackground = true, device = Devices.PIXEL_4_XL, uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable
fun ExistingLayoutPreview() {
AppTheme {
MicToStopIcon(
RecordingPanelUIModel(
isEligibleForFeature = true,
onMicTap = {},
onStopTap = {},
hasPermission = true,
onRequestPermission = {},
actionLabel = R.string.voice_to_content_base_header_label, isEnabled = false
)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.wordpress.android.ui.voicetocontent

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.wordpress.android.fluxc.model.SiteModel
import org.wordpress.android.fluxc.model.jetpackai.JetpackAIAssistantFeature
import org.wordpress.android.fluxc.network.rest.wpcom.jetpackai.JetpackAIAssistantFeatureResponse
import org.wordpress.android.fluxc.store.jetpackai.JetpackAIStore
import javax.inject.Inject

class PrepareVoiceToContentUseCase @Inject constructor(
private val jetpackAIStore: JetpackAIStore
) {
suspend fun execute(site: SiteModel): PrepareVoiceToContentResult =
withContext(Dispatchers.IO) {
when (val response = jetpackAIStore.fetchJetpackAIAssistantFeature(site)) {
is JetpackAIAssistantFeatureResponse.Success -> {
PrepareVoiceToContentResult.Success(model = response.model)
}
is JetpackAIAssistantFeatureResponse.Error -> {
PrepareVoiceToContentResult.Error
}
}
}
}

sealed class PrepareVoiceToContentResult {
data class Success(val model: JetpackAIAssistantFeature) : PrepareVoiceToContentResult()
data object Error : PrepareVoiceToContentResult()
}
Loading

0 comments on commit 69efddc

Please sign in to comment.