Code Monkey home page Code Monkey logo

Comments (4)

mttkay avatar mttkay commented on August 19, 2024

For what it's worth, at SoundCloud we use the latter. We would have e.g. a tracks Observable. I prefer this for two reasons:

  • I think of Observables as collections or streams. Giving them the name of what is being streamed seems natural.
  • You can streamline creation of observables via operators/adaptors or those created by create. At the end of the day, all those methods return observables, so consistency in naming makes sense to me

from rxandroid.

Gi-lo avatar Gi-lo commented on August 19, 2024

I guess I can use this issue for further code structure and code convention questions.

I am relatively new to the RX and MVVM topic but have managed to build my first application using these technics (soon available). While doing so we stumbled across a lot of common issues when doing Android apps. I want to share the gathered experience with you as I would love to hear if I do something terribly wrong or if you solved it in a better a way.

  • Adapters:
public class XXXItemViewAdapter extends ArrayAdapter<XXXItem, XXXItemView> {

    // Constructor

    public XXXItemViewAdapter(Context context, List<XXXItem> array) {
        super(context, array);
    }

    // ArrayAdapter

    @Override
    protected void onPrepareViewForReuse(XXXItemView view) {
        view.getViewModel().prepareForReuse();
    }

    @Override
    protected XXXItemView onCreateViewAtPosition(int position) {
        return new XXXItemView(getContext());
    }

    @Override
    protected void onBindValues(XXXItemView view, XXXItem item) {
        view.getViewModel().itemCommand.onNext(item);
    }
}

ArrayAdapter is a simple base adapter which abstracts reusing handling and getting data from an array. The main thing here is that I only need to implement 4 methods.

    1. The constructor, which is super simple as we are just calling super.
    1. onPrepareViewForReuse which is called by the super class adapter when a view is about to be reused.
    1. onCreateViewAtPositionwhich is called by the super class adapter when a view needs to be created. Same as on "getView" if the input convertView is null.
    1. onBindValues called when the view is created or reused. I simply send the new item to the item command. The view model then decides what needs to be displayed in the item view.

Pros:

  • I don't need to test that code... (I need to test the "ArrayAdapter" once and the view model)
  • It's pretty easy to create new adapters

Cons:

  • Not optimal if you need different view types
  • I think one could reduce the boilerplate code...

TBC

  • ListItemView
  • AbstractContextViewModel
  • Navigation
  • more more more

PS:
I really think we should update the wiki and create more examples, best practices etc. We could also create a markdown file on the main repo, and contribute to it by using pull requests. Just an idea :)

from rxandroid.

mttkay avatar mttkay commented on August 19, 2024

This has come up a few times before: classes implemented OnSubscribe used to be called "operators" in pre-release versions of RxJava, but with the arrival of Operator they got renamed bit by bit and are now all called OnSubscribe*. cf. for instance https://github.com/ReactiveX/RxJava/blob/1.x/src/main/java/rx/internal/operators/OnSubscribeCache.java

Unless anyone has objections, I think we should follow suit and rename our classes accordingly and adopt this convention going forward.

from rxandroid.

JakeWharton avatar JakeWharton commented on August 19, 2024

As part of #172 all of the inconsistent naming conventions have been removed. We will be absolutely mindful of this in the future as things get re-added. Consistency is a great thing!

from rxandroid.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.