Skip to content

Commit

Permalink
Updates in App
Browse files Browse the repository at this point in the history
  • Loading branch information
ARUNJOSE144 committed Jun 20, 2021
1 parent 882cf6c commit c16c6ad
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 29 deletions.
6 changes: 3 additions & 3 deletions app/src/main/java/com/example/myapplication/CoinTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class CoinTO {
private String date_added;
private int cmc_rank;
private String last_updated;
private Double price;
private String price;


public int getId() {
Expand Down Expand Up @@ -67,11 +67,11 @@ public void setLast_updated(String last_updated) {
this.last_updated = last_updated;
}

public Double getPrice() {
public String getPrice() {
return price;
}

public void setPrice(Double price) {
public void setPrice(String price) {
this.price = price;
}

Expand Down
50 changes: 45 additions & 5 deletions app/src/main/java/com/example/myapplication/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.example.myapplication;

import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
Expand All @@ -19,30 +22,62 @@
import org.json.JSONException;
import org.json.JSONObject;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
int last_refresed = 0;
List<String> coinList;
List<String> childList;
Map<String, List<String>> coinCollections;
ExpandableListView expandableListView;
ExpandableListAdapter expandableListAdapter;
List<CoinTO> coinDetails;
Double DollerInINR = 74.14;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getDataFromApi();
final Handler handler = new Handler();

//Created Thread for Calling the API in multiple times
final Runnable r = new Runnable() {
public void run() {
getDataFromApi();
last_refresed = 0;
handler.postDelayed(this, 300000);
}
};

final Runnable lastUpdatedTime = new Runnable() {
public void run() {
last_refresed++;
TextView item = (TextView) findViewById(R.id.last_refreshed);
item.setTypeface(null, Typeface.BOLD);
item.setText("Last Updated in " + last_refresed + " Sec");
handler.postDelayed(this, 1000);
}
};

handler.postDelayed(lastUpdatedTime, 1000);
handler.postDelayed(r, 1);
}

private void loadChild(CoinTO coinTO) {
childList = new ArrayList<>();
childList.add(coinTO.getName());
childList.add(coinTO.getPrice() + "");
childList.add(coinTO.getId() + "");
childList.add(coinTO.getCmc_rank() + "");
childList.add(coinTO.getLast_updated() + "");
childList.add(coinTO.getSymbol() + "");

}

private void createCollections() {
Expand Down Expand Up @@ -113,17 +148,22 @@ void setResponseAfterApiCall(String response) {
coinTO.setLast_updated(object.get("last_updated").toString());
JSONObject quote = object.getJSONObject("quote");
JSONObject usd = quote.getJSONObject("USD");
coinTO.setPrice(Double.parseDouble(usd.get("price").toString()));

DecimalFormat df = new DecimalFormat("#.###");
df.setRoundingMode(RoundingMode.CEILING);

Double finalPrice = Double.parseDouble(usd.get("price").toString()) * DollerInINR;
coinTO.setPrice(df.format(finalPrice));

list.add(coinTO);
}
coinDetails = list;
loadCoin();
createCollections();
expandableListView = findViewById(R.id.expanded_menu);
expandableListAdapter = new MyExpandableListAdapter(this, coinList, coinCollections);
expandableListAdapter = new MyExpandableListAdapter(this, coinDetails);
expandableListView.setAdapter(expandableListAdapter);
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
/*expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
int lastExpandedPosition = -1;
@Override
Expand All @@ -133,7 +173,7 @@ public void onGroupExpand(int i) {
}
lastExpandedPosition = i;
}
});
});*/

expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,38 @@
import android.widget.TextView;

import java.util.List;
import java.util.Map;

public class MyExpandableListAdapter extends BaseExpandableListAdapter {
Context context;
List<String> coinList;
Map<String, List<String>> collectionList;
List<CoinTO> coinTOList;

public MyExpandableListAdapter(Context context, List<String> coinList, Map<String, List<String>> collectionList) {
public MyExpandableListAdapter(Context context, List<CoinTO> coinTOList) {
this.context = context;
this.coinList = coinList;
this.collectionList = collectionList;
this.coinTOList = coinTOList;


}


@Override
public int getGroupCount() {
return collectionList.size();
return coinTOList.size();
}

@Override
public int getChildrenCount(int i) {
return collectionList.get(coinList.get(i)).size();
return 3;
}

@Override
public Object getGroup(int i) {
return coinList.get(i);
return coinTOList.get(i);
}

@Override
public Object getChild(int i, int i1) {

return collectionList.get(coinList.get(i)).get(i1);
return coinTOList.get(i);
}

@Override
Expand All @@ -65,27 +62,36 @@ public boolean hasStableIds() {

@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
String coinName = getGroup(i).toString();
CoinTO coin = coinTOList.get(i);
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.coin_item, null);
}
TextView item = view.findViewById(R.id.coin);
item.setTypeface(null, Typeface.BOLD);
item.setText(coinName);
item.setText(coin.getName() + " - " + coin.getPrice());

return view;
}

@Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
String info = getChild(i, i1).toString();
System.out.println(i1);
CoinTO coin = coinTOList.get(i);
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.coin_details, null);
}

TextView item = view.findViewById(R.id.details);
item.setText(info);

if (i1 == 0)
item.setText("Id : " + coin.getId());
if (i1 == 1)
item.setText("Last Updated : " + coin.getLast_updated());
if (i1 == 2)
item.setText("Symbol : " + coin.getSymbol());

return view;
}

Expand Down
40 changes: 35 additions & 5 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,45 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<TextView
android:id="@+id/last_refreshed"
android:layout_width="301dp"
android:layout_height="25dp"
android:layout_marginTop="4dp"
android:layout_weight="1"
android:text="Last Refreshed"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.409"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />


<ExpandableListView
android:id="@+id/expanded_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_width="411dp"
android:layout_height="709dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/last_refreshed"
app:layout_constraintVertical_bias="0.0"></ExpandableListView>

/>
<TextView
android:id="@+id/powerdBy"
android:layout_width="301dp"
android:layout_height="25dp"
android:layout_weight="1"
android:text="Powered By Inno Solutions"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/expanded_menu" />

</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
4 changes: 3 additions & 1 deletion app/src/main/res/layout/coin_details.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello" />
android:text="" />



</LinearLayout>
4 changes: 3 additions & 1 deletion app/src/main/res/layout/coin_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="50dp"
android:text="" />
android:text=""
android:textColor="@android:color/holo_red_dark" />


</LinearLayout>

0 comments on commit c16c6ad

Please sign in to comment.