Commit 8d91df7d authored by Vladislav Bogdashkin's avatar Vladislav Bogdashkin 🎣

files module integrated

parent 358ac8aa
package com.biganto.visual.roompark.data.repository.file
import android.content.Context
import android.os.Environment
import com.google.gson.JsonElement
import dagger.Module
import timber.log.Timber
import java.io.File
/**
* Created by Vladislav Bogdashkin on 29.06.2018.
*/
@Module
class FilesModule(private val context: Context) {
private val rootFolder: File
get() {
val rootFile = context.filesDir
Environment.getDataDirectory()
if (!rootFile.exists())
rootFile.mkdirs()
return rootFile
}
fun getFile(fileUri: String): File {
try {
val fileName =if (fileUri.contains("/")) fileUri.substring(fileUri.lastIndexOf("/")) else fileUri
val fileDir = if (fileUri.contains("/")) fileUri.substring(0, fileUri.lastIndexOf("/")) else ""
val directory = File(rootFolder, fileDir)
directory.mkdirs()
val file = File(directory, fileName)
return file
}
catch (e:Exception)
{
Timber.e("Caused file: %s",fileUri)
throw error("unconditional file")
}
}
fun saveFileToDisk(file:File,jsonElement: JsonElement){
file.writeText("[$jsonElement]") //to json array because core unity method parse data like TourData[] Estate[] etc..
}
fun saveFileToDisk(file:File,content: String){
file.writeText(content) //to json array because core unity method parse data like TourData[] Estate[] etc..
}
fun deleteFile(uri:String)= getFile(uri).delete()
val getCoreCacheDirectory:String
get(){
return rootFolder.absolutePath
}
companion object {
fun getDirectory(context: Context?, dirType: FileDirectory): String =
context?.filesDir?.absolutePath?.plus(dirType.dir)
?: throw IllegalArgumentException("Context cannot be null!")
fun assetsDirectory(context: Context?): String = context?.filesDir?.absolutePath.
?: throw IllegalArgumentException("Context cannot be null!")
}
sealed class FileDirectory(val dir:String) {
class PlanTypeDir(dir:String = "estates") : FileDirectory(dir)
class ToursDir(dir:String = "tours") : FileDirectory(dir)
class FeedsDir(dir:String = "feeds") : FileDirectory(dir)
class Albums(dir:String = "photos") : FileDirectory(dir)
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment