Skip to content

Commit

Permalink
Fixed using ContactsDao.getAllContactsWithPgpLD().| #1188
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 committed Oct 25, 2021
1 parent 21ca549 commit ae23630
Show file tree
Hide file tree
Showing 10 changed files with 1,060 additions and 63 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import android.database.Cursor
import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Query
import androidx.room.Transaction
import com.flowcrypt.email.database.entity.ContactEntity
import com.flowcrypt.email.database.entity.relation.RecipientWithPubKeys

/**
* This object describes a logic of work with [ContactEntity].
Expand All @@ -31,12 +29,8 @@ interface ContactsDao : BaseDao<ContactEntity> {
@Query("SELECT * FROM contacts")
fun getAllContactsLD(): LiveData<List<ContactEntity>>

@Transaction
@Query("SELECT * FROM contacts WHERE email IN (SELECT recipient FROM public_keys GROUP BY recipient)")
fun getAllContactsWithPgpLD1(): LiveData<List<RecipientWithPubKeys>>

@Query("SELECT * FROM contacts")
//@Query("SELECT * FROM contacts WHERE has_pgp = 1")
//fixed
@Query("SELECT contacts.* FROM contacts INNER JOIN public_keys ON contacts.email = public_keys.recipient GROUP BY contacts.email ORDER BY contacts._id")
fun getAllContactsWithPgpLD(): LiveData<List<ContactEntity>>

@Query("SELECT * FROM contacts")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ class PublicKeyEntity(
requireNotNull(parcel.createByteArray())
)


override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeValue(id)
parcel.writeString(recipient)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
package com.flowcrypt.email.jetpack.viewmodel

import android.app.Application
import android.content.Context
import android.widget.Toast
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.liveData
import androidx.lifecycle.viewModelScope
import com.flowcrypt.email.R
import com.flowcrypt.email.api.retrofit.ApiRepository
import com.flowcrypt.email.api.retrofit.FlowcryptApiRepository
import com.flowcrypt.email.api.retrofit.response.attester.PubResponse
Expand Down Expand Up @@ -241,7 +244,16 @@ class ContactsViewModel(application: Application) : AccountViewModel(application
viewModelScope.launch {
val contact = roomDatabase.contactsDao().getContactByEmailSuspend(pgpContact.email)
if (contact == null) {
roomDatabase.contactsDao().insertSuspend(pgpContact.toContactEntity())
val isInserted = roomDatabase.contactsDao().insertSuspend(pgpContact.toContactEntity()) > 0
if (isInserted) {
roomDatabase.pubKeysDao().insertSuspend(pgpContact.toPubKey())
} else {
val context: Context = getApplication()
Toast.makeText(
context,
context.getString(R.string.could_not_save_new_recipient), Toast.LENGTH_LONG
).show()
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package com.flowcrypt.email.model
import android.os.Parcel
import android.os.Parcelable
import com.flowcrypt.email.database.entity.ContactEntity
import com.flowcrypt.email.database.entity.PublicKeyEntity
import com.flowcrypt.email.security.model.PgpKeyDetails
import java.util.ArrayList
import javax.mail.internet.AddressException
Expand Down Expand Up @@ -66,6 +67,14 @@ data class PgpContact constructor(
)
}

fun toPubKey(): PublicKeyEntity {
return PublicKeyEntity(
recipient = email,
fingerprint = fingerprint!!,
publicKey = pubkey!!.toByteArray()
)
}

companion object {
@JvmField
val CREATOR: Parcelable.Creator<PgpContact> = object : Parcelable.Creator<PgpContact> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package com.flowcrypt.email.ui.activity

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
Expand All @@ -22,7 +23,6 @@ import androidx.test.espresso.idling.CountingIdlingResource
import com.flowcrypt.email.R
import com.flowcrypt.email.api.retrofit.response.base.Result
import com.flowcrypt.email.database.entity.ContactEntity
import com.flowcrypt.email.database.entity.relation.RecipientWithPubKeys
import com.flowcrypt.email.extensions.decrementSafely
import com.flowcrypt.email.extensions.incrementSafely
import com.flowcrypt.email.jetpack.viewmodel.ContactsViewModel
Expand All @@ -41,7 +41,7 @@ import com.flowcrypt.email.util.UIUtil
* E-mail: [email protected]
*/
class SelectContactsActivity : BaseBackStackActivity(),
ContactsRecyclerViewAdapter.OnContactClickListener, SearchView.OnQueryTextListener {
ContactsRecyclerViewAdapter.OnContactActionsListener, SearchView.OnQueryTextListener {

private var progressBar: View? = null
private var recyclerViewContacts: RecyclerView? = null
Expand All @@ -65,7 +65,7 @@ class SelectContactsActivity : BaseBackStackActivity(),

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
contactsRecyclerViewAdapter.onContactClickListener = this
contactsRecyclerViewAdapter.onContactActionsListener = this
//todo-denbond7 need to fix this in the future. Not urgent
//val isMultiply = intent.getBooleanExtra(KEY_EXTRA_IS_MULTIPLY, false)

Expand Down Expand Up @@ -108,12 +108,14 @@ class SelectContactsActivity : BaseBackStackActivity(),
return super.onPrepareOptionsMenu(menu)
}

/*override fun onContactClick(contactEntity: ContactEntity) {
override fun onContactClick(contactEntity: ContactEntity) {
val intent = Intent()
intent.putExtra(KEY_EXTRA_PGP_CONTACT, contactEntity)
setResult(Activity.RESULT_OK, intent)
finish()
}*/
}

