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

Support multiple frames #644

Closed
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
@@ -0,0 +1,122 @@
package org.fossasia.badgemagic.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.widget.AppCompatImageView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.chip.Chip
import org.fossasia.badgemagic.R
import org.fossasia.badgemagic.data.BadgeConfig
import org.fossasia.badgemagic.data.Message
import org.fossasia.badgemagic.helpers.JSONHelper
import org.fossasia.badgemagic.util.SendingUtils

class TransferAdapter(private val context: Context?, private val list: List<Message>, private val listener: OnTransferItemSelected) : RecyclerView.Adapter<TransferAdapter.TransferItemHolder>() {
private var selectedPosition: Int = -1

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TransferItemHolder {
val v = LayoutInflater.from(context).inflate(R.layout.recycler_transfer_item, parent, false)
return TransferItemHolder(v)
}

override fun getItemViewType(position: Int) = position

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

override fun onBindViewHolder(holder: TransferItemHolder, position: Int) {
holder.bind(list[position])
}

override fun getItemCount() = list.size

fun getSelectedItem(): Message? {
return if (selectedPosition == -1) null else list[selectedPosition]
}

fun getItems(): List<Message> {
return list
}

inner class TransferItemHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

private val card: LinearLayout = itemView.findViewById(R.id.card)
private val text: TextView = itemView.findViewById(R.id.text)
private val playPause: AppCompatImageView = itemView.findViewById(R.id.play_pause)
private val delete: AppCompatImageView = itemView.findViewById(R.id.button_delete)
private val chipFlash: Chip = itemView.findViewById(R.id.chip_flash)
private val chipMarquee: Chip = itemView.findViewById(R.id.chip_marquee)
private val chipInverted: Chip = itemView.findViewById(R.id.chip_inverted)
private val chipSpeed: Chip = itemView.findViewById(R.id.chip_speed)
private val chipMode: Chip = itemView.findViewById(R.id.chip_mode)

init {
playPause.setOnClickListener {
changeCardBackgrounds()
listener.onSelected(if (selectedPosition == -1) null else list[selectedPosition])
}
delete.setOnClickListener {
listener.onDelete(list[adapterPosition])
}
}

fun bind(item: Message) {
// TODO: Get item name
// text.text = item.hexStrings.toString()
text.text = ""

card.background = when {
selectedPosition != -1 && selectedPosition == adapterPosition -> ContextCompat.getDrawable(itemView.context, R.color.colorAccent)
else -> ContextCompat.getDrawable(itemView.context, android.R.color.transparent)
}
text.setTextColor(
when {
selectedPosition != -1 && selectedPosition == adapterPosition -> ContextCompat.getColor(itemView.context, android.R.color.white)
else -> ContextCompat.getColor(itemView.context, android.R.color.black)
}
)
playPause.setColorFilter(
when {
selectedPosition != -1 && selectedPosition == adapterPosition -> ContextCompat.getColor(itemView.context, android.R.color.white)
else -> ContextCompat.getColor(itemView.context, android.R.color.black)
}
)
delete.setColorFilter(
when {
selectedPosition != -1 && selectedPosition == adapterPosition -> ContextCompat.getColor(itemView.context, android.R.color.white)
else -> ContextCompat.getColor(itemView.context, android.R.color.black)
}
)

val badge: BadgeConfig = JSONHelper.decodeJSON(SendingUtils.configToJSON(item, false))
chipSpeed.text = (badge.speed.ordinal.plus(1)).toString()
chipMode.text = badge.mode.toString()

chipFlash.visibility = if (badge.isFlash) View.VISIBLE else View.GONE
chipMarquee.visibility = if (badge.isMarquee) View.VISIBLE else View.GONE
chipInverted.visibility = if (badge.isInverted) View.VISIBLE else View.GONE
}

private fun changeCardBackgrounds() {
val lastSelected = selectedPosition

selectedPosition = when {
selectedPosition == -1 -> adapterPosition
selectedPosition != adapterPosition -> adapterPosition
else -> -1
}

notifyItemChanged(adapterPosition)
if (lastSelected != -1) notifyItemChanged(lastSelected)
}
}
}

interface OnTransferItemSelected {
fun onSelected(item: Message?)
fun onDelete(item: Message)
}
2 changes: 2 additions & 0 deletions android/src/main/java/org/fossasia/badgemagic/di/Modules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.fossasia.badgemagic.viewmodels.FilesViewModel
import org.fossasia.badgemagic.viewmodels.SavedClipartViewModel
import org.fossasia.badgemagic.viewmodels.SettingsViewModel
import org.fossasia.badgemagic.viewmodels.TextArtViewModel
import org.fossasia.badgemagic.viewmodels.TransferQueueViewModel
import org.koin.android.ext.koin.androidContext
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.dsl.module
Expand All @@ -28,6 +29,7 @@ val viewModelModules = module {
viewModel { DrawViewModel(get()) }
viewModel { DrawerViewModel(get()) }
viewModel { SavedClipartViewModel(get()) }
viewModel { TransferQueueViewModel() }
}

val singletonModules = module {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import org.fossasia.badgemagic.ui.fragments.SavedBadgesFragment
import org.fossasia.badgemagic.ui.fragments.SavedClipartFragment
import org.fossasia.badgemagic.ui.fragments.SettingsFragment
import org.fossasia.badgemagic.ui.fragments.TextArtFragment
import org.fossasia.badgemagic.ui.fragments.TransferFragment
import org.fossasia.badgemagic.util.SendingUtils
import org.fossasia.badgemagic.util.StorageUtils
import org.fossasia.badgemagic.viewmodels.DrawerViewModel
Expand Down Expand Up @@ -142,6 +143,12 @@ class DrawerActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedLi
switchFragment(SavedClipartFragment.newInstance())
showMenu?.setGroupVisible(R.id.saved_group, false)
}
R.id.transfer -> {
viewModel.swappingOrientation = false
setRotation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
switchFragment(TransferFragment.newInstance())
showMenu?.setGroupVisible(R.id.saved_group, false)
}
R.id.settings -> {
viewModel.swappingOrientation = false
setRotation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ import org.fossasia.badgemagic.R
import org.fossasia.badgemagic.adapter.OnSavedItemSelected
import org.fossasia.badgemagic.adapter.SaveAdapter
import org.fossasia.badgemagic.data.ConfigInfo
import org.fossasia.badgemagic.data.DataToSend
import org.fossasia.badgemagic.data.Message
import org.fossasia.badgemagic.data.Mode
import org.fossasia.badgemagic.data.Speed
import org.fossasia.badgemagic.ui.EditBadgeActivity
import org.fossasia.badgemagic.ui.base.BaseFragment
import org.fossasia.badgemagic.util.BluetoothAdapter
import org.fossasia.badgemagic.util.Converters
import org.fossasia.badgemagic.util.SendingUtils
import org.fossasia.badgemagic.viewmodels.FilesViewModel
import org.koin.android.ext.android.inject
import org.fossasia.badgemagic.viewmodels.TransferQueueViewModel
import org.koin.androidx.viewmodel.ext.android.sharedViewModel

class SavedBadgesFragment : BaseFragment() {
Expand All @@ -38,8 +37,7 @@ class SavedBadgesFragment : BaseFragment() {
private var recyclerAdapter: SaveAdapter? = null

private val viewModel by sharedViewModel<FilesViewModel>()

private val bluetoothAdapter: BluetoothAdapter by inject()
private val transferQueueViewModel by sharedViewModel<TransferQueueViewModel>()

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_main_save, container, false)
Expand Down Expand Up @@ -75,7 +73,7 @@ class SavedBadgesFragment : BaseFragment() {
}
}

override fun getSendData(): DataToSend {
fun getMessage(): Message {
val selectedItem = recyclerAdapter?.getSelectedItem()
return if (selectedItem != null) {
SendingUtils.returnMessageWithJSON(selectedItem.badgeJSON)
Expand Down Expand Up @@ -113,10 +111,9 @@ class SavedBadgesFragment : BaseFragment() {
}

override fun export(item: ConfigInfo) {
if (bluetoothAdapter.isTurnedOn(requireContext())) {
Toast.makeText(requireContext(), getString(R.string.sending_data), Toast.LENGTH_LONG).show()
SendingUtils.sendMessage(requireContext(), getSendData())
}
transferQueueViewModel.add(getMessage())
// TODO: Automatically switch to transfer screen
Toast.makeText(requireContext(), getString(R.string.prepared_for_transfer), Toast.LENGTH_LONG).show()
}

override fun onSelected(item: ConfigInfo?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import com.google.android.material.tabs.TabLayout
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale
import java.util.Timer
import java.util.TimerTask
import kotlinx.android.synthetic.main.effects_layout.*
import kotlinx.android.synthetic.main.fragment_main_textart.*
import kotlinx.android.synthetic.main.sections_tab.*
Expand All @@ -40,7 +38,6 @@ import org.fossasia.badgemagic.adapter.OnDrawableSelected
import org.fossasia.badgemagic.adapter.OnModeSelected
import org.fossasia.badgemagic.core.android.ext.hideKeyboard
import org.fossasia.badgemagic.core.android.ext.showKeyboard
import org.fossasia.badgemagic.data.DataToSend
import org.fossasia.badgemagic.data.DrawableInfo
import org.fossasia.badgemagic.data.Message
import org.fossasia.badgemagic.data.Mode
Expand All @@ -49,14 +46,13 @@ import org.fossasia.badgemagic.data.Speed
import org.fossasia.badgemagic.text.CenteredImageSpan
import org.fossasia.badgemagic.ui.base.BaseFragment
import org.fossasia.badgemagic.ui.custom.knob.Croller
import org.fossasia.badgemagic.util.BluetoothAdapter
import org.fossasia.badgemagic.util.Converters
import org.fossasia.badgemagic.util.DRAWABLE_END
import org.fossasia.badgemagic.util.DRAWABLE_START
import org.fossasia.badgemagic.util.ImageUtils
import org.fossasia.badgemagic.util.SendingUtils
import org.fossasia.badgemagic.viewmodels.TextArtViewModel
import org.koin.android.ext.android.inject
import org.fossasia.badgemagic.viewmodels.TransferQueueViewModel
import org.koin.androidx.viewmodel.ext.android.sharedViewModel
import pl.droidsonroids.gif.GifImageView

Expand All @@ -73,8 +69,7 @@ class TextArtFragment : BaseFragment() {
private val modeAdapter = ModeAdapter()

private val viewModel by sharedViewModel<TextArtViewModel>()

private val bluetoothAdapter: BluetoothAdapter by inject()
private val transferQueueViewModel by sharedViewModel<TransferQueueViewModel>()

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Expand All @@ -91,20 +86,6 @@ class TextArtFragment : BaseFragment() {
return inflater.inflate(R.layout.fragment_main_textart, container, false)
}

override fun getSendData(): DataToSend {
textViewMainText.hideKeyboard()
return SendingUtils.convertToDeviceDataModel(
Message(
Converters.convertEditableToLEDHex(textViewMainText.text.toString(), invertLED.isChecked, viewModel.getClipArts().value
?: SparseArray()),
flash.isChecked,
marquee.isChecked,
Speed.values()[speedKnob.progress.minus(1)],
Mode.values()[modeAdapter.getSelectedItemPosition()]
)
)
}

override fun initializePreview() {
setPreview()
}
Expand All @@ -118,25 +99,17 @@ class TextArtFragment : BaseFragment() {

transfer_button.setOnClickListener {
if (textViewMainText.text.trim().toString() != "") {
if (bluetoothAdapter.isTurnedOn(requireContext())) {
// Easter egg
Toast.makeText(requireContext(), getString(R.string.sending_data), Toast.LENGTH_LONG).show()

transfer_button.visibility = View.GONE
send_progress.visibility = View.VISIBLE

val buttonTimer = Timer()
buttonTimer.schedule(object : TimerTask() {
override fun run() {
activity?.runOnUiThread {
transfer_button.visibility = View.VISIBLE
send_progress.visibility = View.GONE
}
}
}, SCAN_TIMEOUT_MS)

SendingUtils.sendMessage(requireContext(), getSendData())
}
textViewMainText.hideKeyboard()
transferQueueViewModel.add(Message(
Converters.convertEditableToLEDHex(textViewMainText.text.toString(), invertLED.isChecked, viewModel.getClipArts().value
?: SparseArray()),
flash.isChecked,
marquee.isChecked,
Speed.values()[speedKnob.progress.minus(1)],
Mode.values()[modeAdapter.getSelectedItemPosition()]
))
// TODO: Automatically switch to transfer screen
Toast.makeText(requireContext(), getString(R.string.prepared_for_transfer), Toast.LENGTH_LONG).show()
} else
Toast.makeText(requireContext(), getString(R.string.empty_text_to_send), Toast.LENGTH_LONG).show()
}
Expand Down
Loading