jumaallan / AndroidNetworkManager

A library to check the Internet Connection Status in Android (WiFi and Mobile Data)
https://androidstudy.com
52 stars 34 forks source link

monitor method should return Tovuti, but return void #9

Closed Idomo closed 6 years ago

Idomo commented 6 years ago

From some reason, the monitor method return void when it should return Tovuti (that we can save in our class and use it in onStop activity method). In this project: https://github.com/AndroidStudyOpenSource/tovuti everything look ok, but I downloaded this project because it was updated less then a month ago and there the last update was about 3 months ago (and this is version 0.0.2 while there is only 0.0.1).

I also saw this: https://github.com/jumaallan/AndroidNetworkManager/issues/5 what answering on my question, but the documentation wasn't updated accordingly.

So i'd like to know which project should I use and which of them won't be supported soon, because I'm a little bit confused...

UPDATE: I'm seeing that the second project is not found in gradle, so I can use only this project, so please update the documentation so I'll be able to understand how to use it right.

Thanks, Ido.

jumaallan commented 6 years ago

@Idomo Hi Ido, Sorry for the mix up. Now this Repo is in my Personnal Github Account. I got some interested guys who wanted to to be part of the small Open Source Community I wanted to start, and then I created the organization (AndroidStudyOpenSource) - More like a Github Page for my blog. I wanted to open source a couple of Android and Golang projects there. The Tovuti library has been delayed for a while, I switched jobs and this has made me dedicate more time to my new job, and other stuff. Its not published on JCenter, thats why its missing from gradle. At the moment, just use this Repo, version 0.0.2. In a week's time, I will make sure I finish up on Tovuti and publish it. I wanted to have this library move over to the organization for easy and efficient collaborations and contributions. I will send you an invite to join the same, and make those kick-ass contributions. We will have to probably look at that error you are getting and update this project as it is the one that works at the moment. Cheers Man!

Idomo commented 6 years ago

@jumaallan Thanks for the quick replay! I'm not sure that what I wrote is a bug (about the monitor method), I'm just not sure if the library stop itself like you planed to do here: https://github.com/jumaallan/AndroidNetworkManager/issues/5 or that it's keep running also when the activity has been destroyed. The question is: Should I call the Tovuti in all my activities or should I call it only in my MainActivity (the first activity) and it will keep listen to the connectivity changes?

jumaallan commented 6 years ago

@Idomo You should only use it when you want to check internet connection status. Make sure you override onStop and stop Tovuti.

@Override protected void onStop() { super.onStop(); tovuti.stop(); }

To make sure Tovuti stops when you stop the activity, because Monitor keeps running, and may result to crashes. Look at the sample code on the README Page

Idomo commented 6 years ago

@jumaallan This is the problem I wrote here, I can't use the class as you described in the README page because the monitor method return void and not Tovuti object, this is a static class, I can't create an instance of it.

jumaallan commented 6 years ago

@Idomo Could you share your code repo or create a github gist?

Idomo commented 6 years ago

@jumaallan

Tovuti.from(context).monitor(new Monitor.ConnectivityListener(){
    @Override
    public void onConnectivityChanged(int connectionType, boolean isConnected, boolean isFast){
       // Handle connection...
    }
});

I copied the code from the README page (with the private property and onStop method) and then Android Studio told me that I can't put this in the property because it returns void, then I checked the source code and find thats true, there are tow method of monitor and they return void:

public void monitor(int connectionType, Monitor.ConnectivityListener listener) {
    Context context = contextRef.get();
    if (context != null)
        monitors.add(new DefaultMonitorFactory().create(context, connectionType, listener));
    start();
}

public void monitor(Monitor.ConnectivityListener listener) {
    monitor(-1, listener);
}
jumaallan commented 6 years ago

@Idomo It checks to make sure context is not null, then creates a Monitor, which implements an interface to monitor the connection. Could you pull/clone this repository and test it? It works well in a couple of projects I have been working on.

Tovuti.from(this).monitor(new Monitor.ConnectivityListener() { @Override public void onConnectivityChanged(int connectionType, boolean isConnected, boolean isFast) {

        }
    });

This snippet works for Activities. Are you calling it from an Activity or Fragment?

Idomo commented 6 years ago

@jumaallan If you'll look here https://github.com/AndroidStudyOpenSource/tovuti/blob/master/tovuti/src/main/java/com/androidstudy/tovuti/Tovuti.java#L40 you can see that the monitor methods return Tovuti object, what allow me to save it and use it later for onStop method, in this project (version 0.0.2) the methods are return void so I don't have access to the object (unless I call Tovuti.from() to get the object, but you didn't mention it in the README page) and I can't call it in onStop.

And yes, I call it in activity, if I'll call it from fragment I'll need activity context? What is the context I should use so it will work fine for sure? AppCompatActivity?

jumaallan commented 6 years ago

@Idomo I now get what you are saying. Sorry I took too long to understand you, just create a pull request and update the Readme. That would be very nice of you :) 👍

jumaallan commented 6 years ago

@Idomo You need to call Tovuti.from() to get the object, as you are saying. Yes Kindly update the Readme, and add yourself as part of the Authors. I will be glad to merge it Thanks

jumaallan commented 6 years ago

@Idomo I have merged the pull request. I have also invited you as a collaborator in the repo - check your email and feel free to make more contributions. You can mark this issue as resolved, if its okay.

Idomo commented 6 years ago

@jumaallan Done ;)