From 926c2c612ad1011c07202a61e3adf76ecf8f7ef6 Mon Sep 17 00:00:00 2001 From: akaessens <24660231+akaessens@users.noreply.github.com> Date: Fri, 4 Sep 2020 13:23:19 +0200 Subject: [PATCH] first recyclerview with multiple events prototype --- app/build.gradle | 3 +- .../akdev/nofbeventscraper/EventAdapter.java | 104 +++++++++++++++ .../com/akdev/nofbeventscraper/FbEvent.java | 23 ++++ .../com/akdev/nofbeventscraper/FbScraper.java | 14 ++- .../akdev/nofbeventscraper/MainActivity.java | 118 ++++++++---------- app/src/main/res/layout/activity_main.xml | 34 +++-- app/src/main/res/layout/content_main.xml | 49 +++++++- app/src/main/res/layout/item_event.xml | 112 +++++++++++++++++ 8 files changed, 369 insertions(+), 88 deletions(-) create mode 100644 app/src/main/java/com/akdev/nofbeventscraper/EventAdapter.java create mode 100644 app/src/main/res/layout/item_event.xml diff --git a/app/build.gradle b/app/build.gradle index f81b511..cc442ae 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -25,11 +25,12 @@ android { dependencies { 'androidx.coordinatorlayout:coordinatorlayout:1.1.0' + implementation 'androidx.recyclerview:recyclerview:1.1.0' implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.appcompat:appcompat:1.2.0' - implementation 'com.google.android.material:material:1.2.0' + implementation 'com.google.android.material:material:1.2.1' implementation 'androidx.navigation:navigation-fragment:2.3.0' implementation 'androidx.navigation:navigation-ui:2.3.0' diff --git a/app/src/main/java/com/akdev/nofbeventscraper/EventAdapter.java b/app/src/main/java/com/akdev/nofbeventscraper/EventAdapter.java new file mode 100644 index 0000000..dd8d3ce --- /dev/null +++ b/app/src/main/java/com/akdev/nofbeventscraper/EventAdapter.java @@ -0,0 +1,104 @@ +package com.akdev.nofbeventscraper; + + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import androidx.recyclerview.widget.RecyclerView; + +import com.google.android.material.textfield.TextInputEditText; + +import java.util.List; + + +public class EventAdapter extends + RecyclerView.Adapter { + + private List events; + + // Pass in the contact array into the constructor + public EventAdapter(List events) { + this.events = events; + } + + @Override + public EventAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int view_type) { + Context context = parent.getContext(); + LayoutInflater inflater = LayoutInflater.from(context); + + // Inflate the custom layout + View contact_view = inflater.inflate(R.layout.item_event, parent, false); + + // Return a new holder instance + return new ViewHolder(contact_view); + } + @Override + public void onBindViewHolder(EventAdapter.ViewHolder holder, int position) { + // Get the data model based on position + FbEvent event = events.get(position); + + // Set item views based on your views and data model + if (event.name.equals("")) { + //holder.edit_text_event_name.setError(R.string.error_no_name); + } else { + holder.edit_text_event_name.setText(event.name); + } + + if (event.start_date == null) { + //holder.edit_text_event_start.setError(getString(R.string.error_no_start_date)); + } else { + String str = FbEvent.dateTimeToString(event.start_date); + holder.edit_text_event_start.setText(str); + } + + if (event.end_date == null) { + //edit_text_event_end.setError(getString(R.string.error_no_end_date)); + } else { + String str = FbEvent.dateTimeToString(event.end_date); + holder.edit_text_event_end.setText(str); + } + + if (event.location.equals("")) { + //edit_text_event_location.setError(getString(R.string.error_no_location)); + } else { + holder.edit_text_event_location.setText(event.location); + //layout_event_location.setEndIconVisible(true); + } + + if (event.description.equals("")) { + //holder.edit_text_event_description.setError(getString(R.string.error_no_description)); + } else { + holder.edit_text_event_description.setText(event.description); + } + } + + // Returns the total count of items in the list + @Override + public int getItemCount() { + return events.size(); + } + + public class ViewHolder extends RecyclerView.ViewHolder { + + protected TextInputEditText edit_text_event_name; + protected TextInputEditText edit_text_event_start; + protected TextInputEditText edit_text_event_end; + protected TextInputEditText edit_text_event_location; + protected TextInputEditText edit_text_event_description; + + + public ViewHolder(View item_view) { + super(item_view); + + + edit_text_event_name = (TextInputEditText) item_view.findViewById(R.id.edit_text_event_name); + edit_text_event_start = (TextInputEditText) item_view.findViewById(R.id.edit_text_event_start); + edit_text_event_end = (TextInputEditText) item_view.findViewById(R.id.edit_text_event_end); + edit_text_event_location = (TextInputEditText) item_view.findViewById(R.id.edit_text_event_location); + edit_text_event_description = (TextInputEditText) item_view.findViewById(R.id.edit_text_event_description); + + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/akdev/nofbeventscraper/FbEvent.java b/app/src/main/java/com/akdev/nofbeventscraper/FbEvent.java index 707afd5..4a3704e 100644 --- a/app/src/main/java/com/akdev/nofbeventscraper/FbEvent.java +++ b/app/src/main/java/com/akdev/nofbeventscraper/FbEvent.java @@ -1,6 +1,10 @@ package com.akdev.nofbeventscraper; +import android.app.usage.UsageEvents; +import android.util.EventLog; + import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Date; import java.util.Locale; @@ -18,6 +22,15 @@ public class FbEvent { public final String location; public final String image_url; + public FbEvent() { + url = "url"; + name= "name"; + start_date = null; + end_date = null; + description = "description"; + location = "location"; + image_url = null; + } public FbEvent(String url, String name, Date start_date, Date end_date, String description, String location, String image_url) { @@ -30,6 +43,16 @@ public class FbEvent { this.image_url = image_url; } + public static ArrayList createEventList(int num_events) { + ArrayList events = new ArrayList(); + + for (int i = 1; i <= num_events; i++) { + events.add(new FbEvent()); + } + + return events; + } + /** * Converts datetime to epoch. * diff --git a/app/src/main/java/com/akdev/nofbeventscraper/FbScraper.java b/app/src/main/java/com/akdev/nofbeventscraper/FbScraper.java index 40067f3..fd0a661 100644 --- a/app/src/main/java/com/akdev/nofbeventscraper/FbScraper.java +++ b/app/src/main/java/com/akdev/nofbeventscraper/FbScraper.java @@ -17,10 +17,13 @@ import java.net.URISyntaxException; import java.net.URL; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.List; import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; +import static com.akdev.nofbeventscraper.FbEvent.createEventList; + /** * This class can asynchronously scrape public facebook events * and gather the most important information. It is stored in a FbEvent object. @@ -30,7 +33,7 @@ public class FbScraper extends AsyncTask { private int error; private String input_url; private WeakReference main; // no context leak with WeakReference - private FbEvent event; + private List events; /** * Constructor with WeakReference to the main activity, to update it's text fields. @@ -41,6 +44,7 @@ public class FbScraper extends AsyncTask { FbScraper(WeakReference main, String input_url) { this.main = main; this.input_url = input_url; + this.events = createEventList(0); } /** @@ -222,8 +226,8 @@ public class FbScraper extends AsyncTask { // ignore } - event = new FbEvent(url, name, start_date, end_date, description, location, image_url); - + FbEvent event = new FbEvent(url, name, start_date, end_date, description, location, image_url); + this.events.add(event); } catch (URISyntaxException | MalformedURLException e) { e.printStackTrace(); @@ -257,8 +261,8 @@ public class FbScraper extends AsyncTask { super.onPostExecute(aVoid); if (main != null) { - if (this.event != null) { - main.get().update(event); + if (! this.events.isEmpty()) { + main.get().update(events); } else { main.get().error(error); main.get().clear(false); diff --git a/app/src/main/java/com/akdev/nofbeventscraper/MainActivity.java b/app/src/main/java/com/akdev/nofbeventscraper/MainActivity.java index 65c3c13..a5c42ef 100644 --- a/app/src/main/java/com/akdev/nofbeventscraper/MainActivity.java +++ b/app/src/main/java/com/akdev/nofbeventscraper/MainActivity.java @@ -17,6 +17,8 @@ import android.widget.ImageView; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.view.menu.MenuBuilder; import androidx.appcompat.widget.Toolbar; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.CollapsingToolbarLayout; @@ -25,8 +27,10 @@ import com.google.android.material.textfield.TextInputLayout; import com.squareup.picasso.Picasso; import java.lang.ref.WeakReference; +import java.util.List; import java.util.Objects; +import static com.akdev.nofbeventscraper.FbEvent.createEventList; import static com.akdev.nofbeventscraper.FbEvent.dateTimeToEpoch; public class MainActivity extends AppCompatActivity { @@ -35,55 +39,60 @@ public class MainActivity extends AppCompatActivity { protected Button paste_button; protected TextInputEditText edit_text_uri_input; - protected TextInputEditText edit_text_event_name; - protected TextInputEditText edit_text_event_start; - protected TextInputEditText edit_text_event_end; - protected TextInputEditText edit_text_event_location; - protected TextInputEditText edit_text_event_description; protected TextInputLayout layout_uri_input; - protected TextInputLayout layout_event_location; protected ImageView image_view_toolbar; protected CollapsingToolbarLayout layout_toolbar; protected FbScraper scraper; - protected FbEvent event; + protected List events; + EventAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - Toolbar toolbar = findViewById(R.id.toolbar); - setSupportActionBar(toolbar); - - ok_button = (Button) findViewById(R.id.ok_button); - paste_button = (Button) findViewById(R.id.paste_button); edit_text_uri_input = (TextInputEditText) findViewById(R.id.edit_text_uri_input); - edit_text_event_name = (TextInputEditText) findViewById(R.id.edit_text_event_name); - edit_text_event_start = (TextInputEditText) findViewById(R.id.edit_text_event_start); - edit_text_event_end = (TextInputEditText) findViewById(R.id.edit_text_event_end); - edit_text_event_location = (TextInputEditText) findViewById(R.id.edit_text_event_location); - edit_text_event_description = (TextInputEditText) findViewById(R.id.edit_text_event_description); - layout_uri_input = (TextInputLayout) findViewById(R.id.layout_uri_input); - layout_event_location = (TextInputLayout) findViewById(R.id.layout_event_location); + // Lookup the recyclerview in activity layout + RecyclerView recycler_view = (RecyclerView) findViewById(R.id.recycler_view); + + // Initialize contacts + events = createEventList(3); + + // Create adapter passing in the sample user data + adapter = new EventAdapter(events); + // Attach the adapter to the recyclerview to populate items + recycler_view.setAdapter(adapter); + // Set layout manager to position the items + recycler_view.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)); + + // That's all! + /*Toolbar toolbar = findViewById(R.id.toolbar); + setSupportActionBar(toolbar); + + //ok_button = (Button) findViewById(R.id.ok_button); + paste_button = (Button) findViewById(R.id.paste_button); + + + layout_toolbar = (CollapsingToolbarLayout) findViewById(R.id.layout_toolbar); - image_view_toolbar = (ImageView) findViewById(R.id.image_view); + image_view_toolbar = (ImageView) findViewById(R.id.image_view);*/ /* * Default view settings */ - ok_button.setEnabled(false); - layout_event_location.setEndIconVisible(false); - image_view_toolbar.setImageResource(R.drawable.ic_banner_foreground); + //ok_button.setEnabled(false); + //layout_event_location.setEndIconVisible(false); + //image_view_toolbar.setImageResource(R.drawable.ic_banner_foreground); /* * Display title only when toolbar is collapsed */ - AppBarLayout app_bar_layout = (AppBarLayout) findViewById(R.id.app_bar); + /*AppBarLayout app_bar_layout = (AppBarLayout) findViewById(R.id.app_bar); app_bar_layout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { boolean show = true; int scroll_range = -1; @@ -106,7 +115,7 @@ public class MainActivity extends AppCompatActivity { /* * Paste button: get last entry from clipboard */ - paste_button.setOnClickListener(new View.OnClickListener() { + /*paste_button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -141,7 +150,7 @@ public class MainActivity extends AppCompatActivity { /* * Maps button: launch maps intent */ - layout_event_location.setEndIconOnClickListener(new View.OnClickListener() { + /*layout_event_location.setEndIconOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String map_search = "geo:0,0?q=" + edit_text_event_location.getText(); @@ -152,12 +161,12 @@ public class MainActivity extends AppCompatActivity { startActivity(map_intent); } } - }); + });*/ /* * Add to calendar button: launch calendar application */ - ok_button.setOnClickListener(new View.OnClickListener() { + /*ok_button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -179,7 +188,7 @@ public class MainActivity extends AppCompatActivity { startActivity(intent); } } - }); + });*/ /* * Enter button in uri input: start scraping @@ -249,7 +258,7 @@ public class MainActivity extends AppCompatActivity { edit_text_uri_input.setText(""); layout_uri_input.setError(null); } - edit_text_event_name.setText(""); + /*edit_text_event_name.setText(""); edit_text_event_start.setText(""); edit_text_event_end.setText(""); edit_text_event_location.setText(""); @@ -259,7 +268,9 @@ public class MainActivity extends AppCompatActivity { edit_text_event_start.setError(null); edit_text_event_end.setError(null); edit_text_event_location.setError(null); - edit_text_event_description.setError(null); + edit_text_event_description.setError(null);*/ + + this.events.clear(); try { scraper.cancel(true); @@ -269,62 +280,33 @@ public class MainActivity extends AppCompatActivity { } - ok_button.setEnabled(false); + /*ok_button.setEnabled(false); layout_event_location.setEndIconVisible(false); - image_view_toolbar.setImageResource(R.drawable.ic_banner_foreground); + image_view_toolbar.setImageResource(R.drawable.ic_banner_foreground);*/ } /** * Updates the text fields with the event information provided. * If something is missing, the corresponding test field will show an error. * - * @param scraped_event the event information that was scraped by FbScraper + * @param events the event information that was scraped by FbScraper */ - public void update(FbEvent scraped_event) { + public void update(List events) { - this.event = scraped_event; + this.events.addAll(0, events); - edit_text_uri_input.setText(event.url); + adapter.notifyItemInserted(0); - if (event.name.equals("")) { - edit_text_event_name.setError(getString(R.string.error_no_name)); - } else { - edit_text_event_name.setText(event.name); - } + /*edit_text_uri_input.setText(event.url); - if (event.start_date == null) { - edit_text_event_start.setError(getString(R.string.error_no_start_date)); - } else { - String str = FbEvent.dateTimeToString(event.start_date); - edit_text_event_start.setText(str); - } - if (event.end_date == null) { - edit_text_event_end.setError(getString(R.string.error_no_end_date)); - } else { - String str = FbEvent.dateTimeToString(event.end_date); - edit_text_event_end.setText(str); - } - - if (event.location.equals("")) { - edit_text_event_location.setError(getString(R.string.error_no_location)); - } else { - edit_text_event_location.setText(event.location); - layout_event_location.setEndIconVisible(true); - } - - if (event.description.equals("")) { - edit_text_event_description.setError(getString(R.string.error_no_description)); - } else { - edit_text_event_description.setText(event.description); - } Picasso.get() .load(event.image_url) .placeholder(R.drawable.ic_banner_foreground) .into(image_view_toolbar); - ok_button.setEnabled(true); + ok_button.setEnabled(true);*/ } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index f7b82dc..68528b9 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,6 +1,15 @@ - + + + + + + + + - + @@ -63,7 +72,8 @@ android:tooltipText="@string/tooltip_paste" tools:ignore="UnusedAttribute" /> - + + - +--> diff --git a/app/src/main/res/layout/content_main.xml b/app/src/main/res/layout/content_main.xml index 0422d3f..283bc53 100644 --- a/app/src/main/res/layout/content_main.xml +++ b/app/src/main/res/layout/content_main.xml @@ -1,5 +1,50 @@ - + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_event.xml b/app/src/main/res/layout/item_event.xml new file mode 100644 index 0000000..26c40e1 --- /dev/null +++ b/app/src/main/res/layout/item_event.xml @@ -0,0 +1,112 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +