Commit da0f1c5b by Patryk Czarnik

Pobieranie walut wdrożone do aplikacji

parent cc32734e
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"> xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET"/>
<application <application
android:allowBackup="true" android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules" android:dataExtractionRules="@xml/data_extraction_rules"
......
package com.example.projekt4; package com.example.projekt4;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment; import androidx.navigation.fragment.NavHostFragment;
import com.example.projekt4.databinding.FragmentFirstBinding; import com.example.projekt4.databinding.FragmentFirstBinding;
import com.example.waluty.ExchangeRatesTable;
import com.example.waluty.ObslugaNBP;
import com.example.waluty.Rate;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class FirstFragment extends Fragment { public class FirstFragment extends Fragment {
private ExecutorService watkiPobierajace = Executors.newSingleThreadExecutor(); // albo fixedThreadPool
private FragmentFirstBinding binding; private FragmentFirstBinding binding;
...@@ -20,7 +30,6 @@ public class FirstFragment extends Fragment { ...@@ -20,7 +30,6 @@ public class FirstFragment extends Fragment {
@NonNull LayoutInflater inflater, ViewGroup container, @NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState Bundle savedInstanceState
) { ) {
binding = FragmentFirstBinding.inflate(inflater, container, false); binding = FragmentFirstBinding.inflate(inflater, container, false);
return binding.getRoot(); return binding.getRoot();
...@@ -33,6 +42,43 @@ public class FirstFragment extends Fragment { ...@@ -33,6 +42,43 @@ public class FirstFragment extends Fragment {
NavHostFragment.findNavController(FirstFragment.this) NavHostFragment.findNavController(FirstFragment.this)
.navigate(R.id.action_FirstFragment_to_SecondFragment) .navigate(R.id.action_FirstFragment_to_SecondFragment)
); );
binding.buttonPobierzBiezace.setOnClickListener(v -> {
wykonajCalePobieranie();
});
}
private void wykonajCalePobieranie() {
Log.d("Waluty", "pobieranie rozpoczęte");
// komunikacji sieciowej nie wolno robić w wątku głównym
watkiPobierajace.submit(() -> {
Log.d("Waluty", "a kuku 1");
ExchangeRatesTable table = ObslugaNBP.pobierzBiezaceKursy();
Log.d("Waluty", "a kuku 2");
// jednak zmiany w wyglądzie UI powinny być wykonane przez wątek główny
this.getActivity().runOnUiThread(() -> {
odswiezWidok(table);
});
});
Log.d("Waluty", "pobieranie zlecone");
}
private void odswiezWidok(ExchangeRatesTable table) {
Log.d("Waluty", "odswiezWidok");
// gdy mamy binding, nie trzeba już find
TextView textViewStatus = binding.textViewStatus;
LinearLayout ll = binding.linearLayoutWaluty;
if(table == null) {
textViewStatus.setText(R.string.brak_danych);
} else {
textViewStatus.setText(table.toString());
ll.removeAllViews();
for(Rate rate : table.getRates()) {
TextView textView = new TextView(this.getContext());
textView.setText(rate.toString());
ll.addView(textView);
}
}
} }
@Override @Override
......
...@@ -20,7 +20,7 @@ import org.w3c.dom.NodeList; ...@@ -20,7 +20,7 @@ import org.w3c.dom.NodeList;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
public class ObslugaNBP { public class ObslugaNBP {
private static final String ADRES = "http://api.nbp.pl/api/exchangerates/tables"; private static final String ADRES = "https://api.nbp.pl/api/exchangerates/tables";
/** Pobiera tabelę z bieżącymi kursami walut. /** Pobiera tabelę z bieżącymi kursami walut.
* Zwraca null w przypadku błędów. * Zwraca null w przypadku błędów.
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView 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:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".FirstFragment"> tools:context=".FirstFragment">
<androidx.constraintlayout.widget.ConstraintLayout <Button
android:id="@+id/button_pobierz_biezace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pobierz"
app:layout_constraintEnd_toStartOf="@id/button_first"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/next"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/button_pobierz_biezace"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text_view_status"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:padding="16dp"> android:layout_marginTop="16dp"
android:text="status"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_pobierz_biezace" />
<Button <ScrollView
android:id="@+id/button_first" android:id="@+id/scroll_lista_walut"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/next" android:layout_marginTop="24dp"
app:layout_constraintBottom_toTopOf="@id/textview_first" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toBottomOf="@+id/text_view_status">
<TextView <LinearLayout
android:id="@+id/textview_first" android:id="@+id/linear_layout_waluty"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="16dp" android:orientation="vertical" />
android:text="@string/lorem_ipsum" </ScrollView>
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" </androidx.constraintlayout.widget.ConstraintLayout>
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/button_first" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
\ No newline at end of file
...@@ -43,4 +43,6 @@ ...@@ -43,4 +43,6 @@
libero vel nunc consequat, quis tincidunt nisl eleifend. Cras bibendum enim a justo luctus libero vel nunc consequat, quis tincidunt nisl eleifend. Cras bibendum enim a justo luctus
vestibulum. Fusce dictum libero quis erat maximus, vitae volutpat diam dignissim. vestibulum. Fusce dictum libero quis erat maximus, vitae volutpat diam dignissim.
</string> </string>
<string name="pobierz">Pobierz bieżące</string>
<string name="brak_danych">Brak danych</string>
</resources> </resources>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment