forked from organicmaps/organicmaps
[android] Added progress and retry views for catalog screen
This commit is contained in:
parent
2e88c4f490
commit
92041902b6
4 changed files with 132 additions and 19 deletions
|
@ -1,7 +1,37 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<WebView
|
||||
<android.support.constraint.ConstraintLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/webview"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
</WebView>
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
<WebView
|
||||
android:id="@+id/webview"
|
||||
android:visibility="gone"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
</WebView>
|
||||
|
||||
<ProgressBar
|
||||
android:id="@+id/progress"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintLeft_toLeftOf="parent"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintRight_toRightOf="parent"
|
||||
android:minWidth="@dimen/search_progress_size"
|
||||
android:minHeight="@dimen/search_progress_size"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/retry_btn"
|
||||
android:layout_marginLeft="@dimen/margin_double"
|
||||
android:layout_marginRight="@dimen/margin_double"
|
||||
android:visibility="gone"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintLeft_toLeftOf="parent"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintRight_toRightOf="parent"
|
||||
android:text="@string/downloader_retry"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"/>
|
||||
</android.support.constraint.ConstraintLayout>
|
||||
|
|
|
@ -83,6 +83,7 @@
|
|||
<item name="android:paddingTop">@dimen/margin_half_plus</item>
|
||||
<item name="android:paddingBottom">@dimen/margin_half_plus</item>
|
||||
<item name="textAllCaps">true</item>
|
||||
<item name="android:gravity">center</item>
|
||||
</style>
|
||||
|
||||
<style name="MwmWidget.Button.Ghost">
|
||||
|
|
|
@ -21,7 +21,7 @@
|
|||
<item name="cardFrame">@drawable/card_frame</item>
|
||||
<item name="cardBackground">@color/bg_cards</item>
|
||||
<item name="fullscreenDialogTheme">@style/MwmTheme.DialogFragment.Fullscreen</item>
|
||||
|
||||
<item name="android:buttonStyle">@style/MwmWidget.Button.Primary</item>
|
||||
<item name="colorPrimary">@color/bg_primary</item>
|
||||
<item name="colorControlNormal">?secondary</item>
|
||||
<item name="colorAccent">@color/base_accent</item>
|
||||
|
@ -142,6 +142,7 @@
|
|||
<item name="android:fontFamily" tools:targetApi="jelly_bean">@string/robotoRegular</item>
|
||||
<item name="android:colorBackgroundCacheHint">@null</item>
|
||||
<item name="android:windowActionBarOverlay">true</item>
|
||||
<item name="android:buttonStyle">@style/MwmWidget.Button.Primary</item>
|
||||
<item name="clickableBackground">?selectableItemBackground</item>
|
||||
<item name="android:statusBarColor" tools:targetApi="lollipop">@android:color/transparent</item>
|
||||
|
||||
|
|
|
@ -7,6 +7,8 @@ import android.support.annotation.Nullable;
|
|||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.webkit.WebResourceError;
|
||||
import android.webkit.WebResourceRequest;
|
||||
import android.webkit.WebSettings;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
|
@ -14,7 +16,7 @@ import android.webkit.WebViewClient;
|
|||
import com.mapswithme.maps.R;
|
||||
import com.mapswithme.maps.auth.BaseWebViewMwmFragment;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
public class BookmarksCatalogFragment extends BaseWebViewMwmFragment
|
||||
{
|
||||
|
@ -24,6 +26,22 @@ public class BookmarksCatalogFragment extends BaseWebViewMwmFragment
|
|||
@NonNull
|
||||
private String mCatalogUrl;
|
||||
|
||||
@SuppressWarnings("NullableProblems")
|
||||
@NonNull
|
||||
private WebViewBookmarksCatalogClient mWebViewClient;
|
||||
|
||||
@SuppressWarnings("NullableProblems")
|
||||
@NonNull
|
||||
private WebView mWebView;
|
||||
|
||||
@SuppressWarnings("NullableProblems")
|
||||
@NonNull
|
||||
private View mRetryBtn;
|
||||
|
||||
@SuppressWarnings("NullableProblems")
|
||||
@NonNull
|
||||
private View mProgressView;
|
||||
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState)
|
||||
{
|
||||
|
@ -31,23 +49,43 @@ public class BookmarksCatalogFragment extends BaseWebViewMwmFragment
|
|||
mCatalogUrl = getCatalogUrlOrThrow();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyView()
|
||||
{
|
||||
super.onDestroyView();
|
||||
mWebViewClient.clear();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
|
||||
@Nullable Bundle savedInstanceState)
|
||||
{
|
||||
View root = inflater.inflate(R.layout.fragment_bookmarks_catalog, container, false);
|
||||
WebView webView = root.findViewById(getWebViewResId());
|
||||
initWebView(webView);
|
||||
webView.loadUrl(mCatalogUrl);
|
||||
mWebView = root.findViewById(getWebViewResId());
|
||||
mRetryBtn = root.findViewById(R.id.retry_btn);
|
||||
mProgressView = root.findViewById(R.id.progress);
|
||||
initWebView(mWebView);
|
||||
mRetryBtn.setOnClickListener(v -> onRetryClick());
|
||||
mWebView.loadUrl(mCatalogUrl);
|
||||
return root;
|
||||
}
|
||||
|
||||
private void onRetryClick()
|
||||
{
|
||||
mWebViewClient.retry();
|
||||
mRetryBtn.setVisibility(View.GONE);
|
||||
mProgressView.setVisibility(View.VISIBLE);
|
||||
mWebView.loadUrl(mCatalogUrl);
|
||||
}
|
||||
|
||||
@SuppressLint("SetJavaScriptEnabled")
|
||||
private void initWebView(@NonNull WebView webView)
|
||||
{
|
||||
webView.setWebViewClient(new WebViewBookmarksCatalogClient());
|
||||
mWebViewClient = new WebViewBookmarksCatalogClient(this);
|
||||
webView.setWebViewClient(mWebViewClient);
|
||||
final WebSettings webSettings = webView.getSettings();
|
||||
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
|
||||
webSettings.setJavaScriptEnabled(true);
|
||||
}
|
||||
|
||||
|
@ -69,24 +107,67 @@ public class BookmarksCatalogFragment extends BaseWebViewMwmFragment
|
|||
|
||||
private static class WebViewBookmarksCatalogClient extends WebViewClient
|
||||
{
|
||||
@NonNull
|
||||
private final WeakReference<BookmarksCatalogFragment> mReference;
|
||||
|
||||
@Nullable
|
||||
private WebResourceError mError;
|
||||
|
||||
public WebViewBookmarksCatalogClient(@NonNull BookmarksCatalogFragment frag)
|
||||
{
|
||||
mReference = new WeakReference<>(frag);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldOverrideUrlLoading(WebView view, String url)
|
||||
{
|
||||
try
|
||||
{
|
||||
return requestArchive(view, url);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
return super.shouldOverrideUrlLoading(view, url);
|
||||
}
|
||||
return requestArchive(view, url);
|
||||
}
|
||||
|
||||
private boolean requestArchive(@NonNull WebView view, @NonNull String url) throws IOException
|
||||
@Override
|
||||
public void onPageFinished(WebView view, String url)
|
||||
{
|
||||
super.onPageFinished(view, url);
|
||||
BookmarksCatalogFragment frag;
|
||||
if ((frag = mReference.get()) == null || mError != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
frag.mWebView.setVisibility(View.VISIBLE);
|
||||
frag.mProgressView.setVisibility(View.GONE);
|
||||
frag.mRetryBtn.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error)
|
||||
{
|
||||
super.onReceivedError(view, request, error);
|
||||
mError = error;
|
||||
BookmarksCatalogFragment frag;
|
||||
if ((frag = mReference.get()) == null)
|
||||
return;
|
||||
|
||||
frag.mWebView.setVisibility(View.GONE);
|
||||
frag.mProgressView.setVisibility(View.GONE);
|
||||
frag.mRetryBtn.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
private void retry()
|
||||
{
|
||||
mError = null;
|
||||
}
|
||||
|
||||
private boolean requestArchive(@NonNull WebView view, @NonNull String url)
|
||||
{
|
||||
BookmarksDownloadManager dm = BookmarksDownloadManager.from(view.getContext());
|
||||
dm.enqueueRequest(url);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void clear()
|
||||
{
|
||||
mReference.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue