1
0
mirror of https://github.com/etesync/android synced 2024-12-23 15:18:14 +00:00

Extract ResultFragment from ImportFragment

This commit is contained in:
Tal Hacohen 2017-03-30 15:52:17 +03:00
parent a7c5ccadc9
commit 56aba7c669
6 changed files with 118 additions and 97 deletions

View File

@ -8,7 +8,6 @@ import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.ContentProviderClient;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
@ -17,10 +16,7 @@ import android.os.Bundle;
import android.provider.CalendarContract;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.widget.Toast;
import com.etesync.syncadapter.App;
import com.etesync.syncadapter.R;
@ -29,6 +25,7 @@ import com.etesync.syncadapter.resource.LocalAddressBook;
import com.etesync.syncadapter.resource.LocalCalendar;
import com.etesync.syncadapter.resource.LocalContact;
import com.etesync.syncadapter.resource.LocalEvent;
import com.etesync.syncadapter.ui.importlocal.ResultFragment;
import org.apache.commons.codec.Charsets;
@ -36,7 +33,6 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;
import at.bitfire.ical4android.CalendarStorageException;
import at.bitfire.ical4android.Event;
@ -44,10 +40,10 @@ import at.bitfire.ical4android.InvalidCalendarException;
import at.bitfire.vcard4android.Contact;
import at.bitfire.vcard4android.ContactsStorageException;
import lombok.Cleanup;
import lombok.ToString;
import static com.etesync.syncadapter.Constants.KEY_ACCOUNT;
import static com.etesync.syncadapter.Constants.KEY_COLLECTION_INFO;
import static com.etesync.syncadapter.ui.importlocal.ResultFragment.*;
public class ImportFragment extends DialogFragment {
private static final int REQUEST_CODE = 6384; // onActivityResult request
@ -347,68 +343,4 @@ public class ImportFragment extends DialogFragment {
}
}
}
@ToString
static class ImportResult implements Serializable {
long total;
long added;
long updated;
Exception e;
boolean isFailed() {
return (e != null);
}
long getSkipped() {
return total - (added + updated);
}
}
public static class ResultFragment extends DialogFragment {
private static final String KEY_RESULT = "result";
private ImportResult result;
private static ResultFragment newInstance(ImportResult result) {
Bundle args = new Bundle();
args.putSerializable(KEY_RESULT, result);
ResultFragment fragment = new ResultFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
result = (ImportResult) getArguments().getSerializable(KEY_RESULT);
}
@Override
@NonNull
public Dialog onCreateDialog(Bundle savedInstanceState) {
int icon;
int title;
String msg;
if (result.isFailed()) {
icon = R.drawable.ic_error_dark;
title = R.string.import_dialog_failed_title;
msg = result.e.getLocalizedMessage();
} else {
icon = R.drawable.ic_import_export_black;
title = R.string.import_dialog_title;
msg = getString(R.string.import_dialog_success, result.total, result.added, result.updated, result.getSkipped());
}
return new AlertDialog.Builder(getActivity())
.setTitle(title)
.setIcon(icon)
.setMessage(msg)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// dismiss
}
})
.create();
}
}
}

View File

