-
Notifications
You must be signed in to change notification settings - Fork 29
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
Showing
6 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
wallet_app/app/src/main/java/com/example/walletapp/WalletAppApplication.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,11 @@ | ||
package com.example.walletapp | ||
|
||
import android.app.Application | ||
import com.example.walletapp.db.TokenDatabase | ||
|
||
|
||
class WalletAppApplication : Application() { | ||
|
||
// Lazy initialization of the database | ||
val database: TokenDatabase by lazy { TokenDatabase.getDatabase(this) } | ||
} |
19 changes: 19 additions & 0 deletions
19
wallet_app/app/src/main/java/com/example/walletapp/db/TokenDao.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,19 @@ | ||
package com.example.walletapp.db | ||
|
||
|
||
import androidx.room.Dao | ||
import androidx.room.Insert | ||
import androidx.room.OnConflictStrategy | ||
import com.example.walletapp.model.Token | ||
import kotlinx.coroutines.flow.Flow | ||
import androidx.room.Query | ||
|
||
@Dao | ||
interface TokenDao { | ||
|
||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
suspend fun insert(token: Token) | ||
|
||
@Query("SELECT * FROM token_table") | ||
fun getAllTokens(): Flow<List<Token>> | ||
} |
59 changes: 59 additions & 0 deletions
59
wallet_app/app/src/main/java/com/example/walletapp/db/TokenDatabase.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,59 @@ | ||
package com.example.walletapp.db | ||
|
||
import android.content.Context | ||
import androidx.room.Database | ||
import androidx.room.Room | ||
import androidx.room.RoomDatabase | ||
import androidx.room.TypeConverters | ||
import androidx.sqlite.db.SupportSQLiteDatabase | ||
import com.example.walletapp.model.Token | ||
import com.example.walletapp.utils.Converters | ||
import com.swmansion.starknet.data.types.Felt | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
|
||
@Database(entities = [Token::class], version = 1, exportSchema = false) | ||
@TypeConverters(Converters::class) | ||
abstract class TokenDatabase : RoomDatabase() { | ||
|
||
abstract fun tokenDao(): TokenDao | ||
|
||
companion object { | ||
@Volatile | ||
private var INSTANCE: TokenDatabase? = null | ||
|
||
fun getDatabase(context: Context): TokenDatabase { | ||
return INSTANCE ?: synchronized(this) { | ||
val instance = Room.databaseBuilder( | ||
context.applicationContext, | ||
TokenDatabase::class.java, | ||
"token_database" | ||
) | ||
.addCallback(DatabaseCallback()) // Add callback here | ||
.build() | ||
INSTANCE = instance | ||
instance | ||
} | ||
} | ||
} | ||
|
||
private class DatabaseCallback : RoomDatabase.Callback() { | ||
override fun onCreate(db: SupportSQLiteDatabase) { | ||
super.onCreate(db) | ||
INSTANCE?.let { database -> | ||
CoroutineScope(Dispatchers.IO).launch { | ||
populateDatabase(database.tokenDao()) | ||
} | ||
} | ||
} | ||
|
||
suspend fun populateDatabase(tokenDao: TokenDao) { | ||
// Add default tokens | ||
val token1 = Token(contactAddress = Felt.fromHex("0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7"), name = "ethereum", symbol = "ETH", decimals = 18) | ||
val token2 = Token(contactAddress = Felt.fromHex("0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d"), name = "starknet", symbol = "STRK", decimals = 18) | ||
tokenDao.insert(token1) | ||
tokenDao.insert(token2) | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
wallet_app/app/src/main/java/com/example/walletapp/ui/account/TokenRepository.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,20 @@ | ||
package com.example.walletapp.ui.account | ||
|
||
import com.example.walletapp.db.TokenDao | ||
import com.example.walletapp.model.Token | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.withContext | ||
|
||
class TokenRepository(private val tokenDao: TokenDao) { | ||
|
||
// Get all tokens from the database | ||
val allTokens: Flow<List<Token>> = tokenDao.getAllTokens() | ||
|
||
// Insert a token into the database | ||
suspend fun insertToken(token: Token) { | ||
withContext(Dispatchers.IO) { | ||
tokenDao.insert(token) | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
wallet_app/app/src/main/java/com/example/walletapp/ui/account/TokenViewModel.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,39 @@ | ||
package com.example.walletapp.ui.account | ||
|
||
import android.app.Application | ||
import androidx.lifecycle.AndroidViewModel | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.lifecycle.asLiveData | ||
import androidx.lifecycle.viewModelScope | ||
import com.example.walletapp.model.Token | ||
import kotlinx.coroutines.launch | ||
|
||
class TokenViewModel(application: Application, private val repository: TokenRepository) : AndroidViewModel(application) { | ||
|
||
|
||
// Expose all tokens as LiveData | ||
val tokens: LiveData<List<Token>> = repository.allTokens.asLiveData() | ||
|
||
// Function to insert a new token | ||
fun insertToken(token: Token) { | ||
viewModelScope.launch { | ||
repository.insertToken(token) | ||
} | ||
} | ||
|
||
// Define a factory to pass the repository | ||
class Factory(private val application: Application, private val repository: TokenRepository) : | ||
ViewModelProvider.AndroidViewModelFactory(application) { | ||
|
||
override fun <T : ViewModel> create(modelClass: Class<T>): T { | ||
if (modelClass.isAssignableFrom(TokenViewModel::class.java)) { | ||
@Suppress("UNCHECKED_CAST") | ||
return TokenViewModel(application, repository) as T | ||
} | ||
throw IllegalArgumentException("Unknown ViewModel class") | ||
} | ||
} | ||
} | ||
|
17 changes: 17 additions & 0 deletions
17
wallet_app/app/src/main/java/com/example/walletapp/utils/Converters.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,17 @@ | ||
package com.example.walletapp.utils | ||
|
||
import androidx.room.TypeConverter | ||
import com.swmansion.starknet.data.types.Felt | ||
|
||
class Converters { | ||
|
||
@TypeConverter | ||
fun fromFelt(felt: Felt): String { | ||
return felt.hexString() // Convert Felt to String | ||
} | ||
|
||
@TypeConverter | ||
fun toFelt(feltString: String): Felt { | ||
return Felt.fromHex(feltString) // Convert String back to Felt | ||
} | ||
} |