Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

translations via mastodon api #3228

Closed
wants to merge 11 commits into from
Prev Previous commit
Next Next commit
try to make stuff work
  • Loading branch information
Joshix-1 committed Jan 27, 2023
commit 551efba5192ea2e3ff47f9c9e9e407183534f05e
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ class ConversationsFragment : SFragment(), StatusActionListener, Injectable, Res
}
}

override fun onTranslate(alreadyTranslated: Boolean, position: Int) {
TODO("Not yet implemented")
}

override fun onMore(view: View, position: Int) {
adapter.peek(position)?.let { conversation ->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ class SearchStatusesFragment : SearchFragment<StatusViewData.Concrete>(), Status
}
}

override fun onTranslate(alreadyTranslated: Boolean, position: Int) {
TODO("Not yet implemented")
}

override fun onMore(view: View, position: Int) {
searchAdapter.peek(position)?.status?.let {
more(it, view, position)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,11 @@ class TimelineFragment :
viewModel.bookmark(bookmark, status)
}

override fun onTranslate(alreadyTranslated: Boolean, position: Int) {
val status = adapter.peek(position)?.asStatusOrNull() ?: return
viewModel.translate(alreadyTranslated, status)
}

override fun onVoteInPoll(position: Int, choices: List<Int>) {
val status = adapter.peek(position)?.asStatusOrNull() ?: return
viewModel.voteInPoll(choices, status)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,20 @@ abstract class TimelineViewModel(
}
}

fun translate(alreadyTranslated: Boolean, status: StatusViewData.Concrete): Job = viewModelScope.launch {
if (alreadyTranslated) {
timelineCases.dispatchNullTranslation(status.actionableId)
} else {
try {
timelineCases.translate(status.actionableId).await()
} catch (t: Exception) {
ifExpected(t) {
Log.d(TAG, "Failed to translate status " + status.actionableId, t)
}
}
}
}

fun voteInPoll(choices: List<Int>, status: StatusViewData.Concrete): Job = viewModelScope.launch {
val poll = status.status.actionableStatus.poll ?: run {
Log.w(TAG, "No poll on status ${status.id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@ class ViewThreadFragment : SFragment(), OnRefreshListener, StatusActionListener,
viewModel.bookmark(bookmark, status)
}

override fun onTranslate(alreadyTranslated: Boolean, position: Int) {
TODO("Not yet implemented")
}

override fun onMore(view: View, position: Int) {
super.more(adapter.currentList[position].status, view, position)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,11 @@ public void onBookmark(final boolean bookmark, final int position) {
);
}

@Override
public void onTranslate(boolean alreadyTranslated, int position) {
// TODO: not yet implemented
}

private void setBookmarkForStatus(String statusId, boolean bookmark) {
updateStatus(statusId, (s) -> s.copyWithBookmarked(bookmark));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@

import android.view.View;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;

public interface StatusActionListener extends LinkListener {
void onReply(int position);
void onReblog(final boolean reblog, final int position);
void onFavourite(final boolean favourite, final int position);
void onBookmark(final boolean bookmark, final int position);
void onTranslate(final boolean alreadyTranslated, final int position);
void onMore(@NonNull View view, final int position);
void onViewMedia(int position, int attachmentIndex, @Nullable View view);
void onViewThread(int position);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,13 @@ class TimelineCases @Inject constructor(

fun translate(statusId: String): Single<TranslationResult> {
return mastodonApi.translateStatus(statusId)
.doAfterSuccess {translation ->
eventHub.dispatch(TranslationEvent(statusId, translation))
}
.doAfterSuccess {translation ->
eventHub.dispatch(TranslationEvent(statusId, translation))
}
}

fun dispatchNullTranslation(statusId: String) {
eventHub.dispatch(TranslationEvent(statusId, null))
}

fun favourite(statusId: String, favourite: Boolean): Single<Status> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class ListStatusAccessibilityDelegate(
}
info.addAction(if (actionable.favourited) unfavouriteAction else favouriteAction)
info.addAction(if (actionable.bookmarked) unbookmarkAction else bookmarkAction)
info.addAction(if (actionable.translationResult == null) translateAction else untranslateAction)

val mediaActions = intArrayOf(
R.id.action_open_media_1,
Expand Down Expand Up @@ -109,6 +110,8 @@ class ListStatusAccessibilityDelegate(
R.id.action_unfavourite -> statusActionListener.onFavourite(false, pos)
R.id.action_bookmark -> statusActionListener.onBookmark(true, pos)
R.id.action_unbookmark -> statusActionListener.onBookmark(false, pos)
R.id.action_translate -> statusActionListener.onTranslate(false, pos)
R.id.action_untranslate -> statusActionListener.onTranslate(true, pos)
R.id.action_reblog -> statusActionListener.onReblog(true, pos)
R.id.action_unreblog -> statusActionListener.onReblog(false, pos)
R.id.action_open_profile -> {
Expand Down Expand Up @@ -313,6 +316,16 @@ class ListStatusAccessibilityDelegate(
context.getString(R.string.action_bookmark)
)

private val translateAction = AccessibilityActionCompat(
R.id.action_translate,
context.getString(R.string.action_translate)
)

private val untranslateAction = AccessibilityActionCompat(
R.id.action_untranslate,
context.getString(R.string.action_untranslate)
)

private val openProfileAction = AccessibilityActionCompat(
R.id.action_open_profile,
context.getString(R.string.action_view_profile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ sealed class StatusViewData {
init {
if (Build.VERSION.SDK_INT == 23) {
// https://github.com/tuskyapp/Tusky/issues/563
this.content = replaceCrashingCharacters(status.actionableStatus.content.parseAsMastodonHtml())
this.content = replaceCrashingCharacters(status.actionableStatus.displayedContent.parseAsMastodonHtml())
this.spoilerText =
replaceCrashingCharacters(status.actionableStatus.spoilerText).toString()
this.username =
replaceCrashingCharacters(status.actionableStatus.account.username).toString()
} else {
this.content = status.actionableStatus.content.parseAsMastodonHtml()
this.content = status.actionableStatus.displayedContent.parseAsMastodonHtml()
this.spoilerText = status.actionableStatus.spoilerText
this.username = status.actionableStatus.account.username
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/actions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<item name="action_unfavourite" type="id" />
<item name="action_bookmark" type="id" />
<item name="action_unbookmark" type="id" />
<item name="action_translate" type="id" />
<item name="action_untranslate" type="id" />
<item name="action_reblog" type="id" />
<item name="action_unreblog" type="id" />
<item name="action_open_profile" type="id" />
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@
<string name="action_unfavourite">Remove favorite</string>
<string name="action_bookmark">Bookmark</string>
<string name="action_unbookmark">Remove bookmark</string>
<string name="action_translate">Translate</string>
<string name="action_untranslate">Show original</string>
<string name="action_more">More</string>
<string name="action_compose">Compose</string>
<string name="action_login">Login with Tusky</string>
Expand Down