Browse Source

Network toast message move in BaseActivityBubble.

pull/12/head
Mushegh Sahakyan 4 years ago
parent
commit
68f735272d
3 changed files with 55 additions and 31 deletions
  1. +27
    -0
      ui/src/main/java/com/wireguard/android/activity/BaseActivityBubble.java
  2. +8
    -1
      ui/src/main/java/com/wireguard/android/activity/LoginActivity.java
  3. +20
    -30
      ui/src/main/java/com/wireguard/android/repository/DataRepository.java

+ 27
- 0
ui/src/main/java/com/wireguard/android/activity/BaseActivityBubble.java View File

@@ -1,8 +1,20 @@
package com.wireguard.android.activity; package com.wireguard.android.activity;


import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.wireguard.android.R;
import com.wireguard.android.fragment.LoadingDialogFragment; import com.wireguard.android.fragment.LoadingDialogFragment;

import java.net.ConnectException;
import java.net.UnknownHostException;

import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Lifecycle; import androidx.lifecycle.Lifecycle;
@@ -58,4 +70,19 @@ public class BaseActivityBubble extends AppCompatActivity {
loadingDialog.dismissAllowingStateLoss(); loadingDialog.dismissAllowingStateLoss();
} }
} }

protected void showNetworkNotAvailableMessage(Context context) {
if (context instanceof LoginActivity) {
((LoginActivity) context).runOnUiThread(new Runnable() {
@Override public void run() {
final LayoutInflater inflater = ((LoginActivity) context).getLayoutInflater();
final View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) ((LoginActivity) context).findViewById(R.id.custom_toast_container));
final Toast toast = new Toast(context);
toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 60);
toast.setView(layout);
toast.show();
}
});
}
}
} }

+ 8
- 1
ui/src/main/java/com/wireguard/android/activity/LoginActivity.java View File

@@ -177,7 +177,11 @@ public class LoginActivity extends BaseActivityBubble {
closeLoadingDialog(); closeLoadingDialog();
if (encodedCertificate.length == 0) { if (encodedCertificate.length == 0) {
Toast.makeText(LoginActivity.this, getString(R.string.failed_bubble), Toast.LENGTH_SHORT).show(); Toast.makeText(LoginActivity.this, getString(R.string.failed_bubble), Toast.LENGTH_SHORT).show();
} else {
}
else if(encodedCertificate.length == 1){
showNetworkNotAvailableMessage(LoginActivity.this);
}
else {
final Intent intent = KeyChain.createInstallIntent(); final Intent intent = KeyChain.createInstallIntent();
intent.putExtra(KeyChain.EXTRA_CERTIFICATE, encodedCertificate); intent.putExtra(KeyChain.EXTRA_CERTIFICATE, encodedCertificate);
intent.putExtra(KeyChain.EXTRA_NAME, CERTIFICATE_NAME); intent.putExtra(KeyChain.EXTRA_NAME, CERTIFICATE_NAME);
@@ -191,6 +195,9 @@ public class LoginActivity extends BaseActivityBubble {
break; break;
case ERROR: case ERROR:
closeLoadingDialog(); closeLoadingDialog();
if("no network".equals(userStatusResource.message)){
showNetworkNotAvailableMessage(LoginActivity.this);
}
Toast.makeText(LoginActivity.this, getString(R.string.login_failed), Toast.LENGTH_SHORT).show(); Toast.makeText(LoginActivity.this, getString(R.string.login_failed), Toast.LENGTH_SHORT).show();
Log.d("TAG", "Error"); Log.d("TAG", "Error");
break; break;


+ 20
- 30
ui/src/main/java/com/wireguard/android/repository/DataRepository.java View File

@@ -120,8 +120,8 @@ public class DataRepository {
getAllDevices(context); getAllDevices(context);
} }
}, throwable -> { }, throwable -> {
showNetworkNotAvailableMessage(throwable,context);
setMutableLiveData(StatusResource.error(throwable.getMessage()));
setErrorMessage(throwable,this);
// setMutableLiveData(StatusResource.error(throwable.getMessage()));
}); });
compositeDisposable.add(disposableLogin); compositeDisposable.add(disposableLogin);
} }
@@ -146,8 +146,8 @@ public class DataRepository {
addDevice(context); addDevice(context);
} }
}, throwable -> { }, throwable -> {
showNetworkNotAvailableMessage(throwable,context);
setMutableLiveData(StatusResource.error(throwable.getMessage()));
setErrorMessage(throwable,this);
// setMutableLiveData(StatusResource.error(throwable.getMessage()));
}); });
compositeDisposable.add(disposableAllDevices); compositeDisposable.add(disposableAllDevices);
} }
@@ -201,8 +201,8 @@ public class DataRepository {
UserStore.getInstance(context).setDevice(device.getName(), device.getUuid()); UserStore.getInstance(context).setDevice(device.getName(), device.getUuid());
getConfig(context); getConfig(context);
}, throwable -> { }, throwable -> {
showNetworkNotAvailableMessage(throwable,context);
setMutableLiveData(StatusResource.error(throwable.getMessage()));
setErrorMessage(throwable,this);
// setMutableLiveData(StatusResource.error(throwable.getMessage()));
}); });
compositeDisposable.add(disposableAddDevice); compositeDisposable.add(disposableAddDevice);
} else { } else {
@@ -240,21 +240,22 @@ public class DataRepository {
UserStore.getInstance(context).setDevice(device.getName(), device.getUuid()); UserStore.getInstance(context).setDevice(device.getName(), device.getUuid());
getConfig(context); getConfig(context);
}, throwable -> { }, throwable -> {
showNetworkNotAvailableMessage(throwable,context);
setMutableLiveData(StatusResource.error(throwable.getMessage()));
setErrorMessage(throwable,this);
// setMutableLiveData(StatusResource.error(throwable.getMessage()));
}); });
compositeDisposable.add(disposableAddDevice); compositeDisposable.add(disposableAddDevice);
} }
} }
} }
}, throwable -> { }, throwable -> {
showNetworkNotAvailableMessage(throwable,context);
setMutableLiveData(StatusResource.error(NO_INTERNET_CONNECTION));
setErrorMessage(throwable,this);
// setMutableLiveData(StatusResource.error(NO_INTERNET_CONNECTION));
}); });
compositeDisposable.add(disposableAllDevices); compositeDisposable.add(disposableAllDevices);
} }


