diff options
| author | Maksymilian Jopek <maks@jopek.eu> | 2022-10-12 18:31:49 +0200 |
|---|---|---|
| committer | Maksymilian Jopek <maks@jopek.eu> | 2022-10-12 18:31:49 +0200 |
| commit | 0333ad3656502eb7197f45076948b91e53438a4b (patch) | |
| tree | 21e785ad4db6982310e208d721f257e4225ffd6a /app/src/main | |
| parent | 2a8cedad0c14a65616b257ea4bffd0226428b4bf (diff) | |
| download | taupngamoni-0333ad3656502eb7197f45076948b91e53438a4b.tar.gz taupngamoni-0333ad3656502eb7197f45076948b91e53438a4b.tar.zst taupngamoni-0333ad3656502eb7197f45076948b91e53438a4b.zip | |
Calc screen and start chart
Diffstat (limited to 'app/src/main')
6 files changed, 310 insertions, 51 deletions
diff --git a/app/src/main/java/com/jopek/taupngamoni/ChartFragment.java b/app/src/main/java/com/jopek/taupngamoni/ChartFragment.java index 50f7c9f..ce9bfa4 100644 --- a/app/src/main/java/com/jopek/taupngamoni/ChartFragment.java +++ b/app/src/main/java/com/jopek/taupngamoni/ChartFragment.java @@ -1,13 +1,28 @@ package com.jopek.taupngamoni; import android.os.Bundle; - -import androidx.fragment.app.Fragment; - import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import androidx.fragment.app.Fragment; + +import com.anychart.AnyChart; +import com.anychart.AnyChartView; +import com.anychart.chart.common.dataentry.DataEntry; +import com.anychart.chart.common.dataentry.ValueDataEntry; +import com.anychart.charts.Cartesian; +import com.anychart.core.cartesian.series.BaseWithMarkers; +import com.anychart.data.Mapping; +import com.anychart.data.Set; +import com.anychart.enums.Anchor; +import com.anychart.enums.MarkerType; +import com.anychart.enums.TooltipPositionMode; +import com.anychart.graphics.vector.Stroke; + +import java.util.ArrayList; +import java.util.List; + /** * A simple {@link Fragment} subclass. * Use the {@link ChartFragment#newInstance} factory method to @@ -23,6 +38,12 @@ public class ChartFragment extends Fragment { // TODO: Rename and change types of parameters private String mParam1; private String mParam2; + private AnyChartView anyChartView; + private Cartesian cartesian; + private boolean columnChart = true; + private List<DataEntry> seriesData = new ArrayList<>(); + + private BaseWithMarkers series; public ChartFragment() { // Required empty public constructor @@ -59,6 +80,94 @@ public class ChartFragment extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment - return inflater.inflate(R.layout.fragment_chart, container, false); + View view = inflater.inflate(R.layout.fragment_chart, container, false); + anyChartView = view.findViewById(R.id.any_chart_view); + anyChartView.setProgressBar(view.findViewById(R.id.progress_bar)); + + cartesian = AnyChart.line(); + cartesian.animation(true); + cartesian.padding(10d, 20d, 5d, 20d); + + cartesian.crosshair().enabled(true); + cartesian.crosshair() + .yLabel(true) + .yStroke((Stroke) null, null, null, (String) null, (String) null); + cartesian.tooltip().positionMode(TooltipPositionMode.POINT); + + seriesData = new ArrayList<>(); + seriesData.add(new CustomDataEntry("1986", 3.6)); + seriesData.add(new CustomDataEntry("1987", 7.1)); + seriesData.add(new CustomDataEntry("1988", 8.5)); + seriesData.add(new CustomDataEntry("1989", 9.2)); + seriesData.add(new CustomDataEntry("1990", 10.1)); + + refreshChart(); + anyChartView.setChart(cartesian); + +// cartesian.title("Trend of Sales of the Most Popular Products of ACME Corp."); + +// cartesian.yAxis(0).title("Number of Bottles Sold (thousands)"); +// cartesian.xAxis(0).labels().padding(5d, 5d, 5d, 5d); + + + view.findViewById(R.id.b1d).setOnClickListener(v -> onBtnClick("1d")); + view.findViewById(R.id.b7d).setOnClickListener(v -> onBtnClick("7d")); + view.findViewById(R.id.b1m).setOnClickListener(v -> onBtnClick("1m")); + view.findViewById(R.id.b3m).setOnClickListener(v -> onBtnClick("3m")); + view.findViewById(R.id.b6m).setOnClickListener(v -> onBtnClick("6m")); + view.findViewById(R.id.b1y).setOnClickListener(v -> onBtnClick("1y")); + + view.findViewById(R.id.btn_change_chart_type).setOnClickListener(v -> { + columnChart = !columnChart; + refreshChart(); + }); + + return view; + } + + public void refreshChart() { + cartesian.removeAllSeries(); + + Set set = Set.instantiate(); + set.data(seriesData); + Mapping seriesMapping = set.mapAs("{ x: 'x', value: 'value' }"); + + if (columnChart) + series = cartesian.column(seriesMapping); + else + series = cartesian.line(seriesMapping); + + series.data(seriesMapping); +// series.name("Brandy"); + series.hovered().markers() + .enabled(true) + .type(MarkerType.CIRCLE) + .size(4d); + series.tooltip() + .position("right") + .anchor(Anchor.LEFT_CENTER) + .offsetX(5d) + .offsetY(5d); + + cartesian.legend().enabled(true) + .fontSize(13d) + .padding(0d, 0d, 10d, 0d); + } + + public void onBtnClick(String time) { + seriesData = new ArrayList<>(); + seriesData.add(new CustomDataEntry("1982", 8.5)); + seriesData.add(new CustomDataEntry("1992", 7.1)); + seriesData.add(new CustomDataEntry("1992", 10.1)); + seriesData.add(new CustomDataEntry("1992", 9.2)); + seriesData.add(new CustomDataEntry("1992", 3.6)); + + refreshChart(); + } + + private class CustomDataEntry extends ValueDataEntry { + CustomDataEntry(String x, Number value) { + super(x, value); + } } }
\ No newline at end of file diff --git a/app/src/main/java/com/jopek/taupngamoni/Currency.java b/app/src/main/java/com/jopek/taupngamoni/Currency.java index e6c0779..3eb40d2 100644 --- a/app/src/main/java/com/jopek/taupngamoni/Currency.java +++ b/app/src/main/java/com/jopek/taupngamoni/Currency.java @@ -8,25 +8,23 @@ public class Currency { public Bitmap img; public String symbol; public double conversionFactor; - public Currency conversionTo; - public Currency(String code, String description, Bitmap img, String symbol, double conversionFactor, Currency conversionTo) { + public static final Currency USD = new Currency("USD", "United States Dollar", null, "$", 0.0); + public static final Currency GBP = new Currency("GBP", "British Pound Sterling", null, "£", 0.0); + public static final Currency CHF = new Currency("CHF", "Swiss Franc", null, "Fr", 0.0); + public static final Currency PLN = new Currency("PLN", "Polish Zloty", null, "zł", 0.0); + public static final Currency EUR = new Currency("EUR", "Euro", null, "€", 0.0); + public static final Currency JPY = new Currency("JPY", "Japanese Yen", null, "¥", 0.0); + public static final Currency CNY = new Currency("CNY", "Chinese Yuan", null, "¥", 0.0); + public static final Currency UAH = new Currency("UAH", "Ukrainian Hryvnia", null, "₴", 0.0); + public static final Currency CZK = new Currency("CZK", "Czech Republic Koruna", null, "Kč", 0.0); + public static final Currency TWD = new Currency("TWD", "New Taiwan Dollar", null, "圓", 0.0); + + public Currency(String code, String description, Bitmap img, String symbol, double conversionFactor) { this.code = code; this.description = description; this.img = img; this.symbol = symbol; this.conversionFactor = conversionFactor; - this.conversionTo = conversionTo; } - - public static final Currency USD = new Currency("USD", "United States Dollar", null, "$", 0.0, null); - public static final Currency GBP = new Currency("GBP", "British Pound Sterling", null, "£", 2.123, null); - public static final Currency CHF = new Currency("CHF", "Swiss Franc", null, "Fr", 0.0, null); - public static final Currency PLN = new Currency("PLN", "Polish Zloty", null, "zł", 0.0, null); - public static final Currency EUR = new Currency("EUR", "Euro", null, "€", 0.0, null); - public static final Currency JPY = new Currency("JPY", "Japanese Yen", null, "¥", 0.0, null); - public static final Currency CNY = new Currency("CNY", "Chinese Yuan", null, "¥", 0.0, null); - public static final Currency UAH = new Currency("UAH", "Ukrainian Hryvnia", null, "₴", 0.0, null); - public static final Currency CZK = new Currency("CZK", "Czech Republic Koruna", null, "Kč", 0.0, null); - public static final Currency TWD = new Currency("TWD", "New Taiwan Dollar", null, "圓", 0.0, null); } diff --git a/app/src/main/java/com/jopek/taupngamoni/HomeFragment.java b/app/src/main/java/com/jopek/taupngamoni/HomeFragment.java index 9a25328..89738fa 100644 --- a/app/src/main/java/com/jopek/taupngamoni/HomeFragment.java +++ b/app/src/main/java/com/jopek/taupngamoni/HomeFragment.java @@ -1,15 +1,17 @@ package com.jopek.taupngamoni; -import android.annotation.SuppressLint; import android.content.Context; -import android.graphics.Bitmap; import android.os.Bundle; +import android.text.Editable; +import android.text.TextWatcher; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; +import android.widget.EditText; import android.widget.Spinner; +import android.widget.TextView; import android.widget.Toast; import androidx.fragment.app.Fragment; @@ -17,7 +19,8 @@ import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; -import com.jopek.taupngamoni.placeholder.PlaceholderContent; +import org.json.JSONException; +import org.json.JSONObject; import java.util.ArrayList; @@ -28,13 +31,14 @@ public class HomeFragment extends Fragment implements AdapterView.OnItemSelected // TODO: Customize parameter argument names private static final String ARG_COLUMN_COUNT = "column-count"; + public static Currency conversionTo; + public static double conversionAmount = 1.0; + // TODO: Customize parameters private int mColumnCount = 1; private ArrayList<Currency> currencies = new ArrayList<>(); private ArrayList<Currency> selectedCurrencies = new ArrayList<>(); - private HomeRecyclerAdapter recyclerAdapter = new HomeRecyclerAdapter(selectedCurrencies); - private RecyclerView recyclerView; - private Currency selectedCurrency = Currency.USD; + private HomeRecyclerAdapter recyclerAdapter = new HomeRecyclerAdapter(selectedCurrencies, code -> selectedCurrencies.removeIf(cur -> cur.code.equals(code))); private MySpinnerAdapter spinnerAdapter; private Spinner spinner; @@ -79,7 +83,7 @@ public class HomeFragment extends Fragment implements AdapterView.OnItemSelected int finalI = i; requestImage.getBitmap("https://wise.com/public-resources/assets/flags/rectangle/" + cur.code.toLowerCase() + ".png", bitmap -> { cur.img = bitmap; - cur.conversionTo = Currency.USD; + HomeFragment.conversionTo = Currency.USD; if (finalI + 1 == currencies.size()) { spinner.setAdapter(spinnerAdapter); recyclerAdapter.notifyDataSetChanged(); @@ -88,7 +92,6 @@ public class HomeFragment extends Fragment implements AdapterView.OnItemSelected } spinnerAdapter = new MySpinnerAdapter(requireContext(), currencies); - if (getArguments() != null) { mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT); } @@ -104,28 +107,68 @@ public class HomeFragment extends Fragment implements AdapterView.OnItemSelected spinner = view.findViewById(R.id.spinner); spinner.setOnItemSelectedListener(this); - if (recyclerView instanceof RecyclerView) { - Context context = view.getContext(); -// recyclerView = (RecyclerView) view; - if (mColumnCount <= 1) { - recyclerView.setLayoutManager(new LinearLayoutManager(context)); - } else { - recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount)); + EditText amountInput = view.findViewById(R.id.amount_input); + amountInput.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + } + @Override + public void afterTextChanged(Editable s) { + String val = s.toString(); + if (!val.equals("")) { + HomeFragment.conversionAmount = Double.parseDouble(val); + } else { + HomeFragment.conversionAmount = 0.0; + } + recyclerAdapter.notifyDataSetChanged(); + } + }); + TextView btnAdd = view.findViewById(R.id.btn_add); + btnAdd.setOnClickListener(v -> { + if(selectedCurrencies.stream().noneMatch(cur -> cur.code.equals(HomeFragment.conversionTo.code))) { + selectedCurrencies.add(HomeFragment.conversionTo); + updateList(); } - recyclerView.setAdapter(recyclerAdapter); + }); + + Context context = view.getContext(); + if (mColumnCount <= 1) { + recyclerView.setLayoutManager(new LinearLayoutManager(context)); + } else { + recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount)); } + recyclerView.setAdapter(recyclerAdapter); return view; } //Performing action onItemSelected and onNothing selected @Override public void onItemSelected(AdapterView<?> arg0, View view, int position, long id) { - Toast.makeText(requireContext(), currencies.get(position).code, Toast.LENGTH_LONG).show(); - for (Currency cur: selectedCurrencies) { - cur.conversionTo = currencies.get(position); - } - recyclerAdapter.notifyDataSetChanged(); + Currency selCur = currencies.get(position); + HomeFragment.conversionTo = selCur; + updateList(); } + public void updateList() { + String url = "https://api.exchangerate.host/latest?base=" + HomeFragment.conversionTo.code + "&symbols="; + for (Currency cur : selectedCurrencies) { + url += cur.code + ","; + } + Requests request = new Requests(); + request.getJson(url, json -> { + try { + JSONObject rates = json.getJSONObject("rates"); + for (Currency cur : selectedCurrencies) { + cur.conversionFactor = rates.getDouble(cur.code); + } + } catch (JSONException e) { + e.printStackTrace(); + } + recyclerAdapter.notifyDataSetChanged(); + }); + } @Override public void onNothingSelected(AdapterView<?> arg0) { diff --git a/app/src/main/java/com/jopek/taupngamoni/HomeRecyclerAdapter.java b/app/src/main/java/com/jopek/taupngamoni/HomeRecyclerAdapter.java index 40c9614..2bab32c 100644 --- a/app/src/main/java/com/jopek/taupngamoni/HomeRecyclerAdapter.java +++ b/app/src/main/java/com/jopek/taupngamoni/HomeRecyclerAdapter.java @@ -9,9 +9,9 @@ import android.widget.TextView; import androidx.recyclerview.widget.RecyclerView; import com.jopek.taupngamoni.databinding.FragmentScreenHomeBinding; -import com.jopek.taupngamoni.placeholder.PlaceholderContent.PlaceholderItem; import java.util.List; +import java.util.function.Consumer; /** * {@link RecyclerView.Adapter} that can display a {@link Currency}. @@ -20,14 +20,15 @@ import java.util.List; public class HomeRecyclerAdapter extends RecyclerView.Adapter<HomeRecyclerAdapter.ViewHolder> { private final List<Currency> currencies; + public Consumer<String> cb; - public HomeRecyclerAdapter(List<Currency> items) { - currencies = items; + public HomeRecyclerAdapter(List<Currency> currencies, Consumer<String> cb) { + this.currencies = currencies; + this.cb = cb; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { - Log.d("maks", "onCreateViewHolder: " + parent); return new ViewHolder(FragmentScreenHomeBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false)); } @@ -38,11 +39,17 @@ public class HomeRecyclerAdapter extends RecyclerView.Adapter<HomeRecyclerAdapte holder.img.setImageBitmap(item.img); holder.code.setText(item.code); holder.description.setText(item.description); + + holder.itemView.setOnLongClickListener(v -> { + cb.accept(item.code); + return false; + }); + String valueToForeign; String oneToForeign; try { - valueToForeign = item.conversionTo.symbol + MainActivity.round(1 / item.conversionFactor, 4); - oneToForeign = "1 " + item.code + " = " + item.conversionFactor + " " + item.conversionTo.code; + valueToForeign = item.symbol + " " + MainActivity.round(HomeFragment.conversionAmount * item.conversionFactor, 6); + oneToForeign = "1 " + item.code + " = " + item.conversionFactor + " " + HomeFragment.conversionTo.code; } catch (NullPointerException exception) { valueToForeign = "Error"; oneToForeign = "Error"; diff --git a/app/src/main/res/layout/fragment_chart.xml b/app/src/main/res/layout/fragment_chart.xml index be8e789..dcf4778 100644 --- a/app/src/main/res/layout/fragment_chart.xml +++ b/app/src/main/res/layout/fragment_chart.xml @@ -1,14 +1,112 @@ <?xml version="1.0" encoding="utf-8"?> -<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" +<LinearLayout 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" + android:orientation="vertical" tools:context=".ChartFragment"> - <!-- TODO: Update blank fragment layout --> - <TextView + <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" - android:text="@string/hello_blank_fragment" /> + android:layout_weight="4" + android:orientation="horizontal"> -</FrameLayout>
\ No newline at end of file + <Spinner + android:id="@+id/cur_from" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:layout_weight="1" /> + + <Spinner + android:id="@+id/cur_to" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:layout_weight="1" /> + </LinearLayout> + + <Button + android:id="@+id/btn_change_chart_type" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_gravity="center" + android:text="Change chart type" /> + + <LinearLayout + android:layout_width="match_parent" + android:layout_height="match_parent" + android:layout_weight="4" + android:orientation="horizontal"> + + <Button + android:id="@+id/b1d" + style="?android:attr/buttonBarButtonStyle" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:layout_weight="1" + android:text="1D" /> + + <Button + android:id="@+id/b7d" + style="?android:attr/buttonBarButtonStyle" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:layout_weight="1" + android:text="7D" /> + + <Button + android:id="@+id/b1m" + style="?android:attr/buttonBarButtonStyle" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:layout_weight="1" + android:text="1M" /> + + <Button + android:id="@+id/b3m" + style="?android:attr/buttonBarButtonStyle" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:layout_weight="1" + android:text="3M" /> + + <Button + android:id="@+id/b6m" + style="?android:attr/buttonBarButtonStyle" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:layout_weight="1" + android:text="6M" /> + + <Button + android:id="@+id/b1y" + style="?android:attr/buttonBarButtonStyle" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:layout_weight="1" + android:text="1Y" /> + </LinearLayout> + + <androidx.constraintlayout.widget.ConstraintLayout + android:layout_width="match_parent" + android:layout_height="match_parent" + android:layout_weight="1"> + + <!-- <View--> + <com.anychart.AnyChartView + android:id="@+id/any_chart_view" + android:layout_width="match_parent" + android:layout_height="match_parent" /> + + <ProgressBar + android:id="@+id/progress_bar" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent" /> + + </androidx.constraintlayout.widget.ConstraintLayout> +</LinearLayout>
\ No newline at end of file diff --git a/app/src/main/res/layout/fragment_screen_home_list.xml b/app/src/main/res/layout/fragment_screen_home_list.xml index 32d2ab3..a688366 100644 --- a/app/src/main/res/layout/fragment_screen_home_list.xml +++ b/app/src/main/res/layout/fragment_screen_home_list.xml @@ -23,6 +23,9 @@ android:layout_weight="1" /> <EditText + android:id="@+id/amount_input" + android:text="1.0000" + android:inputType="number" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" /> @@ -31,11 +34,12 @@ <TextView android:layout_width="wrap_content" android:background="@color/teal_700" - android:text="Add new currency to list" + android:text="Add current currency to list" android:id="@+id/btn_add" android:textSize="25sp" android:paddingLeft="7dp" android:paddingRight="7dp" + android:layout_marginBottom="15dp" android:layout_gravity="center" android:layout_height="wrap_content"/> |
