0% found this document useful (0 votes)
3 views

message (5)

The document is a Kotlin implementation of a MainActivity for an Android application that manages tasks. It includes functionality for adding, deleting, and displaying tasks, as well as changing icons based on task types. The activity utilizes a ViewModel for data handling and a RecyclerView for displaying the list of tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

message (5)

The document is a Kotlin implementation of a MainActivity for an Android application that manages tasks. It includes functionality for adding, deleting, and displaying tasks, as well as changing icons based on task types. The activity utilizes a ViewModel for data handling and a RecyclerView for displaying the list of tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

lass MainActivity : AppCompatActivity() {

private val binding: MainActivityBinding by lazy


{ MainActivityBinding.inflate(layoutInflater) }
val binding2: ItemViewBinding by lazy { ItemViewBinding.inflate(layoutInflater)
}
private var count=0
private var typeTask: TypeTask=TypeTask.HOME

private val viewModel: MainViewModel by viewModels {


MainViewModel.Factory
}

private val listAdapter: MainListAdapter by lazy {


MainListAdapter(
onChangeIconEndTask = { task -> changeIconEndTask(task) },
)
}

private fun changeIconStartTask(layoutTask: TextInputLayout) {


layoutTask.setStartIconDrawable(
when (count){
0 ->{
++count
R.drawable.work_outline_24
}
1->{
count++
R.drawable.leisure_24
}
2->{
count++
R.drawable.other_24
}
else->{
count=0
R.drawable.person_outline_24
}
}
)
}

private fun changeIconEndTask(task: Task) {


val dialog: Dialog = MaterialAlertDialogBuilder(this)
.setTitle(getString(R.string.delete_confirmation))
.setMessage(getString(R.string.are_you_sure_you_want_to_delete,
task.text))
.setPositiveButton("Accept") { dialog, which ->
viewModel.deleteTask(task)
}
.setNegativeButton("Cancel") { _, _ -> }
.create()

dialog.show()
}

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(binding.root)
ViewCompat.setOnApplyWindowInsetsListener(binding.main) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right,
systemBars.bottom)
insets
}

setupViews()
observeViewModel()
}

private fun observeViewModel() {


lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.tasks.collect { taskList ->
showList(taskList)
}
}
}
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
launch {
viewModel.messages.collect { messages ->
messages.firstOrNull()?.let { uiMessage ->
showUiMessage(uiMessage) }
}
}
}
}
}

private fun showUiMessage(uiMessage: UiMessage) {


Snackbar.make(
binding.root,
uiMessage.text,
Snackbar.LENGTH_LONG
).apply {
if (uiMessage.actionText != null && uiMessage.action != null) {
setAction(uiMessage.actionText) { uiMessage.action.invoke() }
}
}.addCallback(object : BaseTransientBottomBar.BaseCallback<Snackbar>() {
override fun onDismissed(transientBottomBar: Snackbar?, event: Int) {
viewModel.onUiMessageShown(uiMessage)
}

override fun onShown(transientBottomBar: Snackbar?) {


super.onShown(transientBottomBar)
}
}).show()
}

private fun showList(taskLists: List<Task>) {


listAdapter.submitList(taskLists)
binding.emptyView.isVisible = taskLists.isEmpty()
}
private fun setupViews() {
with(binding) {
with(listTasks) {
setHasFixedSize(true)
layoutManager = LinearLayoutManager(context)
adapter = listAdapter
itemAnimator = DefaultItemAnimator()
addItemDecoration(DividerItemDecoration(context,
RecyclerView.VERTICAL))
}

sendImage.setOnClickListener {
comprobateAndAddTask(inputTask.text.toString())
}
inputTask.setOnEditorActionListener { view, actionId, event ->
comprobateAndAddTask(inputTask.text.toString())
true
}

layoutTask.setStartIconOnClickListener
{ changeIconStartTask(layoutTask) }
}
}

private fun comprobateAndAddTask(text: String) {


if (text.isNotBlank() && text.isNotEmpty()) {
when (count) {
0 -> viewModel.addTask(TypeTask.HOME, text)
1 -> viewModel.addTask(TypeTask.WORK, text)
2 -> viewModel.addTask(TypeTask.LEISURE, text)
else -> viewModel.addTask(TypeTask.OTHER, text)
}
}
}
}

You might also like