@ -3,7 +3,9 @@ package com.etesync.syncadapter.ui.importlocal;
import android.accounts.Account;
import android.app.Activity;
import android.app.ProgressDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
@ -13,14 +15,13 @@ import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.etesync.syncadapter.App;
import com.etesync.syncadapter.R;
import com.etesync.syncadapter.model.CollectionInfo;
import com.etesync.syncadapter.ui.ImportFragment;
public class ImportActivity extends AppCompatActivity implements SelectImportMethod, OnImportCallback {
public class ImportActivity extends AppCompatActivity implements SelectImportMethod, ResultFragment.OnImportCallback, DialogInterface {
public final static String EXTRA_ACCOUNT = "account",
EXTRA_COLLECTION_INFO = "collectionInfo";
@ -124,14 +125,18 @@ public class ImportActivity extends AppCompatActivity implements SelectImportMet
}
@Override
public void onImportSuccess() {
//todo tom what would you like to do? toast?
public void onImportResult(ResultFragment.ImportResult importResult) {
ResultFragment fragment = ResultFragment.newInstance(importResult);
fragment.show(getSupportFragmentManager(), null);
}
@Override
public void cancel() {
finish();
}
@Override
public void onImportFailed() {
//todo tom what would you like to do? toast?
public void dismiss() {
finish();
}

View File

@ -34,7 +34,7 @@ public class LocalCalendarImportFragment extends ListFragment {
private Account account;
private CollectionInfo info;
private OnImportCallback importCallback;
private ResultFragment.OnImportCallback importCallback;
public static LocalCalendarImportFragment newInstance(Account account, CollectionInfo info) {
LocalCalendarImportFragment frag = new LocalCalendarImportFragment();
@ -72,7 +72,7 @@ public class LocalCalendarImportFragment extends ListFragment {
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
importCallback = (OnImportCallback) getActivity();
importCallback = (ResultFragment.OnImportCallback) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException(getActivity().toString()
+ " must implement MyInterface ");
@ -86,7 +86,7 @@ public class LocalCalendarImportFragment extends ListFragment {
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
importCallback = (OnImportCallback) activity;
importCallback = (ResultFragment.OnImportCallback) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement MyInterface ");
@ -234,8 +234,7 @@ public class LocalCalendarImportFragment extends ListFragment {
@Override
protected void onPostExecute(Boolean result) {
progressDialog.dismiss();
if (result) importCallback.onImportSuccess();
else importCallback.onImportFailed();
importCallback.onImportResult(new ResultFragment.ImportResult());
}
private boolean importEvents(LocalCalendar fromCalendar) {

View File

@ -36,7 +36,7 @@ public class LocalContactImportFragment extends Fragment {
private Account account;
private CollectionInfo info;
private OnImportCallback importCallback;
private ResultFragment.OnImportCallback importCallback;
public static LocalContactImportFragment newInstance(Account account, CollectionInfo info) {
LocalContactImportFragment frag = new LocalContactImportFragment();
@ -82,7 +82,7 @@ public class LocalContactImportFragment extends Fragment {
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
importCallback = (OnImportCallback) getActivity();
importCallback = (ResultFragment.OnImportCallback) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException(getActivity().toString()
+ " must implement MyInterface ");
@ -96,7 +96,7 @@ public class LocalContactImportFragment extends Fragment {
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
importCallback = (OnImportCallback) activity;
importCallback = (ResultFragment.OnImportCallback) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement MyInterface ");
@ -168,8 +168,7 @@ public class LocalContactImportFragment extends Fragment {
@Override
protected void onPostExecute(Boolean result) {
progressDialog.dismiss();
if (result) importCallback.onImportSuccess();
else importCallback.onImportFailed();
importCallback.onImportResult(new ResultFragment.ImportResult());
}
private boolean importContacts(LocalAddressBook localAddressBook) {

View File

@ -1,11 +0,0 @@
package com.etesync.syncadapter.ui.importlocal;
/**
* Created by tal on 30/03/17.
*/
public interface OnImportCallback {
void onImportSuccess();
void onImportFailed();
}

View File

@ -0,0 +1,97 @@
package com.etesync.syncadapter.ui.importlocal;
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import com.etesync.syncadapter.R;
import java.io.Serializable;
import lombok.ToString;
/**
* Created by tal on 30/03/17.
*/
public class ResultFragment extends DialogFragment {
private static final String KEY_RESULT = "result";
private ImportResult result;
public static ResultFragment newInstance(ImportResult result) {
Bundle args = new Bundle();
args.putSerializable(KEY_RESULT, result);
ResultFragment fragment = new ResultFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
result = (ImportResult) getArguments().getSerializable(KEY_RESULT);
}
@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
Activity activity = getActivity();
if (activity instanceof DialogInterface) {
((DialogInterface)activity).dismiss();
}
}
@Override
@NonNull
public Dialog onCreateDialog(Bundle savedInstanceState) {
int icon;
int title;
String msg;
if (result.isFailed()) {
icon = R.drawable.ic_error_dark;
title = R.string.import_dialog_failed_title;
msg = result.e.getLocalizedMessage();
} else {
icon = R.drawable.ic_import_export_black;
title = R.string.import_dialog_title;
msg = getString(R.string.import_dialog_success, result.total, result.added, result.updated, result.getSkipped());
}
return new AlertDialog.Builder(getActivity())
.setTitle(title)
.setIcon(icon)
.setMessage(msg)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// dismiss
}
})
.create();
}
@ToString
public static class ImportResult implements Serializable {
public long total;
public long added;
public long updated;
public Exception e;
public boolean isFailed() {
return (e != null);
}
public long getSkipped() {
return total - (added + updated);
}
}
public interface OnImportCallback {
void onImportResult(ImportResult importResult);
}
}