private void getConfig(Context context) { private void getConfig(Context context) {
NetworkBoundStatusResource<User> liveData = this;
final String deviceID = UserStore.getInstance(context).getDeviceID(); final String deviceID = UserStore.getInstance(context).getDeviceID();
final String token = UserStore.getInstance(context).getToken(); final String token = UserStore.getInstance(context).getToken();
Request request = new Request.Builder() Request request = new Request.Builder()
@@ -263,8 +264,8 @@ public class DataRepository {
.build(); .build();
client.newCall(request).enqueue(new Callback() { client.newCall(request).enqueue(new Callback() {
@Override public void onFailure(final okhttp3.Call call, final IOException e) { @Override public void onFailure(final okhttp3.Call call, final IOException e) {
showNetworkNotAvailableMessage(e,context);
postMutableLiveData(StatusResource.error(e.getMessage()));
setErrorMessage(e,liveData);
// postMutableLiveData(StatusResource.error(e.getMessage()));
} }


@Override public void onResponse(final okhttp3.Call call, final Response response) throws IOException { @Override public void onResponse(final okhttp3.Call call, final Response response) throws IOException {
@@ -385,8 +386,7 @@ public class DataRepository {
.build(); .build();
client.newCall(request).enqueue(new Callback() { client.newCall(request).enqueue(new Callback() {
@Override public void onFailure(final okhttp3.Call call, final IOException e) { @Override public void onFailure(final okhttp3.Call call, final IOException e) {
showNetworkNotAvailableMessage(e,context);
liveData.postValue(new byte[]{});
liveData.postValue(new byte[]{1});
} }


@Override public void onResponse(final okhttp3.Call call, final Response response) throws IOException { @Override public void onResponse(final okhttp3.Call call, final Response response) throws IOException {
@@ -460,22 +460,6 @@ public class DataRepository {
return liveData; return liveData;
} }


private void showNetworkNotAvailableMessage(Throwable throwable , Context context){
if( throwable instanceof UnknownHostException || throwable instanceof ConnectException){
if(context instanceof LoginActivity) {
((LoginActivity)context).runOnUiThread(new Runnable() {
@Override public void run() {
final LayoutInflater inflater = ((LoginActivity) context).getLayoutInflater();
final View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup)((LoginActivity)context).findViewById(R.id.custom_toast_container));
final Toast toast = new Toast(context);
toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 60);
toast.setView(layout);
toast.show();
}
});
}
}
}


public void setHostName(Context context, String hostname){ public void setHostName(Context context, String hostname){
UserStore.getInstance(context).setHostname(hostname); UserStore.getInstance(context).setHostname(hostname);
@@ -484,4 +468,10 @@ public class DataRepository {
public String getHostname(Context context){ public String getHostname(Context context){
return UserStore.getInstance(context).getHostname(); return UserStore.getInstance(context).getHostname();
} }

private void setErrorMessage(Throwable throwable , NetworkBoundStatusResource<User> liveData){
if( throwable instanceof UnknownHostException || throwable instanceof ConnectException){
liveData.postMutableLiveData(StatusResource.error("no network"));
}
}
} }

Loading…
Cancel
Save