diff --git a/app/build.gradle b/app/build.gradle index 0a90e8f..1914ee9 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' } \ No newline at end of file diff --git a/app/src/main/java/com/example/proj2/Classes/Ability.java b/app/src/main/java/com/example/proj2/Classes/Ability.java index a9c971a..1ab7c4e 100644 --- a/app/src/main/java/com/example/proj2/Classes/Ability.java +++ b/app/src/main/java/com/example/proj2/Classes/Ability.java @@ -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; @@ -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; diff --git a/app/src/main/java/com/example/proj2/Classes/ChampionViewModel.java b/app/src/main/java/com/example/proj2/Classes/ChampionViewModel.java index 93e1e44..14888b7 100644 --- a/app/src/main/java/com/example/proj2/Classes/ChampionViewModel.java +++ b/app/src/main/java/com/example/proj2/Classes/ChampionViewModel.java @@ -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 championData = new MutableLiveData<>(); + private RequestQueue queue; + + public LiveData 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); + } } diff --git a/app/src/main/java/com/example/proj2/SecondFragment.java b/app/src/main/java/com/example/proj2/SecondFragment.java index f273c73..dad53ff 100644 --- a/app/src/main/java/com/example/proj2/SecondFragment.java +++ b/app/src/main/java/com/example/proj2/SecondFragment.java @@ -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; @@ -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; @@ -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(); @@ -62,11 +77,7 @@ 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) @@ -74,47 +85,7 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) { } - 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();