1. Are you ready for the Galaxy S20? Here is everything we know so far!

Structure of my app

Discussion in 'Android Development' started by David Frenzel, Feb 8, 2016.

  1. David Frenzel

    David Frenzel Lurker
    Thread Starter

    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!
     



    1. Download the Forums for Android™ app!


      Download

       
  2. Deleted User

    Deleted User Guest

    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 (Text):
    1.  
    2. public class RefreshService{
    3.  
    4.    private static RefreshService instance =new RefreshService();
    5.  
    6.    private RefreshService() {}
    7.  
    8.    public static RefreshService getInstance(){
    9.       return instance;
    10.    }
    11.  
    12.    public void refreshData() {
    13.       // Put code here to retrieve data from server
    14.    }
    15. }
    16.  
    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.
     
    David Frenzel likes this.
  3. David Frenzel

    David Frenzel Lurker
    Thread Starter

    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?
     
    #3 David Frenzel, Feb 8, 2016
    Last edited: Feb 8, 2016
  4. Deleted User

    Deleted User Guest

    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.
     
  5. David Frenzel

    David Frenzel Lurker
    Thread Starter

    Thanks a ton!
    I'll try to add these suggestions into my project and then share it here.
    I hope it's okay to bother you(or anyone else) if I come across more problems.
    Dunno about the rest of the forum but it seems as if this a good place to look for answers (and if possible help others :) ).
     
  6. Deleted User

    Deleted User Guest

    It's good to share and help. And this is a friendly forum, but honestly, the best place you can possibly look for technical answers is StackOverflow.

    http://stackoverflow.com/
     
Loading...

Share This Page

Loading...