-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ChampionViewModel class added Frag 2 can now get and use champion info extracted from json to set TV
- Loading branch information
John Taoi
committed
Apr 23, 2024
1 parent
24919c2
commit a086de3
Showing
4 changed files
with
105 additions
and
57 deletions.
There are no files selected for viewing
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
61 changes: 60 additions & 1 deletion
61
app/src/main/java/com/example/proj2/Classes/ChampionViewModel.java
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 |
---|---|---|
@@ -1,4 +1,63 @@ | ||
package com.example.proj2.Classes; | ||
|
||
public class ChampionViewModel { | ||
import androidx.lifecycle.LiveData; | ||
import androidx.lifecycle.MutableLiveData; | ||
import androidx.lifecycle.ViewModel; | ||
|
||
import com.android.volley.Request; | ||
import com.android.volley.RequestQueue; | ||
import com.android.volley.toolbox.StringRequest; | ||
import com.android.volley.toolbox.Volley; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import android.content.Context; | ||
|
||
import java.io.File; | ||
|
||
public class ChampionViewModel extends ViewModel { | ||
private MutableLiveData<Champion> championData = new MutableLiveData<>(); | ||
private RequestQueue queue; | ||
|
||
public LiveData<Champion> getChampion() { | ||
return championData; | ||
} | ||
|
||
public void fetchChampionData(Context context, String championName) { | ||
if (queue == null) { | ||
queue = Volley.newRequestQueue(context); | ||
} | ||
String urlTemplate = "https://raw.communitydragon.org/14.5/game/data/characters/"; | ||
String urlInsert = championName.replaceAll("\\s", "").toLowerCase(); | ||
String actualURL = urlTemplate + urlInsert + "/" + urlInsert + ".bin.json"; | ||
|
||
StringRequest stringRequest = new StringRequest(Request.Method.GET, actualURL, | ||
response -> { | ||
try { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
JsonNode rootNode = objectMapper.readTree(response); | ||
String pathName = "Characters/" + championName + "/CharacterRecords/Root"; | ||
Champion champ = new Champion( | ||
rootNode.path(pathName).path("mCharacterName").asText(), | ||
rootNode.path(pathName).path("baseHP").asDouble(), | ||
rootNode.path(pathName).path("hpPerLevel").asDouble(), | ||
rootNode.path(pathName).path("baseDamage").asDouble(), | ||
rootNode.path(pathName).path("damagePerLevel").asDouble(), | ||
rootNode.path(pathName).path("baseArmor").asDouble(), | ||
rootNode.path(pathName).path("armorPerLevel").asDouble(), | ||
rootNode.path(pathName).path("baseSpellBlock").asDouble(), | ||
rootNode.path(pathName).path("spellBlockPerLevel").asDouble(), | ||
rootNode.path(pathName).path("baseMoveSpeed").asDouble(), | ||
rootNode.path(pathName).path("attackSpeed").asDouble(), | ||
rootNode.path(pathName).path("attackSpeedRatio").asDouble(), | ||
rootNode.path(pathName).path("attackSpeedPerLevel").asDouble() | ||
); | ||
championData.postValue(champ); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
}, | ||
error -> error.printStackTrace() | ||
); | ||
queue.add(stringRequest); | ||
} | ||
} |
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