Skip to content

Commit 1f94e94

Browse files
authored
Merge pull request #67 from horizontalsystems/static-clear
Implement static clear() method.
2 parents 52403c9 + ed61bff commit 1f94e94

File tree

21 files changed

+145
-109
lines changed

21 files changed

+145
-109
lines changed

app/src/main/java/io/horizontalsystems/ethereumkit/sample/BalanceFragment.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class BalanceFragment : Fragment() {
2121
lateinit var kitStateValue: TextView
2222
lateinit var erc20StateValue: TextView
2323
lateinit var refreshButton: Button
24+
lateinit var clearButton: Button
2425

2526
override fun onCreate(savedInstanceState: Bundle?) {
2627
super.onCreate(savedInstanceState)
@@ -73,6 +74,7 @@ class BalanceFragment : Fragment() {
7374
balanceValue = view.findViewById(R.id.balanceValue)
7475
tokenBalanceValue = view.findViewById(R.id.tokenBalanceValue)
7576
refreshButton = view.findViewById(R.id.buttonRefresh)
77+
clearButton = view.findViewById(R.id.buttonClear)
7678
feeValue = view.findViewById(R.id.feeValue)
7779
lbhValue = view.findViewById(R.id.lbhValue)
7880
kitStateValue = view.findViewById(R.id.kitStateValue)
@@ -81,5 +83,9 @@ class BalanceFragment : Fragment() {
8183
refreshButton.setOnClickListener {
8284
viewModel.refresh()
8385
}
86+
87+
clearButton.setOnClickListener {
88+
viewModel.clear()
89+
}
8490
}
8591
}

app/src/main/java/io/horizontalsystems/ethereumkit/sample/MainViewModel.kt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package io.horizontalsystems.ethereumkit.sample
33
import android.arch.lifecycle.MutableLiveData
44
import android.arch.lifecycle.ViewModel
55
import android.util.Log
6+
import io.horizontalsystems.erc20kit.core.Erc20Kit
67
import io.horizontalsystems.ethereumkit.core.EthereumKit
78
import io.horizontalsystems.ethereumkit.core.EthereumKit.NetworkType
89
import io.horizontalsystems.ethereumkit.core.EthereumKit.SyncState
@@ -25,10 +26,10 @@ class MainViewModel : ViewModel() {
2526

2627
private val disposables = CompositeDisposable()
2728

28-
private var ethereumKit: EthereumKit
29-
private val ethereumAdapter: EthereumAdapter
29+
private lateinit var ethereumKit: EthereumKit
30+
private lateinit var ethereumAdapter: EthereumAdapter
3031

31-
private val erc20Adapter: Erc20Adapter
32+
private lateinit var erc20Adapter: Erc20Adapter
3233

3334
val transactions = MutableLiveData<List<TransactionRecord>>()
3435
val balance = MutableLiveData<BigDecimal>()
@@ -44,6 +45,10 @@ class MainViewModel : ViewModel() {
4445
val gasPrice: Long = 5_000_000_000
4546

4647
init {
48+
init()
49+
}
50+
51+
fun init() {
4752
// val words = "subway plate brick pattern inform used oblige identify cherry drop flush balance".split(" ")
4853
val words = "mom year father track attend frown loyal goddess crisp abandon juice roof".split(" ")
4954

@@ -168,6 +173,12 @@ class MainViewModel : ViewModel() {
168173
fee.postValue(ethereumKit.fee(gasPrice))
169174
}
170175

176+
fun clear() {
177+
EthereumKit.clear(App.instance)
178+
Erc20Kit.clear(App.instance)
179+
init()
180+
}
181+
171182
fun receiveAddress(): String {
172183
return ethereumKit.receiveAddress
173184
}

app/src/main/res/layout/fragment_balance.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,17 @@
151151
app:layout_constraintTop_toBottomOf="@+id/kitStateTitle"
152152
/>
153153

154+
<Button
155+
android:id="@+id/buttonClear"
156+
android:layout_width="wrap_content"
157+
android:layout_height="wrap_content"
158+
android:layout_marginEnd="8dp"
159+
android:layout_marginStart="8dp"
160+
android:layout_marginTop="8dp"
161+
android:text="Clear"
162+
app:layout_constraintEnd_toEndOf="parent"
163+
app:layout_constraintStart_toStartOf="parent"
164+
app:layout_constraintTop_toBottomOf="@+id/buttonRefresh"
165+
/>
166+
154167
</android.support.constraint.ConstraintLayout>

erc20kit/src/main/java/io/horizontalsystems/erc20kit/core/BalanceManager.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,4 @@ class BalanceManager(private val contractAddress: ByteArray,
3030
}
3131
}
3232

33-
override fun clear() {
34-
storage.clearBalance()
35-
disposables.clear()
36-
}
3733
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package io.horizontalsystems.erc20kit.core
2+
3+
import android.arch.persistence.room.RoomDatabase
4+
import android.content.Context
5+
import android.database.sqlite.SQLiteDatabase
6+
import io.horizontalsystems.erc20kit.core.room.Erc20KitDatabase
7+
import java.io.File
8+
9+
internal object Erc20DatabaseManager {
10+
11+
fun getErc20Database(context: Context, contractAddress: String): Erc20KitDatabase {
12+
val databaseName = "erc20-$contractAddress"
13+
return Erc20KitDatabase.getInstance(context, databaseName).also { addDatabasePath(context, it) }
14+
}
15+
16+
fun clear(context: Context) {
17+
synchronized(this) {
18+
val preferences = context.getSharedPreferences(preferencesName, Context.MODE_PRIVATE)
19+
val paths = HashSet<String>(preferences.getStringSet(DATABASE_PATHS, setOf()))
20+
21+
paths.forEach { path ->
22+
SQLiteDatabase.deleteDatabase(File(path))
23+
}
24+
25+
preferences.edit().clear().apply()
26+
}
27+
}
28+
29+
private const val preferencesName = "erc20_database_preferences"
30+
private const val DATABASE_PATHS = "key_database_paths"
31+
32+
private fun addDatabasePath(context: Context, database: RoomDatabase) {
33+
val path = database.openHelper.writableDatabase.path
34+
35+
synchronized(this) {
36+
val preferences = context.getSharedPreferences(preferencesName, Context.MODE_PRIVATE)
37+
38+
val paths = HashSet<String>(preferences.getStringSet(DATABASE_PATHS, setOf()))
39+
paths.add(path)
40+
preferences.edit().putStringSet(DATABASE_PATHS, paths).apply()
41+
}
42+
}
43+
}

erc20kit/src/main/java/io/horizontalsystems/erc20kit/core/Erc20Kit.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ class Erc20Kit(private val ethereumKit: EthereumKit,
8686
val transactionsFlowable: Flowable<List<TransactionInfo>>
8787
get() = state.transactionsSubject.toFlowable(BackpressureStrategy.BUFFER)
8888

89-
fun clear() {
90-
transactionManager.clear()
91-
balanceManager.clear()
89+
fun stop() {
9290
disposables.clear()
9391
}
9492

@@ -126,7 +124,8 @@ class Erc20Kit(private val ethereumKit: EthereumKit,
126124
val contractAddressRaw = contractAddress.hexStringToByteArray()
127125
val address = ethereumKit.receiveAddressRaw
128126

129-
val roomStorage = RoomStorage(context, "erc20_$contractAddress")
127+
val erc20KitDatabase = Erc20DatabaseManager.getErc20Database(context, contractAddress)
128+
val roomStorage = Erc20Storage(erc20KitDatabase)
130129
val transactionStorage: ITransactionStorage = roomStorage
131130
val balanceStorage: ITokenBalanceStorage = roomStorage
132131

@@ -142,6 +141,10 @@ class Erc20Kit(private val ethereumKit: EthereumKit,
142141

143142
return erc20Kit
144143
}
144+
145+
fun clear(context: Context) {
146+
Erc20DatabaseManager.clear(context)
147+
}
145148
}
146149

147150
}

erc20kit/src/main/java/io/horizontalsystems/erc20kit/core/RoomStorage.kt renamed to erc20kit/src/main/java/io/horizontalsystems/erc20kit/core/Erc20Storage.kt

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
package io.horizontalsystems.erc20kit.core
22

3-
import android.content.Context
43
import io.horizontalsystems.erc20kit.core.room.Erc20KitDatabase
54
import io.horizontalsystems.erc20kit.models.TokenBalance
65
import io.horizontalsystems.erc20kit.models.Transaction
76
import io.reactivex.Single
87
import java.math.BigInteger
98

10-
class RoomStorage(context: Context, databaseName: String) : ITransactionStorage, ITokenBalanceStorage {
11-
12-
private val database = Erc20KitDatabase.getInstance(context, databaseName)
13-
9+
class Erc20Storage(private val database: Erc20KitDatabase) : ITransactionStorage, ITokenBalanceStorage {
1410

1511
// ITransactionStorage
1612

@@ -48,10 +44,6 @@ class RoomStorage(context: Context, databaseName: String) : ITransactionStorage,
4844
database.transactionDao.insert(transactions)
4945
}
5046

51-
override fun clearTransactions() {
52-
database.transactionDao.deleteAll()
53-
}
54-
5547

5648
// ITokenBalanceStorage
5749

@@ -63,7 +55,4 @@ class RoomStorage(context: Context, databaseName: String) : ITransactionStorage,
6355
database.tokenBalanceDao.insert(TokenBalance(balance))
6456
}
6557

66-
override fun clearBalance() {
67-
database.tokenBalanceDao.deleteAll()
68-
}
69-
}
58+
}

erc20kit/src/main/java/io/horizontalsystems/erc20kit/core/Interfaces.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ interface ITransactionManager {
2020
fun getTransactions(fromTransaction: TransactionKey?, limit: Int?): Single<List<Transaction>>
2121
fun sync()
2222
fun send(to: ByteArray, value: BigInteger, gasPrice: Long, gasLimit: Long): Single<Transaction>
23-
fun clear()
2423
}
2524

2625
interface IBalanceManagerListener {
@@ -33,7 +32,6 @@ interface IBalanceManager {
3332

3433
val balance: BigInteger?
3534
fun sync()
36-
fun clear()
3735
}
3836

3937
interface ITransactionStorage {
@@ -42,13 +40,11 @@ interface ITransactionStorage {
4240
fun getTransactions(fromTransaction: TransactionKey?, limit: Int?): Single<List<Transaction>>
4341
fun getPendingTransactions(): List<Transaction>
4442
fun save(transactions: List<Transaction>)
45-
fun clearTransactions()
4643
}
4744

4845
interface ITokenBalanceStorage {
4946
fun getBalance(): BigInteger?
5047
fun save(balance: BigInteger)
51-
fun clearBalance()
5248
}
5349

5450
interface IDataProvider {

erc20kit/src/main/java/io/horizontalsystems/erc20kit/core/TransactionManager.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,4 @@ class TransactionManager(private val contractAddress: ByteArray,
9494
}
9595
}
9696

97-
override fun clear() {
98-
disposables.clear()
99-
storage.clearTransactions()
100-
}
10197
}

erc20kit/src/main/java/io/horizontalsystems/erc20kit/core/room/Erc20KitDatabase.kt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,7 @@ abstract class Erc20KitDatabase : RoomDatabase() {
1818

1919
companion object {
2020

21-
@Volatile
22-
private var INSTANCE: Erc20KitDatabase? = null
23-
2421
fun getInstance(context: Context, databaseName: String): Erc20KitDatabase {
25-
return INSTANCE ?: synchronized(this) {
26-
INSTANCE ?: buildDatabase(context, databaseName).also { INSTANCE = it }
27-
}
28-
}
29-
30-
private fun buildDatabase(context: Context, databaseName: String): Erc20KitDatabase {
3122
return Room.databaseBuilder(context, Erc20KitDatabase::class.java, databaseName)
3223
.fallbackToDestructiveMigration()
3324
.allowMainThreadQueries()

0 commit comments

Comments
 (0)