From 8b8130c1e4f2fb5555df9da78cb92bcc316baa87 Mon Sep 17 00:00:00 2001 From: Apostolof Date: Sat, 3 Dec 2016 18:49:49 +0200 Subject: [PATCH] Shared elements activity transitions. WebView's link handling fixes. --- .../activities/topic/TopicActivity.java | 67 ++++++++++++------- app/src/main/res/layout/activity_topic.xml | 5 +- app/src/main/res/values-v21/styles.xml | 3 + 3 files changed, 51 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java index ff183e06..23a2158f 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java @@ -11,7 +11,9 @@ import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.support.design.widget.FloatingActionButton; +import android.support.v4.app.ActivityOptionsCompat; import android.support.v4.content.ContextCompat; +import android.support.v4.util.Pair; import android.support.v7.app.AlertDialog; import android.support.v7.widget.CardView; import android.support.v7.widget.Toolbar; @@ -55,6 +57,7 @@ import okhttp3.Response; import static gr.thmmy.mthmmy.session.SessionManager.LOGGED_IN; import static gr.thmmy.mthmmy.session.SessionManager.LOGIN_STATUS; +@SuppressWarnings("unchecked") public class TopicActivity extends BaseActivity { //-----------------------------------------CLASS VARIABLES------------------------------------------ @@ -315,11 +318,16 @@ public class TopicActivity extends BaseActivity { private void changePage(int pageRequested) { if (pageRequested != thisPage - 1) { //Restart activity with new page + Pair p1 = Pair.create((View)replyFAB, "fab"); + Pair p2 = Pair.create((View)toolbar, "toolbar"); + ActivityOptionsCompat options = ActivityOptionsCompat. + makeSceneTransitionAnimation(this, p1, p2); + Intent intent = getIntent(); intent.putExtra("TOPIC_URL", pagesUrls.get(pageRequested)); intent.putExtra("TOPIC_TITLE", topicTitle); + startActivity(intent, options.toBundle()); finish(); - startActivity(intent); } } //------------------------------------BOTTOM NAV BAR METHODS END------------------------------------ @@ -466,18 +474,10 @@ public class TopicActivity extends BaseActivity { //Post's WebView parameters set post.setClickable(true); - //post.setWebViewClient(new LinkLauncher()); - - + post.setWebViewClient(new LinkLauncher()); post.getSettings().setJavaScriptEnabled(true); //TODO post.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND); - post.setWebViewClient(new WebViewClient() { - @Override - public boolean shouldOverrideUrlLoading(WebView view, String url) { - return false; - } - }); @@ -642,6 +642,7 @@ public class TopicActivity extends BaseActivity { * When link url is one that the app can handle internally, it does. * Otherwise user is prompt to open the link in a browser. */ + @SuppressWarnings("unchecked") private class LinkLauncher extends WebViewClient { //Used to handle link clicks //Older versions @SuppressWarnings("deprecation") @@ -663,28 +664,48 @@ public class TopicActivity extends BaseActivity { private boolean handleUri(final Uri uri) { //Method always returns true as we don't want any url to be loaded in WebViews - Log.i(TAG, "Uri =" + uri); + Log.i(TAG, "Uri = " + uri); final String host = uri.getHost(); //Get requested url's host + final String uriString = uri.toString(); //Determine if you are going to pass the url to a //host's application activity or load it in a browser. if (Objects.equals(host, "www.thmmy.gr")) { //This is my web site, so figure out what Activity should launch - if (uri.toString().contains("topic=")) { //This url points to a topic + if (uriString.contains("topic=")) { //This url points to a topic //Is the link pointing to current topic? if (Objects.equals( - uri.toString().substring(0, uri.toString().lastIndexOf(".")), base_url)) { - //Don't restart Activity - //Just change post focus - //TODO - } else { - //Restart activity with new data - Intent intent = getIntent(); - intent.putExtra("TOPIC_URL", uri.toString()); - intent.putExtra("TOPIC_TITLE", ""); - finish(); - startActivity(intent); + uriString.substring(0, uriString.lastIndexOf(".")), base_url)) { + + //Get uri's targeted message's index number + String msgIndexReq = uriString.substring(uriString.indexOf("msg") + 3); + if (msgIndexReq.contains("#")) + msgIndexReq = msgIndexReq.substring(0, msgIndexReq.indexOf("#")); + else + msgIndexReq = msgIndexReq.substring(0, msgIndexReq.indexOf(";")); + + //Is this post already shown now? (is it in current page?) + for (Post post : postsList) { + if (post.getPostIndex() == Integer.parseInt(msgIndexReq)) { + //Don't restart Activity + //Just change post focus + //TODO + return true; + } + } } + //Restart activity with new data + Pair p1 = Pair.create((View) replyFAB, "fab"); + Pair p2 = Pair.create((View) toolbar, "toolbar"); + ActivityOptionsCompat options = ActivityOptionsCompat. + makeSceneTransitionAnimation(TopicActivity.this, p1, p2); + + Intent intent = getIntent(); + intent.putExtra("TOPIC_URL", uri.toString()); + intent.putExtra("TOPIC_TITLE", ""); + startActivity(intent, options.toBundle()); + finish(); + } return true; } diff --git a/app/src/main/res/layout/activity_topic.xml b/app/src/main/res/layout/activity_topic.xml index f0577c61..4c619b5e 100644 --- a/app/src/main/res/layout/activity_topic.xml +++ b/app/src/main/res/layout/activity_topic.xml @@ -21,6 +21,7 @@ android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" + android:transitionName="toolbar" app:popupTheme="@style/ToolbarTheme"> @@ -30,8 +31,8 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="top|start" - android:layout_marginTop="64dp" android:layout_marginBottom="50dp" + android:layout_marginTop="64dp" android:background="@color/background" android:scrollbars="none" tools:context="gr.thmmy.mthmmy.activities.topic.TopicActivity"> @@ -54,6 +55,7 @@ android:layout_height="50dp" android:layout_gravity="bottom|end" android:layout_marginTop="-2dp" + android:transitionName="bottom_navigation_bar" app:elevation="8dp"> diff --git a/app/src/main/res/values-v21/styles.xml b/app/src/main/res/values-v21/styles.xml index dbbdd40f..04e77b64 100644 --- a/app/src/main/res/values-v21/styles.xml +++ b/app/src/main/res/values-v21/styles.xml @@ -1,4 +1,7 @@ +