Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
5.6 kB
2
Indexable
Never
package app.keyboardly.welcome

import android.annotation.SuppressLint
import android.os.CountDownTimer
import android.os.Handler
import android.os.Looper
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import android.widget.EditText
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import app.keyboardly.dev.keyboard.utils.gone
import app.keyboardly.lib.KeyboardActionDependency
import app.keyboardly.lib.KeyboardActionView
import app.keyboardly.lib.helper.InputPresenter
import app.keyboardly.lib.helper.OnViewReady
import app.keyboardly.style.helper.clear
import app.keyboardly.style.helper.visible
import app.keyboardly.welcome.databinding.WelcomeHelloWordLayoutBinding
import app.keyboardly.welcome.repository.GetContactRepository
import app.keyboardly.welcome.utils.PreferenceHelper
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import timber.log.Timber

class WelcomeActionView(dependency: KeyboardActionDependency) :
    KeyboardActionView(dependency), InputPresenter {

    private var floatingRv: RecyclerView? = null
    private lateinit var tagsAdapter: TagsListAdapter
    private lateinit var binding: WelcomeHelloWordLayoutBinding
    private var itemList = mutableListOf<String>()

    override fun onCreate() {
        binding = WelcomeHelloWordLayoutBinding.inflate(getLayoutInflater())
        val pref = PreferenceHelper(getContext())
        binding.apply {
            if (pref.token.isNotBlank()) {
                setView(binding)
            }
            backButton.setOnClickListener {
                dependency.viewAddOnNavigation()
            }
            inputKey.setOnClickListener {
                dependency.requestInput(inputKey)
            }
            listTagsDummy()
            tagsAdapter = TagsListAdapter(
                getContext(), itemList,
                object : TagsListAdapter.TagsCallback {
                    @SuppressLint("SetTextI18n")
                    override fun onClick(data: String) {
                        floatingRv?.gone()
                        dependency.showTitleAboveList(false)
                        dependency.viewLayoutAction()
                    }
                })

            tagsList.apply {
                layoutManager = LinearLayoutManager(context)
                adapter = tagsAdapter
            }

            footerWelcome.apply {
                footerBackBtn.setOnClickListener {
                    dependency.viewAddOnNavigation()
                }
                footerSubmitButton.setOnClickListener {
                    if (pref.token.isBlank()) {
                        val name = inputKey.text.toString()
                        inputKey.error = null
                        if (name.isEmpty()) {
                            inputKey.error = "can't empty"
                        } else {
                            pref.token = name
                            setView(binding)
                        }
                    } else {
                        val repository = GetContactRepository(pref)
                        val onlyNumber = Regex("[0-9]+").matches(inputKey.text.toString())
                        if (onlyNumber) {
                            dependency.loadingMain(true)
                            CoroutineScope(Dispatchers.Main).launch {
                                repository.getTags(phoneWa = inputKey.text.toString()) {
                                    if (it.isSuccess) {
                                        dependency.loadingMain(false)
                                        val dataTags = it.getOrThrow()?.dataTags
                                        val listTags = dataTags?.tags
                                        if (listTags != null) {
                                            itemList.addAll(listTags)
                                            tagsAdapter.updateList(itemList)
                                            print("SUCCESSSSS $itemList")
                                            Log.e("TAGS", "TAGS-Number: ${dataTags.number}")
                                            Log.e("TAGS", "TAGS-tags: ${dataTags.tags}")

                                            toast("Success GetTags ${dataTags.number} !")

                                        }
                                    } else {
                                        dependency.loadingMain(false)
                                        toast("Error GetTags ${it.getOrThrow()?.message}")
                                    }
                                }
                            }
                        } else {
                            toast("Nomor tidak valid")
                        }
                    }
                }
            }
        }
        viewLayout = binding.root
    }

    private fun listTagsDummy() {
        val listA = listOf<String>("Example", "Program", "Tutorial")
        itemList.addAll(listA)
    }

    private fun setView(view: WelcomeHelloWordLayoutBinding) {
        view.apply {
            textHello.text = "Cari Kontak Profile"
            inputKey.clear()
            inputKey.hint = "Cari Kontak"
            footerWelcome.apply {
                footerSubmitButton.text = "Cari"
            }
        }
    }

    override fun onDone(text: String, editText: EditText?) {

    }


}