session-android/src/org/thoughtcrime/securesms/loki/redesign/activities/HomeActivity.kt

167 lines
7.6 KiB
Kotlin
Raw Normal View History

2019-12-17 14:27:59 +01:00
package org.thoughtcrime.securesms.loki.redesign.activities
2019-12-19 16:10:11 +01:00
import android.annotation.SuppressLint
2019-12-19 11:15:58 +01:00
import android.arch.lifecycle.Observer
2019-12-19 16:10:11 +01:00
import android.content.Context
2019-12-17 15:15:13 +01:00
import android.content.Intent
2019-12-19 11:49:23 +01:00
import android.database.Cursor
2019-12-19 16:10:11 +01:00
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Paint
2019-12-17 14:27:59 +01:00
import android.os.Bundle
2019-12-19 11:49:23 +01:00
import android.support.v4.app.LoaderManager
import android.support.v4.content.Loader
2019-12-17 14:27:59 +01:00
import android.support.v7.widget.LinearLayoutManager
2019-12-19 16:10:11 +01:00
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.helper.ItemTouchHelper
2019-12-17 14:27:59 +01:00
import kotlinx.android.synthetic.main.activity_home.*
import network.loki.messenger.R
import org.thoughtcrime.securesms.ApplicationContext
import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity
2019-12-17 15:15:13 +01:00
import org.thoughtcrime.securesms.conversation.ConversationActivity
2019-12-17 14:27:59 +01:00
import org.thoughtcrime.securesms.database.DatabaseFactory
2019-12-17 15:15:13 +01:00
import org.thoughtcrime.securesms.database.model.ThreadRecord
2019-12-19 16:10:11 +01:00
import org.thoughtcrime.securesms.loki.getColorWithID
2019-12-17 15:15:13 +01:00
import org.thoughtcrime.securesms.loki.redesign.utilities.push
import org.thoughtcrime.securesms.loki.redesign.views.ConversationView
2019-12-19 11:15:58 +01:00
import org.thoughtcrime.securesms.mms.GlideApp
import org.thoughtcrime.securesms.mms.GlideRequests
2019-12-17 14:27:59 +01:00
import org.thoughtcrime.securesms.util.TextSecurePreferences
2019-12-19 16:10:11 +01:00
import kotlin.math.abs
2019-12-17 14:27:59 +01:00
2019-12-17 15:15:13 +01:00
class HomeActivity : PassphraseRequiredActionBarActivity, ConversationClickListener {
2019-12-19 11:15:58 +01:00
private lateinit var glide: GlideRequests
2019-12-17 14:27:59 +01:00
2020-01-06 04:26:52 +01:00
private val hexEncodedPublicKey: String
get() {
val masterHexEncodedPublicKey = TextSecurePreferences.getMasterHexEncodedPublicKey(this)
val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(this)
return masterHexEncodedPublicKey ?: userHexEncodedPublicKey
}
// region Lifecycle
2019-12-17 14:27:59 +01:00
constructor() : super()
override fun onCreate(savedInstanceState: Bundle?, isReady: Boolean) {
super.onCreate(savedInstanceState, isReady)
// Set content view
setContentView(R.layout.activity_home)
2020-01-07 02:00:30 +01:00
// Set custom toolbar
2020-01-06 02:07:55 +01:00
setSupportActionBar(toolbar)
2019-12-19 11:15:58 +01:00
// Set up Glide
glide = GlideApp.with(this)
2020-01-06 02:07:55 +01:00
// Set up toolbar buttons
profileButton.glide = glide
2020-01-06 04:26:52 +01:00
profileButton.hexEncodedPublicKey = hexEncodedPublicKey
2020-01-06 02:07:55 +01:00
profileButton.update()
profileButton.setOnClickListener { openSettings() }
joinPublicChatButton.setOnClickListener { joinPublicChat() }
2019-12-17 14:27:59 +01:00
// Set up recycler view
val cursor = DatabaseFactory.getThreadDatabase(this).conversationList
2019-12-19 11:15:58 +01:00
val homeAdapter = HomeAdapter(this, cursor)
homeAdapter.glide = glide
homeAdapter.conversationClickListener = this
recyclerView.adapter = homeAdapter
2019-12-17 14:27:59 +01:00
recyclerView.layoutManager = LinearLayoutManager(this)
2019-12-19 16:10:11 +01:00
ItemTouchHelper(SwipeCallback(this)).attachToRecyclerView(recyclerView)
2019-12-19 11:49:23 +01:00
// This is a workaround for the fact that CursorRecyclerViewAdapter doesn't actually auto-update (even though it says it will)
LoaderManager.getInstance(this).restartLoader(0, null, object : LoaderManager.LoaderCallbacks<Cursor> {
override fun onCreateLoader(id: Int, bundle: Bundle?): Loader<Cursor> {
return HomeLoader(this@HomeActivity)
}
override fun onLoadFinished(loader: Loader<Cursor>, cursor: Cursor?) {
homeAdapter.changeCursor(cursor)
}
override fun onLoaderReset(cursor: Loader<Cursor>) {
homeAdapter.changeCursor(null)
}
})
2019-12-17 15:15:13 +01:00
// Set up new conversation button
newConversationButton.setOnClickListener { createPrivateChat() }
2019-12-19 11:15:58 +01:00
// Set up typing observer
2019-12-19 11:49:23 +01:00
ApplicationContext.getInstance(this).typingStatusRepository.typingThreads.observe(this, Observer<Set<Long>> { threadIDs ->
val adapter = recyclerView.adapter as HomeAdapter
adapter.typingThreadIDs = threadIDs ?: setOf()
2019-12-19 11:15:58 +01:00
})
2019-12-17 14:27:59 +01:00
// Set up public chats and RSS feeds if needed
if (TextSecurePreferences.getLocalNumber(this) != null) {
val application = ApplicationContext.getInstance(this)
application.createDefaultPublicChatsIfNeeded()
application.createRSSFeedsIfNeeded()
application.lokiPublicChatManager.startPollersIfNeeded()
application.startRSSFeedPollersIfNeeded()
}
}
2019-12-17 15:15:13 +01:00
override fun onConversationClick(view: ConversationView) {
val thread = view.thread ?: return
openConversation(thread)
}
override fun onLongConversationClick(view: ConversationView) {
// TODO: Implement
}
private fun openConversation(thread: ThreadRecord) {
val intent = Intent(this, ConversationActivity::class.java)
intent.putExtra(ConversationActivity.ADDRESS_EXTRA, thread.recipient.getAddress())
intent.putExtra(ConversationActivity.THREAD_ID_EXTRA, thread.threadId)
intent.putExtra(ConversationActivity.DISTRIBUTION_TYPE_EXTRA, thread.distributionType)
intent.putExtra(ConversationActivity.TIMING_EXTRA, System.currentTimeMillis())
intent.putExtra(ConversationActivity.LAST_SEEN_EXTRA, thread.lastSeen)
intent.putExtra(ConversationActivity.STARTING_POSITION_EXTRA, -1)
push(intent)
}
2020-01-06 02:07:55 +01:00
private fun openSettings() {
2020-01-06 04:26:52 +01:00
val intent = Intent(this, SettingsActivity::class.java)
startActivity(intent)
2020-01-06 02:07:55 +01:00
}
2019-12-17 15:15:13 +01:00
private fun createPrivateChat() {
val intent = Intent(this, CreatePrivateChatActivity::class.java)
2019-12-17 15:15:13 +01:00
startActivity(intent)
}
private fun joinPublicChat() {
val intent = Intent(this, JoinPublicChatActivity::class.java)
startActivity(intent)
}
2019-12-19 16:10:11 +01:00
private class SwipeCallback(val context: Context) : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
return false
}
@SuppressLint("StaticFieldLeak")
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
// TODO: Implement
}
override fun onChildDraw(c: Canvas, recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, dx: Float, dy: Float, actionState: Int, isCurrentlyActive: Boolean) {
if (actionState != ItemTouchHelper.ACTION_STATE_SWIPE) {
super.onChildDraw(c, recyclerView, viewHolder, dx, dy, actionState, isCurrentlyActive)
} else {
val itemView = viewHolder.itemView
val alpha = 1.0f - abs(dx) / viewHolder.itemView.width.toFloat()
if (dx < 0) {
val backgroundPaint = Paint()
backgroundPaint.color = context.resources.getColorWithID(R.color.destructive, context.theme)
c.drawRect(itemView.right.toFloat() - abs(dx), itemView.top.toFloat(), itemView.right.toFloat(), itemView.bottom.toFloat(), backgroundPaint)
val icon = BitmapFactory.decodeResource(context.resources, R.drawable.ic_trash_filled_32)
val iconPaint = Paint()
val left = itemView.right.toFloat() - abs(dx) + context.resources.getDimension(R.dimen.medium_spacing)
val top = itemView.top.toFloat() + (itemView.bottom.toFloat() - itemView.top.toFloat() - icon.height) / 2
c.drawBitmap(icon, left, top, iconPaint)
}
viewHolder.itemView.alpha = alpha
viewHolder.itemView.translationX = dx
}
}
}
// endregion
2019-12-17 14:27:59 +01:00
}