Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class GroupChannelChatAdapter(context: Context, listener: OnItemClickListener) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private var messages: MutableList<BaseMessage>
private var context: Context
private var listener: OnItemClickListener
init {
messages = ArrayList()
this.context = context
this.listener = listener
}
fun insert(messageList: MutableList<BaseMessage>) {
for (message in messageList) {
val index = SyncManagerUtil.findIndexOfMessage(messages, message)
this.messages.add(index, message)
notifyItemInserted(index)
}
}
fun update(messageList: MutableList<BaseMessage>) {
for (message in messageList) {
val index = SyncManagerUtil.findIndexOfMessage(messages, message)
if (index != -1) {
this.messages.add(index, message)
notifyItemChanged(index)
}
}
}
fun remove(messageList: MutableList<BaseMessage>) {
for (message in messageList) {
val index = SyncManagerUtil.findIndexOfMessage(messages, message)
if (index != -1) {
this.messages.removeAt(index)
notifyItemRemoved(index)
}
}
}
fun clear() {
messages.clear()
notifyDataSetChanged()
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): RecyclerView.ViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
when (viewType) {
AppConstants.VIEW_TYPE_LOCATION_ME -> {
return MyLocationHolder(layoutInflater.inflate(R.layout.item_gchat_map_me, parent, false))
}
AppConstants.VIEW_TYPE_LOCATION_OTHER -> {
return OtherLocationHolder(layoutInflater.inflate(R.layout.item_gchat_map_other, parent, false))
}
}
}
override fun getItemViewType(position: Int): Int {
val message = messages.get(position)
when (message) {
is UserMessage -> {
return if (message.sender.userId == SendBird.getCurrentUser().userId) {
if (message.customType == "location") {
AppConstants.VIEW_TYPE_LOCATION_ME
}
} else {
if (message.customType == "location") {
AppConstants.VIEW_TYPE_LOCATION_OTHER
}
}
}
else -> return -1
}
}
override fun getItemCount() = messages.size
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val message = messages.get(position)
var isNewDay = false
when {
(position < messages.size - 1) -> {
val previousMessage = messages.get(position + 1)
isNewDay = !DateUtil.isSameDay(message.createdAt, previousMessage.createdAt)
}
(position == messages.size - 1) -> {
isNewDay = true
}
}
when (holder.itemViewType) {
AppConstants.VIEW_TYPE_LOCATION_ME -> {
holder as MyLocationHolder
holder.bindView(context, messages.get(position) as UserMessage, isNewDay)
}
AppConstants.VIEW_TYPE_LOCATION_OTHER -> {
holder as OtherLocationHolder
holder.bindView(context, messages.get(position) as UserMessage, isNewDay)
}
}
}
}
itemView.setOnClickListener {
listener.onFileMessageClicked(message)
}
}
}
class MyLocationHolder(view: View) : RecyclerView.ViewHolder(view), OnMapReadyCallback {
private lateinit var map: GoogleMap
private lateinit var context: Context
private lateinit var latLng: LatLng
private var longitude = 0.0
private var latitude = 0.0
private val mapView = view.map_gchat_me
private val date = view.text_gchat_map_date_me
private val timestamp = view.text_gchat_map_timestamp_me
/** Initialises the MapView by calling its lifecycle methods */
init {
with(mapView) {
// Initialise the MapView
onCreate(null)
// Set the map ready callback to receive the GoogleMap object
getMapAsync(this@MyLocationHolder)
}
}
fun bindView(context: Context, message: UserMessage, isNewDay: Boolean) {
this.context = context
timestamp.text = DateUtil.formatTime(message.createdAt)
val cords = message.message.split(",")
longitude = cords[0].toDouble()
latitude = cords[1].toDouble()
latLng = LatLng(latitude, longitude)
if (isNewDay) {
date.visibility = View.VISIBLE
date.text = DateUtil.formatDate(message.createdAt)
} else {
date.visibility = View.GONE
}
setLocation()
}
override fun onMapReady(googleMap: GoogleMap?) {
MapsInitializer.initialize(context.applicationContext)
map = googleMap ?: return
setLocation()
}
private fun setLocation() {
if (!::map.isInitialized) return
with(map) {
moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 11f))
addMarker(MarkerOptions().position(latLng))
mapType = GoogleMap.MAP_TYPE_NORMAL
}
mapView.onResume()
}
}
class OtherLocationHolder(view: View) : RecyclerView.ViewHolder(view), OnMapReadyCallback {
private lateinit var map: GoogleMap
private lateinit var context: Context
private lateinit var latLng: LatLng
private var longitude = 0.0
private var latitude = 0.0
private val mapView = view.map_gchat_other
private val date = view.text_gchat_map_date_other
private val timestamp = view.text_gchat_map_timestamp_other
private val profileImage = view.image_gchat_map_profile_other
private val username = view.text_gchat_map_user_other
/** Initialises the MapView by calling its lifecycle methods */
init {
with(mapView) {
// Initialise the MapView
onCreate(null)
// Set the map ready callback to receive the GoogleMap object
getMapAsync(this@OtherLocationHolder)
}
}
fun bindView(context: Context, message: UserMessage, isNewDay: Boolean) {
this.context = context
val cords = message.message.split(",")
longitude = cords[0].toDouble()
latitude = cords[1].toDouble()
latLng = LatLng(latitude, longitude)
username.text = message.sender.nickname
timestamp.text = DateUtil.formatTime(message.createdAt)
Glide.with(context).load(message.sender.profileUrl).apply(RequestOptions().override(75, 75))
.into(profileImage)
if (isNewDay) {
date.visibility = View.VISIBLE
date.text = DateUtil.formatDate(message.createdAt)
} else {
date.visibility = View.GONE
}
setLocation()
}
override fun onMapReady(googleMap: GoogleMap?) {
MapsInitializer.initialize(context.applicationContext)
map = googleMap ?: return
setLocation()
}
private fun setLocation() {
if (!::map.isInitialized) return
with(map) {
moveCamera(com.google.android.gms.maps.CameraUpdateFactory.newLatLngZoom(latLng, 11f))
addMarker(com.google.android.gms.maps.model.MarkerOptions().position(latLng))
mapType = com.google.android.gms.maps.GoogleMap.MAP_TYPE_NORMAL
}
mapView.onResume()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.