Bluetooth module | #5

Extensions.kt

import android.widget.ImageButton
import androidx.core.graphics.drawable.DrawableCompat
import androidx.fragment.app.Fragment

fun Fragment.changeButtonColor(button: ImageButton, color: Int){
val drawable = button.drawable
DrawableCompat.setTint(drawable, color)
button.setImageDrawable(drawable)
}

BaseActivity.kt

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class BaseActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_base)
// initRcView()
supportFragmentManager.beginTransaction()
.replace(R.id.placeHolder, DeviceListFragment()).commit()
}

private fun initRcView(){
val rcView = findViewById<RecyclerView>(R.id.rcViewPaired)
rcView.layoutManager = LinearLayoutManager(this)
val adapter = ItemAdapter()
rcView.adapter = adapter
adapter.submitList(createDeviceList())
}

private fun createDeviceList(): List<ListItem>{
val list = ArrayList<ListItem>()

for (i in 0 until 5){
list.add(
ListItem(
"Device $i",
"34:56:89:56"
)
)
}
return list
}
}

DeviceListFragment.kt

import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothManager
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.neco_dev.bt_def.databinding.FragmentListBinding

class DeviceListFragment : Fragment() {
private var bAdapter: BluetoothAdapter? = null
private lateinit var binding: FragmentListBinding

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentListBinding.inflate(inflater, container, false)
return binding.root
}

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

private fun initBtAdapter(){
val bManager = activity?.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
bAdapter = bManager.adapter
}

private fun bluetoothState(){
if (bAdapter?.isEnabled == true){
changeButtonColor(binding.imBluetoothOn, Color.GREEN)
}
}

}

Добавить комментарий

Ваш адрес email не будет опубликован.