• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Structure of my app

Since I'm a student in high school not learning any coding at school I've been messing around with a couple different coding languages the last years but never really learned a language 'fluently'. I'm not a complete newbie but still missing some basic things I guess.

I know some of the things are in German - if there are any language issues I'll try to clarify those!

Okay, so I'm creating an app for my school that pulls an xml file from our school website that has the substitutions in it. It's an xml file output by the school's software (found here: http://vplankl.gymnasium-beetzendorf.de/Vertretungsplan_Klassen.xml) which is updated regularly. My goal is it to bring the information to the students in a more a appealing and customizable way. Which means the information is presented in an optimized way for smartphones, you don't have to open your browser and you can select the class you're in to avoid unnecessary information.

So far my app is able to pull the file from the server, extracts all the information from it, filter them by class (chosen in a preference activity) and saves the data to a database on the device.

The way my app is constructed:
I have my MainActivity which is my main screen that'll display the different days I pull the substitutions for in a ViewPager connected to a TabLayout. Within the ViewPager I'm displaying fragments that have a RecyclerView displaying all the information wrapped in a SwipeRefreshLayout. So far so good, now the part I'm kinda stuck at.

It's about the general idea of how to refresh the data and display it if anything has changed.
There a three options to refresh the information. First I have an options menu with a refresh button, secondly by using the SwipeRefreshLayout and thirdly when exiting the settings screen to go back to the MainActivity. So I need to able to call the same method from three different points in the app(MainActivity and TabFragment). The method needs to be able to request the file from the server again, do all the parsing and then display the new data.
The general logic on how that works is clear but I have no clue where to put this refresh function so it can do all that stuff.
My project might be a little messy and any(and I really mean any) suggestions are highly appreciated. I shared the source code on github (https://github.com/Crosswind/Vertretungsplan).
If I left anything out feel free to ask! Thanks in advance!
 
Sounds like you've made some good progress. There are a number of ways your data refresh feature could be implemented, but the way I would do it is by creating a service class, and instantiating it as a singleton.
The singleton pattern basically ensures that only one instance (object) of the class exists. Because the method to get the instance is static, it can be called from anywhere in your program:

Code:
public class RefreshService{

   private static RefreshService instance =new RefreshService();

   private RefreshService() {}

   public static RefreshService getInstance(){
      return instance;
   }

   public void refreshData() {
      // Put code here to retrieve data from server
   }
}

You could actually extend the service class to implement caching internally. So any code calling refreshData() would get the internally held data, and only if it was out of date would a network call be made to the server.
 
  • Like
Reactions: David Frenzel
Upvote 0
Thanks for your fast reply! Appreciate it.

It does make sense what you're saying. Just to clarify whether I understood you correctly or if what I'm trying to accomplish is possible.
Will this RefreshService be able to modify the data that is display? I'd need to pass in the view in order to access the elements and add items to the recycler view and so on, correct?

Using that RefreshServices should I outsource my inner class that downloads the xml file into that service?

And while thinking about it:
Within the refreshing process I'd do the following things:
- download the file from the server
- parse the data
- check if the data is newer than the current one by using the timestamp that's included in the file's data
- if it's new data put it in the database/replace the current entries
- display it in the UI
- if not I'd simiply notify that there's no new data available

In other words - all of the above things would be put into this Refresh Service?

Edit: While even more thinking about it. You might have noticed that I created a Helper class which was kinda meant to do exactly what you suggested me to do. So I might have been on the right way the whole time..

Edit2: As seen in my Helper class I pass in the view in the constructor to then use it to modify the UI. Is this the best practice to do it, or should I be doing it differently?
 
Last edited:
Upvote 0
Yes sure you can include all the code which either downloads or parses the received data in some way. Actually this is a very good architecture because it implements nice encapsulation of your data. You can include all the above features you have suggested.
Any code outside this class doesn't need to know where the data comes from, or even that its raw form is XML.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones