Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: app specific directory storage #559

Merged
merged 4 commits into from
Dec 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ClipArtService : KoinComponent {
private val clipArts = MutableLiveData<SparseArray<Drawable>>()
private val storageClipArts = MutableLiveData<HashMap<String, Drawable?>>()
private val resourceHelper: Resource by inject()
private val storageUtils: StorageUtils by inject()

init {
updateClipArts()
Expand Down Expand Up @@ -56,7 +57,7 @@ class ClipArtService : KoinComponent {
}

fun updateClipArts() {
storageClipArts.value = StorageUtils.getAllClips()
storageClipArts.value = storageUtils.getAllClips()
clipArts.value = getAllClips()
}

Expand All @@ -65,7 +66,7 @@ class ClipArtService : KoinComponent {
fun getClipsFromStorage(): LiveData<HashMap<String, Drawable?>> = storageClipArts

fun deleteClipart(fileName: String) {
StorageUtils.deleteClipart(fileName)
storageUtils.deleteClipart(fileName)
updateClipArts()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,33 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import org.fossasia.badgemagic.data.fragments.ConfigInfo
import org.fossasia.badgemagic.util.StorageUtils
import org.koin.core.KoinComponent
import org.koin.core.inject

class StorageFilesService {
class StorageFilesService : KoinComponent {
private val files = MutableLiveData<List<ConfigInfo>>()
private val storageUtils: StorageUtils by inject()

init {
files.value = StorageUtils.getAllFiles()
files.value = storageUtils.getAllFiles()
}

fun deleteFile(fileName: String) {
StorageUtils.deleteFile(fileName)
files.value = StorageUtils.getAllFiles()
storageUtils.deleteFile(fileName)
files.value = storageUtils.getAllFiles()
}

fun getFiles(): LiveData<List<ConfigInfo>> = files

fun update() {
files.value = StorageUtils.getAllFiles()
files.value = storageUtils.getAllFiles()
}

fun saveFile(filename: String, json: String) {
StorageUtils.saveFile(filename, json)
storageUtils.saveFile(filename, json)
}

fun getAbsPath(fileName: String): String? = StorageUtils.getAbsolutePathofFiles(fileName)
fun getAbsPath(fileName: String): String? = storageUtils.getAbsolutePathofFiles(fileName)

fun checkIfFilePresent(fileName: String): Boolean = StorageUtils.checkIfFilePresent(fileName)
fun checkIfFilePresent(fileName: String): Boolean = storageUtils.checkIfFilePresent(fileName)
}
2 changes: 2 additions & 0 deletions app/src/main/java/org/fossasia/badgemagic/di/Modules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.fossasia.badgemagic.database.ClipArtService
import org.fossasia.badgemagic.database.StorageFilesService
import org.fossasia.badgemagic.util.PreferenceUtils
import org.fossasia.badgemagic.util.Resource
import org.fossasia.badgemagic.util.StorageUtils
import org.fossasia.badgemagic.viewmodels.DrawViewModel
import org.fossasia.badgemagic.viewmodels.DrawerViewModel
import org.fossasia.badgemagic.viewmodels.EditBadgeViewModel
Expand Down Expand Up @@ -35,4 +36,5 @@ val singletonModules = module {
val utilModules = module {
single { PreferenceUtils(androidContext()) }
single { Resource(androidContext()) }
single { StorageUtils(androidContext()) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import org.fossasia.badgemagic.ui.fragments.TextArtFragment
import org.fossasia.badgemagic.util.SendingUtils
import org.fossasia.badgemagic.util.StorageUtils
import org.fossasia.badgemagic.viewmodels.DrawerViewModel
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.viewModel

class DrawerActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedListener {
Expand All @@ -48,6 +49,7 @@ class DrawerActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedLi
private var showMenu: Menu? = null
private var drawerCheckedID = R.id.create
private var isItemCheckedNew = false
private val storageUtils: StorageUtils by inject()

private val viewModel by viewModel<DrawerViewModel>()

Expand Down Expand Up @@ -206,10 +208,10 @@ class DrawerActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedLi
private fun showImportDialog(uri: Uri?) {
AlertDialog.Builder(this)
.setTitle(getString(R.string.import_dialog))
.setMessage("${getString(R.string.import_dialog_message)} ${StorageUtils.getFileName(this, uri
.setMessage("${getString(R.string.import_dialog_message)} ${storageUtils.getFileName(this, uri
?: Uri.EMPTY)}")
.setPositiveButton(android.R.string.ok) { _: DialogInterface, _: Int ->
if (!StorageUtils.checkIfFilePresent(this, uri)) {
if (!storageUtils.checkIfFilePresent(this, uri)) {
saveImportFile(uri)
} else
showOverrideDialog(uri)
Expand Down Expand Up @@ -274,7 +276,7 @@ class DrawerActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedLi
}

private fun saveImportFile(uri: Uri?) {
if (StorageUtils.copyFileToDirectory(this, uri)) {
if (storageUtils.copyFileToDirectory(this, uri)) {
Toast.makeText(this, R.string.success_import_json, Toast.LENGTH_SHORT).show()
viewModel.updateList()
} else Toast.makeText(this, R.string.invalid_import_json, Toast.LENGTH_SHORT).show()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ import org.fossasia.badgemagic.util.Converters
import org.fossasia.badgemagic.util.SendingUtils
import org.fossasia.badgemagic.util.StorageUtils
import org.fossasia.badgemagic.viewmodels.EditBadgeViewModel
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.viewModel

class EditBadgeActivity : AppCompatActivity() {

private val viewModel by viewModel<EditBadgeViewModel>()
private lateinit var fileName: String
private val storageUtils: StorageUtils by inject()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -41,7 +43,7 @@ class EditBadgeActivity : AppCompatActivity() {
Converters.convertStringsToLEDHex(draw_layout.getCheckedList()),
false
)
badgeConfig?.let { config -> StoreAsync(fileName, config, viewModel).execute() }
badgeConfig?.let { config -> StoreAsync(fileName, config, viewModel, storageUtils).execute() }
Toast.makeText(this, R.string.saved_edited_badge, Toast.LENGTH_LONG).show()
finish()
}
Expand Down Expand Up @@ -73,9 +75,9 @@ class EditBadgeActivity : AppCompatActivity() {

companion object {

class StoreAsync(private val fileName: String, private val badgeConfig: BadgeConfig, private val viewModel: EditBadgeViewModel) : AsyncTask<Void, Void, Void>() {
class StoreAsync(private val fileName: String, private val badgeConfig: BadgeConfig, private val viewModel: EditBadgeViewModel, val storageUtil: StorageUtils) : AsyncTask<Void, Void, Void>() {
override fun doInBackground(vararg params: Void?): Void? {
StorageUtils.saveEditedBadge(badgeConfig, fileName)
storageUtil.saveEditedBadge(badgeConfig, fileName)
return null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import org.fossasia.badgemagic.databinding.ActivityEditClipartBinding
import org.fossasia.badgemagic.util.Converters
import org.fossasia.badgemagic.util.StorageUtils
import org.fossasia.badgemagic.viewmodels.EditClipartViewModel
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.viewModel

class EditClipartActivity : AppCompatActivity() {

private val editClipartViewModel by viewModel<EditClipartViewModel>()
private lateinit var fileName: String
private val storageUtils: StorageUtils by inject()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -28,12 +30,12 @@ class EditClipartActivity : AppCompatActivity() {

if (intent.hasExtra("fileName")) {
fileName = intent?.extras?.getString("fileName") ?: ""
editClipartViewModel.drawingJSON.set(Converters.convertDrawableToLEDHex(StorageUtils.getClipartFromPath(fileName), false))
editClipartViewModel.drawingJSON.set(Converters.convertDrawableToLEDHex(storageUtils.getClipartFromPath(fileName), false))
}

editClipartViewModel.savedButton.observe(this, Observer {
if (it) {
if (StorageUtils.saveEditedClipart(Converters.convertStringsToLEDHex(draw_layout.getCheckedList()), fileName)) {
if (storageUtils.saveEditedClipart(Converters.convertStringsToLEDHex(draw_layout.getCheckedList()), fileName)) {
Toast.makeText(this, R.string.clipart_saved_success, Toast.LENGTH_LONG).show()
editClipartViewModel.updateClipArts()
} else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.fossasia.badgemagic.ui.base.BaseFragment
import org.fossasia.badgemagic.util.Converters
import org.fossasia.badgemagic.util.StorageUtils
import org.fossasia.badgemagic.viewmodels.DrawViewModel
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.viewModel

class DrawFragment : BaseFragment() {
Expand All @@ -25,6 +26,7 @@ class DrawFragment : BaseFragment() {
}

private val drawViewModel by viewModel<DrawViewModel>()
private val storageUtils: StorageUtils by inject()

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val binding = DataBindingUtil.inflate<FragmentDrawBinding>(inflater, R.layout.fragment_draw, container, false)
Expand All @@ -37,7 +39,7 @@ class DrawFragment : BaseFragment() {

drawViewModel.savedButton.observe(this, Observer {
if (it) {
if (StorageUtils.saveClipArt(Converters.convertStringsToLEDHex(draw_layout.getCheckedList()))) {
if (storageUtils.saveClipArt(Converters.convertStringsToLEDHex(draw_layout.getCheckedList()))) {
Toast.makeText(requireContext(), R.string.clipart_saved_success, Toast.LENGTH_LONG).show()
drawViewModel.updateCliparts()
} else
Expand Down
20 changes: 9 additions & 11 deletions app/src/main/java/org/fossasia/badgemagic/util/StorageUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Environment
import java.io.BufferedReader
import java.io.File
import java.io.FileOutputStream
Expand All @@ -19,12 +18,11 @@ import org.fossasia.badgemagic.data.fragments.CONF_SPEED
import org.fossasia.badgemagic.data.fragments.ConfigInfo
import org.json.JSONObject

object StorageUtils {
private val EXTERNAL_STORAGE_DIRECTORY = "${Environment.getExternalStorageDirectory()
.absolutePath}/Badge-Magic/"
class StorageUtils(val context: Context) {
private val EXTERNAL_STORAGE_DIRECTORY = context.getExternalFilesDir(null)?.absolutePath
private val EXTERNAL_CLIPART_DIRECTORY = "${EXTERNAL_STORAGE_DIRECTORY}ClipArts/"
private const val BADGE_EXTENSION = ".txt"
private const val CLIP_EXTENSION = ".png"
private val BADGE_EXTENSION = ".txt"
private val CLIP_EXTENSION = ".png"

private fun checkDirectory(): Boolean {
val directory = File(EXTERNAL_STORAGE_DIRECTORY)
Expand Down Expand Up @@ -105,11 +103,11 @@ object StorageUtils {
return try {
val obj = JSONObject(jsonString)
return obj.has(CONF_HEX_STRINGS) &&
obj.has(CONF_INVERTED) &&
obj.has(CONF_MARQUEE) &&
obj.has(CONF_FLASH) &&
obj.has(CONF_MODE) &&
obj.has(CONF_SPEED)
obj.has(CONF_INVERTED) &&
obj.has(CONF_MARQUEE) &&
obj.has(CONF_FLASH) &&
obj.has(CONF_MODE) &&
obj.has(CONF_SPEED)
} catch (e: Exception) {
false
}
Expand Down