Search in sources :

Example 11 with Optional

use of com.gh4a.utils.Optional in project gh4a by slapperwan.

the class RefPathDisambiguationTask method resolve.

// returns ref, path
private Single<Optional<Pair<String, String>>> resolve() throws ApiRequestException {
    // first check whether the path redirects to HEAD
    if (mRefAndPath.startsWith("HEAD")) {
        return Single.just(Optional.of(Pair.create("HEAD", mRefAndPath.startsWith("HEAD/") ? mRefAndPath.substring(5) : null)));
    }
    final RepositoryBranchService branchService = ServiceFactory.get(RepositoryBranchService.class, false);
    final RepositoryService repoService = ServiceFactory.get(RepositoryService.class, false);
    // then look for matching branches
    return ApiHelpers.PageIterator.toSingle(page -> branchService.getBranches(mRepoOwner, mRepoName, page)).compose(this::matchBranch).flatMap(result -> result.orOptionalSingle(() -> ApiHelpers.PageIterator.toSingle(page -> repoService.getTags(mRepoOwner, mRepoName, page)).compose(this::matchBranch))).map(resultOpt -> resultOpt.orOptional(() -> {
        // at this point, the first item may still be a SHA1 - check with a simple regex
        int slashPos = mRefAndPath.indexOf('/');
        String potentialSha = slashPos > 0 ? mRefAndPath.substring(0, slashPos) : mRefAndPath;
        if (SHA1_PATTERN.matcher(potentialSha).matches()) {
            return Optional.of(Pair.create(potentialSha, slashPos > 0 ? mRefAndPath.substring(slashPos + 1) : ""));
        }
        return Optional.absent();
    }));
}
Also used : RepositoryBranchService(com.meisolsson.githubsdk.service.repositories.RepositoryBranchService) FileViewerActivity(com.gh4a.activities.FileViewerActivity) ApiHelpers(com.gh4a.utils.ApiHelpers) Pair(android.util.Pair) RepositoryService(com.meisolsson.githubsdk.service.repositories.RepositoryService) Intent(android.content.Intent) TextUtils(android.text.TextUtils) Single(io.reactivex.Single) VisibleForTesting(android.support.annotation.VisibleForTesting) List(java.util.List) RepositoryActivity(com.gh4a.activities.RepositoryActivity) FragmentActivity(android.support.v4.app.FragmentActivity) Optional(com.gh4a.utils.Optional) Branch(com.meisolsson.githubsdk.model.Branch) ApiRequestException(com.gh4a.ApiRequestException) Pattern(java.util.regex.Pattern) ServiceFactory(com.gh4a.ServiceFactory) RepositoryBranchService(com.meisolsson.githubsdk.service.repositories.RepositoryBranchService) RepositoryService(com.meisolsson.githubsdk.service.repositories.RepositoryService)

Example 12 with Optional

use of com.gh4a.utils.Optional in project gh4a by slapperwan.

the class ContentListContainerFragment method loadModuleMap.

private void loadModuleMap() {
    RepositoryContentService service = ServiceFactory.get(RepositoryContentService.class, false);
    String repoOwner = mRepository.owner().login();
    String repoName = mRepository.name();
    service.getContents(repoOwner, repoName, ".gitmodules", mSelectedRef).map(ApiHelpers::throwOnFailure).map(Optional::of).compose(RxUtils.mapFailureToValue(HttpURLConnection.HTTP_NOT_FOUND, Optional.<Content>absent())).map(contentOpt -> contentOpt.map(content -> StringUtils.fromBase64(content.content()))).map(this::parseModuleMap).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).compose(mRxLoader.makeSingleTransformer(ID_LOADER_MODULEMAP, true)).subscribe(resultOpt -> {
        mGitModuleMap = resultOpt.orNull();
        if (mContentListFragment != null) {
            mContentListFragment.onSubModuleNamesChanged(getSubModuleNames(mContentListFragment));
        }
    }, ((BaseActivity) getActivity())::handleLoadFailure);
}
Also used : PathBreadcrumbs(com.gh4a.widget.PathBreadcrumbs) HttpURLConnection(java.net.HttpURLConnection) Context(android.content.Context) Repository(com.meisolsson.githubsdk.model.Repository) Bundle(android.os.Bundle) FileViewerActivity(com.gh4a.activities.FileViewerActivity) Uri(android.net.Uri) HashMap(java.util.HashMap) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) Stack(java.util.Stack) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Commit(com.meisolsson.githubsdk.model.Commit) SwipeRefreshLayout(com.gh4a.widget.SwipeRefreshLayout) RxLoader(com.philosophicalhacker.lib.RxLoader) Content(com.meisolsson.githubsdk.model.Content) Map(java.util.Map) R(com.gh4a.R) ContentType(com.meisolsson.githubsdk.model.ContentType) View(android.view.View) Schedulers(io.reactivex.schedulers.Schedulers) StringUtils(com.gh4a.utils.StringUtils) ApiHelpers(com.gh4a.utils.ApiHelpers) LayoutInflater(android.view.LayoutInflater) Fragment(android.support.v4.app.Fragment) Set(java.util.Set) TextUtils(android.text.TextUtils) ViewGroup(android.view.ViewGroup) RepositoryContentService(com.meisolsson.githubsdk.service.repositories.RepositoryContentService) FragmentManager(android.support.v4.app.FragmentManager) List(java.util.List) RepositoryActivity(com.gh4a.activities.RepositoryActivity) RxUtils(com.gh4a.utils.RxUtils) Optional(com.gh4a.utils.Optional) Nullable(android.support.annotation.Nullable) ServiceFactory(com.gh4a.ServiceFactory) FragmentTransaction(android.support.v4.app.FragmentTransaction) BaseActivity(com.gh4a.BaseActivity) Content(com.meisolsson.githubsdk.model.Content) RepositoryContentService(com.meisolsson.githubsdk.service.repositories.RepositoryContentService) ApiHelpers(com.gh4a.utils.ApiHelpers)

Aggregations

Optional (com.gh4a.utils.Optional)12 ApiHelpers (com.gh4a.utils.ApiHelpers)11 ServiceFactory (com.gh4a.ServiceFactory)9 RxUtils (com.gh4a.utils.RxUtils)9 Intent (android.content.Intent)8 Single (io.reactivex.Single)8 List (java.util.List)7 IntentUtils (com.gh4a.utils.IntentUtils)6 Context (android.content.Context)5 VisibleForTesting (android.support.annotation.VisibleForTesting)5 FragmentActivity (android.support.v4.app.FragmentActivity)5 LayoutInflater (android.view.LayoutInflater)5 View (android.view.View)5 R (com.gh4a.R)5 ViewGroup (android.view.ViewGroup)4 RepositoryActivity (com.gh4a.activities.RepositoryActivity)4 Repository (com.meisolsson.githubsdk.model.Repository)4 ReviewComment (com.meisolsson.githubsdk.model.ReviewComment)4 Bundle (android.os.Bundle)3 Nullable (android.support.annotation.Nullable)3