Android
16
Вклад в тег
layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);
PagerSnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(recyclerView);
public void loadData(String url){
repository.fetchProducts().get(products->{
view.showProducts(products);
}, throwable ->{
if(throwable instanceof IOexception){
view.showNoNetwork();
}else(throwable instanceof HTTPException){
HTTPException exception = (HTTPException)throwable;
switch(exception.getCode()){
case 400:
view.showError(exception.getMessage());
break;
case 401:
view.showUnauthorized();
break;
...
}
}
...
});
}
public void loadData(String url){
repository.fetchProducts().get(products-> view.showProducts(products),
throwable -> ErrorUtil.handleError(throwable, view));
}
public class ErrorUtil{
public static void handleError(Throwable throwable, View view){
if(throwable instanceof IOexception){
view.showNoNetwork();
}else(throwable instanceof HTTPException){
HTTPException exception = (HTTPException)throwable;
switch(exception.getCode()){
case 400:
view.showError(exception.getMessage());
break;
case 401:
view.showUnauthorized();
break;
...
}
}
...
}
}
public class ToolbarManager{
public void showToolbar(int type){
switch(type){
case MAIN:
....
//огромный кусок кода для показа тулбара для главного экрана
....
break;
case PROFILE:
...
//огромный кусок кода для показа тулбара для экрана профиля
...
...
}
}
}
public interface ToolbarManager{
void showToolbar();
}
public class MainToolbarManager implements ToolbarManager{
public void showToolbar(){
....
//огромный кусок кода для показа тулбара для главного экрана
....
}
}
public class ProfileToolbarManager implements ToolbarManager{
public void showToolbar(){
....
//огромный кусок кода для показа тулбара для экрана профиля
....
}
}
public interface Car {
void turnOnEngine(); //запустить двигатель
void accelerate(); //подать газ
}
public class MotorCar implements Car {
private Engine engine;
public void turnOnEngine() {
//вруби мотор!
engine.on();
}
public void accelerate() {
//поезжай вперед!
engine.powerOn(1000);
}
}
public class ElectricCar implements Car {
public void turnOnEngine() {
throw new AssertionError("А у меня вообще нет двигателя");
}
public void accelerate() {
//ускорение сумасшедшее!
}
}
public interface Engineful {
void turnOnEngine();
}
public interface Acceleratable {
void accelerate();
}
public class MotorCar implements Engineful, Acceleratable {
private Engine engine;
public void turnOnEngine() {
//вруби мотор!
engine.on();
}
public void accelerate() {
//поезжай вперед!
engine.powerOn(1000);
}
}
public class ElectricCar implements Acceleratable {
public void accelerate() {
//ускорение сумасшедшее!
}
}
public interface TextWatcher{
public void beforeTextChanged(CharSequence s, int start, int count, int after);
public void onTextChanged(CharSequence s, int start, int before, int count);
public void afterTextChanged(Editable s);
}
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
//метод, который нам нужен
//какие-то полезные действия
}
});
public class Repository{
private final NetworkManager networkManager;
private final AppDataBase appDataBase;
public Repository(){
this.networkManager = new NetworkManager();
this.AppDataBase = new AppDataBase();
}
}
public interface RemoteDataSource{}
public interface LocalDataSource{}
public class NetworkManager implements RemoteDataSource{}
public class AppDataBase implements LocalDataSource{}
public class Repository{
private final RemoteDataSource remoteDataSource;
private final LocalDataSource localDataSource;
public Repository(RemoteDataSource remoteDataSource, LocalDataSource localDataSource){
this.remoteDataSource = remoteDataSource;
this.localDataSource = localDataSource;
}
}
public class FakeNetworkManager implements RemoteDataSource{}
public class FakeAppDataBase implements LocalDataSource{}
Repository repository = new Repository(new FakeNetworkManager(), new FakeAppDataBase());