Created
October 3, 2019 13:02
-
-
Save renanboni/20264bde60cc98d6c45c6786ddd366db to your computer and use it in GitHub Desktop.
chat activity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package br.com.neon.helpcenter.ui.chat | |
| import android.content.Context | |
| import android.content.Intent | |
| import android.os.Bundle | |
| import android.widget.EditText | |
| import android.widget.ImageView | |
| import androidx.constraintlayout.widget.ConstraintLayout | |
| import androidx.recyclerview.widget.LinearLayoutManager | |
| import androidx.recyclerview.widget.RecyclerView | |
| import br.com.neon.base.BaseActivity | |
| import br.com.neon.common.arch.ViewModelInterface | |
| import br.com.neon.common.arch.ViewState | |
| import br.com.neon.common.base.bind | |
| import br.com.neon.common.base.setValue | |
| import br.com.neon.common.util.extensions.setOnSafeClickListener | |
| import br.com.neon.helpcenter.R | |
| import br.com.neon.helpcenter.di.ChatModule | |
| import br.com.neon.helpcenter.di.HelpCenterInjector | |
| import br.com.neon.helpcenter.model.ChatMessage | |
| import br.com.neon.helpcenter.viewmodel.ChatViewModel | |
| import br.com.neon.helpcenter.viewmodel.ChatViewState | |
| import br.com.neon.navigation.ChatNavigation | |
| import br.com.neon.ui.NeonTextView | |
| import br.com.neon.ui.extensions.gone | |
| import zendesk.belvedere.Belvedere | |
| import zendesk.belvedere.Callback | |
| import zendesk.belvedere.MediaResult | |
| import javax.inject.Inject | |
| class ChatActivity : BaseActivity(), ViewModelInterface<ChatViewModel>, | |
| ChatAttachmentBottomSheet.OnTypeSelectedListener { | |
| @Inject | |
| override lateinit var viewModel: ChatViewModel | |
| private val rcvMessageItems: RecyclerView by bind(R.id.chat_rcv_messages) | |
| private val txtSendMessage: NeonTextView by bind(R.id.chat_txt_send) | |
| private val edtChatInput: EditText by bind(R.id.chat_edt_input) | |
| private val ctnChatInput: ConstraintLayout by bind(R.id.layout_chat_ctn_input) | |
| private val imgChatAttachment: ImageView by bind(R.id.chat_img_upload_image) | |
| private var adapter: ChatAdapter by lazy { ChatAdapter() } | |
| private lateinit var belvedere: Belvedere | |
| companion object { | |
| const val EXTRA_REQUEST_ID = "REQUEST_ID" | |
| const val EXTRA_TICKET_OPEN = "TICKET_OPEN" | |
| fun getStartIntent( | |
| context: Context, | |
| requestId: String = "", | |
| isTicketOpen: Boolean = true, | |
| shouldCreateTicket: Boolean = false, | |
| ticketDescription: String = "", | |
| ticketSubject: String = "" | |
| ): Intent = | |
| Intent(context, ChatActivity::class.java) | |
| .putExtra(EXTRA_REQUEST_ID, requestId) | |
| .putExtra(EXTRA_TICKET_OPEN, isTicketOpen) | |
| .putExtra(ChatNavigation.EXTRA_SHOULD_CREATE_NEW_TICKET, shouldCreateTicket) | |
| .putExtra(ChatNavigation.EXTRA_TICKET_DESCRIPTION, ticketDescription) | |
| .putExtra(ChatNavigation.EXTRA_TICKET_SUBJECT, ticketSubject) | |
| } | |
| override fun inject() { | |
| HelpCenterInjector.get(this).plus(ChatModule(this)).inject(this) | |
| } | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_chat) | |
| setupToolbar(R.string.chat_toolbat_title, true, this) | |
| setupView() | |
| belvedere = Belvedere.Builder(this).debug(true).build() | |
| } | |
| private fun setupView() { | |
| txtSendMessage.setOnSafeClickListener { | |
| viewModel.doNewMessageRequest(edtChatInput.text.toString()) | |
| edtChatInput.setText("") | |
| } | |
| imgChatAttachment.setOnSafeClickListener { | |
| ChatAttachmentBottomSheet.newInstance().show(supportFragmentManager) | |
| } | |
| } | |
| override fun renderState(state: ViewState?) { | |
| super.renderState(state) | |
| when (state) { | |
| is ChatViewState.OpenTicket -> renderOpenTicketMessages(state.messages) | |
| is ChatViewState.NewMessage -> renderNewMessage(state.message) | |
| is ChatViewState.ClosedTicket -> renderClosedTicketMessages(state.messages) | |
| } | |
| } | |
| private fun renderOpenTicketMessages(messageList: MutableList<ChatMessage>) { | |
| renderMessages(messageList) | |
| } | |
| private fun renderClosedTicketMessages(messageList: MutableList<ChatMessage>) { | |
| renderMessages(messageList) | |
| ctnChatInput.gone() | |
| } | |
| private fun renderNewMessage(message: ChatMessage) { | |
| adapter.addNewMessage(message) | |
| rcvMessageItems.smoothScrollToPosition(adapter.itemCount) | |
| } | |
| private fun renderMessages(messages: MutableList<ChatMessage>) { | |
| adapter.messageList = messages | |
| rcvMessageItems.adapter = adapter | |
| rcvMessageItems.layoutManager = LinearLayoutManager(this).apply { | |
| orientation = LinearLayoutManager.VERTICAL | |
| isSmoothScrollbarEnabled = true | |
| } | |
| rcvMessageItems.smoothScrollToPosition(adapter.itemCount) | |
| } | |
| override fun onCameraSelected() { | |
| belvedere | |
| .document() | |
| .contentType("application/pdf") | |
| .allowMultiple(false) | |
| .open(this) | |
| } | |
| override fun onGallerySelected() { | |
| // do nothing | |
| } | |
| override fun onDocumentSelected() { | |
| // do nothing | |
| } | |
| override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | |
| super.onActivityResult(requestCode, resultCode, data) | |
| belvedere.getFilesFromActivityOnResult(requestCode, resultCode, data, object : | |
| Callback<List<MediaResult>>() { | |
| override fun success(result: List<MediaResult>) { | |
| } | |
| }) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment