Created
July 27, 2019 21:56
-
-
Save AlexPrestonSB/230b4aca24ddea46cf07dfc48f7fc778 to your computer and use it in GitHub Desktop.
This file contains 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
class GroupChannelChatPresenterImpl @Inject constructor(private val preferenceHelper: AppPreferenceHelper) : | |
GroupChannelChatPresenter { | |
private lateinit var view: GroupChannelChatView | |
private lateinit var channelUrl: String | |
private lateinit var message: String | |
private var channel: GroupChannel? = null | |
private lateinit var context: Context | |
private var messageCollection: MessageCollection? = null | |
private val messageFilter = MessageFilter(BaseChannel.MessageTypeFilter.ALL, null, null) | |
override fun setView(view: GroupChannelChatView) { | |
this.view = view | |
} | |
override fun onResume(context: Context, channelUrl: String) { | |
this.context = context | |
val userId = preferenceHelper.getUserId() | |
this.channelUrl = channelUrl | |
SendBirdSyncManager.setup(context, userId) { | |
(context as Activity).runOnUiThread { | |
if (!SendBird.getConnectionState().equals(SendBird.ConnectionState.OPEN)) { | |
refresh() | |
} | |
createMessageCollection(channelUrl) | |
ConnectionUtil.addConnectionManagementHandler( | |
AppConstants.CONNECTION_HANDLER_ID, | |
userId, | |
object : ConnectionUtil.ConnectionManagementHandler { | |
override fun onConnected(connected: Boolean) { | |
refresh() | |
} | |
}) | |
} | |
} | |
} | |
override fun refresh() { | |
if (channel != null) { | |
channel!!.refresh { | |
if (it != null) { | |
it.printStackTrace() // ADD MORE error handling | |
return@refresh | |
} | |
//TODO update UI components | |
} | |
if (messageCollection != null) { | |
messageCollection!!.fetch(MessageCollection.Direction.NEXT, null) | |
} | |
} else { | |
createMessageCollection(channelUrl) | |
} | |
} | |
@SuppressLint("MissingPermission") //Should obviously implicitly check permissions and not assume they have been given. (Declared in manifest for test sake) | |
override fun shareLocation(context: Context) { | |
val lm = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager | |
val location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER) | |
val longitude = location.longitude | |
val latitude = location.latitude | |
val result = "$longitude,$latitude" | |
val params = UserMessageParams() | |
params.setMessage(result) | |
params.setCustomType("location") | |
if (channel != null) { | |
channel!!.sendUserMessage(params) { userMessage, e -> | |
if (e != null) { | |
view.showValidationMessage(1) | |
} else { | |
messageCollection?.appendMessage(userMessage as BaseMessage) | |
} | |
} | |
} | |
} | |
private fun createMessageCollection(channelUrl: String) { | |
if (SendBird.getConnectionState() != SendBird.ConnectionState.OPEN) { | |
MessageCollection.create(channelUrl, messageFilter, Long.MAX_VALUE) { collection, e -> | |
if (e == null) { | |
if (messageCollection == null) { | |
messageCollection = collection | |
messageCollection?.setCollectionHandler(messageCollectionHandler) | |
channel = messageCollection?.channel | |
messageCollection?.fetch(MessageCollection.Direction.PREVIOUS) { | |
if (it != null) { | |
return@fetch | |
} | |
(context as Activity).runOnUiThread() { | |
view.markAllRead() | |
} | |
} | |
} | |
} | |
} | |
} else { | |
GroupChannel.getChannel(channelUrl) { groupChannel, e -> | |
if (e == null) { | |
channel = groupChannel | |
if (messageCollection == null) { | |
messageCollection = MessageCollection(groupChannel, messageFilter, Long.MAX_VALUE) | |
messageCollection?.setCollectionHandler(messageCollectionHandler) | |
messageCollection?.fetch(MessageCollection.Direction.PREVIOUS) { | |
if (it != null) { | |
return@fetch | |
} | |
(context as Activity).runOnUiThread() { | |
view.markAllRead() | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
private val messageCollectionHandler = MessageCollectionHandler { collection, messages, action -> | |
Log.d("SyncManager", "onMessageEvent: size = " + messages.size + ", action = " + action) | |
(context as Activity).runOnUiThread() { | |
when (action) { | |
MessageEventAction.INSERT -> { | |
val title = channel!!.members[0].nickname + ", " + channel!!.members[1].nickname + "..." | |
view.displayChatTitle(title) | |
view.insert(messages) | |
} | |
MessageEventAction.REMOVE -> { | |
view.remove(messages) | |
} | |
MessageEventAction.UPDATE -> { | |
view.update(messages) | |
} | |
MessageEventAction.CLEAR -> { | |
view.clear() | |
} | |
else -> { | |
view.showValidationMessage(1) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment