Skip to content

Repository files navigation

MessageKit for Android

中文README, Download APK

About

Introduction

MessageKit is an open-source repository about chat messages, it shows how to design and develop elegant, useful layouts on displaying messages.

Pull requests to fix mistakes or improve performance are welcome.

image

Feature

  • Repository contains layouts of some staple messages, read the form below for more details.
  • Automatically scales the size of thumbnail according to the loading image.
  • The style of message draws using paint instead of 9-Patch-PNG to achieve the smaller apk size.
  • The layout has been abstracted from incoming or outgoing messages.
  • Base message model has been abstracted as IMessage to decouple data model from business.
  • The mask of the long pressing is at the foreground of the views instead of background for the best visual effects.
  • Loader for loading avatars and images is decoupled, so you can use your favourite framwork.
  • Multiple selection of message items is supported, but needs further develpoment.
  • Except using LayoutInflator, layouts implemented by Anko(Experimental) are included to avoid blocking the main thread when inflating views with reflection.
  • Using design patterns to reduce the complexity when maintaining the project.

Precautions

  • The requirements are variable, this repository does not have the feature of out-of-the-box. The migration of source code to your project is highly recommended. Also, no maven dependency is provided.
  • To fix mistakes, please read the update list regularly.
  • Further development to satisfy reqeriments after migrating code may take a long time, think twice before using in business project.
  • Please implement features base on the migrated code.

Supported types

Max Scrap on screen resolution: 1920*1080. The bigger screen, the larger max scrap.

TypeMessage NameLayout TypeMax Scrap
TextMESSAGE_TYPE_TEXTlayout_msg_text15
UrlMESSAGE_TYPE_URLlayout_msg_url10
NoticeMESSAGE_TYPE_NOTICElayout_msg_notice8
LocationMESSAGE_TYPE_LOCATIONlayout_msg_location8
ImageMESSAGE_TYPE_IMAGElayout_msg_media8
VideoMESSAGE_TYPE_VIDEOlayout_msg_media8
AudioMESSAGE_TYPE_AUDIOlayout_msg_audio14
FileMESSAGE_TYPE_FILElayout_msg_file11

Usage

Migrate the base source code to your project, includes layout resource, string resource, classes and gradle dependencies.

Model

This shows how the class implemented IMessage provides data.

publicabstractclassMessageimplementsIMessage {
/** * Message type, required. */privateStringtype;
/** * Message id, required. */privateStringmessageId;
/** * Message body, required. */privateStringbody;
/** * Message sender, required; */privateStringsender;
/** * Message comes from roomId, required. */privateStringroomId;
/** * Message timestamp, required. */privatelongtimestamp;
publicMessage(@NonNullStringtype, @NonNullStringbody) {
this.type = type;
this.body = body;
}
publicMessage(Stringtype, StringmessageId,
Stringbody, Stringsender,
StringroomId, longtimestamp) {
this.type = type;
this.messageId = messageId;
this.body = body;
this.sender = sender;
this.roomId = roomId;
this.timestamp = timestamp;
} }

View binding

Implement your view holder based on BaseViewHolder or AbstractViewHolder.

classes

BaseViewHolder extends AbstractViewHolder, contains the instance of image loader and etc.

classLocationViewHolder(itemView:View) : BaseViewHolder(itemView) {
/** * The name of the location, required.*/privateval mName:TextView= itemView.findViewById(R.id.name)
/** * The address of the location, required.*/privateval mAddress:TextView= itemView.findViewById(R.id.address)
/** * The map image of the location, optional.*/privateval mImage:ImageView= itemView.findViewById(R.id.image)
overridefunonBind(activity:Activity, message:IMessage) {
super.onBind(activity, message)
val msg = message asLocationMessage
mName.text = msg.name
mAddress.text = msg.address
mResLoader.loadImage(activity, message.image ?:"", mImage)
}
}

Use AbstractViewHolder as super class for simpler implements, refer to NoticeViewHolder for more detail.

classNoticeViewHolder(itemView:View) : AbstractViewHolder(itemView) {
/** * Notice text.*/privateval mText:TextView= itemView.notice
/** * Override as an empty implementation.*/overridefunonHolderCreated() {
}
overridefunonBind(activity:Activity, message:IMessage) {
mText.text = message.getBody()
}
}

Register views to HolderRegister.

object HolderRegister {
privateconstvalHOLDER_TEXT=2privateval sViewType =HashMap<String, Int>()
privateval sContentTypes =SparseArray<HolderConfig>()
init {
sViewType.put(Message.MESSAGE_TYPE_TEXT, HOLDER_TEXT)
val textConfig =HolderConfig(R.layout.vkit_layout_msg_text, ::TextViewHolder, 15)
sContentTypes.put(HOLDER_TEXT, textConfig)
}
}

RecyclerView

RecyclerView from android or custom class that conflicts fixed are both acceptable.

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/messageView"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@android:color/white"android:overScrollMode="never"android:scrollbars="vertical"tools:listitem="@layout/vkit_layout_msg_text"/>

Adapter

Extends and implements the abstract method of AbstractMessageAdapter<RecyclerView.ViewHolder>.

openclassMessageAdapter(privatevalmActivity:Activity,
privatevalmItemListener:IMessageItemListener,
resLoader:IMessageResLoader)
:AbstractMessageAdapter<RecyclerView.ViewHolder>() {
// Some abstract methods must to implement here.
}

Initialization

Instantiate the class which extends AbstractMessageAdapter.

  • MessageItemListener processes the action of click and long click.

  • MessageResLoader is the subclass of IMessageResLoader to load images.

classMessagesActivity : AppCompatActivity() {
privatelateinitvar mAdapter:MessageAdapter/** * LinearLayoutManager for MessageAdapter.*/privateval mLayoutManager =LinearLayoutManager(this, RecyclerView.VERTICAL, false)
overridefunonCreate(savedInstanceState:Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_message)
// MessageResLoader is a Kotlin singleton.
mAdapter =MessageAdapter(this, MessageItemListener(), MessageResLoader)
mAdapter.setHasStableIds(true)
mLayoutManager.isSmoothScrollbarEnabled =true
messageView.layoutManager = mLayoutManager
messageView.adapter = mAdapter
messageView.setHasFixedSize(true)
MessageHolders.setMaxScrap(messageView)
}
}

Refresh

Refresh UI with Adapter.notifyItemInserted.

val msg =TextMessage("Hello")
msg.setSender("Austin")
msg.setTimestamp(1548518400) // millisecond.
mAdapter.add(msg)
mAdapter.notifyDataSetChanged()

License

MIT License
Copyright (c) 2019 WenKang Tan
https://github.com/phantomVK/MessageKit/blob/master/LICENSE
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

About

Chat Messages Kit repo for Android.

Resources

Stars

22 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages