-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
285 additions
and
4 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
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
91 changes: 91 additions & 0 deletions
91
app/src/main/kotlin/io/github/janmalch/woroboro/business/ImportExportManager.kt
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,91 @@ | ||
package io.github.janmalch.woroboro.business | ||
|
||
import android.content.Context | ||
import android.util.Log | ||
import androidx.core.net.toFile | ||
import androidx.core.net.toUri | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import io.github.janmalch.woroboro.models.Exercise | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.withContext | ||
import java.io.File | ||
import java.time.LocalDateTime | ||
import javax.inject.Inject | ||
|
||
interface ImportExportManager { | ||
|
||
suspend fun export(): File | ||
|
||
suspend fun clean() | ||
} | ||
|
||
|
||
class ImportExportManagerImpl @Inject constructor( | ||
private val exerciseRepository: ExerciseRepository, | ||
private val routineRepository: RoutineRepository, | ||
@ApplicationContext private val context: Context, | ||
) : ImportExportManager { | ||
|
||
private val exportsDir = File(context.cacheDir, "exports").also(File::mkdirs) | ||
|
||
override suspend fun export(): File { | ||
val exercises = exerciseRepository.findAll().first() | ||
val routines = routineRepository.findAll().first() | ||
|
||
return withContext(Dispatchers.IO) { | ||
val exportFile = File( | ||
exportsDir, | ||
"woroboro-${ | ||
LocalDateTime.now().toString().replace('T', '_').replace('.', '-') | ||
.replace(':', '-') | ||
}.zip" | ||
) | ||
exportFile.createNewFile() | ||
writeZip(exportFile, | ||
exercises.map<Exercise, ZipContent> { | ||
ZipContent.TextFile( | ||
name = it.name + ".woroboro.txt", | ||
content = it.asText(includeMedia = true) | ||
) | ||
} + exercises.flatMap { | ||
it.media.map { media -> | ||
ZipContent.MediaFile( | ||
file = media.source.toUri().toFile() | ||
) | ||
} | ||
} | ||
) | ||
exportFile | ||
} | ||
} | ||
|
||
override suspend fun clean() { | ||
withContext(Dispatchers.IO) { | ||
exportsDir.listFiles()?.forEach { file -> | ||
runCatching { file.delete() } | ||
.onFailure { tr -> | ||
Log.e( | ||
"ImportExportManagerImpl", | ||
"Error while deleting export file.", | ||
tr | ||
) | ||
}.onSuccess { deleted -> | ||
if (deleted) { | ||
Log.d( | ||
"ImportExportManagerImpl", | ||
"Export file '${file.name}' has been deleted successfully." | ||
) | ||
} else { | ||
Log.w( | ||
"ImportExportManagerImpl", | ||
"Export file '${file.name}' has not been deleted." | ||
) | ||
|
||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
app/src/main/kotlin/io/github/janmalch/woroboro/business/ZipFiles.kt
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,43 @@ | ||
package io.github.janmalch.woroboro.business | ||
|
||
import java.io.BufferedInputStream | ||
import java.io.BufferedOutputStream | ||
import java.io.File | ||
import java.io.FileInputStream | ||
import java.io.FileOutputStream | ||
import java.util.zip.ZipEntry | ||
import java.util.zip.ZipOutputStream | ||
|
||
sealed interface ZipContent { | ||
data class TextFile(val name: String, val content: String) : ZipContent | ||
data class MediaFile(val file: File) : ZipContent | ||
} | ||
|
||
fun writeZip( | ||
dest: File, | ||
contents: List<ZipContent>, | ||
) { | ||
ZipOutputStream(BufferedOutputStream(FileOutputStream(dest))).use { out -> | ||
for (content in contents) { | ||
when (content) { | ||
is ZipContent.MediaFile -> { | ||
FileInputStream(content.file).use { fi -> | ||
BufferedInputStream(fi).use { buffi -> | ||
val entry = ZipEntry(content.file.name) | ||
out.putNextEntry(entry) | ||
buffi.copyTo(out, 1024) | ||
out.closeEntry() | ||
} | ||
} | ||
} | ||
|
||
is ZipContent.TextFile -> { | ||
val entry = ZipEntry(content.name) | ||
out.putNextEntry(entry) | ||
out.write(content.content.toByteArray()) | ||
out.closeEntry() | ||
} | ||
} | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.