Bladeren bron

Add feature: project rename

cooperku_kdanmobile 6 jaren geleden
bovenliggende
commit
c5131fc8ce

+ 2 - 2
src/main/java/com/bomostory/sceneeditmodule/Config.kt

@@ -7,8 +7,8 @@ object Config {
     private val ROOT = File(Environment.getExternalStorageDirectory(), "Bomo")
     private const val ASSETS_FOLDER_PATH = "/Bomo/Assets"
     private const val PROJECTS_FOLDER_PATH = "/Bomo/Projects"
-    private const val IMAGE_FOLDER_PATH = "/Bomo/Image"
-    val IMAGE_FOLDER = File(Environment.getExternalStorageDirectory(), IMAGE_FOLDER_PATH)
+    const val RECORD_FOLDER_NAME = "Record"
+    const val IMAGE_FOLDER_NAME = "Image"
     val ASSETS_FOLDER = File(Environment.getExternalStorageDirectory(), ASSETS_FOLDER_PATH)
     val PROJECTS_FOLDER = File(Environment.getExternalStorageDirectory(), PROJECTS_FOLDER_PATH)
     const val PROJECT_FILE_NAME = "index"

+ 9 - 5
src/main/java/com/bomostory/sceneeditmodule/SceneEditActivity.kt

@@ -360,7 +360,7 @@ class SceneEditActivity : AppCompatActivity(), ActorAdapter.OnActorDragListener,
             //initNavigationBarView(currentSceneIndex)
             navigationBar.removeAllViews()
             initNavigationFunction()
-            FileUtils.saveBitmapForActor(this, paint_view.bitmap, System.currentTimeMillis().toString())
+            FileUtils.saveBitmapForActor(this, project, paint_view.bitmap, System.currentTimeMillis().toString())
             project.story?.let {
                 it.scenes?.let{
                     sceneEditView.scene = it[currentSceneIndex]
@@ -524,8 +524,7 @@ class SceneEditActivity : AppCompatActivity(), ActorAdapter.OnActorDragListener,
     }
 
     private fun getRecordFilePath(): String {
-        val projectFolder = File(Config.PROJECTS_FOLDER, project.name)
-        return "${projectFolder.absolutePath}/Record/${System.currentTimeMillis()}.mp3"
+        return "${FileUtils.getProjectPath(project)}/${Config.RECORD_FOLDER_NAME}/${System.currentTimeMillis()}.mp3"
     }
 
     private fun initRecord(){
@@ -956,6 +955,8 @@ class SceneEditActivity : AppCompatActivity(), ActorAdapter.OnActorDragListener,
                         resourcePath = actor.resourcePath
                         parentLayerIndex = currentLayerIndex
                         isMovable = true
+                        opacity = actor.opacity
+                        isMirror = actor.isMirror
                     }
                     it[currentSceneIndex].layers[currentLayerIndex].actors.add(actorData)
                     setActorPositionZ(it[currentSceneIndex].layers[currentLayerIndex].actors)
@@ -1078,6 +1079,7 @@ class SceneEditActivity : AppCompatActivity(), ActorAdapter.OnActorDragListener,
                         textAlign =  actor.textAlign
                         isDialogue = actor.isDialogue
                         opacity = actor.opacity
+                        isMirror = actor.isMirror
                     }
                     it[currentSceneIndex].layers[currentLayerIndex].actors.add(actorData)
                     setActorPositionZ(it[currentSceneIndex].layers[currentLayerIndex].actors)
@@ -1275,9 +1277,9 @@ class SceneEditActivity : AppCompatActivity(), ActorAdapter.OnActorDragListener,
                     Activity.RESULT_OK -> if (data != null) {
                         var uri = data.data
                         if (uri.path.endsWith(".gif"))
-                            FileUtils.saveGifForActor(this, data.data, System.currentTimeMillis().toString())
+                            FileUtils.saveGifForActor(this, project, data.data, System.currentTimeMillis().toString())
                         else
-                            FileUtils.saveImageForActor(this, data.data, System.currentTimeMillis().toString())
+                            FileUtils.saveImageForActor(this, project, data.data, System.currentTimeMillis().toString())
                     }
                     Activity.RESULT_CANCELED -> {
                     }
@@ -1369,6 +1371,8 @@ class SceneEditActivity : AppCompatActivity(), ActorAdapter.OnActorDragListener,
                         resourcePath = actor.resourcePath
                         parentLayerIndex = selectLayer
                         isMovable = true
+                        opacity = actor.opacity
+                        isMirror = actor.isMirror
                     }
                     it[currentSceneIndex].layers[selectLayer].actors.add(actorData)
                     setActorPositionZ(it[currentSceneIndex].layers[selectLayer].actors)

+ 88 - 13
src/main/java/com/bomostory/sceneeditmodule/utils/FileUtils.kt

@@ -19,6 +19,10 @@ import java.io.*
 
 object FileUtils {
 
+    fun getProjectPath(project: Project): String {
+        return File(Config.PROJECTS_FOLDER, project.name).absolutePath
+    }
+
     fun getVideoFile(project: Project): File {
         val projectFolder = File(Config.PROJECTS_FOLDER, project.name)
         return File(projectFolder, Config.VIDEO_FILE_NAME)
@@ -150,10 +154,10 @@ object FileUtils {
         return "com.google.android.apps.photos.content" == uri.authority
     }
 
-    fun saveGifForActor(context: Context, uri: Uri, fileName: String) {
-        val dir = File(Config.IMAGE_FOLDER,"")
-        dir.mkdir()
-        val file = File(Config.IMAGE_FOLDER, fileName.plus(".gif"))
+    fun saveGifForActor(context: Context, project: Project, uri: Uri, fileName: String) {
+        val dir = File("${getProjectPath(project)}/${Config.IMAGE_FOLDER_NAME}/")
+        dir.mkdirs()
+        val file = File(dir, fileName.plus(".gif"))
         try {
             var sourceFile = File(getRealPathFromURI(context, uri))
             val bis = BufferedInputStream(FileInputStream(sourceFile))
@@ -176,10 +180,10 @@ object FileUtils {
         }
     }
 
-    fun saveImageForActor(context: Context, uri: Uri, fileName: String){
-        val dir = File(Config.IMAGE_FOLDER,"")
-        dir.mkdir()
-        val file = File(Config.IMAGE_FOLDER, fileName)
+    fun saveImageForActor(context: Context, project: Project, uri: Uri, fileName: String){
+        val dir = File("${getProjectPath(project)}/${Config.IMAGE_FOLDER_NAME}/")
+        dir.mkdirs()
+        val file = File(dir, fileName.plus(".jpg"))
         try {
             val bitmap = MediaStore.Images.Media.getBitmap(context.contentResolver, uri)
             val bos = BufferedOutputStream(FileOutputStream(file))
@@ -196,10 +200,10 @@ object FileUtils {
         }
     }
 
-    fun saveBitmapForActor(context: Context, bitmap: Bitmap?, fileName: String){
-        val dir = File(Config.IMAGE_FOLDER,"")
-        dir.mkdir()
-        val file = File(Config.IMAGE_FOLDER, fileName.plus(".png"))
+    fun saveBitmapForActor(context: Context, project: Project, bitmap: Bitmap?, fileName: String){
+        val dir = File("${getProjectPath(project)}/${Config.IMAGE_FOLDER_NAME}/")
+        dir.mkdirs()
+        val file = File(dir, fileName.plus(".png"))
         try {
             val bos = BufferedOutputStream(FileOutputStream(file))
             bitmap?.compress(Bitmap.CompressFormat.PNG, 80, bos)
@@ -215,10 +219,80 @@ object FileUtils {
         }
     }
 
+    private fun readFileToString(file: File): String {
+        if (!file.exists()) return ""
+        return FileReader(file).run {
+            val text = readText()
+            close()
+            text
+        }
+    }
+
     fun changeProjectName(oldName: String, newName: String): Boolean {
+        val oldProjectFile = File("${Config.PROJECTS_FOLDER}/$oldName/${Config.PROJECT_FILE_NAME}")
+        if (!oldProjectFile.exists())
+            return false
+        var oldProject = Gson().fromJson<Project>(readFileToString(oldProjectFile), Project::class.java)
+
         val oldProjectFolder = File(Config.PROJECTS_FOLDER, oldName)
         val newProjectFolder = File(Config.PROJECTS_FOLDER, newName)
-        return oldProjectFolder.renameTo(newProjectFolder)
+        val result = oldProjectFolder.renameTo(newProjectFolder)
+
+        if (result) {
+
+            val newProjectFile = File("${Config.PROJECTS_FOLDER}/$newName/${Config.PROJECT_FILE_NAME}")
+            var oldProjectPath = File(Config.PROJECTS_FOLDER, oldName).absolutePath
+            var newProjectPath = File(Config.PROJECTS_FOLDER, newName).absolutePath
+            fun changeFilePath(file: File): File {
+                println("resultProjectPath = ${file.absolutePath.replace(oldProjectPath, newProjectPath)}")
+                return File(file.absolutePath.replace(oldProjectPath, newProjectPath))
+            }
+            println("oldProjectPath = $oldProjectPath")
+            println("newProjectPath = $newProjectPath")
+            val newProject = Project(
+                    oldProject.themeAssetIndex,
+                    changeFilePath(oldProject.projectFile),
+                    changeFilePath(oldProject.projectFolder),
+                    changeFilePath(oldProject.scene1File),
+                    changeFilePath(oldProject.scene2File),
+                    changeFilePath(oldProject.coverFile),
+                    changeFilePath(oldProject.assetFolder),
+                    newName,
+                    oldProject.author,
+                    oldProject.frontCoverColor,
+                    oldProject.backCoverColor,
+                    oldProject.category,
+                    oldProject.scene1,
+                    oldProject.scene2,
+                    oldProject.story,
+                    oldProject.actorResource,
+                    oldProject.sceneResource
+            )
+
+            val scenes = newProject.story?.scenes
+            if (scenes != null) {
+                var count = 0
+                for (scene in scenes) {
+                    for (layer in scene.layers) {
+                        for (actor in layer.actors) {
+                            actor.resourcePath = actor.resourcePath.replace(oldProjectPath, newProjectPath)
+                            count++
+                        }
+                    }
+                }
+                println("count = $count")
+            }
+
+            try {
+                val fileWriter = FileWriter(newProjectFile)
+                fileWriter.write(Gson().toJson(newProject))
+                fileWriter.flush()
+                fileWriter.close()
+            } catch (e: Exception) {
+                e.printStackTrace()
+            }
+        }
+        return result
     }
 
     fun saveProject(context: Context, project: Project, scaleWidth: Int, scaleHeight: Int): Completable {
@@ -260,6 +334,7 @@ object FileUtils {
                         }
                     }
                 }
+                fileWriter.close()
             } catch (e: Exception) {
                 e.printStackTrace()
             }

+ 1 - 1
src/main/res/layout/view_front_cover_editor.xml

@@ -62,7 +62,7 @@
 
     <!-- TODO: remove enabled="false" after solve project rename issue -->
     <EditText
-        android:enabled="false"
+        android:enabled="true"
         android:id="@+id/et_frontCoverEditor_projectName"
         style="@style/BomoEditText"
         android:layout_width="320dp"