Skip to content

Commit

Permalink
feat: draw badge option in drawer
Browse files Browse the repository at this point in the history
  • Loading branch information
adityastic committed Jun 20, 2019
1 parent dd6c926 commit b911f0d
Show file tree
Hide file tree
Showing 17 changed files with 367 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.fossasia.badgemagic.adapter

import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.VectorDrawable
import android.view.View
import android.widget.ImageView
Expand All @@ -16,7 +17,11 @@ class DrawableItemHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var listener: OnDrawableSelected? = null

fun bind(drawableInfo: DrawableInfo) {
image.setImageBitmap(ImageUtils.trim(ImageUtils.vectorToBitmap(drawableInfo.image as VectorDrawable), 200))
image.setImageBitmap(ImageUtils.trim(
if (drawableInfo.image is VectorDrawable)
ImageUtils.vectorToBitmap(drawableInfo.image)
else
(drawableInfo.image as BitmapDrawable).bitmap, 200))

image.setColorFilter(itemView.context.resources.getColor(android.R.color.black))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import org.fossasia.badgemagic.R
import org.fossasia.badgemagic.util.Resource
import org.fossasia.badgemagic.util.StorageUtils
import org.koin.core.KoinComponent
import org.koin.core.inject

Expand All @@ -14,6 +15,10 @@ class ClipArtService : KoinComponent {
private val resourceHelper: Resource by inject()

init {
updateClipArts()
}

private fun getAllClips(): SparseArray<Drawable> {
val tempSparseArray = SparseArray<Drawable>()
val listOfDrawables = listOf(
resourceHelper.getDrawable(R.drawable.clip_apple),
Expand All @@ -34,12 +39,23 @@ class ClipArtService : KoinComponent {
resourceHelper.getDrawable(R.drawable.clip_sun),
resourceHelper.getDrawable(R.drawable.clip_thumbs_up)
)
var lastIndex = 0
listOfDrawables.forEachIndexed { index, drawable ->
drawable?.let {
lastIndex = index
tempSparseArray.append(index, it)
}
}
clipArts.value = tempSparseArray
val drawablesInStorage = StorageUtils.getAllClips()
drawablesInStorage.forEach {
tempSparseArray.append(++lastIndex, it)
}

return tempSparseArray
}

fun updateClipArts() {
clipArts.value = getAllClips()
}

fun getClipArts(): LiveData<SparseArray<Drawable>> = clipArts
Expand Down
4 changes: 4 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 @@ -7,6 +7,8 @@ import org.fossasia.badgemagic.util.Resource
import org.fossasia.badgemagic.viewmodels.FilesViewModel
import org.fossasia.badgemagic.viewmodels.TextArtViewModel
import org.fossasia.badgemagic.viewmodels.SettingsViewModel
import org.fossasia.badgemagic.viewmodels.DrawViewModel
import org.fossasia.badgemagic.viewmodels.DrawerViewModel
import org.fossasia.badgemagic.viewmodels.EditBadgeViewModel
import org.koin.android.ext.koin.androidContext
import org.koin.androidx.viewmodel.dsl.viewModel
Expand All @@ -17,6 +19,8 @@ val viewModelModules = module {
viewModel { FilesViewModel(get()) }
viewModel { SettingsViewModel(get()) }
viewModel { EditBadgeViewModel(get()) }
viewModel { DrawViewModel(get()) }
viewModel { DrawerViewModel(get()) }
}

val singletonModules = module {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.fossasia.badgemagic.extensions

import android.app.Activity
import androidx.annotation.Keep

@Keep
fun <A : Activity> A.setRotation(rotation: Int) {
this.requestedOrientation = rotation
}
47 changes: 33 additions & 14 deletions app/src/main/java/org/fossasia/badgemagic/ui/DrawerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ import kotlinx.android.synthetic.main.activity_drawer.*
import kotlinx.android.synthetic.main.app_bar_drawer.*
import org.fossasia.badgemagic.R
import org.fossasia.badgemagic.core.android.log.Timber
import org.fossasia.badgemagic.extensions.setRotation
import org.fossasia.badgemagic.ui.base.BaseFragment
import org.fossasia.badgemagic.viewmodels.FilesViewModel
import org.fossasia.badgemagic.ui.base.BaseActivity
import org.fossasia.badgemagic.ui.fragments.AboutFragment
import org.fossasia.badgemagic.ui.fragments.SavedBadgesFragment
import org.fossasia.badgemagic.ui.fragments.SettingsFragment
import org.fossasia.badgemagic.ui.fragments.TextArtFragment
import org.fossasia.badgemagic.ui.base.BaseActivity
import org.fossasia.badgemagic.ui.fragments.DrawFragment
import org.fossasia.badgemagic.util.SendingUtils
import org.fossasia.badgemagic.util.StorageUtils
import org.fossasia.badgemagic.viewmodels.DrawerViewModel
import org.koin.androidx.viewmodel.ext.android.viewModel

class DrawerActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedListener {
Expand All @@ -47,7 +49,7 @@ class DrawerActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedLi
private var showMenu: Menu? = null
private var drawerCheckedID = R.id.create

private val viewModel by viewModel<FilesViewModel>()
private val viewModel by viewModel<DrawerViewModel>()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -80,14 +82,26 @@ class DrawerActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedLi
override fun onDrawerClosed(drawerView: View) {
when (drawerCheckedID) {
R.id.create -> {
viewModel.swappingOrientation = false
setRotation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
switchFragment(TextArtFragment.newInstance())
showMenu?.setGroupVisible(R.id.saved_group, false)
}
R.id.draw -> {
viewModel.swappingOrientation = true
setRotation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
switchFragment(DrawFragment.newInstance())
showMenu?.setGroupVisible(R.id.saved_group, false)
}
R.id.saved -> {
viewModel.swappingOrientation = false
setRotation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
switchFragment(SavedBadgesFragment.newInstance())
showMenu?.setGroupVisible(R.id.saved_group, true)
}
R.id.settings -> {
viewModel.swappingOrientation = false
setRotation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
switchFragment(SettingsFragment.newInstance())
showMenu?.setGroupVisible(R.id.saved_group, false)
}
Expand All @@ -109,17 +123,22 @@ class DrawerActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedLi
})

nav_view.setNavigationItemSelectedListener(this)
when (intent.action) {
Intent.ACTION_MAIN, "org.fossasia.badgemagic.createBadge.shortcut" -> {
switchFragment(TextArtFragment.newInstance())
showMenu?.setGroupVisible(R.id.saved_group, false)
nav_view.setCheckedItem(R.id.create)
}
"org.fossasia.badgemagic.savedBadges.shortcut" -> {
switchFragment(SavedBadgesFragment.newInstance())
showMenu?.setGroupVisible(R.id.saved_group, true)
nav_view.setCheckedItem(R.id.saved)
}
if (!viewModel.swappingOrientation)
when (intent.action) {
Intent.ACTION_MAIN, "org.fossasia.badgemagic.createBadge.shortcut" -> {
switchFragment(TextArtFragment.newInstance())
showMenu?.setGroupVisible(R.id.saved_group, false)
nav_view.setCheckedItem(R.id.create)
}
"org.fossasia.badgemagic.savedBadges.shortcut" -> {
switchFragment(SavedBadgesFragment.newInstance())
showMenu?.setGroupVisible(R.id.saved_group, true)
nav_view.setCheckedItem(R.id.saved)
}
} else {
switchFragment(DrawFragment.newInstance())
showMenu?.setGroupVisible(R.id.saved_group, false)
nav_view.setCheckedItem(R.id.draw)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.fossasia.badgemagic.ui.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import kotlinx.android.synthetic.main.fragment_draw.*
import org.fossasia.badgemagic.R
import org.fossasia.badgemagic.databinding.FragmentDrawBinding
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.androidx.viewmodel.ext.android.viewModel

class DrawFragment : BaseFragment() {

companion object {
@JvmStatic
fun newInstance() =
DrawFragment()
}

private val viewModel by viewModel<DrawViewModel>()

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val binding = DataBindingUtil.inflate<FragmentDrawBinding>(inflater, R.layout.fragment_draw, container, false)
binding.viewModel = viewModel
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

viewModel.savedButton.observe(this, Observer {
if (it) {
if (StorageUtils.saveClipArt(Converters.convertStringsToLEDHex(draw_layout.getCheckedList()))) {
Toast.makeText(requireContext(), R.string.clipart_saved_success, Toast.LENGTH_LONG).show()
viewModel.updateCliparts()
} else
Toast.makeText(requireContext(), R.string.clipart_saved_error, Toast.LENGTH_LONG).show()
}
})

viewModel.resetButton.observe(this, Observer {
if (it) {
draw_layout.resetCheckListWithDummyData()
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.content.Context
import android.content.DialogInterface
import android.content.res.Configuration
import android.graphics.Color
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.VectorDrawable
import android.os.AsyncTask
import android.os.Bundle
Expand Down Expand Up @@ -378,7 +379,11 @@ class TextArtFragment : BaseFragment() {
private fun putBitmapInEditText(drawableInfo: DrawableInfo) {
val strToAppend = "«${drawableInfo.id}»"
val spanStringBuilder = SpannableStringBuilder(strToAppend)
spanStringBuilder.setSpan(CenteredImageSpan(requireContext(), ImageUtils.trim(ImageUtils.vectorToBitmap(drawableInfo.image as VectorDrawable), 70)), 0, strToAppend.length, 33)
spanStringBuilder.setSpan(CenteredImageSpan(requireContext(), ImageUtils.trim(
if (drawableInfo.image is VectorDrawable)
ImageUtils.vectorToBitmap(drawableInfo.image)
else
(drawableInfo.image as BitmapDrawable).bitmap, 70)), 0, strToAppend.length, 33)
val editable = textViewMainText.text
val n = textViewMainText.selectionEnd
if (n < editable.length) {
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/java/org/fossasia/badgemagic/util/Converters.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.fossasia.badgemagic.util

import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.graphics.drawable.VectorDrawable
import android.util.SparseArray
Expand All @@ -14,7 +15,11 @@ const val DRAWABLE_END = '»'

object Converters {
private fun convertDrawableToLEDHex(drawableIcon: Drawable?, invertLED: Boolean): List<String> {
val bm = ImageUtils.scaleBitmap(ImageUtils.vectorToBitmap(drawableIcon as VectorDrawable), 40)
val bm = ImageUtils.scaleBitmap(
if (drawableIcon is VectorDrawable)
ImageUtils.vectorToBitmap(drawableIcon)
else
(drawableIcon as BitmapDrawable).bitmap, 40)
return convertBitmapToLEDHex(bm, invertLED)
}

Expand Down
42 changes: 38 additions & 4 deletions app/src/main/java/org/fossasia/badgemagic/util/StorageUtils.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.fossasia.badgemagic.util

import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Environment
import org.fossasia.badgemagic.data.fragments.CONF_HEX_STRINGS
Expand All @@ -14,18 +16,23 @@ import org.fossasia.badgemagic.data.fragments.BadgeConfig
import org.json.JSONObject
import java.io.File
import java.io.BufferedReader
import java.io.FileOutputStream
import java.io.InputStreamReader

object StorageUtils {
private val EXTERNAL_STORAGE_DIRECTORY = Environment.getExternalStorageDirectory()
.absolutePath + "/Badge-Magic/"
private val EXTERNAL_STORAGE_DIRECTORY = "${Environment.getExternalStorageDirectory()
.absolutePath}/Badge-Magic/"
private val EXTERNAL_CLIPART_DIRECTORY = "${EXTERNAL_STORAGE_DIRECTORY}ClipArts/"
private const val BADGE_EXTENSION = ".txt"
private const val CLIP_EXTENSION = ".png"

private fun checkDirectory(): Boolean {
val directory = File(EXTERNAL_STORAGE_DIRECTORY)
if (!directory.exists()) {
val directoryClips = File(EXTERNAL_CLIPART_DIRECTORY)
if (!directory.exists())
return directory.mkdirs()
}
if (!directoryClips.exists())
return directoryClips.mkdirs()
return true
}

Expand Down Expand Up @@ -138,4 +145,31 @@ object StorageUtils {
val saveFile = File(EXTERNAL_STORAGE_DIRECTORY, fileName)
saveFile.writeText(MoshiUtils.getAdapter().toJson(badgeConfig))
}

fun saveClipArt(bitmap: Bitmap): Boolean {
checkDirectory()
val file = File.createTempFile("clip", CLIP_EXTENSION, File(EXTERNAL_CLIPART_DIRECTORY))
try {
val out = FileOutputStream(file)
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)
out.flush()
out.close()
} catch (e: Exception) {
return false
}
return true
}

fun getAllClips(): List<Drawable> {
checkDirectory()
val list = mutableListOf<Drawable>()

val files = File(EXTERNAL_CLIPART_DIRECTORY).listFiles() ?: return list
for (i in files.indices) {
if (getFileExtension(files[i].name) == CLIP_EXTENSION) {
list.add(Drawable.createFromPath(files[i].absolutePath))
}
}
return list
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.fossasia.badgemagic.viewmodels

import androidx.databinding.ObservableBoolean
import androidx.databinding.ObservableField
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import org.fossasia.badgemagic.data.draw_layout.DrawMode
import org.fossasia.badgemagic.database.ClipArtService

class DrawViewModel(
private val clipArtService: ClipArtService
) : ViewModel() {
var drawModeState: ObservableField<DrawMode> = ObservableField(DrawMode.NOTHING)

var drawState: ObservableBoolean = ObservableBoolean(false)
var eraseState: ObservableBoolean = ObservableBoolean(false)

var savedButton: MutableLiveData<Boolean> = MutableLiveData()
var resetButton: MutableLiveData<Boolean> = MutableLiveData()

init {
savedButton.value = false
resetButton.value = false
}

fun changeDrawState() {
drawState.set(!drawState.get())
eraseState.set(false)
drawModeState.set(if (drawState.get()) DrawMode.DRAW else DrawMode.NOTHING)
}

fun changeEraseState() {
eraseState.set(!eraseState.get())
drawState.set(false)
drawModeState.set(if (eraseState.get()) DrawMode.ERASE else DrawMode.NOTHING)
}

fun saveBadge() {
savedButton.value = true
}

fun changeResetState() {
resetButton.value = true
}

fun updateCliparts() = clipArtService.updateClipArts()
}
Loading

0 comments on commit b911f0d

Please sign in to comment.