Skip to content

Commit

Permalink
Support sharing ayahs with translation
Browse files Browse the repository at this point in the history
In translation mode, long pressing an ayah now shows a toolbar (similar
to the one in the images screen), but with the share menu. Choosing to
copy or share will copy the verse in Arabic (if it is not disabled in
the settings) followed by all currently enabled translations as text,
and then copy or share.
  • Loading branch information
ahmedre committed May 22, 2017
1 parent 7873500 commit 2bbfa7c
Show file tree
Hide file tree
Showing 14 changed files with 288 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

import android.support.annotation.NonNull;

import com.quran.labs.androidquran.R;
import com.quran.labs.androidquran.common.QuranAyahInfo;
import com.quran.labs.androidquran.data.QuranInfo;
import com.quran.labs.androidquran.data.SuraAyah;
import com.quran.labs.androidquran.database.TranslationsDBAdapter;
import com.quran.labs.androidquran.di.QuranPageScope;
import com.quran.labs.androidquran.model.translation.TranslationModel;
import com.quran.labs.androidquran.ui.PagerActivity;
import com.quran.labs.androidquran.util.QuranSettings;
import com.quran.labs.androidquran.util.ShareUtil;

import java.util.List;

Expand Down Expand Up @@ -62,6 +66,29 @@ public void onComplete() {
});
}

public void onTranslationAction(PagerActivity activity,
QuranAyahInfo ayah,
String[] translationNames,
int actionId) {
switch (actionId) {
case R.id.cab_share_ayah_link: {
SuraAyah bounds = new SuraAyah(ayah.sura, ayah.ayah);
activity.shareAyahLink(bounds, bounds);
break;
}
case R.id.cab_share_ayah_text:
case R.id.cab_copy_ayah: {
String shareText = ShareUtil.getShareText(activity, ayah, translationNames);
if (actionId == R.id.cab_share_ayah_text) {
ShareUtil.shareViaIntent(activity, shareText, R.string.share_ayah_text);
} else {
ShareUtil.copyToClipboard(activity, shareText);
}
break;
}
}
}

private int getPage(List<QuranAyahInfo> result) {
final int page;
if (pages.length == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2039,7 +2039,7 @@ private void shareAyah(SuraAyah start, SuraAyah end, final boolean isCopy) {
}));
}

