本文整理汇总了Java中android.arch.lifecycle.Transformations类的典型用法代码示例。如果您正苦于以下问题:Java Transformations类的具体用法?Java Transformations怎么用?Java Transformations使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Transformations类属于android.arch.lifecycle包,在下文中一共展示了Transformations类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: grepData
import android.arch.lifecycle.Transformations; //导入依赖的package包/类
@MainThread
static LiveData<LogcatContent> grepData(LiveData<LogcatContent> rawData, String grep) {
return Transformations.map(rawData, logcatData -> {
String content = logcatData.getContent();
if (GREP_SIGNAL.equals(content)) {
return logcatData;
}
if (content != null) {
logcatData.setContent(parseHtml2(content,grep));
}
return logcatData;
});
}
示例2: subscribeToDbChanges
import android.arch.lifecycle.Transformations; //导入依赖的package包/类
private void subscribeToDbChanges() {
LiveData<List<LoanWithUserAndBook>> loans
= mDb.loanModel().findLoansByNameAfter("Mike", getYesterdayDate());
// Instead of exposing the list of Loans, we can apply a transformation and expose Strings.
mLoansResult = Transformations.map(loans,
new Function<List<LoanWithUserAndBook>, String>() {
@Override
public String apply(List<LoanWithUserAndBook> loansWithUserAndBook) {
StringBuilder sb = new StringBuilder();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm",
Locale.US);
for (LoanWithUserAndBook loan : loansWithUserAndBook) {
sb.append(String.format("%s\n (Returned: %s)\n",
loan.bookTitle,
simpleDateFormat.format(loan.endTime)));
}
return sb.toString();
}
});
}
示例3: subscribeToDbChanges
import android.arch.lifecycle.Transformations; //导入依赖的package包/类
private void subscribeToDbChanges() {
// TODO: Modify this query to show only recent loans from specific user
LiveData<List<LoanWithUserAndBook>> loans
= mDb.loanModel().findAllWithUserAndBook();
// Instead of exposing the list of Loans, we can apply a transformation and expose Strings.
mLoansResult = Transformations.map(loans,
new Function<List<LoanWithUserAndBook>, String>() {
@Override
public String apply(List<LoanWithUserAndBook> loansWithUserAndBook) {
StringBuilder sb = new StringBuilder();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm",
Locale.US);
for (LoanWithUserAndBook loan : loansWithUserAndBook) {
sb.append(String.format("%s\n (Returned: %s)\n",
loan.bookTitle,
simpleDateFormat.format(loan.endTime)));
}
return sb.toString();
}
});
}
示例4: 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
示例5: 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);
}
});
}
示例6: 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);
}
});
}
示例7: 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());
}
示例8: 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);
}
});
}
示例9: bindViewModel
import android.arch.lifecycle.Transformations; //导入依赖的package包/类
public void bindViewModel(@NonNull final CheckoutShippingRatesViewModel viewModel) {
if (this.viewModel != null) {
throw new IllegalStateException("Already bound");
}
this.viewModel = viewModel;
Transformations.map(viewModel.selectedShippingRateLiveData(), shippingRate ->
shippingRate != null ? shippingRate.title : null)
.observe(this, title -> {
if (title != null) {
shippingLineView.setText(title);
} else {
shippingLineView.setText(R.string.checkout_shipping_method_not_selected);
}
});
Transformations.map(viewModel.selectedShippingRateLiveData(), shippingRate ->
shippingRate != null ? CURRENCY_FORMAT.format(shippingRate.price)
: getResources().getString(R.string.checkout_shipping_method_price_not_available))
.observe(this, price -> priceView.setText(price));
}
示例10: 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();
}
}
});
}
});
}
示例11: loadOrdered
import android.arch.lifecycle.Transformations; //导入依赖的package包/类
public LiveData<List<Repo>> loadOrdered(List<Integer> repoIds) {
SparseIntArray order = new SparseIntArray();
int index = 0;
for (Integer repoId : repoIds) {
order.put(repoId, index++);
}
return Transformations.map(loadById(repoIds), repositories -> {
Collections.sort(repositories, (r1, r2) -> {
int pos1 = order.get(r1.id);
int pos2 = order.get(r2.id);
return pos1 - pos2;
});
return repositories;
});
}
示例12: 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);
});
}
示例13: 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);
}
示例14: 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);
}
});
}
示例15: loadOrdered
import android.arch.lifecycle.Transformations; //导入依赖的package包/类
public LiveData<List<Repo>> loadOrdered(List<Integer> repoIds) {
SparseIntArray order = new SparseIntArray();
int index = 0;
for (Integer repoId : repoIds) {
order.put(repoId, index++);
}
return Transformations.map(loadById(repoIds), repositories -> {
Collections.sort(repositories, (r1, r2) -> {
int pos1 = order.get(r1.id);
int pos2 = order.get(r2.id);
return pos1 - pos2;
});
return repositories;
});
}