override fun onDeleteContact(contactEntity: ContactEntity) {}

override fun onQueryTextSubmit(query: String): Boolean {
searchPattern = query
Expand Down Expand Up @@ -180,8 +182,4 @@ class SelectContactsActivity : BaseBackStackActivity(),
return intent
}
}

override fun onContactClick(contactWithPubKeys: RecipientWithPubKeys) {
TODO("Not yet implemented")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.flowcrypt.email.R
import com.flowcrypt.email.api.retrofit.response.base.Result
import com.flowcrypt.email.database.entity.relation.RecipientWithPubKeys
import com.flowcrypt.email.database.entity.ContactEntity
import com.flowcrypt.email.extensions.navController
import com.flowcrypt.email.jetpack.viewmodel.ContactsViewModel
import com.flowcrypt.email.ui.activity.ImportPgpContactActivity
import com.flowcrypt.email.ui.activity.fragment.base.BaseFragment
Expand All @@ -32,8 +33,7 @@ import com.flowcrypt.email.util.UIUtil
* Time: 6:11 PM
* E-mail: [email protected]
*/
class ContactsListFragment : BaseFragment(), ContactsRecyclerViewAdapter.OnDeleteContactListener,
ContactsRecyclerViewAdapter.OnContactClickListener {
class ContactsListFragment : BaseFragment(), ContactsRecyclerViewAdapter.OnContactActionsListener {

private var progressBar: View? = null
private var recyclerViewContacts: RecyclerView? = null
Expand All @@ -46,8 +46,7 @@ class ContactsListFragment : BaseFragment(), ContactsRecyclerViewAdapter.OnDelet

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
contactsRecyclerViewAdapter.onDeleteContactListener = this
contactsRecyclerViewAdapter.onContactClickListener = this
contactsRecyclerViewAdapter.onContactActionsListener = this
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Expand All @@ -71,20 +70,20 @@ class ContactsListFragment : BaseFragment(), ContactsRecyclerViewAdapter.OnDelet
}
}

/*override fun onContactClick(contactEntity: ContactEntity) {
override fun onContactClick(contactEntity: ContactEntity) {
navController?.navigate(
ContactsListFragmentDirections
.actionContactsListFragmentToPublicKeyDetailsFragment(contactEntity)
)
}*/
}

/*override fun onDeleteContact(contactEntity: ContactEntity) {
override fun onDeleteContact(contactEntity: ContactEntity) {
contactsViewModel.deleteContact(contactEntity)
Toast.makeText(
context, getString(R.string.the_contact_was_deleted, contactEntity.email),
Toast.LENGTH_SHORT
).show()
}*/
}

private fun initViews(root: View) {
this.progressBar = root.findViewById(R.id.progressBar)
Expand Down Expand Up @@ -122,7 +121,7 @@ class ContactsListFragment : BaseFragment(), ContactsRecyclerViewAdapter.OnDelet
if (it.data.isNullOrEmpty()) {
UIUtil.exchangeViewVisibility(true, emptyView, recyclerViewContacts)
} else {
//contactsRecyclerViewAdapter.swap(it.data)
contactsRecyclerViewAdapter.swap(it.data)
UIUtil.exchangeViewVisibility(false, emptyView, recyclerViewContacts)
}
}
Expand All @@ -136,12 +135,4 @@ class ContactsListFragment : BaseFragment(), ContactsRecyclerViewAdapter.OnDelet
companion object {
private const val REQUEST_CODE_START_IMPORT_PUB_KEY_ACTIVITY = 0
}

override fun onContactClick(contactWithPubKeys: RecipientWithPubKeys) {
TODO("Not yet implemented")
}

override fun onDeleteContact(contactWithPubKeys: RecipientWithPubKeys) {
TODO("Not yet implemented")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,12 @@ class PreviewImportPgpContactFragment : BaseFragment(), View.OnClickListener,

emails.add(keyOwner)

/*if (weakRef.get() != null) {
val contact = FlowCryptRoomDatabase.getDatabase(weakRef.get()?.requireContext()!!)
.contactsDao().getContactByEmail(keyOwner)?.toPgpContact()
if (weakRef.get() != null) {
//todo-denbond7 fix me
val contact = null/*FlowCryptRoomDatabase.getDatabase(weakRef.get()?.requireContext()!!)
.contactsDao().getContactByEmail(keyOwner)?.toPgpContact()*/
return PublicKeyInfo(fingerprint, keyOwner, contact, pgpKeyDetails.publicKey)
}*/
}
}
return null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.flowcrypt.email.R
import com.flowcrypt.email.database.entity.relation.RecipientWithPubKeys
import com.flowcrypt.email.database.entity.ContactEntity

