dwlakes

Untitled

Jun 25th, 2022
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.81 KB | None | 0 0
  1. package com.example.newsaggregator;
  2.  
  3. import androidx.annotation.RequiresApi;
  4. import androidx.appcompat.app.AlertDialog;
  5. import androidx.appcompat.app.AppCompatActivity;
  6.  
  7. import android.content.Intent;
  8. import android.net.Uri;
  9. import android.os.AsyncTask;
  10. import android.os.Build;
  11. import android.os.Bundle;
  12. import android.view.View;
  13. import android.widget.Button;
  14. import android.widget.CheckBox;
  15. import android.widget.ImageView;
  16. import android.widget.LinearLayout;
  17. import android.widget.Switch;
  18. import android.widget.Toast;
  19.  
  20. import org.jsoup.Jsoup;
  21. import org.jsoup.nodes.Document;
  22. import org.jsoup.select.Elements;
  23.  
  24. import java.io.IOException;
  25. import java.util.ArrayList;
  26. import java.util.Collections;
  27. import java.util.Comparator;
  28. import java.util.concurrent.Executor;
  29.  
  30. public class SelectSources<closePopup> extends AppCompatActivity {
  31.     ArrayList<String> selectedSources = new ArrayList<>();
  32.     Switch newSwitch;
  33.  
  34.     //Private variables below for displaying the bias popup for source
  35.     private AlertDialog.Builder dialogBuilder;
  36.     private AlertDialog dialog;
  37.     private ImageView credibility;
  38.     private ImageView bias;
  39.     private Button readMore;
  40.     private Button closePopup;
  41.  
  42.  
  43.     @RequiresApi(api = Build.VERSION_CODES.N)
  44.     @Override
  45.     protected void onCreate(Bundle savedInstanceState) {
  46.         super.onCreate(savedInstanceState);
  47.         setContentView(R.layout.activity_select_sources);
  48.         Button selectSourcesButton = findViewById(R.id.selectSourcesButton);
  49.         Switch biasSwitch = findViewById(R.id.biasAnalysisSwitch);
  50.  
  51.         // calling the action bar
  52.         getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  53.         ArrayList<Source> sources = new ArrayList<>();
  54.  
  55.         Source ap = new Source("AP", "https://mediabiasfactcheck.com/associated-press/", null, null);
  56.         Source npr = new Source("NPR", "https://mediabiasfactcheck.com/npr/", null, null);
  57.         Source bbc = new Source("BBC", "https://mediabiasfactcheck.com/bbc/", null, null);
  58.  
  59.  
  60.         Collections.addAll(sources, ap, npr, bbc);
  61.  
  62.         //Alphabetizes sources
  63.         Collections.sort(sources, Source.sourceNameComparator);
  64.  
  65.         for (Source source : sources) {
  66.             scrapeSource(source);
  67.             createCheckButton(source, biasSwitch);
  68.  
  69.         }
  70.         selectSourcesButton.setOnClickListener(view -> {
  71.             Intent intent = new Intent(SelectSources.this,MainActivity.class);
  72.             intent.putExtra("selectedSourcesArray",selectedSources);
  73.             System.out.println(selectedSources);
  74.             startActivity(intent);
  75.         });
  76.     }
  77.  
  78.  
  79.     private void scrapeSource(Source source) {
  80.         Document sourceDoc = null;
  81.         // Finds AP site and scrapes it
  82.         try {
  83.             sourceDoc = Jsoup.connect("https://mediabiasfactcheck.com/associated-press").get();
  84.  
  85.         } catch (IOException e) {
  86.             e.printStackTrace();
  87.         }
  88.         Elements sourceElements = sourceDoc.getElementsByClass("entry-class clearfix");
  89.         System.out.println(sourceElements);
  90.     }
  91.     /*Comparator for sorting the list by Student Name*/
  92.  
  93.  
  94.  
  95.     private void postCheckBox(Switch newSwitch, Source source, Switch biasSwitch) {
  96.         LinearLayout layout = (LinearLayout) findViewById(R.id.rootLayout2);
  97.         layout.addView(newSwitch);
  98.         newSwitch.setOnClickListener(view -> {
  99.             if (newSwitch.isChecked()) {
  100.                 selectedSources.add(newSwitch.getText().toString());
  101.                 if (biasSwitch.isChecked()){
  102.                     showBiasAndCredibility(newSwitch);
  103.                 }
  104.             } else {
  105.                 selectedSources.remove(newSwitch.getText().toString());
  106.             }
  107.  
  108.         });
  109.     }
  110.  
  111.     private void createCheckButton(Source source, Switch biasSwitch) {
  112.  
  113.         newSwitch = new Switch(this);
  114.         newSwitch.setText(source.name);
  115.         newSwitch.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
  116.         postCheckBox(newSwitch, source, biasSwitch);
  117.  
  118.     }
  119.  
  120.     public void showBiasAndCredibility(Switch checkBox){
  121.         dialogBuilder = new AlertDialog.Builder(this);
  122.         final View popupView = getLayoutInflater().inflate(R.layout.popup,null);
  123.         credibility = (ImageView) popupView.findViewById(R.id.credibilityPopup);
  124.         bias = (ImageView) popupView.findViewById(R.id.biasPopup);
  125.         readMore = (Button) popupView.findViewById(R.id.readMore);
  126.         closePopup = (Button) popupView.findViewById(R.id.closePopup);
  127.         readMore.setText("Read more about " +selectedSources.toString().replaceAll("\\[|\\]", "")+"'s bias and credibility");
  128.  
  129.         dialogBuilder.setView(popupView);
  130.         dialog = dialogBuilder.create();
  131.         dialog.show();
  132.  
  133.         closePopup.setOnClickListener(view -> {
  134.             dialog.dismiss();
  135.         });
  136.  
  137.     }
  138.  
  139. }
  140.  
  141.  
Advertisement
Add Comment
Please, Sign In to add comment