enpassio / Databinding

Various examples on databinding which can help not only understand how to get started with it but it also shows how this can be used in complex and real use cases
Apache License 2.0
26 stars 14 forks source link

databinding newsapi kotlin #31

Closed OyaCanli closed 5 years ago

OyaCanli commented 5 years ago

Added the kotlin version of NewsApi databinding sample

OyaCanli commented 5 years ago

@rajtheinnovator I added two commits: -I corrected the return type as nullable. I hope this will fix the crash. -I added an enum for UIState, and put it in an observable field in viewmodel. It facilitates the switch between loading, success and error states -I moved network check to fragment instead of repository. It was complicating the code with listeners and callbacks for no reason. For pinging internet for connection, some of the methods proposed in SO are returning false although there is internet. And some others require using background thread. So it requires some extra work. So may be you try it like this, to see whether that nullable type correction solved the crash. Else, we'll discuss which way to go for pinging internet.

These two didn't work:


public static Boolean isOnline() {
        try {
            Process p1 = java.lang.Runtime.getRuntime().exec("ping -c 1 www.google.com");
            int returnVal = p1.waitFor();
            return (returnVal == 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

//Returns false although there is internet
public static boolean thereIsInternet(){
        boolean internetConnectionStatus;
        try {
            InetAddress ipAddr = InetAddress.getByName("google.com");
            //You can replace it with your name
            internetConnectionStatus = !ipAddr.toString().equals("");
        } catch (Exception e) {
            internetConnectionStatus = false;
        }
        return internetConnectionStatus;
    } ```
OyaCanli commented 5 years ago

@rajtheinnovator I think I found a way of making a real internet connection check without complicating the process with extra asynctasks and listeners. I tried it on java version. I'll push that one so you can take a look. If it seems better to you, I'll change kotlin version accordingly.