Created
August 30, 2016 07:23
-
-
Save VladSumtsov/0fd6255ae93c8ec184c15602807b65a4 to your computer and use it in GitHub Desktop.
for mortar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ghm.mortar; | |
import android.app.Activity; | |
import android.app.SearchManager; | |
import android.os.Bundle; | |
import android.os.Parcel; | |
import android.os.Parcelable; | |
import android.support.annotation.DrawableRes; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.SearchView; | |
import android.view.Menu; | |
import android.view.MenuInflater; | |
import android.view.MenuItem; | |
import android.view.View; | |
import com.ghm.R; | |
import com.ghm.ui.util.RxSearchView; | |
import com.ghm.ui.util.SearchViewQueryTextEvent; | |
import com.ghm.ui.view.ContactsView; | |
import com.jakewharton.rxbinding.view.RxView; | |
import mortar.Presenter; | |
import mortar.bundler.BundleService; | |
import rx.Observable; | |
import rx.functions.Action0; | |
import rx.functions.Action1; | |
import rx.subjects.PublishSubject; | |
import static android.content.Context.SEARCH_SERVICE; | |
import static mortar.bundler.BundleService.getBundleService; | |
/** | |
* Created by Dmitriy Puzak on 6/8/16. | |
*/ | |
public class ActivityOwnerPresenter extends Presenter<ActivityOwnerPresenter.Behaviour> { | |
public interface Behaviour { | |
void setShowHomeEnabled(boolean enabled); | |
void showUpButton(boolean enabled); | |
void setTitle(String title); | |
void shouldShowActionBar(boolean isVisible); | |
void showHorizontalProgress(boolean isVisible); | |
void showCircleProgress(boolean isVisible); | |
void setMainBackground(@DrawableRes int drawableRes); | |
Activity getActivity(); | |
void addMenuAction(View view, MenuAction menuAction); | |
void removeMenuAction(View view); | |
void setToolbarCustomView(View view); | |
void setTranslucentStatus(boolean fullscreen); | |
void setTranslucentNavigation(boolean fullscreen); | |
} | |
private AppCompatActivity getActivity() { | |
return (AppCompatActivity) getView().getActivity(); | |
} | |
public static class Config implements Parcelable { | |
private boolean showHomeEnabled; | |
private boolean showUpButton; | |
private boolean showActionBar; | |
private boolean showHorizontalProgress; | |
private boolean showCircleProgress; | |
private int mainBackground; | |
private String title; | |
private int volumeControlStream = -1; | |
private Action0 updateListener = () -> { | |
}; | |
private View toolbarCustomView; | |
private boolean translucentStatus; | |
private boolean translucentNavigation; | |
public Config shouldShowActionBar(boolean isVisible) { | |
this.showActionBar = isVisible; | |
return this; | |
} | |
public Config showUpButton(boolean show) { | |
this.showUpButton = show; | |
return this; | |
} | |
public Config setTitle(String title) { | |
this.title = title; | |
return this; | |
} | |
public Config setShowCircleProgress(boolean showCircleProgress) { | |
this.showCircleProgress = showCircleProgress; | |
return this; | |
} | |
public Config setTranslucentStatus(boolean translucentStatus) { | |
this.translucentStatus = translucentStatus; | |
return this; | |
} | |
public Config setTranslucentNavigation(boolean translucentNavigation) { | |
this.translucentNavigation = translucentNavigation; | |
return this; | |
} | |
public Config setShowHorizontalProgress(boolean showHorizontalProgress) { | |
this.showHorizontalProgress = showHorizontalProgress; | |
return this; | |
} | |
public Config setMainBackground(int mainBackground) { | |
this.mainBackground = mainBackground; | |
return this; | |
} | |
public Config setVolumeControlStream(int volumeControlStream) { | |
this.volumeControlStream = volumeControlStream; | |
return this; | |
} | |
public boolean isShowActionBar() { | |
return showActionBar; | |
} | |
void setUpdateListener(Action0 updateListener) { | |
this.updateListener = updateListener; | |
} | |
public Config setToolbarCustomView(View screenView, View toolbarCustomView) { | |
RxView.detaches(screenView) | |
.subscribe(a -> { | |
this.toolbarCustomView = null; | |
update(); | |
}); | |
this.toolbarCustomView = toolbarCustomView; | |
return this; | |
} | |
@Override public int describeContents() { | |
return 0; | |
} | |
@Override public void writeToParcel(Parcel dest, int flags) { | |
dest.writeByte(this.translucentNavigation ? (byte) 1 : (byte) 0); | |
dest.writeByte(this.translucentStatus ? (byte) 1 : (byte) 0); | |
dest.writeByte(this.showHomeEnabled ? (byte) 1 : (byte) 0); | |
dest.writeByte(this.showUpButton ? (byte) 1 : (byte) 0); | |
dest.writeByte(this.showActionBar ? (byte) 1 : (byte) 0); | |
dest.writeByte(this.showHorizontalProgress ? (byte) 1 : (byte) 0); | |
dest.writeByte(this.showCircleProgress ? (byte) 1 : (byte) 0); | |
dest.writeInt(this.mainBackground); | |
dest.writeInt(this.volumeControlStream); | |
dest.writeString(this.title); | |
} | |
public Config() { | |
} | |
protected Config(Parcel in) { | |
this.translucentNavigation = in.readByte() != 0; | |
this.translucentStatus = in.readByte() != 0; | |
this.showHomeEnabled = in.readByte() != 0; | |
this.showUpButton = in.readByte() != 0; | |
this.showActionBar = in.readByte() != 0; | |
this.showHorizontalProgress = in.readByte() != 0; | |
this.showCircleProgress = in.readByte() != 0; | |
this.mainBackground = in.readInt(); | |
this.volumeControlStream = in.readInt(); | |
this.title = in.readString(); | |
} | |
public static final Parcelable.Creator<Config> CREATOR = new Parcelable.Creator<Config>() { | |
@Override public Config createFromParcel(Parcel source) { | |
return new Config(source); | |
} | |
@Override public Config[] newArray(int size) { | |
return new Config[size]; | |
} | |
}; | |
public void update() { | |
updateListener.call(); | |
} | |
} | |
private Action1<Menu> menuUpdated; | |
public void setSoftInputMode(int softInputMode) { | |
getActivity().getWindow().setSoftInputMode(softInputMode); | |
} | |
public Observable<SearchViewQueryTextEvent> attachSearch(View view) { | |
PublishSubject<SearchViewQueryTextEvent> subject = PublishSubject.create(); | |
createMenu((inflater, menu) -> { | |
inflater.inflate(R.menu.search_item, menu); | |
SearchManager searchManager = (SearchManager) getActivity().getSystemService(SEARCH_SERVICE); | |
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView(); | |
searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName())); | |
RxSearchView.queryTextChangeEvents(searchView).subscribe(subject::onNext); | |
}, view); | |
return subject.asObservable(); | |
} | |
public void createMenu(CreateMenu createMenu, View view) { | |
createMenu(createMenu, null, view); | |
} | |
public void createMenu(SelectMenu selectMenu, View view) { | |
createMenu(null, selectMenu, view); | |
} | |
public void createMenu(CreateMenu createMenu, SelectMenu selectMenu, View view) { | |
MenuAction menuAction = new MenuAction() { | |
@Override public void onOptionsItemSelected(MenuItem item) { | |
if (selectMenu != null && view.getParent() != null) | |
selectMenu.onOptionsItemSelected(item); | |
} | |
@Override public void onCreateOptionsMenu(MenuInflater inflater, Menu menu) { | |
if (createMenu != null && view.getParent() != null) | |
createMenu.onCreateOptionsMenu(inflater, menu); | |
if (menuUpdated != null) menuUpdated.call(menu); | |
} | |
}; | |
getView().addMenuAction(view, menuAction); | |
RxView.detaches(view) | |
.filter(a -> getView() != null) | |
.subscribe(o -> getView().removeMenuAction(view)); | |
} | |
public void menuUpdated(Action1<Menu> action, ContactsView view) { | |
this.menuUpdated = action; | |
RxView.detaches(view).subscribe(v -> menuUpdated = null); | |
} | |
public interface MenuAction extends CreateMenu, SelectMenu { | |
} | |
public interface CreateMenu { | |
void onCreateOptionsMenu(MenuInflater inflater, Menu menu); | |
} | |
public interface SelectMenu { | |
void onOptionsItemSelected(MenuItem item); | |
} | |
public void updateConfigFromParent(Config config) { | |
this.config = config; | |
this.config = getConfig(); | |
} | |
private Config config; | |
public ActivityOwnerPresenter() { | |
} | |
@Override protected BundleService extractBundleService(Behaviour view) { | |
return getBundleService(view.getActivity()); | |
} | |
@Override public void onLoad(Bundle savedInstanceState) { | |
super.onLoad(savedInstanceState); | |
update(); | |
} | |
public Config getConfig() { | |
config = config == null ? new Config() : config; | |
config.setUpdateListener(() -> update()); | |
return config; | |
} | |
public void updateConfigFromParent() { | |
Config c = new Config(); | |
c.showActionBar = getConfig().showActionBar; | |
c.mainBackground = getConfig().mainBackground; | |
c.translucentStatus = getConfig().translucentStatus; | |
c.updateListener = getConfig().updateListener; | |
config = c; | |
} | |
public void update() { | |
Behaviour view = getView(); | |
if (view == null || config == null) return; | |
view.shouldShowActionBar(config.showActionBar); | |
view.setShowHomeEnabled(config.showHomeEnabled); | |
view.showUpButton(config.showUpButton); | |
view.setTitle(config.title); | |
view.showCircleProgress(config.showCircleProgress); | |
view.showHorizontalProgress(config.showHorizontalProgress); | |
view.setMainBackground(config.mainBackground); | |
view.setToolbarCustomView(config.toolbarCustomView); | |
view.setTranslucentStatus(config.translucentStatus); | |
view.setTranslucentNavigation(config.translucentNavigation); | |
if (config.volumeControlStream != -1) | |
view.getActivity().setVolumeControlStream(config.volumeControlStream); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment