Skip to content

Commit

Permalink
courses: add webview for links (fixes #2708) (#2709)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
Okuro3499 and dogi authored Nov 16, 2023
1 parent d3178d4 commit d559a99
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
applicationId "org.ole.planet.myplanet"
minSdkVersion 21
targetSdkVersion 34
versionCode 1148
versionName "0.11.48"
versionCode 1149
versionName "0.11.49"
ndkVersion '21.3.6528147'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@


import android.os.Bundle;
import android.text.Spannable;
import android.text.method.LinkMovementMethod;
import android.text.style.URLSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -26,6 +29,7 @@
import org.ole.planet.myplanet.ui.exam.TakeExamFragment;
import org.ole.planet.myplanet.utilities.CameraUtils;
import org.ole.planet.myplanet.utilities.Constants;
import org.ole.planet.myplanet.utilities.CustomClickableSpan;

import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -117,10 +121,28 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
hideTestIfNoQuestion();
fragmentCourseStepBinding.tvTitle.setText(step.getStepTitle());
markwon.setMarkdown(fragmentCourseStepBinding.description, step.getDescription());
fragmentCourseStepBinding.description.setMovementMethod(LinkMovementMethod.getInstance());

if (!RealmMyCourse.isMyCourse(user.getId(), step.getCourseId(), mRealm)) {
fragmentCourseStepBinding.btnTakeTest.setVisibility(View.INVISIBLE);
}
setListeners();

CharSequence textWithSpans = fragmentCourseStepBinding.description.getText();
if (textWithSpans instanceof Spannable) {
Spannable spannable = (Spannable) textWithSpans;
URLSpan[] urlSpans = spannable.getSpans(0, spannable.length(), URLSpan.class);

for (URLSpan urlSpan : urlSpans) {
int start = spannable.getSpanStart(urlSpan);
int end = spannable.getSpanEnd(urlSpan);

String dynamicTitle = spannable.subSequence(start, end).toString();

spannable.setSpan(new CustomClickableSpan(urlSpan.getURL(), dynamicTitle, getActivity()), start, end, spannable.getSpanFlags(urlSpan));
spannable.removeSpan(urlSpan);
}
}
}

private void hideTestIfNoQuestion() {
Expand Down Expand Up @@ -179,4 +201,5 @@ public void onDownloadComplete() {
@Override
public void onImageCapture(String fileUri) {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.ole.planet.myplanet.utilities;

import android.content.Context;
import android.content.Intent;
import android.text.style.ClickableSpan;
import android.view.View;

import androidx.annotation.NonNull;

import org.ole.planet.myplanet.ui.viewer.WebViewActivity;

public class CustomClickableSpan extends ClickableSpan {
private final String url;
private final String title;
private final Context context;

public CustomClickableSpan(String url, String title, Context context) {
this.url = url;
this.title = title;
this.context = context;
}
@Override
public void onClick(@NonNull View widget) {
openLinkInWebView(url, title);
}

private void openLinkInWebView(String link, String title) {
if (link != null && !link.isEmpty()) {
Intent webViewIntent = new Intent(context, WebViewActivity.class);
webViewIntent.putExtra("title", title);
webViewIntent.putExtra("link", link);
context.startActivity(webViewIntent);
} else {
Utilities.toast(context, "Invalid link");
}
}
}

0 comments on commit d559a99

Please sign in to comment.