本文整理汇总了Java中android.arch.lifecycle.Transformations.switchMap方法的典型用法代码示例。如果您正苦于以下问题:Java Transformations.switchMap方法的具体用法?Java Transformations.switchMap怎么用?Java Transformations.switchMap使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.arch.lifecycle.Transformations
的用法示例。
在下文中一共展示了Transformations.switchMap方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: ConversationViewModel
import android.arch.lifecycle.Transformations; //导入方法依赖的package包/类
@Inject
public ConversationViewModel(InboxRepository repository) {
this.conversationId = new MutableLiveData<>();
conversation = Transformations.switchMap(conversationId, input -> {
if (input.isEmpty()) {
return AbsentLiveData.create();
}
return repository.conversation(input);
});
metaData = Transformations.switchMap(conversationId, input -> {
if (input.isEmpty()) {
return AbsentLiveData.create();
}
return repository.conversationMetaData(input);
});
}
开发者ID:hbmartin,项目名称:firebase-chat-android-architecture-components,代码行数:19,代码来源:ConversationViewModel.java
示例2: RepoViewModel
import android.arch.lifecycle.Transformations; //导入方法依赖的package包/类
@Inject
public RepoViewModel(RepoRepository repository) {
this.repoId = new MutableLiveData<>();
repo = Transformations.switchMap(repoId, input -> {
if (input.isEmpty()) {
return AbsentLiveData.create();
}
return repository.loadRepo(input.owner, input.name);
});
contributors = Transformations.switchMap(repoId, input -> {
if (input.isEmpty()) {
return AbsentLiveData.create();
} else {
return repository.loadContributors(input.owner, input.name);
}
});
}
示例3: UserViewModel
import android.arch.lifecycle.Transformations; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
@Inject
public UserViewModel(UserRepository userRepository, RepoRepository repoRepository) {
user = Transformations.switchMap(login, login -> {
if (login == null) {
return AbsentLiveData.create();
} else {
return userRepository.loadUser(login);
}
});
repositories = Transformations.switchMap(login, login -> {
if (login == null) {
return AbsentLiveData.create();
} else {
return repoRepository.loadRepos(login);
}
});
}
示例4: DocListViewModel
import android.arch.lifecycle.Transformations; //导入方法依赖的package包/类
/**
* Default constructor.
* @param application
*/
public DocListViewModel(Application application) {
super(application);
// transform database created flag to live data.
// if it is not created, it will return ABSENT field.
// if it is created, it will return room live data.
final DatabaseManager databaseManager = DatabaseManager.getInstance(application);
LiveData<Boolean> databaseCreated = databaseManager.isDatabaseCreated();
mObservableDocuments = Transformations.switchMap(databaseCreated,
new Function<Boolean, LiveData<List<DocumentMetadata>>>() {
@Override
public LiveData<List<DocumentMetadata>> apply(Boolean isDbCreated) {
if (!Boolean.TRUE.equals(isDbCreated)) {
return ABSENT;
} else {
return databaseManager.getDatabase().documentDao().loadAllDocumentsMetadata();
}
}
});
databaseManager.initDbAsync(this.getApplication());
}
示例5: PostsListViewModel
import android.arch.lifecycle.Transformations; //导入方法依赖的package包/类
PostsListViewModel(@NonNull Application application, String slug, int postCount, Retrofit retrofit) {
super(application);
mSlug = slug;
mPostCount = postCount;
mRetrofit = retrofit;
mIsLoading.setValue(true);
mOffset.setValue(0);
// 当 mOffset 的值发生改变,就会执行 apply
mListLiveData = Transformations.switchMap(mOffset, new Function<Integer, LiveData<List<PostsListBean>>>() {
@Override
public LiveData<List<PostsListBean>> apply(Integer input) {
return handleData(input);
}
});
}
示例6: FlexibleViewModel
import android.arch.lifecycle.Transformations; //导入方法依赖的package包/类
public FlexibleViewModel() {
identifier = new MutableLiveData<>();
liveItems = Transformations.switchMap(identifier, new Function<Identifier, LiveData<List<AdapterItem>>>() {
@Override
public LiveData<List<AdapterItem>> apply(Identifier input) {
return Transformations.map(getSource(input), new Function<Source, List<AdapterItem>>() {
@Override
public List<AdapterItem> apply(Source source) {
if (isSourceValid(source)) {
return map(source);
} else {
return liveItems.getValue();
}
}
});
}
});
}
示例7: HeroViewModel
import android.arch.lifecycle.Transformations; //导入方法依赖的package包/类
public HeroViewModel() {
this.heroRepository = HeroRepository.getInstance();
this.heroResult = Transformations.switchMap(forceFetchHero, forceFetch -> {
if (forceFetch == null) {
return AbsentLiveData.create();
}
return heroRepository.getHero(forceFetch);
});
}
示例8: MainViewModel
import android.arch.lifecycle.Transformations; //导入方法依赖的package包/类
@Inject
MainViewModel(MainRepository repository) {
this.repository = repository;
filters.setValue(Filters.getDefault());
isSignedIn = new LiveData<Boolean>() {
@Override
protected void onActive() {
super.onActive();
setValue(FirebaseAuth.getInstance().getCurrentUser() != null);
}
};
restaurants = Transformations.switchMap(filters, repository::restaurants);
}
示例9: SearchViewModel
import android.arch.lifecycle.Transformations; //导入方法依赖的package包/类
@Inject
SearchViewModel(RepoRepository repoRepository) {
nextPageHandler = new NextPageHandler(repoRepository);
results = Transformations.switchMap(query, search -> {
if (search == null || search.trim().length() == 0) {
return AbsentLiveData.create();
} else {
return repoRepository.search(search);
}
});
}
示例10: UserViewModel
import android.arch.lifecycle.Transformations; //导入方法依赖的package包/类
@Inject
public UserViewModel(UserRepository userRepository) {
this.repository = userRepository;
if (user == null) {
Timber.d("Init UserViewModel");
userIdentifier = new MutableLiveData<>();
user = Transformations.switchMap(userIdentifier, input -> {
if (input == null) {
return AbsentLiveData.create();
}
return repository.login(new LoginRequest(input.username, input.password));
});
}
}
示例11: VideosViewModel
import android.arch.lifecycle.Transformations; //导入方法依赖的package包/类
@Inject
public VideosViewModel(Application application, VideosRepository repository) {
super(application);
mRepository = repository;
mAllCategories = mRepository.getAllCategories();
mSearchResults = Transformations.switchMap(
mQuery, new Function<String, LiveData<List<VideoEntity>>>() {
@Override
public LiveData<List<VideoEntity>> apply(final String queryMessage) {
return mRepository.getSearchResult(queryMessage);
}
});
mVideoById = Transformations.switchMap(
mVideoId, new Function<Long, LiveData<VideoEntity>>() {
@Override
public LiveData<VideoEntity> apply(final Long videoId) {
return mRepository.getVideoById(videoId);
}
});
/**
* Using switch map function to react to the change of observed variable, the benefits of
* this mapping method is we don't have to re-create the live data every time.
*/
mAllVideosByCategory = Transformations.switchMap(mVideoCategory, new Function<String, LiveData<List<VideoEntity>>>() {
@Override
public LiveData<List<VideoEntity>> apply(String category) {
return mRepository.getVideosInSameCategoryLiveData(category);
}
});
}
示例12: LogcatContentViewModel
import android.arch.lifecycle.Transformations; //导入方法依赖的package包/类
public LogcatContentViewModel(@NonNull Application application, LogcatContentRepository repository) {
super(application);
this.repository = repository;
contentLiveData = Transformations.switchMap(isStartFirstLoad, isStart -> repository.getLogcatContent());
}
示例13: creat
import android.arch.lifecycle.Transformations; //导入方法依赖的package包/类
public LiveData<String> creat(String grep) {
return Transformations.switchMap(transData, boo -> {
return data;
});
}
示例14: MainActivityViewModel
import android.arch.lifecycle.Transformations; //导入方法依赖的package包/类
@Inject
public MainActivityViewModel() {
App.getComponent().inject(this);
this.weatherInfoLiveData = Transformations.switchMap(cityNameLiveData, weatherRepository::getWeather);
}
示例15: RatingViewModel
import android.arch.lifecycle.Transformations; //导入方法依赖的package包/类
@Inject
RatingViewModel(RestaurantRepository repository) {
this.repository = repository;
restaurant = Transformations.switchMap(id, repository::restaurant);
ratings = Transformations.switchMap(id, repository::ratings);
}