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 2 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
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 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
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ import org.fossasia.badgemagic.data.fragments.CONF_SPEED
import org.fossasia.badgemagic.data.fragments.ConfigInfo
import org.json.JSONObject

object StorageUtils {
class StorageUtils(val context: Context) {
private val EXTERNAL_STORAGE_DIRECTORY = "${Environment.getExternalStorageDirectory()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try to change Environment.getExternalStorageDirectory to app specific directory code now

.absolutePath}/Badge-Magic/"
.absolutePath}/Android/data/org.fossasia.badgemagic/Badge-Magic/"
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