-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8bd3552
commit 07861d6
Showing
59 changed files
with
1,250 additions
and
217 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
plugins { | ||
id("com.tonapps.wallet.data") | ||
id("kotlin-parcelize") | ||
} | ||
|
||
android { | ||
namespace = Build.namespacePrefix("wallet.data.contacts") | ||
} | ||
|
||
dependencies { | ||
implementation(project(Dependence.Wallet.Data.core)) | ||
implementation(project(Dependence.Lib.extensions)) | ||
implementation(project(Dependence.Lib.sqlite)) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<manifest /> |
56 changes: 56 additions & 0 deletions
56
...wallet/data/contacts/src/main/java/com/tonapps/wallet/data/contacts/ContactsRepository.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.tonapps.wallet.data.contacts | ||
|
||
import android.content.Context | ||
import com.tonapps.wallet.data.contacts.entities.ContactEntity | ||
import com.tonapps.wallet.data.contacts.source.DatabaseSource | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.SharingStarted | ||
import kotlinx.coroutines.flow.filterNotNull | ||
import kotlinx.coroutines.flow.firstOrNull | ||
import kotlinx.coroutines.flow.stateIn | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.withContext | ||
|
||
class ContactsRepository(context: Context, scope: CoroutineScope) { | ||
|
||
private val database = DatabaseSource(context) | ||
|
||
private val _contactsFlow = MutableStateFlow<List<ContactEntity>?>(null) | ||
val contactsFlow = _contactsFlow.stateIn(scope, SharingStarted.Lazily, null).filterNotNull() | ||
|
||
init { | ||
scope.launch { | ||
_contactsFlow.value = database.getContacts() | ||
} | ||
} | ||
|
||
fun isHidden(accountId: String, testnet: Boolean) = database.isHidden(accountId, testnet) | ||
|
||
fun hide(accountId: String, testnet: Boolean) = database.setHidden(accountId, testnet, true) | ||
|
||
suspend fun add(name: String, address: String): ContactEntity = withContext(Dispatchers.IO) { | ||
val contact = database.addContact(name, address) | ||
_contactsFlow.value = _contactsFlow.value.orEmpty().toMutableList().apply { | ||
add(contact) | ||
} | ||
contact | ||
} | ||
|
||
suspend fun deleteContact(id: Long) = withContext(Dispatchers.IO) { | ||
database.deleteContact(id) | ||
_contactsFlow.value = _contactsFlow.value.orEmpty().toMutableList().apply { | ||
removeAll { it.id == id } | ||
} | ||
} | ||
|
||
suspend fun editContact(id: Long, name: String) = withContext(Dispatchers.IO) { | ||
database.editContact(id, name) | ||
val oldContacts = _contactsFlow.value.orEmpty().toMutableList() | ||
val index = oldContacts.indexOfFirst { it.id == id } | ||
if (index == -1) return@withContext | ||
oldContacts[index] = oldContacts[index].copy(name = name) | ||
_contactsFlow.value = oldContacts | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
apps/wallet/data/contacts/src/main/java/com/tonapps/wallet/data/contacts/Module.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.tonapps.wallet.data.contacts | ||
|
||
import org.koin.core.module.dsl.singleOf | ||
import org.koin.dsl.module | ||
|
||
val contactsModule = module { | ||
singleOf(::ContactsRepository) | ||
} |
12 changes: 12 additions & 0 deletions
12
...et/data/contacts/src/main/java/com/tonapps/wallet/data/contacts/entities/ContactEntity.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.tonapps.wallet.data.contacts.entities | ||
|
||
import android.os.Parcelable | ||
import kotlinx.parcelize.Parcelize | ||
|
||
@Parcelize | ||
data class ContactEntity( | ||
val id: Long, | ||
val name: String, | ||
val address: String, | ||
val date: Long | ||
): Parcelable |
98 changes: 98 additions & 0 deletions
98
...let/data/contacts/src/main/java/com/tonapps/wallet/data/contacts/source/DatabaseSource.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package com.tonapps.wallet.data.contacts.source | ||
|
||
import android.content.ContentValues | ||
import android.content.Context | ||
import android.database.sqlite.SQLiteDatabase | ||
import com.tonapps.extensions.currentTimeSeconds | ||
import com.tonapps.sqlite.SQLiteHelper | ||
import com.tonapps.wallet.data.contacts.entities.ContactEntity | ||
|
||
internal class DatabaseSource( | ||
context: Context | ||
): SQLiteHelper(context, DATABASE_NAME, DATABASE_VERSION) { | ||
|
||
private companion object { | ||
private const val DATABASE_NAME = "contacts" | ||
private const val DATABASE_VERSION = 1 | ||
|
||
private const val CONTACTS_TABLE = "contacts" | ||
private const val CONTACTS_ID = "_id" | ||
private const val CONTACTS_NAME = "name" | ||
private const val CONTACTS_ADDRESS = "address" | ||
private const val CONTACTS_DATE = "date" | ||
|
||
private val contactsField = arrayOf( | ||
CONTACTS_ID, | ||
CONTACTS_NAME, | ||
CONTACTS_ADDRESS, | ||
CONTACTS_DATE | ||
).joinToString(",") | ||
|
||
private const val KEY_HIDDEN = "hidden" | ||
} | ||
|
||
private val prefs = context.getSharedPreferences("contacts", Context.MODE_PRIVATE) | ||
|
||
override fun create(db: SQLiteDatabase) { | ||
db.execSQL("CREATE TABLE $CONTACTS_TABLE (" + | ||
"$CONTACTS_ID INTEGER PRIMARY KEY AUTOINCREMENT," + | ||
"$CONTACTS_NAME TEXT NOT NULL," + | ||
"$CONTACTS_ADDRESS TEXT NOT NULL," + | ||
"$CONTACTS_DATE INTEGER NOT NULL" + | ||
")") | ||
} | ||
|
||
fun getContacts(): List<ContactEntity> { | ||
val contacts = mutableListOf<ContactEntity>() | ||
val query = "SELECT $contactsField FROM $CONTACTS_TABLE LIMIT 100" | ||
val cursor = readableDatabase.rawQuery(query, null) | ||
val idIndex = cursor.getColumnIndex(CONTACTS_ID) | ||
val nameIndex = cursor.getColumnIndex(CONTACTS_NAME) | ||
val addressIndex = cursor.getColumnIndex(CONTACTS_ADDRESS) | ||
val dateIndex = cursor.getColumnIndex(CONTACTS_DATE) | ||
while (cursor.moveToNext()) { | ||
contacts.add(ContactEntity( | ||
id = cursor.getLong(idIndex), | ||
name = cursor.getString(nameIndex), | ||
address = cursor.getString(addressIndex), | ||
date = cursor.getLong(dateIndex) | ||
)) | ||
} | ||
cursor.close() | ||
return contacts | ||
} | ||
|
||
fun addContact(name: String, address: String): ContactEntity { | ||
val date = currentTimeSeconds() | ||
val values = ContentValues().apply { | ||
put(CONTACTS_NAME, name) | ||
put(CONTACTS_ADDRESS, address) | ||
put(CONTACTS_DATE, date) | ||
} | ||
val id = writableDatabase.insert(CONTACTS_TABLE, null, values) | ||
return ContactEntity(id, name, address, date) | ||
} | ||
|
||
fun editContact(id: Long, name: String) { | ||
val values = ContentValues().apply { | ||
put(CONTACTS_NAME, name) | ||
} | ||
writableDatabase.update(CONTACTS_TABLE, values, "$CONTACTS_ID = ?", arrayOf(id.toString())) | ||
} | ||
|
||
fun deleteContact(id: Long) { | ||
writableDatabase.delete(CONTACTS_TABLE, "$CONTACTS_ID = ?", arrayOf(id.toString())) | ||
} | ||
|
||
private fun prefixHidden(accountId: String, testnet: Boolean): String { | ||
return "$KEY_HIDDEN:$accountId:${if (testnet) "testnet" else "mainnet"}" | ||
} | ||
|
||
fun isHidden(accountId: String, testnet: Boolean): Boolean { | ||
return prefs.getBoolean(prefixHidden(accountId, testnet), false) | ||
} | ||
|
||
fun setHidden(accountId: String, testnet: Boolean, hidden: Boolean) { | ||
prefs.edit().putBoolean(prefixHidden(accountId, testnet), hidden).apply() | ||
} | ||
} |
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
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
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
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
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
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
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
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
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
Oops, something went wrong.