From 2a8cedad0c14a65616b257ea4bffd0226428b4bf Mon Sep 17 00:00:00 2001 From: Maksymilian Jopek Date: Tue, 11 Oct 2022 22:45:36 +0200 Subject: Good Spinner and static list --- .../main/java/com/jopek/taupngamoni/Currency.java | 32 ++++++++++ .../java/com/jopek/taupngamoni/HomeFragment.java | 68 +++++++++++----------- .../com/jopek/taupngamoni/HomeRecyclerAdapter.java | 58 ++++++++++++------ .../java/com/jopek/taupngamoni/MainActivity.java | 15 ++--- 4 files changed, 112 insertions(+), 61 deletions(-) create mode 100644 app/src/main/java/com/jopek/taupngamoni/Currency.java (limited to 'app/src/main/java') diff --git a/app/src/main/java/com/jopek/taupngamoni/Currency.java b/app/src/main/java/com/jopek/taupngamoni/Currency.java new file mode 100644 index 0000000..e6c0779 --- /dev/null +++ b/app/src/main/java/com/jopek/taupngamoni/Currency.java @@ -0,0 +1,32 @@ +package com.jopek.taupngamoni; + +import android.graphics.Bitmap; + +public class Currency { + public String code; + public String description; + 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) { + 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 604e337..9a25328 100644 --- a/app/src/main/java/com/jopek/taupngamoni/HomeFragment.java +++ b/app/src/main/java/com/jopek/taupngamoni/HomeFragment.java @@ -1,8 +1,10 @@ package com.jopek.taupngamoni; +import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Bitmap; import android.os.Bundle; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -29,6 +31,10 @@ public class HomeFragment extends Fragment implements AdapterView.OnItemSelected // TODO: Customize parameters private int mColumnCount = 1; private ArrayList currencies = new ArrayList<>(); + private ArrayList selectedCurrencies = new ArrayList<>(); + private HomeRecyclerAdapter recyclerAdapter = new HomeRecyclerAdapter(selectedCurrencies); + private RecyclerView recyclerView; + private Currency selectedCurrency = Currency.USD; private MySpinnerAdapter spinnerAdapter; private Spinner spinner; @@ -52,26 +58,31 @@ public class HomeFragment extends Fragment implements AdapterView.OnItemSelected public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - currencies.add(new Currency("USD", "United States Dollar", null)); - currencies.add(new Currency("GBP", "British Pound Sterling", null)); - currencies.add(new Currency("CHF", "Swiss Franc", null)); - currencies.add(new Currency("PLN", "Polish Zloty", null)); - currencies.add(new Currency("EUR", "Euro", null)); - currencies.add(new Currency("JPY", "Japanese Yen", null)); - currencies.add(new Currency("CNY", "Chinese Yuan", null)); - currencies.add(new Currency("UAH", "Ukrainian Hryvnia", null)); - currencies.add(new Currency("CZK", "Czech Republic Koruna", null)); - currencies.add(new Currency("TWD", "New Taiwan Dollar", null)); - - ArrayList currenciesCopy = (ArrayList) currencies.clone(); + currencies.add(Currency.USD); + currencies.add(Currency.GBP); + currencies.add(Currency.CHF); + currencies.add(Currency.PLN); + currencies.add(Currency.EUR); + currencies.add(Currency.JPY); + currencies.add(Currency.CNY); + currencies.add(Currency.UAH); + currencies.add(Currency.CZK); + currencies.add(Currency.TWD); + + selectedCurrencies.add(Currency.USD); + selectedCurrencies.add(Currency.GBP); + selectedCurrencies.add(Currency.PLN); + for (int i = 0; i < currencies.size(); i++) { Currency cur = currencies.get(i); RequestImage requestImage = new RequestImage(); 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; if (finalI + 1 == currencies.size()) { spinner.setAdapter(spinnerAdapter); + recyclerAdapter.notifyDataSetChanged(); } }); } @@ -87,26 +98,21 @@ public class HomeFragment extends Fragment implements AdapterView.OnItemSelected public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_screen_home_list, container, false); + RecyclerView recyclerView = view.findViewById(R.id.list); + Log.d("maks", "onCreateView: " + view + container + recyclerView); spinner = view.findViewById(R.id.spinner); spinner.setOnItemSelectedListener(this); -// spinner.setAdapter(spinnerAdapter); - - //Creating the ArrayAdapter instance having the country list - // ArrayAdapter aa = new ArrayAdapter(requireContext(), R.layout.spinner_item, currenciesCodes.toArray(new String[0])); - // aa.setDropDownViewResource(R.layout.spinner_item); - // Setting the ArrayAdapter data on the Spinner - // spin.setAdapter(new MySpinnerAdapter(requireContext(), currenciesCodes)); - // Set the adapter - if (view instanceof RecyclerView) { + + if (recyclerView instanceof RecyclerView) { Context context = view.getContext(); - RecyclerView recyclerView = (RecyclerView) view; +// recyclerView = (RecyclerView) view; if (mColumnCount <= 1) { recyclerView.setLayoutManager(new LinearLayoutManager(context)); } else { recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount)); } - recyclerView.setAdapter(new HomeRecyclerAdapter(PlaceholderContent.ITEMS)); + recyclerView.setAdapter(recyclerAdapter); } return view; } @@ -115,6 +121,10 @@ public class HomeFragment extends Fragment implements AdapterView.OnItemSelected @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(); } @Override @@ -122,15 +132,3 @@ public class HomeFragment extends Fragment implements AdapterView.OnItemSelected // TODO Auto-generated method stub } } - -class Currency { - public String code; - public String description; - public Bitmap img; - - public Currency(String code, String description, Bitmap img) { - this.code = code; - this.description = description; - this.img = img; - } -} \ No newline at end of file diff --git a/app/src/main/java/com/jopek/taupngamoni/HomeRecyclerAdapter.java b/app/src/main/java/com/jopek/taupngamoni/HomeRecyclerAdapter.java index 43fcad2..40c9614 100644 --- a/app/src/main/java/com/jopek/taupngamoni/HomeRecyclerAdapter.java +++ b/app/src/main/java/com/jopek/taupngamoni/HomeRecyclerAdapter.java @@ -1,61 +1,81 @@ package com.jopek.taupngamoni; -import androidx.recyclerview.widget.RecyclerView; - +import android.util.Log; import android.view.LayoutInflater; import android.view.ViewGroup; +import android.widget.ImageView; import android.widget.TextView; -import com.jopek.taupngamoni.placeholder.PlaceholderContent.PlaceholderItem; +import androidx.recyclerview.widget.RecyclerView; + import com.jopek.taupngamoni.databinding.FragmentScreenHomeBinding; +import com.jopek.taupngamoni.placeholder.PlaceholderContent.PlaceholderItem; import java.util.List; /** - * {@link RecyclerView.Adapter} that can display a {@link PlaceholderItem}. + * {@link RecyclerView.Adapter} that can display a {@link Currency}. * TODO: Replace the implementation with code for your data type. */ public class HomeRecyclerAdapter extends RecyclerView.Adapter { - private final List mValues; + private final List currencies; - public HomeRecyclerAdapter(List items) { - mValues = items; + public HomeRecyclerAdapter(List items) { + currencies = items; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { - + Log.d("maks", "onCreateViewHolder: " + parent); return new ViewHolder(FragmentScreenHomeBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false)); - } @Override public void onBindViewHolder(final ViewHolder holder, int position) { - holder.mItem = mValues.get(position); - holder.mIdView.setText(mValues.get(position).id); - holder.mContentView.setText(mValues.get(position).content); + Currency item = currencies.get(position); + holder.mItem = item; + holder.img.setImageBitmap(item.img); + holder.code.setText(item.code); + holder.description.setText(item.description); + String valueToForeign; + String oneToForeign; + try { + valueToForeign = item.conversionTo.symbol + MainActivity.round(1 / item.conversionFactor, 4); + oneToForeign = "1 " + item.code + " = " + item.conversionFactor + " " + item.conversionTo.code; + } catch (NullPointerException exception) { + valueToForeign = "Error"; + oneToForeign = "Error"; + } + holder.valueToForeign.setText(valueToForeign); + holder.oneToForeign.setText(oneToForeign); } @Override public int getItemCount() { - return mValues.size(); + return currencies.size(); } public class ViewHolder extends RecyclerView.ViewHolder { - public final TextView mIdView; - public final TextView mContentView; - public PlaceholderItem mItem; + public final ImageView img; + public final TextView code; + public final TextView description; + public final TextView valueToForeign; + public final TextView oneToForeign; + public Currency mItem; public ViewHolder(FragmentScreenHomeBinding binding) { super(binding.getRoot()); - mIdView = binding.itemNumber; - mContentView = binding.content; + img = binding.img; + code = binding.code; + description = binding.description; + valueToForeign = binding.valueToForeign; + oneToForeign = binding.oneToForeign; } @Override public String toString() { - return super.toString() + " '" + mContentView.getText() + "'"; + return super.toString() + " '" + code.getText() + "'"; } } } \ No newline at end of file diff --git a/app/src/main/java/com/jopek/taupngamoni/MainActivity.java b/app/src/main/java/com/jopek/taupngamoni/MainActivity.java index 5d30048..dc33dfe 100644 --- a/app/src/main/java/com/jopek/taupngamoni/MainActivity.java +++ b/app/src/main/java/com/jopek/taupngamoni/MainActivity.java @@ -8,6 +8,10 @@ import android.annotation.SuppressLint; import android.os.Bundle; import android.view.MenuItem; +import java.math.RoundingMode; +import java.text.DecimalFormat; +import java.util.Collections; + public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnItemSelectedListener { public static final String TAG = "maks"; @@ -40,13 +44,10 @@ public class MainActivity extends AppCompatActivity implements BottomNavigationV } return false; } -} -class Tuple { - public final X x; - public final Y y; - public Tuple(X x, Y y) { - this.x = x; - this.y = y; + public static String round(double num, int nPlaces) { + DecimalFormat df = new DecimalFormat("#." + String.join("", Collections.nCopies(nPlaces, "#"))); + df.setRoundingMode(RoundingMode.CEILING); + return df.format(num); } } -- cgit v1.3.1