Guest User

InstrumentFragment

a guest
Aug 9th, 2021
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 8.23 KB | None | 0 0
  1. class InstrumentFragment : Fragment(), InstrumentView {
  2.  
  3.     private var ecosystemRepository: EcosystemRepository? = null
  4.  
  5.     private lateinit var noteTitleTable: TableLayout
  6.     private lateinit var instrumentTable: TableLayout
  7.     private lateinit var velocityTable: TableLayout
  8.  
  9.     private lateinit var noteScroll: HorizontalScrollView
  10.     private lateinit var velocityScroll: HorizontalScrollView
  11.     private lateinit var velocityTableRow: TableRow
  12.  
  13.     private val checkTable = ArrayList<ArrayList<CheckBox>>()
  14.     private var instrumentScreen: InstrumentScreen? = null
  15.  
  16.  
  17.     @RequiresApi(Build.VERSION_CODES.M)
  18.     override fun onCreateView(
  19.         inflater: LayoutInflater, container: ViewGroup?,
  20.         savedInstanceState: Bundle?
  21.     ): View? {
  22.  
  23.         return inflater.inflate(R.layout.fragment_instrument, container, false)
  24.     }
  25.  
  26.     @RequiresApi(Build.VERSION_CODES.M)
  27.     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  28.         super.onViewCreated(view, savedInstanceState)
  29.         if (instrumentScreen != null) {
  30.             initView()
  31.             showTable()
  32.         }
  33.     }
  34.    
  35.     companion object {
  36.         fun newInstance() = InstrumentFragment()
  37.     }
  38.  
  39.     fun setInstrument(instrumentScreen: InstrumentScreen) {
  40.         this.instrumentScreen = instrumentScreen
  41.     }
  42.  
  43.     fun getInstrument() = instrumentScreen
  44.  
  45.     @RequiresApi(Build.VERSION_CODES.M)
  46.     private fun initView() {
  47.  
  48.         noteTitleTable = requireView().findViewById(R.id.note_table)
  49.         instrumentTable = requireView().findViewById(R.id.instrument_table)
  50.         velocityTable = requireView().findViewById(R.id.velocity_table)
  51.  
  52.         noteScroll = requireView().findViewById(R.id.note_scroll)
  53.         velocityScroll = requireView().findViewById(R.id.velocity_scroll)
  54.  
  55.         noteScroll.setOnScrollChangeListener { _, i, _, _, _ ->
  56.             velocityScroll.scrollTo(i, 0)
  57.         }
  58.  
  59.         velocityScroll.setOnScrollChangeListener { _, i, _, _, _ ->
  60.             noteScroll.scrollTo(i, 0)
  61.         }
  62.  
  63.  
  64.         for (item in instrumentScreen!!.notes) {
  65.             val checkBoxes = ArrayList<CheckBox>()
  66.             for (i in 0 until item.size) {
  67.                 val checkBox = CheckBox(requireContext())
  68.                 checkBoxes.add(checkBox)
  69.             }
  70.             checkTable.add(checkBoxes)
  71.  
  72.         }
  73.  
  74.     }
  75.  
  76.     @SuppressLint("UseCompatLoadingForDrawables", "ResourceType")
  77.     @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
  78.     override fun showTable() {
  79.         for (i in 0 until instrumentScreen!!.notes.size) {
  80.             val tableRow = TableRow(requireContext())
  81.  
  82.             if (instrumentScreen!!.notesTitle != null) {
  83.                 val noteRow = TableRow(requireContext())
  84.                 val noteTitle = TextView(requireContext())
  85.                 noteTitle.setTextColor(Color.WHITE)
  86.                 noteTitle.background = requireContext().getDrawable(R.drawable.piano_note_title_bg)
  87.                 noteTitle.gravity = Gravity.CENTER
  88.                 noteTitle.textSize = 13.3F
  89.                 noteTitle.text = instrumentScreen!!.notesTitle!![i]
  90.                 noteRow.addView(noteTitle)
  91.                 noteTitleTable.addView(noteRow, i)
  92.             }
  93.  
  94.             if (instrumentScreen!!.name == DRUM) {
  95.                 tableRow.setPadding(15)
  96.             }
  97.  
  98.             for (j in 0 until instrumentScreen!!.notes[i].size) {
  99.                 if (instrumentScreen!!.notes[i][j].isChecked) {
  100.                     checkTable[i][j].isChecked = true
  101.                 }
  102.  
  103.                 checkTable[i][j].setOnCheckedChangeListener { _, b ->
  104.                     ecosystemRepository?.logEventByName(MANAGER)
  105.                     if (b) {
  106.  
  107.                         instrumentScreen!!.notes[i][j].isChecked = true
  108.                         check(b)
  109.                             .subscribeOn(Schedulers.io())
  110.                             .subscribe({
  111.                                 instrumentScreen!!.notes[i][j].play(instrumentScreen!!.volume)
  112.                                 velocityTableRow.getChildAt(j).visibility = View.VISIBLE
  113.                             }, {
  114.                             })
  115.                     } else {
  116.                         instrumentScreen!!.notes[i][j].isChecked = false
  117.                     }
  118.                 }
  119.                 checkTable[i][j].buttonDrawable =
  120.                     requireContext().getDrawable(instrumentScreen!!.notes[i][j].btn)
  121.                 if (instrumentScreen!!.name == DRUM) {
  122.                     if (j % 8 == 0 || j % 8 == 1 || j % 8 == 2 || j % 8 == 3) {
  123.                         checkTable[i][j].setBackgroundColor(Color.parseColor("#29FF8E00"))
  124.                     } else {
  125.                         checkTable[i][j].setBackgroundColor(Color.parseColor("#000000"))
  126.                     }
  127.  
  128.                     checkTable[i][j].setPadding(15)
  129.                 } else {
  130.                     instrumentTable.setBackgroundColor(Color.parseColor("#2915FF74"))
  131.                     if (j % 8 == 0 || j % 8 == 1 || j % 8 == 2 || j % 8 == 3) {
  132.                         checkTable[i][j].buttonDrawable =
  133.                             requireContext().getDrawable(R.drawable.note_btn)
  134.                     } else {
  135.                         checkTable[i][j].buttonDrawable =
  136.                             requireContext().getDrawable(R.drawable.note_btn_four)
  137.                     }
  138.                 }
  139.                 if (checkTable[i][j].parent != null) {
  140.                     ((checkTable[i][j].parent) as ViewGroup).removeView(checkTable[i][j])
  141.                 }
  142.                 tableRow.addView(checkTable[i][j], j)
  143.             }
  144.  
  145.             if (tableRow.parent != null) {
  146.                 ((tableRow.parent) as ViewGroup).removeView(tableRow)
  147.             }
  148.             instrumentTable.addView(tableRow, i)
  149.  
  150.         }
  151.  
  152.  
  153.         velocityTableRow = TableRow(requireContext())
  154.  
  155.         for (i in 0 until instrumentScreen!!.notes[0].size) {
  156.             val velocityConstraint = RelativeLayout(requireContext())
  157.             val velocityItem =
  158.                 if (instrumentScreen!!.name == PIANO) LayoutInflater.from(requireContext())
  159.                     .inflate(R.layout.velocity_item_layout, velocityConstraint, false)
  160.                 else LayoutInflater.from(requireContext())
  161.                     .inflate(R.layout.drum_velocity_item_layout, velocityConstraint, false)
  162.  
  163.             val seekBar: SeekBar = velocityItem.findViewById(R.id.velocity_sb)
  164.             seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
  165.                 override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
  166.                     for (noteRow in instrumentScreen!!.notes) {
  167.  
  168.                         for ((index, note) in noteRow.withIndex()) {
  169.                             if (index == i) {
  170.                                 note.volume = p1
  171.                             }
  172.                         }
  173.                     }
  174.                 }
  175.  
  176.                 override fun onStartTrackingTouch(p0: SeekBar?) {
  177.  
  178.                 }
  179.  
  180.                 override fun onStopTrackingTouch(p0: SeekBar?) {
  181.  
  182.                 }
  183.             })
  184.  
  185.  
  186.             velocityConstraint.addView(velocityItem)
  187.             velocityTableRow.addView(velocityConstraint, i)
  188.         }
  189.  
  190.         val params = FrameLayout.LayoutParams(
  191.             FrameLayout.LayoutParams.MATCH_PARENT,
  192.             FrameLayout.LayoutParams.WRAP_CONTENT
  193.         )
  194.  
  195.         params.gravity = Gravity.BOTTOM
  196.         if (instrumentScreen!!.name == PIANO) {
  197.             val r: Resources = resources
  198.             val px = TypedValue.applyDimension(
  199.                 TypedValue.COMPLEX_UNIT_DIP,
  200.                 37F,
  201.                 r.displayMetrics
  202.             )
  203.             params.marginStart = px.toInt()
  204.         }
  205.         velocityScroll.layoutParams = params
  206.         velocityTable.addView(velocityTableRow)
  207.     }
  208.  
  209.     fun resetTable() {
  210.         for (row in checkTable) {
  211.             for (check in row) {
  212.                 check.isChecked = false
  213.             }
  214.         }
  215.     }
  216.  
  217.     private fun check(boolean: Boolean): Observable<Boolean> {
  218.         return Observable.just(boolean)
  219.     }
  220.  
  221.     fun setVelocity(flag: Boolean) {
  222.         velocityScroll.visibility = if (flag) View.VISIBLE else View.GONE
  223.     }
  224. }
Add Comment
Please, Sign In to add comment