Skip to content

Ba/issue 3 #21849

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

Open
wants to merge 7 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package org.wordpress.android.ui;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
Expand All @@ -8,6 +13,7 @@
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
Expand Down Expand Up @@ -66,6 +72,7 @@
import org.wordpress.android.viewmodel.wpwebview.WPWebViewViewModel.WebPreviewUiState.WebPreviewFullscreenUiState;
import org.wordpress.android.widgets.WPSnackbar;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
Expand Down Expand Up @@ -227,6 +234,8 @@ public final void configureView() {

final Bundle extras = getIntent().getExtras();

fetchHtmlAndLoad(extras.getString(URL_TO_LOAD));

if (extras != null) {
mPreviewModeChangeAllowed = extras.getBoolean(SHOW_PREVIEW_MODE_TOGGLE, false);
if (!mPreviewModeChangeAllowed) {
Expand All @@ -242,6 +251,42 @@ public final void configureView() {
mViewModel.track(Stat.WEBVIEW_DISPLAYED, getSource());
}

private void fetchHtmlAndLoad(final String url) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();

client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("WPWebViewActivity", "Failed to fetch HTML", e);
}

@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
String html = response.body() != null ? response.body().string() : "";

final String str = html.replaceAll(
"(https://youtu\\.be/([\\w\\-]+))",
"<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/$2\" frameborder=\"0\" allowfullscreen></iframe>"
);


runOnUiThread(new Runnable() {
@Override
public void run() {
mWebView.loadDataWithBaseURL(url, str, "text/html", "utf-8", null);
}
});
} else {
Log.e("WPWebViewActivity", "Response was not successful: " + response.code());
}
}
});
}

private void setupToolbar() {
Toolbar toolbar = findViewById(R.id.toolbar);
if (toolbar != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@ public static String removeWPVideoPress(String str) {
public static String getFormattedDate(PostModel post) {
if (PostStatus.fromPost(post) == PostStatus.SCHEDULED) {
return DateUtils.formatDateTime(WordPress.getContext(),
DateTimeUtils.timestampFromIso8601Millis(post.getDateCreated()),
DateUtils.FORMAT_ABBREV_ALL);
DateTimeUtils.timestampFromIso8601Millis(post.getDateCreated()),
DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL | DateUtils.FORMAT_SHOW_TIME);
} else {
return DateTimeUtils.javaDateToTimeSpan(DateTimeUtils.dateUTCFromIso8601(post.getDateCreated()),
WordPress.getContext());
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

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