Code Monkey home page Code Monkey logo

mcpe-maps-mvp's Introduction

Map collection for MCPE(Android)

Technologies used:

  • Rx - for handling complex parallel/async tasks ( server calls and database operations).
  • realm - as a database model-layer hosted locally on device as a set of tables in text files).
  • backendless - also model-layer hosted remotely on backendless server as set of tables.
  • retrolambda - for adding Java8 lambdas compatible with 1.6, 1.7.

Links for developers:

This project uses moxy library as MVP basis.

MVP refers to an abstract project architecture narrowed down to the relations and communicating between Model, View,ViewState and Presenter

We DO NOT communicate between fragments,activities,toolbars,views,services etc. Instead, we abstract to communicate between M, V and P only - that's the main point which simplifies the whole architecture a lot.

View1 -> Presenter1 -> model (save to db, because db triggers all listeners)-> Presenter2 -> View2

Don't be scared of this squares. Imagine a Cafe, where you make your orders using iPad embedded in every table.

Then:

  • Models include kitchen, fridge, coffee machine, billing system, vip members list, etc...
  • ViewState role goes to Waiter.
  • View is a Cafe's Vision of a Customer or in other words, a set of all possible actions that Cafe wants to perform on customers (0+ times).
  • Presenter is an iPad : showing menu, taking orders, processing bills; it handles all data i/o between:
    • customers (View == anyone who implements CustomerView's methods)
    • waiter (ViewState);
    • kitchen, billing system, etc... (Model)

presenter delivers results of cooking (cafe commands) to CustomerView with the help of Waiter .

Important!

Please think of View in terms of the system designer's vision_ of the process.That kind of meaning. Not the synonym of 'look' or 'widget'.

People are different. Cafe visitors are different. App users are different. But our aim is to find such typical actions that do not depend on the difference between customers/users. That's one of the most importance. Both visitor and organisation can use cafe : use menu, place an order, pay bill. And to succeed the process there is no difference for cafe how old is the organisation or what annual income the visitor has.

  • Let's compare cafe visit to adding MVP functional to app component:

Property | Real Cafe | Android app using Moxy-MVP ------------ | ------------ | ------------- Entity we use with MVP | Customer | ? implements CustomerView subject | A person, group, organisation | Fragment, Activity, CustomView+MvpDelegate subject qualifier | Willing to use cafe's service | Implemented CustomerView with all it's methods MVP-important behaviour | Can wait, order,smile, eat, pay, leave| Various UI changes on screen - display text,image, widget MVP-independent actions | go peeing, answer call, browse memes | Incoming calls, power, no network, system update, app switch

The main rule is to allow cafe see a Customer-like behaviour described in Cafe's vision (View) of a typical visitor.

Multiple views attached to single presenter

Imagine yourself in cafe with a friend. Think of it as a good place to attach another customer to same iPad; It will easily handle same operations for second customer on the same table, independent of the fact, that your friends reaction will differ from yours; And properties (_age, gender, appettite) usually differs from yours. That's just doesn't play any role in MVP angle of this process.

Let's look at your typical actions through the prism of Moxy-MVP:

  1. You have an initial state : your eyes render hungry, your whole body is running performLongWaitAnimation()

  2. iPad (presenter) presents a menu:

     Customer places order on coffee.
     Customer places order on donut.
     Customer confirms the whole order.
    
  3. iPad receives onOrderReceived callback with data.

     iPad's perspective:
     	-> onOrderReceived( List<MenuItem> orderedItems); 
    
  4. presenter begins new Async( ()->launchCoffeeMachine("latte") ),

     	-> model.coffeemachine.makeEspresso()  //prepare coffee 
    
  5. presenter launches another syncronous operation - fridge.retrieveDonut() ,

     	-> get donut from `model.openFridge().getDonut(4,20)`
     	-> send Waiter with donut to Customer
    
  6. presenter listens to events:

    • from model.getCoffeeMachine() - when its ready- iPad issues a command (tells waiter coffee to move coffee from model:machine to Customer;
    • from CustomerViewcallbacks:
      • Customer performs Eating.
      • When finished - CustomerView calls back to presenter to ask a bill: mPresenter.onEatFinished();

The main aspect to keep in mind all the time - it is up to you to decide:

  • What part of your code to consider models and views
  • Whether to fit code to MVP at all;

Mixing MVP and non-MVP is already good, while moving more code to MVP is still better.

Nevertheless there are some tactics which allow to drastically reduce the volume of code needed to create a system if and only if you define the roles of your classes according to some rules - let's call them Best practices

Details:

Model

model - is literally anything where we can read/write/get/set/download/upload/configure/change any digital data:

  • online data input/output/storage:
    • retrofit
    • backendless
    • rest api's
  • offline storage create/read/update/delete (CRUD):
    • realm
    • SQLite
    • SharedPreferences, etc...
  • deivce services:
    • intents
    • services
    • geo
    • display
    • audio
    • camera, etc...
  • device or 3rd-party sensors:
    • gyroscope
    • light sensor
    • accelerometer
    • microphone
    • compass
    • EMV sensor
    • wifi, Bluetooth, connected gadgets etc.

if there is something we can get data from - we want to use it as a model-layer in this architecture). No specific MVP code is needed to consider any data source a model. Use the same code you would use if you write non-MVP. The only rule here is to interact with model from presenter! This will make the magic of commands history in ViewState

View

view- is an interface defining what action some entity (device screen, RelativeLayout, widget, sound device) is meant to be able to perform. Each and every Customer, including you, implements CustomerView - defines how this entity reacts to events which might happen in cafe (defined above^).

Steps to construct a View part of Moxy- MVP:

  1. define interface (make it implement MvpView)
		public interface CustomerView implements MvpView {				
			void welcome(String greetingsPhrase);
			void assignSeat (int tableNumber);
			void presentMenu (List<MenuItems> menu);
			void orderReady(MenuItem cookedItem);
			void showBill(Map<MenuItem,String> chequeBiu);
			void auRevoir();
		}
  1. Now you take Activity,Fragment or any CustomView+delegate and add implements CustomerView to make them be a 100% View V of MVP. @InjectPresenter MvpPresenter myCustomerPresenter;

example

public class MyFragment extends MvpAppCompatFragment implements CustomerView {
		
			@InjectPresenter
			MyMoxyPresenter myPresenterObject;
			
			@Override
			public void onCreate{
	
			...
			}
			
			@Override
			void welcome( String greetingsPhrase ){
			
				greet_tv.setText( greetingsPhrase )
			}
			...
		}

The above @InjectPresenter annotation tells moxy to generate ViewState class for this View implementor.

Important : android.View is totally different from View V of MVP . Really. Not samesame kind of stuff.

ViewState

First, it is generated automatically by Moxy and it works perfect.

@InjectsPresenter - generates ViewState.

ViewState is a class which :

  • holds the current state of view and also history of commands from presenter to views.
  • manages the activity/fragments lifecycle mess for you - and makes it perfectly.

Presenter

Presenter is a java class which implements MvpPresenter

Generally you'll find yourself writing 3 types of methods here:

  1. Methods defining how you retrieve/save data from model (model == code talking to):
  1. Methods to manipulate the data (so-called business logic of the app)

  2. Callbacks - write methods you will call from View, when events( eg. clicks, touches, end of animation) happen there.

Presenter methods examples:

  • Type 1 (CRUD)

  • local

	private List<Map> getLocalMaps(){	
		Model model = getLocalRepository(); 
		return model.where(Map.class).findAll();	 
	}
  • remote
	private refreshMaps(){	
		Model model = Backendless.Persistence();
		model.of(Map.class)
			.where("objectId",map.id)
			.find( results -> { getLocalModel().copyOrupdate(results); });
		}
  • Type 2 (logic):
		private Map incrementLikes(Map map){
			int oldLikes = map.getLikes();			// inner data manipulations
			map.setLikes( oldLikes + 1);
			return map;
		}
		
		onSettingsClickedFromActivity(){
			
			UserConfig currentSettings = model.getConfigs(); //retrieve up-to-date configs from db
		
			//View(State) We send the configs object to view;
			// View in it's turn has a databinded layout which takes userConfig 
			// and displays it's variables we took from db - that's the job of presenter as designed;
			getViewState().displaySettingsWindowUsingConfig(config); 	
		}		
  • type 3 (callbacks)

    • simple:
    		onNewLevel(){
      		getViewState().showSuccessAnimation();
      		getViewState().displayAd();
      	}
    • mixed:
    		onLikeButtonClicked(int mapId){		//type 3: this method is called from activity,(like btn ClickListener)
      		getViewState().runLikeAnimation();		// ui command ( View's method)
      		getViewState().showBackgroundProgress();	// ui command ( View's method)
      		
      		map = getLocalMaps().getById(mapId);		// type 1 - retrieving
      		incrementLikes(map);				// type 2 - logic
      	
      		model.saveToDatabase(map);    			// type 1 - saving updated map to db 
      		
      		getViewState().hideBackgroundProgress();	// ui command ( View's method)
      		}

to be continued...

mcpe-maps-mvp's People

Contributors

bufferunderflower avatar dark-keeper avatar tomassonmsi avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

mcpe-maps-mvp's Issues

Markdown - beautiful text

Markdown example

  • bold_one
  • two

What a Terrible Failure! No img provided...and we don't give a fuck

Inline code
I think you <mudach'e>

GitHub Flavored Markdown

syntax highlighting
  • this is a complete item
  • this is an incomplete item
First Header Second Header three
Content from cell 1 Content from cell 2
Content in the first column Content in the second column
raw www 5 bottles! 5!

tilda

Header occupates 50%+ of the screen.

  1. Reproduce :
    Go to Main_Activity, scroll the list of maps down: user would see 3+- maps (pics) on the screen -> try to scroll up: user would see no more than 1 map (pic) on the screen
    image

  2. Result :
    Header occupies 50%+ of the screen.
    image

  3. Expected:
    The major part of the viewport should show list items, not header.

[UI discuss] List Items size

  1. Reproduce:
    Go to main Activity

  2. Result :
    Too little visible list items on the screen.
    image

  3. Expected:
    increase the number of visible list items on the screen.
    image

Concourse

Concourse is a Continuous Integration service - easy to install and connect with GitHub. Seems very simple yet powerful, advanced and easy scalable.

Progress Bar

Новый прогресс бар

  • сделать анимированный прогресс бар в стиле майнкрафт
  • отображать не проценты а байты/килобайты/мегабайты при загрузке
  • собственная отрисовка в векторной графике

Learn vectorDrawables and spread knowledge

Vector draawables are Essential for composing a pleasant ui.

Some whitespaces in knowledge I'd like to fill are:

  1. wWhat's the difference and how does the difference looks like, in action.
  2. What is the recommended by Android creators way to :
  • use in imageView
  • use .svg as any view's background
    • includes definite (create small wiki) (#6 Create wiki) knowledge concerning paddings/margins/shadows (!important)
  • to animate a single unique view backedby vector
  • to animate a group of views

So the best way i could find by this moment is to create a small repository and have a project describing above stuff in action. Fresh project

Класс VectorDrawable
Основой API векторной графики в Android является класс VectorDrawable. Объекты этого класса являются ресурсами и описываются при помощи языка XML, размещаются они в директории res/drawable нашего проекта. Корневым элементом класса является тэг vector. У тэга vector есть несколько атрибутов, обязательными являются две группы атрибутов, которые определяют размер изображения нашего drawable:width и height — они указываются с использованием неких единиц измерения (набор их стандартен для Android — dp, sp и px), а также viewportWidth и viewportHeight — они указываются без единиц измерения. При помощи width и height мы можем указать физический размер, который займет наш drawable на экране, а viewport можно сравнить с окошком, через которое мы смотрим на наше изображение. Размеры viewport могут как совпадать, так и отличаться от размеров изображения. При этом, изменяя размер viewport, мы можем изменять площадь занимаемую фигурой внутри drawable.
Стоит отметить, что у viewport отсутствует возможность задать координат pivotPoint и изменения размеров viewport будут отсчитываться от левого верхнего угла.

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.