Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
#19 ダイバーログのCSVエクスポートのロジック追加
- Loading branch information
Showing
11 changed files
with
221 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
app/src/main/java/com/slymapp/diverlog/domain/DiverLogCsvExporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.slymapp.diverlog.domain; | ||
|
||
import android.content.Context; | ||
import android.os.Environment; | ||
import android.util.Log; | ||
|
||
import com.slymapp.diverlog.infrastructure.realm.DiverLogRepositoryImpl; | ||
import com.slymapp.diverlog.utils.DiverLogHelper; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.util.List; | ||
|
||
import au.com.bytecode.opencsv.CSVWriter; | ||
|
||
/** | ||
* {@link DiverLog}をCSV形式でエクスポートするクラス | ||
*/ | ||
public class DiverLogCsvExporter implements DiverLogExporter { | ||
|
||
//TODO Daggerを利用してDIに置き換える | ||
private DiverLogRepository repository = new DiverLogRepositoryImpl(); | ||
|
||
@Override | ||
public void exportAllLog(Context context, String exportedName) { | ||
List<DiverLog> list = repository.fetchAll(); | ||
|
||
//TODO パーミッションを確認するフローを追加する | ||
File file = new File(Environment.getExternalStorageDirectory().getPath(), exportedName); | ||
Log.d("EXPORT_FILE", file.getAbsolutePath()); | ||
try { | ||
if (!file.createNewFile()) { | ||
return; | ||
} | ||
CSVWriter writer = new CSVWriter(new PrintWriter(file)); | ||
for (DiverLog log : list) { | ||
writer.writeNext(DiverLogHelper.toStringArray(log)); | ||
} | ||
writer.flush(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
app/src/main/java/com/slymapp/diverlog/domain/DiverLogExporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.slymapp.diverlog.domain; | ||
|
||
import android.content.Context; | ||
|
||
/** | ||
* ダイバーログの外部保存・読み込みをするインターフェース | ||
*/ | ||
public interface DiverLogExporter { | ||
|
||
/** | ||
* ダイバーログをファイル保存する | ||
* 外部保存をするため、外部ストレージの書き込みパーミッションが必要 | ||
* | ||
* @param context {@link Context} | ||
* @param exportedName 出力ファイル名 | ||
*/ | ||
void exportAllLog(Context context, String exportedName); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
app/src/main/java/com/slymapp/diverlog/utils/DiverLogHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.slymapp.diverlog.utils; | ||
|
||
import com.slymapp.diverlog.domain.DiverLog; | ||
|
||
/** | ||
* {@link com.slymapp.diverlog.domain.DiverLog}のヘルパークラス | ||
*/ | ||
public class DiverLogHelper { | ||
|
||
/** | ||
* {@link DiverLog}を文字列配列に変換する | ||
* @param diverLog {@link DiverLog} | ||
* @return ダイビングNo,ダイビング日時,潜水場所,開始時圧力,終了時圧力,開始時間,終了時間,入水方法,透明度,スーツ種別,ウェイト,平均深度,最大深度,水温 | ||
*/ | ||
public static String[] toStringArray(DiverLog diverLog) { | ||
return new String[]{ | ||
String.valueOf(diverLog.getDivingNumber()), | ||
DateUtils.toDateTimeString(diverLog.getDate()), | ||
diverLog.getWeather(), | ||
diverLog.getPlace(), | ||
String.valueOf(diverLog.getStartPressure()), | ||
String.valueOf(diverLog.getEndPressure()), | ||
DateUtils.toDateTimeString(diverLog.getStartTime()), | ||
DateUtils.toDateTimeString(diverLog.getEndTime()), | ||
diverLog.getEntryMethod(), | ||
String.valueOf(diverLog.getTransparent()), | ||
diverLog.getSuits(), | ||
String.valueOf(diverLog.getWeight()), | ||
String.valueOf(diverLog.getAverageDepth()), | ||
String.valueOf(diverLog.getMaxDepth()), | ||
String.valueOf(diverLog.getTemperature()) | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
app/src/test/java/com/slymapp/diverlog/utils/DiverLogHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.slymapp.diverlog.utils; | ||
|
||
import com.slymapp.diverlog.domain.DiverLog; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.Calendar; | ||
import java.util.GregorianCalendar; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.notNullValue; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class DiverLogHelperTest { | ||
@Test | ||
public void toStringArray() throws Exception { | ||
DiverLog diverLog = createDiverLogMock(); | ||
|
||
String[] actual = DiverLogHelper.toStringArray(diverLog); | ||
|
||
assertThat(actual, is(notNullValue())); | ||
assertThat(actual.length, is(15)); | ||
assertThat(actual[0], is("1")); | ||
assertThat(actual[1], is("2017-12-23 00:00:00")); | ||
assertThat(actual[2], is("晴れ")); | ||
assertThat(actual[3], is("どこかの海")); | ||
assertThat(actual[4], is("300")); | ||
assertThat(actual[5], is("0")); | ||
assertThat(actual[6], is("2017-12-23 12:00:00")); | ||
assertThat(actual[7], is("2017-12-23 12:30:00")); | ||
assertThat(actual[8], is("ボート")); | ||
assertThat(actual[9], is("30")); | ||
assertThat(actual[10], is("ドライ")); | ||
assertThat(actual[11], is("15")); | ||
assertThat(actual[12], is("20.0")); | ||
assertThat(actual[13], is("40.0")); | ||
assertThat(actual[14], is("10.0")); | ||
} | ||
|
||
private DiverLog createDiverLogMock() { | ||
DiverLog diverLog = new DiverLog(); | ||
diverLog.setDivingNumber(1); | ||
diverLog.setDate(new GregorianCalendar(2017, Calendar.DECEMBER, 23, 0, 0, 0).getTime()); | ||
diverLog.setWeather("晴れ"); | ||
diverLog.setPlace("どこかの海"); | ||
diverLog.setStartPressure(300); | ||
diverLog.setEndPressure(0); | ||
diverLog.setStartTime(new GregorianCalendar(2017, Calendar.DECEMBER, 23, 12, 0, 0).getTime()); | ||
diverLog.setEndTime(new GregorianCalendar(2017, Calendar.DECEMBER, 23, 12, 30, 0).getTime()); | ||
diverLog.setEntryMethod("ボート"); | ||
diverLog.setTransparent(30); | ||
diverLog.setSuits("ドライ"); | ||
diverLog.setWeight(15); | ||
diverLog.setAverageDepth(20f); | ||
diverLog.setMaxDepth(40f); | ||
diverLog.setTemperature(10f); | ||
return diverLog; | ||
} | ||
|
||
|
||
} |