refactor, moved code from baseactivity to feedbackutils class

This commit is contained in:
marcin.jedynski 2018-11-18 01:53:07 +01:00
parent 8669e040b5
commit e664e00212
3 changed files with 134 additions and 88 deletions

View File

@ -23,6 +23,7 @@ import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
@ -51,6 +52,7 @@ import com.uam.wmi.findmytutor.network.ApiClient;
import com.uam.wmi.findmytutor.service.BackgroundLocalizationService;
import com.uam.wmi.findmytutor.service.FeedbackService;
import com.uam.wmi.findmytutor.utils.ActiveFragment;
import com.uam.wmi.findmytutor.utils.FeedbackUtils;
import com.uam.wmi.findmytutor.utils.PrefUtils;
import com.uam.wmi.findmytutor.utils.RestApiHelper;
@ -87,7 +89,7 @@ public abstract class BaseActivity
protected DrawerLayout sideDrawer;
protected Toolbar toolbar;
protected boolean isTutor;
protected FeedbackUtils feedbackUtils;
private ActionBarDrawerToggle actionBarDrawerToggle;
private SharingFragment sharingFragment;
@ -100,7 +102,7 @@ public abstract class BaseActivity
setContentView(getContentViewId());
drawerNavigationView = findViewById(R.id.nav_view);
sideDrawer = findViewById(R.id.activity_container);
feedbackUtils = new FeedbackUtils(BaseActivity.this);
drawerNavigationView.setNavigationItemSelectedListener(
item -> {
String itemName = (String) item.getTitle();
@ -134,9 +136,12 @@ public abstract class BaseActivity
startActivity(i);
finish();
} else if (itemName.equals(getResources().getString(R.string.navigation_item_feedback))) {
showNoteDialog(BaseActivity.this, "FEEDBACK");
feedbackUtils.showNoteDialog("FEEDBACK");
/*showNoteDialog(BaseActivity.this, );*/
} else if (itemName.equals(getResources().getString(R.string.navigation_item_bug))) {
showNoteDialog(BaseActivity.this, "BUG REPORT");
feedbackUtils.showNoteDialog("BUG REPORT");
/*showNoteDialog(BaseActivity.this, "BUG REPORT");*/
}
sideDrawer.closeDrawers();
@ -387,86 +392,7 @@ public abstract class BaseActivity
item.setChecked(true);
}
private void showNoteDialog(Context context, String subject) {
LayoutInflater layoutInflaterAndroid = LayoutInflater.from(BaseActivity.this);
View view = layoutInflaterAndroid.inflate(R.layout.feedback_modal, null);
AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(BaseActivity.this);
alertDialogBuilderUserInput.setView(view);
final AlertDialog alertDialog = alertDialogBuilderUserInput.create();
EditText modalUserInput = view.findViewById(R.id.feedback_input);
CheckBox modalIsAnonymous = view.findViewById(R.id.feedback_is_anonymous);
TextView modalTitle = view.findViewById(R.id.feedback_modal_title);
TextView modalSubtitle = view.findViewById(R.id.feedback_modal_subtitle);
modalTitle.setText(subject);
if( subject == "BUG REPORT"){
modalSubtitle.setText("Tell us what you noticed!(max 1000 characters)");
} else if (subject == "FEEDBACK")
{
modalSubtitle.setText("Tell us what you think!(max 1000 characters)");
}
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE,"SEND", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String body = modalUserInput.getText().toString();
boolean mode = modalIsAnonymous.isChecked();
sendFeedback(context,subject,body,mode);
}
});
alertDialog.show();
}
/**
* Email client intent to send support mail
* Appends the necessary device information to email body
* useful when providing support
*/
private void sendFeedback(Context context, String header, String body, boolean mode) {
String appVersion = null;
String metadata = null;
try {
appVersion = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
if( !mode ){
metadata = "\n-----------------------------\n" +
"User ID: " + PrefUtils.getUserId(context) + "\n" +
"Device OS: Android\n" +
"Device OS version: " + Build.VERSION.RELEASE + "\n" +
"App Version: " + appVersion + "\n" +
"Device Brand: " + Build.BRAND + "\n" +
"Device Model: " + Build.MODEL + "\n" +
"Device Manufacturer: " + Build.MANUFACTURER + "\n" +
"-----------------------------\n";
body = metadata + body;
header = header + " - " + PrefUtils.getUserFirstName(context) + " " + PrefUtils.getUserLastName(context);
}
} catch (PackageManager.NameNotFoundException e) {
}
FeedbackService feedbackService = ApiClient.getClient(context).create(FeedbackService.class);
Feedback userFeedback = new Feedback(mode,header,body);
CompositeDisposable disposable = new CompositeDisposable();
disposable.add(feedbackService.postFeedback(userFeedback)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::handleResponse, this::handleError));
}
private void handleResponse(Response<Void> resp) {
Toast.makeText(getApplicationContext(), "Thank you for subbmiting your feedback", Toast.LENGTH_SHORT).show();
}
private void handleError(Throwable error) {
if (error instanceof HttpException) {
ResponseBody responseBody = ((HttpException) error).response().errorBody();
Toast.makeText(getApplicationContext(),
RestApiHelper.getErrorMessage(responseBody), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(),
"Network error " + error.getMessage(), Toast.LENGTH_SHORT).show();
Log.d("FEEDBACK",error.getMessage());
}
}
abstract int getNavigationMenuItemId();

