Skip to content

Instantly share code, notes, and snippets.

@mattiaferigutti
Created April 27, 2021 21:59
Show Gist options
  • Save mattiaferigutti/7e270eac21a80031677876d496c80171 to your computer and use it in GitHub Desktop.
Save mattiaferigutti/7e270eac21a80031677876d496c80171 to your computer and use it in GitHub Desktop.
package com.example.bettingsignals.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.example.bettingsignals.Activity.MainActivity;
import com.example.bettingsignals.Adapter.FragmentLifeCycles;
import com.example.bettingsignals.Adapter.RecyclerViewAdapterHome;
import com.example.bettingsignals.Database.Article;
import com.example.bettingsignals.OnDataLoaded;
import com.example.bettingsignals.R;
import java.util.ArrayList;
import java.util.List;
public class Day02Fragment extends Fragment implements FragmentLifeCycles, SwipeRefreshLayout.OnRefreshListener, OnDataLoaded {
private View v;
private SwipeRefreshLayout refreshLayout;
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
((MainActivity) getActivity()).setOnDataLoadedListener(this);
}
public Day02Fragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
if (savedInstanceState == null) {
v = inflater.inflate(R.layout.fragment_day02, container, false);
}
return v;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
refreshLayout = view.findViewById(R.id.swipe_container);
refreshLayout.setOnRefreshListener(this);
//refreshLayout.setColorScheme();
}
/* Imposta il RecyclerView */
private void setUpRecyclerView(View v) {
RecyclerView recyclerView = v.findViewById(R.id.recyclerView);
RecyclerViewAdapterHome adapter = new RecyclerViewAdapterHome(MainActivity.getDay02List(), getActivity());
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
}
@Override
public void dataChanged() {
if (!MainActivity.getDay02List().isEmpty()) {
setUpRecyclerView(v);
}
}
@Override
public void onResumeFragment() {
if (!MainActivity.getDay02List().isEmpty()) {
setUpRecyclerView(v);
}
}
@Override
public void onRefresh() {
new MainActivity().updateDay02(getActivity());
refreshLayout.setRefreshing(false);
}
}
package com.example.bettingsignals.Activity;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.viewpager.widget.ViewPager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.bettingsignals.Adapter.FragmentLifeCycles;
import com.example.bettingsignals.Adapter.PagerAdapter;
import com.example.bettingsignals.Database.Article;
import com.example.bettingsignals.Fragment.Day01Fragment;
import com.example.bettingsignals.Fragment.Day02Fragment;
import com.example.bettingsignals.Fragment.Day03Fragment;
import com.example.bettingsignals.Fragment.Day04Fragment;
import com.example.bettingsignals.Fragment.Day05Fragment;
import com.example.bettingsignals.Fragment.Day06Fragment;
import com.example.bettingsignals.Fragment.Day07Fragment;
import com.example.bettingsignals.HelpActivity;
import com.example.bettingsignals.OnDataLoaded;
import com.example.bettingsignals.R;
import com.google.android.material.tabs.TabLayout;
//import com.squareup.picasso.Picasso;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private ViewPager viewPager;
private TabLayout tabLayout;
private String responseRequest;
private OnDataLoaded onDataLoaded;
private static List<Article> day01List;
private static List<Article> day02List;
private static List<Article> day03List;
private static List<Article> day04List;
private static List<Article> day05List;
private static List<Article> day06List;
private static List<Article> day07List;
private Context mContext;
private PagerAdapter adapter;
ImageButton help_button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
help_button = (ImageButton) findViewById(R.id.icon_help_button);
help_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentLoadNewActivity = new Intent(MainActivity.this, HelpActivity.class);
startActivity(intentLoadNewActivity);
}
});
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mContext = getApplicationContext();
// Inizializa Liste
day01List = new ArrayList<>();
day02List = new ArrayList<>();
day03List = new ArrayList<>();
day04List = new ArrayList<>();
day05List = new ArrayList<>();
day06List = new ArrayList<>();
day07List = new ArrayList<>();
viewPager = findViewById(R.id.viewPager);
tabLayout = findViewById(R.id.tabLayout);
setUpViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
// Download Xml e creo Liste
downloadData("https://os.betsig.com/rss/1.php?rssapikey=yw8o3%C3%A0hr!", day01List, mContext);
downloadData("https://os.betsig.com/rss/2.php?rssapikey=yw8o3%C3%A0hr!", day02List, mContext);
downloadData("https://os.betsig.com/rss/3.php?rssapikey=yw8o3%C3%A0hr!", day03List, mContext);
downloadData("https://os.betsig.com/rss/4.php?rssapikey=yw8o3%C3%A0hr!", day04List, mContext);
downloadData("https://os.betsig.com/rss/5.php?rssapikey=yw8o3%C3%A0hr!", day05List, mContext);
downloadData("https://os.betsig.com/rss/6.php?rssapikey=yw8o3%C3%A0hr!", day06List, mContext);
downloadData("https://os.betsig.com/rss/7.php?rssapikey=yw8o3%C3%A0hr!", day07List, mContext);
// Image Load
/*ImageView imageView = findViewById(R.id.imageBandiera);
String url = "https://cdn.sportmonks.com/images/soccer/leagues/501.png";
Picasso.with(this).load(url).into(imageView);*/
// Move to actual date tab when open app
viewPager.setCurrentItem(1);
}
public void setOnDataLoadedListener(OnDataLoaded onDataLoaded) {
this.onDataLoaded = onDataLoaded;
}
public void updateDay01(Context context) {
day01List.clear();
downloadData("https://os.betsig.com/rss/1.php?rssapikey=yw8o3%C3%A0hr!", day01List, context);
}
public void updateDay02(Context context) {
day02List.clear();
downloadData("https://os.betsig.com/rss/2.php?rssapikey=yw8o3%C3%A0hr!", day02List, context);
}
public void updateDay03(Context context) {
day03List.clear();
downloadData("https://os.betsig.com/rss/3.php?rssapikey=yw8o3%C3%A0hr!", day03List, context);
}
public void updateDay04(Context context) {
day04List.clear();
downloadData("https://os.betsig.com/rss/4.php?rssapikey=yw8o3%C3%A0hr!", day04List, context);
}
public void updateDay05(Context context) {
day05List.clear();
downloadData("https://os.betsig.com/rss/5.php?rssapikey=yw8o3%C3%A0hr!", day05List, context);
}
public void updateDay06(Context context) {
day06List.clear();
downloadData("https://os.betsig.com/rss/6.php?rssapikey=yw8o3%C3%A0hr!", day06List, context);
}
public void updateDay07(Context context) {
day07List.clear();
downloadData("https://os.betsig.com/rss/7.php?rssapikey=yw8o3%C3%A0hr!", day07List, context);
}
private void setUpViewPager(ViewPager viewPager) {
/* Generazione Date dei Giorni */
Calendar calendar = Calendar.getInstance();
/* Giorno 1 */
calendar.add(Calendar.DAY_OF_YEAR, -1);
Date Day01Date = calendar.getTime();
String Day01 = new SimpleDateFormat("EEE\ndd").format(Day01Date);
/* Giorno 2 */
calendar.add(Calendar.DAY_OF_YEAR, 1);
Date Day02Date = calendar.getTime();
String Day02 = new SimpleDateFormat("EEE\ndd").format(Day02Date);
/* Giorno 3 */
calendar.add(Calendar.DAY_OF_YEAR, 1);
Date Day03Date = calendar.getTime();
String Day03 = new SimpleDateFormat("EEE\ndd").format(Day03Date);
/* Giorno 4 */
calendar.add(Calendar.DAY_OF_YEAR, 1);
Date Day04Date = calendar.getTime();
String Day04 = new SimpleDateFormat("EEE\ndd").format(Day04Date);
/* Giorno 5 */
calendar.add(Calendar.DAY_OF_YEAR, 1);
Date Day05Date = calendar.getTime();
String Day05 = new SimpleDateFormat("EEE\ndd").format(Day05Date);
/* Giorno 6 */
calendar.add(Calendar.DAY_OF_YEAR, 1);
Date Day06Date = calendar.getTime();
String Day06 = new SimpleDateFormat("EEE\ndd").format(Day06Date);
/* Giorno 7 */
calendar.add(Calendar.DAY_OF_YEAR, 1);
Date Day07Date = calendar.getTime();
String Day07 = new SimpleDateFormat("EEE\ndd").format(Day07Date);
adapter = new PagerAdapter(getSupportFragmentManager());
adapter.addFragment(Day01, new Day01Fragment());
adapter.addFragment(Day02, new Day02Fragment());
adapter.addFragment(Day03, new Day03Fragment());
adapter.addFragment(Day04, new Day04Fragment());
adapter.addFragment(Day05, new Day05Fragment());
adapter.addFragment(Day06, new Day06Fragment());
adapter.addFragment(Day07, new Day07Fragment());
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(pageChangeListener);
}
public void downloadData(String url, final List<Article> articles, Context context) {
final StringRequest request = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.length() > 0) {
responseRequest = response;
try {
parseXML(articles);
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
Volley.newRequestQueue(context).add(request);
}
private void parseXML(List<Article> articles) throws XmlPullParserException, IOException {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
InputStream is = new ByteArrayInputStream(responseRequest.getBytes("UTF-8"));
parser.setInput(is, null);
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
processParsing(parser, articles);
if (onDataLoaded != null) {
onDataLoaded.dataChanged();
}
}
private void processParsing(XmlPullParser parser, List<Article> articles) throws XmlPullParserException, IOException {
int evenType = parser.getEventType();
Article article = null;
while (evenType != XmlPullParser.END_DOCUMENT) {
String valName;
switch (evenType) {
case XmlPullParser.START_DOCUMENT:
break;
case XmlPullParser.START_TAG:
valName = parser.getName();
if (valName.equals("item")) {
article = new Article();
articles.add(article);
} else if (article != null) {
if (valName.equals("nazione")) {
article.setNazione(parser.nextText());
} else if (valName.equals("data")) {
article.setData(parser.nextText());
} else if (valName.equals("campionato")) {
article.setCampionato(parser.nextText());
} else if (valName.equals("squadracasa")) {
article.setSquadracasa(parser.nextText());
} else if (valName.equals("squadraospite")) {
article.setSquadraospite(parser.nextText());
} else if (valName.equals("squadracasalvresult")) {
article.setSquadracasalvresult(parser.nextText());
} else if (valName.equals("squadraospitelvresult")) {
article.setSquadraospitelvresult(parser.nextText());
} else if (valName.equals("squadracasahtresult")) {
article.setSquadracasahtresult(parser.nextText());
} else if (valName.equals("squadraospitehtresult")) {
article.setSquadraospitehtresult(parser.nextText());
} else if (valName.equals("squadracasaftresult")) {
article.setSquadracasaftresult(parser.nextText());
} else if (valName.equals("squadraospiteftresult")) {
article.setSquadraospiteftresult(parser.nextText());
} else if (valName.equals("market")) {
article.setMarket(parser.nextText());
} else if (valName.equals("quota")) {
article.setQuota(parser.nextText());
} else if (valName.equals("probabilita")) {
article.setProbabilita(parser.nextText());
}
}
break;
}
evenType = parser.next();
}
}
public static List<Article> getDay01List() {
return day01List;
}
public static List<Article> getDay02List() {
return day02List;
}
public static List<Article> getDay03List() {
return day03List;
}
public static List<Article> getDay04List() {
return day04List;
}
public static List<Article> getDay05List() {
return day05List;
}
public static List<Article> getDay06List() {
return day06List;
}
public static List<Article> getDay07List() {
return day07List;
}
private ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
FragmentLifeCycles fragmentLifeCycles = (FragmentLifeCycles) adapter.getItem(position);
fragmentLifeCycles.onResumeFragment();
}
@Override
public void onPageScrollStateChanged(int state) {
}
};
}
package com.example.bettingsignals;
import com.example.bettingsignals.Database.Article;
import java.util.List;
public interface OnDataLoaded {
void dataChanged();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment