Last active
October 5, 2022 07:36
Understanding Persistence Data Storage in Android
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
//Module-app (gradle) | |
dependencies { | |
// Room | |
implementation "androidx.room:room-runtime:2.4.3" | |
kapt "androidx.room:room-compiler:2.4.3" | |
// Kotlin Extensions and Coroutines support for Room | |
implementation "androidx.room:room-ktx:2.4.3" | |
// Coroutines | |
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.1' | |
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.1' | |
} |
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
const val TOKEN = "TOKEN" | |
const val DESIGNER = "DESIGNER" |
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
@Dao | |
interface Dao { | |
//Inserting data | |
@Insert | |
suspend fun insertNFTs(nft: NFTs) | |
//Fetching data using simple SQL Query | |
@Transaction | |
@Query("SELECT * FROM NFTs") | |
suspend fun getMyNFTs() : List<NFTs> | |
} |
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
@Database( | |
entities = [ | |
NFTs::class, | |
], version = 1) | |
abstract class database : RoomDatabase() { | |
abstract val dao: Dao | |
companion object { | |
@Volatile | |
private var instance: database? = null | |
fun getInstance(context: Context): database { | |
synchronized(this) { | |
return instance ?: Room.databaseBuilder( | |
context.applicationContext, | |
database::class.java, | |
"my_nft_collection" //DATABASE_NAME | |
).fallbackToDestructiveMigration() | |
.build().also { | |
instance = it | |
} | |
} | |
} | |
} | |
} | |
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
class MainAcctivity : AppCompatActivity() { | |
private lateinit var binding: AcctivityMainBinding | |
private val prefRepository by lazy { PrefRepository(this) } | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
binding = AcctivityMainBinding.inflate(layoutInflater) | |
prefRepository.setToken("buyme123") | |
prefRepository.setDesigner("sick?yes") | |
setContentView(binding.root) | |
} | |
} |
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
private lateinit var data: Dao | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
data = database.getInstance(this).dao | |
CoroutineScope(Dispatchers.IO).async { | |
data.insertNFTs("123Buyme","Sick?yes","Most Popular") | |
data.getMyNFTs() | |
} | |
} |
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
@Entity | |
data class NFTs( | |
@PrimaryKey(autoGenerate = false) | |
var token: String, | |
var designer : String? = null, | |
var desc : String? = null | |
) |
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
class PrefRepository(val context: Context) { | |
private val pref: SharedPreferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE) | |
private val editor = pref.edit() | |
private val gson = Gson() | |
private fun String.put(long: Long) { | |
editor.putLong(this, long) | |
editor.commit() | |
} | |
private fun String.put(int: Int) { | |
editor.putInt(this, int) | |
editor.commit() | |
} | |
private fun String.put(string: String) { | |
editor.putString(this, string) | |
editor.commit() | |
} | |
private fun String.put(boolean: Boolean) { | |
editor.putBoolean(this, boolean) | |
editor.commit() | |
} | |
//clear | |
private fun String.clear() { | |
editor.remove(this) | |
editor.commit() | |
} | |
private fun String.getLong() = pref.getLong(this, 0) | |
private fun String.getInt() = pref.getInt(this, 0) | |
private fun String.getString() = pref.getString(this, "")!! | |
private fun String.getBoolean() = pref.getBoolean(this, false) | |
fun setToken(token : String){ | |
TOKEN.put(token) | |
} | |
fun getToken() = TOKEN.getString() | |
fun setDesigner(designer : String){ | |
DESIGNER.put(designer) | |
} | |
fun getDesigner() = DESIGNER.getString() | |
fun clearData() { | |
editor.clear() | |
editor.commit() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment