Skip to content

Commit

Permalink
Viewmodel implemented
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 57 deletions.
4 changes: 3 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,7 @@ dependencies {
//Junit Test
testImplementation 'junit:junit:4.13.2'


//View model and Lifecycle
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
}
20 changes: 18 additions & 2 deletions app/src/main/java/com/example/proj2/Classes/Ability.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
package com.example.proj2.Classes;

public class Ability {
//Eability
//
//Qability
//
//Rability
//
//Wability
//Fields:
//Name
//damage:number
//ratioAP
//ratioAD
//isDamage
//isSummon
//instances
//duration

String name;
String spellnames;
Expand All @@ -14,8 +30,8 @@ enum damageType{trueDamage,physicalDamage, magicalDamage};
int stages;

//ratio
double[] coefficentAD;
double[] coefficentAP;
double[] ratioAD;
double[] ratioAP;
double[] coefficentArmor;
double[] coefficentMR;
double[] coefficentHP;
Expand Down
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);
}
}
77 changes: 24 additions & 53 deletions app/src/main/java/com/example/proj2/SecondFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.android.volley.toolbox.JsonObjectRequest;
import com.example.proj2.Classes.Champion;
import com.example.proj2.Classes.ChampionViewModel;
import com.example.proj2.databinding.FragmentSecondBinding;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
Expand All @@ -28,6 +29,7 @@
import com.android.volley.toolbox.Volley;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import androidx.lifecycle.ViewModelProvider;

import org.json.JSONObject;

Expand All @@ -45,15 +47,28 @@ public View onCreateView(
) {

binding = FragmentSecondBinding.inflate(inflater, container, false);
//Get Argument From Frag 1
//ViewModel
ViewModel viewModel = new ViewModelProvider(this).get(ChampionViewModel.class);
//Bundle
Bundle result = getArguments();
if(result != null){
this.ChampionName = result.getString("Name");
Log.d("Succeed",ChampionName);
}else{
Log.d("Failed","NULL");
//Fetch to viewmodel
if (result != null) {
String championName = result.getString("Name");
((ChampionViewModel) viewModel).fetchChampionData(getContext(), championName);
} else {
Log.d("Failed", "NULL");
}
createChampionObject();
((ChampionViewModel) viewModel).getChampion().observe(getViewLifecycleOwner(), champion -> {
if (champion != null) {
TextView championNameDisplay = binding.champName;
championNameDisplay.setText(champion.getName());
// Update other views based on champion data
}else{
Log.d("Failed","failed");
}
});



return binding.getRoot();

Expand All @@ -62,59 +77,15 @@ public View onCreateView(

public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//Change ChampionName TV
TextView championNameDisplay = getView().findViewById(R.id.champName);
championNameDisplay.setText(ChampionName);
//Log.d("Testing2",currChamp.getName());
//Return button

binding.buttonSecond.setOnClickListener(v ->
NavHostFragment.findNavController(SecondFragment.this)
.navigate(R.id.action_SecondFragment_to_FirstFragment)
);


}
public void createChampionObject(){
//Base Template
String urlTemplate = "https://raw.communitydragon.org/14.5/game/data/characters/";
//Modify string for URL usage
String urlInsert = ChampionName.replaceAll("\\s", "").toLowerCase();
String actualURL = urlTemplate+urlInsert+"/"+urlInsert+".bin.json";
queue = Volley.newRequestQueue(getActivity().getApplicationContext());
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";
currChamp = 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()
);
Log.d("Testing",currChamp.getName());
// Handle UI updates or further processing here
} catch (Exception e) {
e.printStackTrace();
}
},
error -> {
error.printStackTrace();
});
//Log.d("Testing2",currChamp.getName());
queue.add(stringRequest);
Log.d("Succeed","Succeed");
}

@Override
public void onDestroyView() {
super.onDestroyView();
Expand Down

0 comments on commit a086de3

Please sign in to comment.