private void shareAyahLink(SuraAyah start, SuraAyah end) {
public void shareAyahLink(SuraAyah start, SuraAyah end) {
showProgressDialog();
compositeDisposable.add(
QuranAppUtils.getQuranAppUrlObservable(getString(R.string.quranapp_key), start, end)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.quran.labs.androidquran.ui.fragment;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.RectF;
Expand All @@ -17,6 +18,7 @@
import com.quran.labs.androidquran.common.AyahBounds;
import com.quran.labs.androidquran.common.QuranAyahInfo;
import com.quran.labs.androidquran.dao.Bookmark;
import com.quran.labs.androidquran.data.QuranInfo;
import com.quran.labs.androidquran.module.fragment.QuranPageModule;
import com.quran.labs.androidquran.presenter.quran.QuranPagePresenter;
import com.quran.labs.androidquran.presenter.quran.QuranPageScreen;
Expand All @@ -29,6 +31,7 @@
import com.quran.labs.androidquran.ui.helpers.AyahSelectedListener;
import com.quran.labs.androidquran.ui.helpers.AyahTracker;
import com.quran.labs.androidquran.ui.helpers.QuranPage;
import com.quran.labs.androidquran.ui.translation.OnTranslationActionListener;
import com.quran.labs.androidquran.ui.translation.TranslationView;
import com.quran.labs.androidquran.ui.util.PageController;
import com.quran.labs.androidquran.util.QuranSettings;
Expand All @@ -50,7 +53,8 @@

public class TabletFragment extends Fragment
implements PageController, TranslationPresenter.TranslationScreen,
QuranPage, QuranPageScreen, AyahTrackerPresenter.AyahInteractionHandler {
QuranPage, QuranPageScreen, AyahTrackerPresenter.AyahInteractionHandler,
OnTranslationActionListener {
private static final String FIRST_PAGE_EXTRA = "pageNumber";
private static final String MODE_EXTRA = "mode";

Expand Down Expand Up @@ -106,6 +110,8 @@ public View onCreateView(LayoutInflater inflater,
PagerActivity pagerActivity = (PagerActivity) context;
leftTranslation.setTranslationClickedListener(v -> pagerActivity.toggleActionBar());
rightTranslation.setTranslationClickedListener(v -> pagerActivity.toggleActionBar());
leftTranslation.setOnTranslationActionListener(this);
rightTranslation.setOnTranslationActionListener(this);
mainView.setPageController(null, pageNumber, pageNumber - 1);
}
return mainView;
Expand Down Expand Up @@ -195,6 +201,19 @@ public void onDetach() {
compositeDisposable.clear();
}

@Override
public void onTranslationAction(QuranAyahInfo ayah, String[] translationNames, int actionId) {
Activity activity = getActivity();
if (activity instanceof PagerActivity) {
translationPresenter.get()
.onTranslationAction((PagerActivity) activity, ayah, translationNames, actionId);
}

int page = QuranInfo.getPageFromSuraAyah(ayah.sura, ayah.ayah);
TranslationView translationView = page == pageNumber ? leftTranslation : rightTranslation;
translationView.unhighlightAyat();
}

@Override
public void setPageDownloadError(@StringRes int errorMessage) {
mainView.showError(errorMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.quran.labs.androidquran.ui.PagerActivity;
import com.quran.labs.androidquran.ui.helpers.AyahTracker;
import com.quran.labs.androidquran.ui.helpers.QuranPage;
import com.quran.labs.androidquran.ui.translation.OnTranslationActionListener;
import com.quran.labs.androidquran.ui.translation.TranslationView;
import com.quran.labs.androidquran.util.QuranSettings;
import com.quran.labs.androidquran.widgets.QuranTranslationPageLayout;
Expand All @@ -27,7 +28,9 @@
import javax.inject.Inject;

public class TranslationFragment extends Fragment implements
AyahTrackerPresenter.AyahInteractionHandler, QuranPage, TranslationPresenter.TranslationScreen {
AyahTrackerPresenter.AyahInteractionHandler, QuranPage,
TranslationPresenter.TranslationScreen,
OnTranslationActionListener {
private static final String PAGE_NUMBER_EXTRA = "pageNumber";

private static final String SI_PAGE_NUMBER = "SI_PAGE_NUMBER";
Expand Down Expand Up @@ -78,11 +81,12 @@ public View onCreateView(LayoutInflater inflater,
translationView = mainView.getTranslationView();
translationView.setTranslationClickedListener(v -> {
final Activity activity = getActivity();
if (activity != null && activity instanceof PagerActivity) {
if (activity instanceof PagerActivity) {
((PagerActivity) getActivity()).toggleActionBar();
}
});

translationView.setOnTranslationActionListener(this);
return mainView;
}

Expand All @@ -98,6 +102,15 @@ public void onAttach(Context context) {
.inject(this);
}

@Override
public void onTranslationAction(QuranAyahInfo ayah, String[] translationNames, int actionId) {
Activity activity = getActivity();
if (activity instanceof PagerActivity) {
presenter.onTranslationAction((PagerActivity) activity, ayah, translationNames, actionId);
}
translationView.unhighlightAyat();
}

@Override
public void updateView() {
if (isAdded()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.quran.labs.androidquran.ui.translation;

import com.quran.labs.androidquran.common.QuranAyahInfo;

public interface OnTranslationActionListener {
void onTranslationAction(QuranAyahInfo ayah, String[] translationNames, int actionId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class TranslationAdapter extends RecyclerView.Adapter<TranslationAdapter.RowView
private final RecyclerView recyclerView;
private final List<TranslationViewRow> data;
private View.OnClickListener onClickListener;
private OnVerseSelectedListener onVerseSelectedListener;

private int fontSize;
private int textColor;
Expand All @@ -55,20 +56,24 @@ class TranslationAdapter extends RecyclerView.Adapter<TranslationAdapter.RowView
private int highlightedRowCount;
private int highlightedStartPosition;

private View.OnClickListener defaultClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onClickListener != null) {
onClickListener.onClick(v);
}
private View.OnClickListener defaultClickListener = v -> {
if (onClickListener != null) {
onClickListener.onClick(v);
}
};

TranslationAdapter(Context context, RecyclerView recyclerView) {
private View.OnLongClickListener defaultLongClickListener = this::selectVerseRows;

TranslationAdapter(Context context,
RecyclerView recyclerView,
View.OnClickListener onClickListener,
OnVerseSelectedListener verseSelectedListener) {
this.context = context;
this.data = new ArrayList<>();
this.recyclerView = recyclerView;
this.inflater = LayoutInflater.from(context);
this.onClickListener = onClickListener;
this.onVerseSelectedListener = verseSelectedListener;
}

void setData(List<TranslationViewRow> data) {
Expand Down Expand Up @@ -138,10 +143,6 @@ void unhighlight() {
highlightedStartPosition = -1;
}

void setOnTranslationClickedListener(View.OnClickListener listener) {
this.onClickListener = listener;
}

void refresh(QuranSettings quranSettings) {
this.fontSize = quranSettings.getTranslationTextSize();
isNightMode = quranSettings.isNightMode();
Expand All @@ -167,6 +168,42 @@ void refresh(QuranSettings quranSettings) {
}
}

private boolean selectVerseRows(View view) {
int position = recyclerView.getChildAdapterPosition(view);
if (position != RecyclerView.NO_POSITION && onVerseSelectedListener != null) {
QuranAyahInfo ayahInfo = data.get(position).ayahInfo;
highlightAyah(ayahInfo.ayahId, true);
onVerseSelectedListener.onVerseSelected(ayahInfo);
return true;
}
return false;
}

int[] getSelectedVersePopupPosition() {
int[] result = null;
if (highlightedStartPosition > -1) {
int versePosition = -1;
int highlightedEndPosition = highlightedStartPosition + highlightedRowCount;
for (int i = highlightedStartPosition; i < highlightedEndPosition; i++) {
if (data.get(i).type == TranslationViewRow.Type.VERSE_NUMBER) {
versePosition = i;
break;
}
}

if (versePosition > -1) {
RowViewHolder viewHolder =
(RowViewHolder) recyclerView.findViewHolderForAdapterPosition(versePosition);
if (viewHolder != null && viewHolder.ayahNumber != null) {
result = new int[2];
result[0] += viewHolder.ayahNumber.getLeft() + viewHolder.ayahNumber.getBoxCenterX();
result[1] += viewHolder.ayahNumber.getTop() + viewHolder.ayahNumber.getBoxBottomY();
}
}
}
return result;
}

@Override
public int getItemViewType(int position) {
return data.get(position).type;
Expand Down Expand Up @@ -290,6 +327,11 @@ class RowViewHolder extends RecyclerView.ViewHolder {
this.wrapperView = itemView;
ButterKnife.bind(this, itemView);
itemView.setOnClickListener(defaultClickListener);
itemView.setOnLongClickListener(defaultLongClickListener);
}
}

interface OnVerseSelectedListener {
void onVerseSelected(QuranAyahInfo ayahInfo);
}
}
Loading

0 comments on commit 2bbfa7c

Please sign in to comment.