Compare commits
9 Commits
master
...
persistanc
Author | SHA1 | Date |
---|---|---|
|
70564b5f03 | 2 years ago |
|
e60ed56936 | 2 years ago |
|
0b20d7745c | 2 years ago |
|
e1f661f44d | 2 years ago |
|
7186a5e76b | 2 years ago |
|
b228d1b486 | 2 years ago |
|
0d5ffbb323 | 2 years ago |
|
7514a5168d | 2 years ago |
|
e0db3f5eb9 | 2 years ago |
@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="VcsDirectoryMappings">
|
<component name="VcsDirectoryMappings">
|
||||||
<mapping directory="$PROJECT_DIR$/../" vcs="Git" />
|
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
@ -0,0 +1,42 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="AutoImportSettings">
|
||||||
|
<option name="autoReloadType" value="NONE" />
|
||||||
|
</component>
|
||||||
|
<component name="ChangeListManager">
|
||||||
|
<list default="true" id="c1d3a00b-b752-49c3-9cf2-bbd65379556a" name="Changes" comment="" />
|
||||||
|
<option name="SHOW_DIALOG" value="false" />
|
||||||
|
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||||
|
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
||||||
|
<option name="LAST_RESOLUTION" value="IGNORE" />
|
||||||
|
</component>
|
||||||
|
<component name="Git.Settings">
|
||||||
|
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/../.." />
|
||||||
|
</component>
|
||||||
|
<component name="ProjectId" id="2N679bs9cUF2LZx6uhUfUbDiBup" />
|
||||||
|
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
|
||||||
|
<component name="ProjectViewState">
|
||||||
|
<option name="hideEmptyMiddlePackages" value="true" />
|
||||||
|
<option name="showLibraryContents" value="true" />
|
||||||
|
</component>
|
||||||
|
<component name="PropertiesComponent"><![CDATA[{
|
||||||
|
"keyToString": {
|
||||||
|
"RunOnceActivity.OpenProjectViewOnStart": "true",
|
||||||
|
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||||
|
"RunOnceActivity.cidr.known.project.marker": "true",
|
||||||
|
"cidr.known.project.marker": "true",
|
||||||
|
"last_opened_file_path": "D:/ShakeAndCraft/ShakeAndCraft/ShakeAndCraft"
|
||||||
|
}
|
||||||
|
}]]></component>
|
||||||
|
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
|
||||||
|
<component name="TaskManager">
|
||||||
|
<task active="true" id="Default" summary="Default task">
|
||||||
|
<changelist id="c1d3a00b-b752-49c3-9cf2-bbd65379556a" name="Changes" comment="" />
|
||||||
|
<created>1678972527401</created>
|
||||||
|
<option name="number" value="Default" />
|
||||||
|
<option name="presentableId" value="Default" />
|
||||||
|
<updated>1678972527401</updated>
|
||||||
|
</task>
|
||||||
|
<servers />
|
||||||
|
</component>
|
||||||
|
</project>
|
@ -0,0 +1,8 @@
|
|||||||
|
package com.example.shakecraft
|
||||||
|
|
||||||
|
import android.app.Application
|
||||||
|
import com.example.shakecraft.data.DataBase
|
||||||
|
|
||||||
|
class PlayerApplication: Application(){
|
||||||
|
val database: DataBase by lazy { DataBase.getInstance(this) }
|
||||||
|
}
|
@ -0,0 +1,60 @@
|
|||||||
|
package com.example.shakecraft
|
||||||
|
|
||||||
|
import androidx.lifecycle.LiveData
|
||||||
|
import androidx.lifecycle.asLiveData
|
||||||
|
import androidx.lifecycle.ViewModel
|
||||||
|
import androidx.lifecycle.ViewModelProvider
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
|
import com.example.shakecraft.data.PlayerDao
|
||||||
|
import com.example.shakecraft.model.Player
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
|
class PlayerViewModel(private val playerDao: PlayerDao): ViewModel() {
|
||||||
|
val player:LiveData<Player> = playerDao.getPlayer()
|
||||||
|
.asLiveData()
|
||||||
|
fun updatePlayer(
|
||||||
|
Id: Int,
|
||||||
|
pseudo: String,
|
||||||
|
level: Int,
|
||||||
|
xp: Int,
|
||||||
|
image: Int,
|
||||||
|
rank: String,
|
||||||
|
){
|
||||||
|
val updatePlayer= getUpdatePlayerEntry(Id, pseudo, level, xp, image, rank)
|
||||||
|
updatePlayer(updatePlayer)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun updatePlayer(player: Player) {
|
||||||
|
viewModelScope.launch{
|
||||||
|
playerDao.update(player)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getUpdatePlayerEntry(
|
||||||
|
Id: Int,
|
||||||
|
pseudo: String,
|
||||||
|
level: Int,
|
||||||
|
xp: Int,
|
||||||
|
image: Int,
|
||||||
|
rank: String,
|
||||||
|
): Player {
|
||||||
|
return Player(
|
||||||
|
id= Id,
|
||||||
|
pseudo= pseudo,
|
||||||
|
level= level,
|
||||||
|
xp= xp,
|
||||||
|
image= image,
|
||||||
|
rank= rank,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class PlayerViewModelFactory(private val playerDao: PlayerDao): ViewModelProvider.Factory{
|
||||||
|
fun <T : ViewModel?> create(modelClass: Class<T>): T {
|
||||||
|
if (modelClass.isAssignableFrom(PlayerViewModel::class.java)){
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
return PlayerViewModel(playerDao) as T
|
||||||
|
}
|
||||||
|
throw IllegalArgumentException("Unknown ViewModel class")
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.example.shakecraft.data
|
||||||
|
import androidx.room.Database
|
||||||
|
import androidx.room.Room
|
||||||
|
import androidx.room.RoomDatabase
|
||||||
|
import com.example.shakecraft.model.Player
|
||||||
|
import android.content.Context
|
||||||
|
|
||||||
|
@Database(entities = [Player::class],version = 1)
|
||||||
|
abstract class DataBase: RoomDatabase() {
|
||||||
|
abstract fun playerDao(): PlayerDao
|
||||||
|
|
||||||
|
companion object{
|
||||||
|
private var INSTANCE : DataBase? = null
|
||||||
|
fun getInstance(context: Context): DataBase{
|
||||||
|
return INSTANCE ?: synchronized(this){
|
||||||
|
val instance = Room.databaseBuilder(
|
||||||
|
context.applicationContext,
|
||||||
|
DataBase::class.java,
|
||||||
|
"player_database"
|
||||||
|
)
|
||||||
|
.fallbackToDestructiveMigration()
|
||||||
|
.build()
|
||||||
|
INSTANCE = instance
|
||||||
|
instance
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package com.example.shakecraft.data
|
||||||
|
|
||||||
|
import androidx.room.*
|
||||||
|
import com.example.shakecraft.model.Player
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
|
@Dao
|
||||||
|
interface PlayerDao {
|
||||||
|
|
||||||
|
@Query("SELECT * FROM Player")
|
||||||
|
fun getPlayer(): Flow<Player>
|
||||||
|
|
||||||
|
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||||
|
suspend fun insert(player: Player)
|
||||||
|
|
||||||
|
@Update
|
||||||
|
suspend fun update(player: Player)
|
||||||
|
|
||||||
|
@Delete
|
||||||
|
suspend fun delete(player: Player)
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
package com.example.shakecraft.model
|
package com.example.shakecraft.model
|
||||||
|
|
||||||
class ItemType (val name : String,val image : Int,val rarity : Int, val xpReward : Int){
|
|
||||||
|
|
||||||
|
class ItemType (val name : String,val image : Int,val rarity : Int, val xpReward : Int){
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in new issue