Skip to content

Commit

Permalink
CATROID-1393 Remove redundant code of directory-import
Browse files Browse the repository at this point in the history
  • Loading branch information
Walcho1125 committed Apr 14, 2022
1 parent 25adb87 commit 9a4efc7
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 163 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2021 The Catrobat Team
* Copyright (C) 2010-2022 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
Expand All @@ -27,9 +27,15 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.catrobat.catroid.common.Constants
import org.catrobat.catroid.common.Constants.CACHE_DIR
import org.catrobat.catroid.common.FlavoredConstants
import org.catrobat.catroid.content.backwardcompatibility.ProjectMetaDataParser
import org.catrobat.catroid.io.StorageOperations
import org.catrobat.catroid.io.XstreamSerializer
import org.catrobat.catroid.io.ZipArchiver
import org.catrobat.catroid.ui.recyclerview.util.UniqueNameProvider
import org.catrobat.catroid.utils.FileMetaDataExtractor
import java.io.File
import java.io.IOException

Expand Down Expand Up @@ -63,8 +69,56 @@ private fun unzipAndImportProject(projectDir: File): Boolean = try {
StorageOperations.deleteDir(cachedProjectDir)
}
ZipArchiver().unzip(projectDir, cachedProjectDir)
ProjectImportTask.task(listOf(cachedProjectDir))
importProject(cachedProjectDir)
} catch (e: IOException) {
Log.e(TAG, "Cannot unzip project " + projectDir.name, e)
false
}

private fun getProjectName(projectDir: File): String? {
val xmlFile = File(projectDir, Constants.CODE_XML_FILE_NAME)
if (!xmlFile.exists()) {
Log.e(TAG, "No xml file found for project " + projectDir.name)
return null
}
return try {
ProjectMetaDataParser(xmlFile).projectMetaData.name
} catch (e: IOException) {
Log.d(TAG, "Cannot extract projectName from xml", e)
null
}
}

private fun importProject(projectDir: File): Boolean {
var projectName = getProjectName(projectDir) ?: return false
projectName = UniqueNameProvider().getUniqueName(projectName, FileMetaDataExtractor
.getProjectNames(FlavoredConstants.DEFAULT_ROOT_DIRECTORY))
val destinationDirectory = File(
FlavoredConstants.DEFAULT_ROOT_DIRECTORY,
FileMetaDataExtractor.encodeSpecialCharsForFileSystem(projectName))
return try {
copyProject(projectDir, destinationDirectory, projectName)
true
} catch (e: IOException) {
Log.e(TAG, "Something went wrong while importing project ${projectDir.name}", e)
errorWhileImporting(projectDir, destinationDirectory)
false
}
}

private fun copyProject(projectDir: File, destinationDirectory: File, projectName: String) {
StorageOperations.copyDir(projectDir, destinationDirectory)
XstreamSerializer.renameProject(
File(destinationDirectory, Constants.CODE_XML_FILE_NAME), projectName)
}

private fun errorWhileImporting(projectDir: File, destinationDirectory: File) {
if (destinationDirectory.isDirectory) {
Log.e(TAG, "Folder exists, trying to delete folder.")
try {
StorageOperations.deleteDir(projectDir)
} catch (deleteException: IOException) {
Log.e(TAG, "Cannot delete folder $projectDir", deleteException)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2018 The Catrobat Team
* Copyright (C) 2010-2022 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2018 The Catrobat Team
* Copyright (C) 2010-2022 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2021 The Catrobat Team
* Copyright (C) 2010-2022 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -51,8 +51,6 @@ import org.catrobat.catroid.exceptions.LoadingProjectException
import org.catrobat.catroid.io.StorageOperations
import org.catrobat.catroid.io.XstreamSerializer
import org.catrobat.catroid.io.asynctask.ProjectCopier
import org.catrobat.catroid.io.asynctask.ProjectImportTask
import org.catrobat.catroid.io.asynctask.ProjectImportTask.ProjectImportListener
import org.catrobat.catroid.io.asynctask.ProjectLoader
import org.catrobat.catroid.io.asynctask.ProjectLoader.ProjectLoadListener
import org.catrobat.catroid.io.asynctask.ProjectRenamer
Expand All @@ -76,17 +74,13 @@ import java.util.ArrayList
class ProjectListFragment : RecyclerViewFragment<ProjectData?>(), ProjectLoadListener {
private var filesForUnzipAndImportTask: ArrayList<File>? = null
private var hasUnzipAndImportTaskFinished = false
private var filesForImportTask: ArrayList<File>? = null
private var hasImportTaskFinished = false

private val projectManager: ProjectManager by inject()

override fun onActivityCreated(savedInstance: Bundle?) {
super.onActivityCreated(savedInstance)
filesForImportTask = ArrayList()
filesForUnzipAndImportTask = ArrayList()
hasUnzipAndImportTaskFinished = true
hasImportTaskFinished = true
if (arguments != null) {
importProject(requireArguments().getParcelable("intent"))
}
Expand Down Expand Up @@ -116,7 +110,7 @@ class ProjectListFragment : RecyclerViewFragment<ProjectData?>(), ProjectLoadLis

private fun onRenameFinished(success: Boolean) {
if (success) {
if (hasImportTaskFinished && hasUnzipAndImportTaskFinished) {
if (hasUnzipAndImportTaskFinished) {
ToastUtil.showSuccess(
requireContext(),
getString(R.string.renamed_project)
Expand All @@ -130,27 +124,6 @@ class ProjectListFragment : RecyclerViewFragment<ProjectData?>(), ProjectLoadLis
setShowProgressBar(false)
}

private val projectImportListener = object : ProjectImportListener {
override fun onImportFinished(success: Boolean) {
hasImportTaskFinished = true
setAdapterItems(adapter.projectsSorted)
if (hasImportTaskFinished && hasUnzipAndImportTaskFinished) {
ToastUtil.showSuccess(
requireContext(),
resources.getQuantityString(
R.plurals.imported_projects,
filesForImportTask?.size ?: 0,
filesForImportTask?.size ?: 0
)
)
filesForImportTask?.clear()
} else {
ToastUtil.showError(requireContext(), R.string.error_import_project)
}
setShowProgressBar(false)
}
}

override fun onResume() {
if (actionModeType != IMPORT_LOCAL) {
projectManager.currentProject = null
Expand Down Expand Up @@ -298,12 +271,6 @@ class ProjectListFragment : RecyclerViewFragment<ProjectData?>(), ProjectLoadLis

private fun importProjectUris(uris: ArrayList<Uri>) {
prepareFilesForImport(uris)
filesForImportTask?.apply {
if (isNotEmpty()) {
val filesToImport = filesForImportTask?.toList() ?: listOf()
ProjectImportTask(filesToImport).setListener(projectImportListener).execute()
}
}
filesForUnzipAndImportTask?.apply {
if (isNotEmpty()) {
val filesToUnzipAndImport = filesForUnzipAndImportTask?.toTypedArray() ?: arrayOf()
Expand All @@ -329,8 +296,8 @@ class ProjectListFragment : RecyclerViewFragment<ProjectData?>(), ProjectLoadLis
val filePath = uri.path ?: return
val src = File(filePath)
if (src.isDirectory) {
filesForImportTask?.add(src)
hasImportTaskFinished = false
//filesForImportTask?.add(src)
//hasImportTaskFinished = false
} else {
copyFileContentToCacheFile(uri, fileName)
}
Expand Down

0 comments on commit 9a4efc7

Please sign in to comment.