Skip to content

Commit

Permalink
Refactor Exporters
Browse files Browse the repository at this point in the history
  • Loading branch information
italankin committed May 6, 2021
1 parent 1b6adc3 commit 8b68505
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
12 changes: 6 additions & 6 deletions app/src/main/java/com/italankin/fifteen/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class MainActivity extends Activity implements ExportCallback, Exporter.C
private static final int REQUEST_CODE_CREATE_DOC_RECORDS = 1;
private static final int REQUEST_CODE_CREATE_DOC_SESSION = 2;

private RecordsExporter mRecordsExporter;
private SessionExporter mSessionExporter;
private Exporter mRecordsExporter;
private Exporter mSessionExporter;
private DBHelper mDbHelper;
private GameSurface mGameView;

Expand Down Expand Up @@ -88,17 +88,17 @@ public void onBackPressed() {
public void exportRecords() {
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/csv");
intent.putExtra(Intent.EXTRA_TITLE, RecordsExporter.DEFAULT_FILENAME);
intent.setType(Exporter.MIME_TYPE);
intent.putExtra(Intent.EXTRA_TITLE, mRecordsExporter.defaultFilename());
startActivityForResult(intent, REQUEST_CODE_CREATE_DOC_RECORDS);
}

@Override
public void exportSession() {
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/csv");
intent.putExtra(Intent.EXTRA_TITLE, SessionExporter.defaultFilename());
intent.setType(Exporter.MIME_TYPE);
intent.putExtra(Intent.EXTRA_TITLE, mSessionExporter.defaultFilename());
startActivityForResult(intent, REQUEST_CODE_CREATE_DOC_SESSION);
}

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/italankin/fifteen/export/Exporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

public interface Exporter {

String MIME_TYPE = "text/csv";

char DELIMITER = ';';

void export(Uri uri, Callback callback);

String defaultFilename();

interface Callback {

void onExportSuccess();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

public class RecordsExporter implements Exporter {

public static final String DEFAULT_FILENAME = "15-puzzle-records.csv";
private static final String DEFAULT_FILENAME = "15-puzzle-records.csv";
private static final char DELIMITER = ';';

private final Context context;
Expand All @@ -41,6 +41,11 @@ public void export(Uri uri, Callback callback) {
executor.execute(new ExportTask(uri, callback));
}

@Override
public String defaultFilename() {
return DEFAULT_FILENAME;
}

private class ExportTask implements Runnable {
private final Uri uri;
private final Callback callback;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@

public class SessionExporter implements Exporter {

private static final String DEFAULT_FILENAME = "15-puzzle-session-%s.csv";

public static String defaultFilename() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd", Locale.getDefault());
return String.format(DEFAULT_FILENAME, dateFormat.format(new Date()));
}
private static final String DEFAULT_FILENAME_FORMAT = "15-puzzle-session-%s.csv";

private final Context context;
private final Executor executor = Executors.newSingleThreadExecutor();
Expand All @@ -47,6 +42,12 @@ public void export(Uri uri, Callback callback) {
executor.execute(new ExportTask(uri, callback));
}

@Override
public String defaultFilename() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd", Locale.getDefault());
return String.format(DEFAULT_FILENAME_FORMAT, dateFormat.format(new Date()));
}

private class ExportTask implements Runnable {
private final Uri uri;
private final Callback callback;
Expand Down

0 comments on commit 8b68505

Please sign in to comment.