__________     __ __     __  _______    ________
  / ____/ __ \   / // /    / / / /  _/ |  / / ____/
 / / __/ / / /  / // /_   / /_/ // / | | / / __/
/ /_/ / /_/ /  /__  __/  / __  // /  | |/ / /___
\____/\____/     /_/    /_/ /_/___/  |___/_____/

 --- A GOPHER-LIKE INTERFACE FOR HIVE BLOCKCHAIN ---

Reimplementing CrytpoNews in Native Android (UPDATE #3 : NETWORK LISTENER)

BY: @mathemandy | CREATED: March 28, 2018, 11:19 a.m. | VOTES: 17 | PAYOUT: $38.59 | [ VOTE ]

@princessdharmy and I decided to build a native version of CryptoNews originally started by @johnesan in xamarin(which was discontinued due to being a cross platform framework, it comes with so many limitations that makes the application unable to fully harness the power of native functionalities).

History

Update and New Features

How it was implemented

A ConnectionClassLiveData class that extends LiveData was created . This class contains the BroadcastReceiver which monitors the network state.
The class shown below

public class ConnectionClassLiveData extends LiveData {

    private Context context;


    @Inject
    public ConnectionClassLiveData(Context context) {
        this.context = context;
    }


    @Override
    protected void onActive() {
        super.onActive();
        IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        context.registerReceiver(networkReceiver, filter);

    }


    @Override
    protected void onInactive() {
        super.onInactive();
        context.unregisterReceiver(networkReceiver);
    }

    private BroadcastReceiver networkReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getExtras() != null){
                NetworkInfo actvenetwork = (NetworkInfo)
                        intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
                boolean isConnected = actvenetwork != null &&
                        actvenetwork.isConnectedOrConnecting();

                if (isConnected){
                    switch (actvenetwork.getType()){
                        case  ConnectivityManager.TYPE_WIFI:
                            postValue(new ConnectionModel(WifiData, true));
                            break;
                        case  ConnectivityManager.TYPE_MOBILE:
                            postValue(new ConnectionModel(MobileData, true));
                            break;

                    }
                }else {
                    postValue(new ConnectionModel(0, false));
                }
            }

        }
    };
}

The BroadcastReceiver is registered in the active onActive and InActive() methods.

In our fragment, i was able to provide the ConnectionClassLiveData class with Dagger and implemented the way the snackbar behaves when the network Changed.

 //Connection Listener to give us rea time internet connection status
        connectionClassLiveData.observe(this, connectionModel -> {
            if (connectionModel.isConnected()) {
                isConnected = true;
                if (newsList.size() == 0) {
                    newsViewModel.refresh();
                }
            } else {
                isConnected = false;
                Snackbar.make(mContainer, R.string.error, Snackbar.LENGTH_LONG).show();

            }

The Swipe-to-refresh layout was implemnted in the onCreateView and was dismissed when the data was gotten.

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view  = inflater.inflate(R.layout.fragment_latest_news, container, false);
        ButterKnife.bind(this, view);
        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.setRefreshing(true);
        swipeRefreshLayout.setColorSchemeColors(R.color.colorAccent, R.color.colorAccent, R.color.colorAccent);
        setupViews();

        return view;
    }

RECORDING OF ADDED FEATURE

https://youtu.be/lvEtUKSigEI

Roadmap

Resources

Posted on Utopian.io - Rewarding Open Source Contributors

TAGS: [ #utopian-io ] [ #stach ] [ #steemdev ] [ #android ] [ #open-source ]

Replies

@codingdefined | March 30, 2018, 5:31 a.m. | Votes: 0 | [ VOTE ]

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

@chiboyzz | March 30, 2018, 10:51 p.m. | Votes: 0 | [ VOTE ]

Bro i really need your help on this utopian thing.

How can i contact you on discord.

@utopian-io | March 31, 2018, 12:45 a.m. | Votes: 0 | [ VOTE ]

Hey @mathemandy I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
- Vote for my Witness With SteemConnect
- Proxy vote to Utopian Witness with SteemConnect
- Or vote/proxy on Steemit Witnesses

[IMAGE: https://steemitimages.com/DQmYPUuQRptAqNBCQRwQjKWAqWU3zJkL3RXVUtEKVury8up/mooncryption-s-utopian-io-witness-gif.gif]

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

[ BACK TO TRENDING ] [ BACK TO MENU ]
CMD>