View File

@ -0,0 +1,122 @@
package com.uam.wmi.findmytutor.utils;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.jakewharton.retrofit2.adapter.rxjava2.HttpException;
import com.uam.wmi.findmytutor.R;
import com.uam.wmi.findmytutor.activity.BaseActivity;
import com.uam.wmi.findmytutor.model.Feedback;
import com.uam.wmi.findmytutor.network.ApiClient;
import com.uam.wmi.findmytutor.service.FeedbackService;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;
import retrofit2.Response;
public class FeedbackUtils {
private Context activityContext;
public FeedbackUtils(Context context){
activityContext = context;
}
public void showNoteDialog(String subject) {
LayoutInflater layoutInflaterAndroid = LayoutInflater.from(activityContext);
View view = layoutInflaterAndroid.inflate(R.layout.feedback_modal, null);
AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(activityContext);
alertDialogBuilderUserInput.setView(view).setPositiveButton("SEND",null);
final AlertDialog alertDialog = alertDialogBuilderUserInput.create();
EditText modalUserInput = view.findViewById(R.id.feedback_input);
CheckBox modalIsAnonymous = view.findViewById(R.id.feedback_is_anonymous);
TextView modalTitle = view.findViewById(R.id.feedback_modal_title);
TextView modalSubtitle = view.findViewById(R.id.feedback_modal_subtitle);
modalTitle.setText(subject);
if( subject == "BUG REPORT"){
modalSubtitle.setText("Tell us what you noticed!(max 1000 characters)");
} else if (subject == "FEEDBACK")
{
modalSubtitle.setText("Tell us what you think!(max 1000 characters)");
}
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
Button sendButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String body = modalUserInput.getText().toString();
if(TextUtils.isEmpty(body)){
Toast.makeText(activityContext, "Please describe your insights.", Toast.LENGTH_SHORT).show();
modalUserInput.requestFocus();
}else{
boolean mode = modalIsAnonymous.isChecked();
sendFeedback(subject,body,mode);
alertDialog.dismiss();
}
}
});
}
});
alertDialog.show();
}
private void sendFeedback(String header, String body, boolean mode) {
String appVersion = null;
String metadata = null;
try {
appVersion = activityContext.getPackageManager().getPackageInfo(activityContext.getPackageName(), 0).versionName;
if( !mode ){
metadata = "\n-----------------------------\n" +
"User ID: " + PrefUtils.getUserId(activityContext) + "\n" +
"Device OS: Android\n" +
"Device OS version: " + Build.VERSION.RELEASE + "\n" +
"App Version: " + appVersion + "\n" +
"Device Model: " + Build.MODEL + "\n" +
"Device Manufacturer: " + Build.MANUFACTURER + "\n" +
"-----------------------------\n";
body = metadata + body;
header = header + " - " + PrefUtils.getUserFirstName(activityContext) + " " + PrefUtils.getUserLastName(activityContext);
}
} catch (PackageManager.NameNotFoundException e) {
}
FeedbackService feedbackService = ApiClient.getClient(activityContext).create(FeedbackService.class);
Feedback userFeedback = new Feedback(mode,header,body);
CompositeDisposable disposable = new CompositeDisposable();
disposable.add(feedbackService.postFeedback(userFeedback)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::handleResponse, this::handleError));
}
private void handleResponse(Response<Void> resp) {
Toast.makeText(activityContext, "Thank you for subbmiting your feedback", Toast.LENGTH_SHORT).show();
}
private void handleError(Throwable error) {
if (error instanceof HttpException) {
ResponseBody responseBody = ((HttpException) error).response().errorBody();
Toast.makeText(activityContext,
RestApiHelper.getErrorMessage(responseBody), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(activityContext,
"Network error " + error.getMessage(), Toast.LENGTH_SHORT).show();
Log.d("FEEDBACK",error.getMessage());
}
}
}

View File

@ -14,7 +14,7 @@
android:id="@+id/feedback_modal_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignStart="@+id/feedback_input"
android:layout_marginBottom="@dimen/dimen_10"
android:fontFamily="sans-serif-medium"
android:lineSpacingExtra="8sp"
@ -43,8 +43,6 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/feedback_input"
android:layout_marginTop="20dp"
android:layout_marginEnd="4dp"
android:layout_toStartOf="@+id/feedback_is_anonymous"
android:text="@string/modal_feedback_question"
android:textSize="16sp" />
@ -52,8 +50,8 @@
<CheckBox
android:id="@+id/feedback_is_anonymous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView5"
android:layout_height="22dp"
android:layout_below="@+id/feedback_input"
android:layout_alignEnd="@+id/feedback_input"
android:textSize="14sp" />