Skip to content

Commit

Permalink
Merge pull request #1 from smartnsoft/feature/handle_multiple_error_m…
Browse files Browse the repository at this point in the history
…essages

Feature/handle multiple error messages
  • Loading branch information
ThomasEcalle authored Nov 6, 2018
2 parents 56813ea + 70e7a2f commit 3050679
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android
minSdkVersion 14
targetSdkVersion 28

versionCode 4
versionName "2.1"
versionCode 5
versionName "2.2"
}

buildTypes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;

import com.smartnsoft.droid4me.LifeCycle.BusinessObjectsRetrievalAsynchronousPolicy;
import com.smartnsoft.droid4me.support.v4.app.SmartFragment;
Expand All @@ -57,6 +58,10 @@ public class WebViewFragment<AggregateClass>

public static final String SCREEN_TITLE_EXTRA = "screenTitleExtra";

public static final String DEFAULT_ERROR_MESSAGE_EXTRA = "defaultErrorMessageExtra";

public static final String ERROR_MESSAGES_TABLE_EXTRA = "errorMessagesTableExtra";

//Views
protected View loadingErrorAndRetry;

Expand All @@ -66,6 +71,8 @@ public class WebViewFragment<AggregateClass>

protected Button retry;

protected TextView errorTextView;

//webview state
protected boolean webViewRestored = false;

Expand All @@ -89,6 +96,12 @@ public void onReceive(Context context, Intent intent)

protected Map<String, Boolean> networkStatus = new HashMap<>();

protected Map<Integer, String> errorMessagesTable = null;

protected String defaultErrorMessage = null;

protected int errorWhileLoading = -100;

@Override
public void onCreate(Bundle savedInstanceState)
{
Expand Down Expand Up @@ -121,6 +134,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
loadingErrorAndRetry = rootView.findViewById(R.id.loadingErrorAndRetry);
errorAndRetry = rootView.findViewById(R.id.errorAndRetry);
retry = rootView.findViewById(R.id.retry);
errorTextView = rootView.findViewById(R.id.errorText);

retry.setOnClickListener(this);

Expand Down Expand Up @@ -259,6 +273,12 @@ public void onRetrieveBusinessObjects()
throws BusinessObjectUnavailableException
{
url = getActivity().getIntent().getStringExtra(WebViewFragment.PAGE_URL_EXTRA);
defaultErrorMessage = getActivity().getIntent().getStringExtra(WebViewFragment.DEFAULT_ERROR_MESSAGE_EXTRA);
if (getActivity().getIntent().hasExtra(WebViewFragment.ERROR_MESSAGES_TABLE_EXTRA)
&& getActivity().getIntent().getSerializableExtra(WebViewFragment.ERROR_MESSAGES_TABLE_EXTRA) instanceof Map)
{
errorMessagesTable = (Map<Integer, String>) getActivity().getIntent().getSerializableExtra(WebViewFragment.ERROR_MESSAGES_TABLE_EXTRA);
}
}

@Override
Expand Down Expand Up @@ -365,8 +385,9 @@ public void onAnimationRepeat(Animation animation)
}
}

protected void showErrorScreen()
protected void showErrorScreen(int errorCode)
{
handleErrorCode(errorCode);
errorAndRetry.setVisibility(View.VISIBLE);
loadingErrorAndRetry.setVisibility(View.VISIBLE);
}
Expand All @@ -387,7 +408,7 @@ protected void refresh()
}
else
{
showErrorScreen();
showErrorScreen(WebViewClient.ERROR_CONNECT);
}
}

Expand Down Expand Up @@ -482,7 +503,7 @@ public void onPageFinished(WebView view, String currentURL)
}
else
{
showErrorScreen();
showErrorScreen(errorWhileLoading);
}

refreshMenu();
Expand All @@ -493,16 +514,17 @@ public void onReceivedError(WebView view, int errorCode, String description, Str
{
super.onReceivedError(view, errorCode, description, failingUrl);
errorWhenLoadingPage = true;
showErrorScreen();
errorWhileLoading = errorCode;
refreshMenu();
}

@TargetApi(Build.VERSION_CODES.M)
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error)
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError webSoWebResourceError)
{
super.onReceivedError(view, request, error);
super.onReceivedError(view, request, webSoWebResourceError);
errorWhenLoadingPage = true;
showErrorScreen();
errorWhileLoading = webSoWebResourceError.getErrorCode();
refreshMenu();
}
};
Expand All @@ -518,7 +540,19 @@ public void onReceivedError(WebView view, WebResourceRequest request, WebResourc
}
else
{
showErrorScreen();
showErrorScreen(WebViewClient.ERROR_CONNECT);
}
}

private void handleErrorCode(int errorCode)
{
if (errorMessagesTable != null && errorMessagesTable.containsKey(errorCode))
{
errorTextView.setText(errorMessagesTable.get(errorCode));
}
else if (defaultErrorMessage != null)
{
errorTextView.setText(defaultErrorMessage);
}
}

Expand Down

0 comments on commit 3050679

Please sign in to comment.