/**
* This adapter describes logic to prepare show contacts from the database.
Expand All @@ -26,10 +26,8 @@ import com.flowcrypt.email.database.entity.relation.RecipientWithPubKeys
class ContactsRecyclerViewAdapter constructor(private val isDeleteEnabled: Boolean = true) :
RecyclerView.Adapter<ContactsRecyclerViewAdapter.ViewHolder>() {

private val list: MutableList<RecipientWithPubKeys> = mutableListOf()
var onDeleteContactListener: OnDeleteContactListener? = null
var onContactClickListener: OnContactClickListener? = null

private val list: MutableList<ContactEntity> = mutableListOf()
var onContactActionsListener: OnContactActionsListener? = null

override fun onCreateViewHolder(
parent: ViewGroup,
Expand All @@ -41,45 +39,45 @@ class ContactsRecyclerViewAdapter constructor(private val isDeleteEnabled: Boole
}

override fun onBindViewHolder(viewHolder: ContactsRecyclerViewAdapter.ViewHolder, position: Int) {
val contactWithPubKeys = list[position]
val contactEntity = list[position]

if (contactWithPubKeys.contact.name.isNullOrEmpty()) {
if (contactEntity.name.isNullOrEmpty()) {
viewHolder.textViewName.visibility = View.GONE
viewHolder.textViewEmail.visibility = View.GONE
viewHolder.textViewOnlyEmail.visibility = View.VISIBLE

viewHolder.textViewOnlyEmail.text = contactWithPubKeys.contact.email
viewHolder.textViewOnlyEmail.text = contactEntity.email
viewHolder.textViewEmail.text = null
viewHolder.textViewName.text = null
} else {
viewHolder.textViewName.visibility = View.VISIBLE
viewHolder.textViewEmail.visibility = View.VISIBLE
viewHolder.textViewOnlyEmail.visibility = View.GONE

viewHolder.textViewEmail.text = contactWithPubKeys.contact.email
viewHolder.textViewName.text = contactWithPubKeys.contact.name
viewHolder.textViewEmail.text = contactEntity.email
viewHolder.textViewName.text = contactEntity.name
viewHolder.textViewOnlyEmail.text = null
}

if (isDeleteEnabled) {
viewHolder.imageButtonDeleteContact.visibility = View.VISIBLE
viewHolder.imageButtonDeleteContact.setOnClickListener {
onDeleteContactListener?.onDeleteContact(contactWithPubKeys)
onContactActionsListener?.onDeleteContact(contactEntity)
}
} else {
viewHolder.imageButtonDeleteContact.visibility = View.GONE
}

viewHolder.itemView.setOnClickListener {
onContactClickListener?.onContactClick(contactWithPubKeys)
onContactActionsListener?.onContactClick(contactEntity)
}
}

override fun getItemCount(): Int {
return list.size
}

fun swap(newList: List<RecipientWithPubKeys>) {
fun swap(newList: List<ContactEntity>) {
val diffUtilCallback = DiffUtilCallback(this.list, newList)
val productDiffResult = DiffUtil.calculateDiff(diffUtilCallback)

Expand All @@ -88,12 +86,9 @@ class ContactsRecyclerViewAdapter constructor(private val isDeleteEnabled: Boole
productDiffResult.dispatchUpdatesTo(this)
}

interface OnDeleteContactListener {
fun onDeleteContact(contactWithPubKeys: RecipientWithPubKeys)
}

interface OnContactClickListener {
fun onContactClick(contactWithPubKeys: RecipientWithPubKeys)
interface OnContactActionsListener {
fun onDeleteContact(contactEntity: ContactEntity)
fun onContactClick(contactEntity: ContactEntity)
}

/**
Expand All @@ -107,13 +102,13 @@ class ContactsRecyclerViewAdapter constructor(private val isDeleteEnabled: Boole
}

inner class DiffUtilCallback(
private val oldList: List<RecipientWithPubKeys>,
private val newList: List<RecipientWithPubKeys>
private val oldList: List<ContactEntity>,
private val newList: List<ContactEntity>
) : DiffUtil.Callback() {
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
val oldItem = oldList[oldItemPosition]
val newItem = newList[newItemPosition]
return oldItem.publicKeys == newItem.publicKeys
return oldItem.email == newItem.email
}

override fun getOldListSize(): Int = oldList.size
Expand Down
1 change: 1 addition & 0 deletions FlowCrypt/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -535,4 +535,5 @@
<string name="decrypt_error_message_no_mdc">Security threat!\n\nMessage is missing integrity checks (MDC). The sender should update their outdated software.\n\nDisplay the message at your own risk.</string>
<string name="msg_contains_not_valid_pub_key">This message contained a Public Key that was not valid.\n\n%1$s</string>
<string name="msg_contains_not_valid_block">This message contained a block with type = %1$s that was not valid.\n\n%2$s</string>
<string name="could_not_save_new_recipient">Couldn\'t save a new recipient</string>
</resources>

0 comments on commit ae23630

Please sign in to comment.