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: draw clipart option in drawer #388

Merged
merged 1 commit into from
Jun 20, 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 @@ -5,25 +5,37 @@ import android.view.LayoutInflater
import android.view.ViewGroup
import org.fossasia.badgemagic.R
import org.fossasia.badgemagic.data.DrawableInfo
import org.fossasia.badgemagic.ui.DrawerActivity

class DrawableAdapter : RecyclerView.Adapter<DrawableItemHolder>() {
var onDrawableSelected: OnDrawableSelected? = null
private val drawableList = mutableListOf<DrawableInfo>()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DrawableItemHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.recycler_item, parent, false)
return DrawableItemHolder(v)
return DrawableItemHolder(
when (viewType) {
R.layout.recycler_item -> LayoutInflater.from(parent.context).inflate(R.layout.recycler_item, parent, false)
else -> LayoutInflater.from(parent.context).inflate(R.layout.recycler_item_add, parent, false)
}
)
}

override fun getItemViewType(position: Int) = position
override fun getItemViewType(position: Int) = if (position == drawableList.size) R.layout.recycler_item_add else R.layout.recycler_item

override fun getItemId(position: Int) = position.toLong()

override fun onBindViewHolder(holder: DrawableItemHolder, position: Int) {
holder.apply {
bind(drawableList[position])
listener = onDrawableSelected
}
if (position != drawableList.size)
holder.apply {
bind(drawableList[position])
listener = onDrawableSelected
}
else
holder.itemView.setOnClickListener {
val contextAct = it.context
if (contextAct is DrawerActivity)
contextAct.switchToDrawLayout()
}
}

fun addAll(newDrawableList: List<DrawableInfo>) {
Expand All @@ -32,7 +44,7 @@ class DrawableAdapter : RecyclerView.Adapter<DrawableItemHolder>() {
notifyDataSetChanged()
}

override fun getItemCount() = drawableList.size
override fun getItemCount() = drawableList.size + 1
}

interface OnDrawableSelected {
Expand Down
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), 80))

if (drawableInfo.image is VectorDrawable)
image.setImageBitmap(ImageUtils.trim(ImageUtils.vectorToBitmap(drawableInfo.image), 80))
else if (drawableInfo.image is BitmapDrawable)
image.setImageBitmap(ImageUtils.trim(drawableInfo.image.bitmap, 80))

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
}
109 changes: 73 additions & 36 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 @@ -46,8 +48,9 @@ class DrawerActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedLi

private var showMenu: Menu? = null
private var drawerCheckedID = R.id.create
private var isItemCheckedNew = false

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

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -70,6 +73,10 @@ class DrawerActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedLi
drawer_layout.addDrawerListener(toggle)
toggle.syncState()

Timber.e {
"OnCreate, ${viewModel.swappingOrientation}"
}

drawer_layout.addDrawerListener(object : DrawerLayout.DrawerListener {
override fun onDrawerStateChanged(newState: Int) {
}
Expand All @@ -78,29 +85,44 @@ class DrawerActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedLi
}

override fun onDrawerClosed(drawerView: View) {
when (drawerCheckedID) {
R.id.create -> {
switchFragment(TextArtFragment.newInstance())
showMenu?.setGroupVisible(R.id.saved_group, false)
}
R.id.saved -> {
switchFragment(SavedBadgesFragment.newInstance())
showMenu?.setGroupVisible(R.id.saved_group, true)
}
R.id.settings -> {
switchFragment(SettingsFragment.newInstance())
showMenu?.setGroupVisible(R.id.saved_group, false)
}
R.id.feedback -> {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/fossasia/badge-magic-android/issues")))
}
R.id.buy -> {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://sg.pslab.io")))
}
R.id.about -> {
switchFragment(AboutFragment.newInstance())
showMenu?.setGroupVisible(R.id.saved_group, false)
if (isItemCheckedNew) {
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)
}
R.id.feedback -> {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/fossasia/badge-magic-android/issues")))
}
R.id.buy -> {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://sg.pslab.io")))
}
R.id.about -> {
switchFragment(AboutFragment.newInstance())
showMenu?.setGroupVisible(R.id.saved_group, false)
}
}
isItemCheckedNew = false
}
}

Expand All @@ -109,20 +131,34 @@ 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)
}
}

fun switchToDrawLayout() {
drawerCheckedID = R.id.draw
viewModel.swappingOrientation = true
setRotation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
switchFragment(DrawFragment.newInstance())
showMenu?.setGroupVisible(R.id.saved_group, false)
nav_view.setCheckedItem(R.id.draw)
}

private fun prepareForScan() {
if (isBleSupported()) {
checkManifestPermission()
Expand Down Expand Up @@ -269,6 +305,7 @@ class DrawerActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedLi
}

override fun onNavigationItemSelected(item: MenuItem): Boolean {
isItemCheckedNew = true
drawerCheckedID = item.itemId
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
drawerLayout.closeDrawer(GravityCompat.START)
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 drawViewModel 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 = drawViewModel
return binding.root
}

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

drawViewModel.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()
drawViewModel.updateCliparts()
} else
Toast.makeText(requireContext(), R.string.clipart_saved_error, Toast.LENGTH_LONG).show()
}
})

drawViewModel.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)
if (drawableInfo.image is VectorDrawable)
spanStringBuilder.setSpan(CenteredImageSpan(requireContext(), ImageUtils.trim(ImageUtils.vectorToBitmap(drawableInfo.image), 70)), 0, strToAppend.length, 33)
else if (drawableInfo.image is BitmapDrawable)
spanStringBuilder.setSpan(CenteredImageSpan(requireContext(), ImageUtils.trim((drawableInfo.image).bitmap, 70)), 0, strToAppend.length, 33)

val editable = textViewMainText.text
val n = textViewMainText.selectionEnd
if (n < editable.length) {
Expand Down
Loading