Search in sources :

Example 1 with Content

use of com.meisolsson.githubsdk.model.Content in project gh4a by slapperwan.

the class ContentListFragment method onCreateDataSingle.

@Override
protected Single<List<Content>> onCreateDataSingle(boolean bypassCache) {
    ArrayList<Content> contents = getArguments().getParcelableArrayList("contents");
    if (contents != null && !contents.isEmpty()) {
        return Single.just(contents);
    }
    RepositoryContentService contentService = ServiceFactory.get(RepositoryContentService.class, bypassCache);
    String repoOwner = mRepository.owner().login();
    String repoName = mRepository.name();
    String ref = mRef != null ? mRef : mRepository.defaultBranch();
    return ApiHelpers.PageIterator.toSingle(page -> contentService.getDirectoryContents(repoOwner, repoName, mPath, ref, page)).compose(RxUtils.mapFailureToValue(HttpURLConnection.HTTP_NOT_FOUND, new ArrayList<Content>())).compose(RxUtils.sortList(COMPARATOR));
}
Also used : Content(com.meisolsson.githubsdk.model.Content) RepositoryContentService(com.meisolsson.githubsdk.service.repositories.RepositoryContentService)

Example 2 with Content

use of com.meisolsson.githubsdk.model.Content in project gh4a by slapperwan.

the class ContentListFragment method onContextItemSelected.

@Override
public boolean onContextItemSelected(MenuItem item) {
    ContextMenuAwareRecyclerView.RecyclerContextMenuInfo info = (ContextMenuAwareRecyclerView.RecyclerContextMenuInfo) item.getMenuInfo();
    if (info.position >= mAdapter.getItemCount()) {
        return false;
    }
    int id = item.getItemId();
    if (id == MENU_HISTORY) {
        Content contents = mAdapter.getItemFromAdapterPosition(info.position);
        Intent intent = CommitHistoryActivity.makeIntent(getActivity(), mRepository.owner().login(), mRepository.name(), mRef, contents.path(), true);
        startActivityForResult(intent, REQUEST_FILE_HISTORY);
        return true;
    }
    return super.onContextItemSelected(item);
}
Also used : Content(com.meisolsson.githubsdk.model.Content) Intent(android.content.Intent) ContextMenuAwareRecyclerView(com.gh4a.widget.ContextMenuAwareRecyclerView)

Example 3 with Content

use of com.meisolsson.githubsdk.model.Content in project gh4a by slapperwan.

the class ContentListContainerFragment method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mRxLoader = new RxLoader(getActivity(), getLoaderManager());
    mRepository = getArguments().getParcelable("repository");
    mSelectedRef = getArguments().getString("ref");
    mStateSaved = false;
    if (savedInstanceState != null) {
        mDirStack.addAll(savedInstanceState.getStringArrayList(STATE_KEY_DIR_STACK));
        int prefixLen = STATE_KEY_CONTENT_CACHE_PREFIX.length();
        for (String key : savedInstanceState.keySet()) {
            if (key.startsWith(STATE_KEY_CONTENT_CACHE_PREFIX)) {
                String cacheKey = key.substring(prefixLen);
                if (cacheKey.equals("/")) {
                    cacheKey = null;
                }
                ArrayList<Content> content = savedInstanceState.getParcelableArrayList(key);
                mContentCache.put(cacheKey, content);
            }
        }
        mInitialPathToLoad = savedInstanceState.getStringArrayList(STATE_KEY_INITIAL_PATH);
    } else {
        mDirStack.push("");
        String initialPath = getArguments().getString("initialpath");
        if (initialPath != null) {
            mInitialPathToLoad = new ArrayList<>();
            int pos = initialPath.indexOf("/");
            while (pos > 0) {
                mInitialPathToLoad.add(initialPath.substring(0, pos));
                pos = initialPath.indexOf("/", pos + 1);
            }
            mInitialPathToLoad.add(initialPath);
        }
    }
}
Also used : Content(com.meisolsson.githubsdk.model.Content) RxLoader(com.philosophicalhacker.lib.RxLoader)

Example 4 with Content

use of com.meisolsson.githubsdk.model.Content in project PocketHub by pockethub.

the class HttpImageGetter method requestRepositoryImage.

/**
 * Request an image using the contents API if the source URI is a path to a
 * file already in the repository
 *
 * @param source
 * @return
 * @throws IOException
 */
private Drawable requestRepositoryImage(final String source) throws IOException {
    if (TextUtils.isEmpty(source)) {
        return null;
    }
    Uri uri = Uri.parse(source);
    if (!HOST_DEFAULT.equals(uri.getHost())) {
        return null;
    }
    List<String> segments = uri.getPathSegments();
    if (segments.size() < 5) {
        return null;
    }
    String prefix = segments.get(2);
    // github.com/github/android/blob/master/app/res/drawable-xhdpi/app_icon.png?raw=true
    if (!("raw".equals(prefix) || ("blob".equals(prefix) && !TextUtils.isEmpty(uri.getQueryParameter("raw"))))) {
        return null;
    }
    String owner = segments.get(0);
    if (TextUtils.isEmpty(owner)) {
        return null;
    }
    String name = segments.get(1);
    if (TextUtils.isEmpty(name)) {
        return null;
    }
    String branch = segments.get(3);
    if (TextUtils.isEmpty(branch)) {
        return null;
    }
    StringBuilder path = new StringBuilder(segments.get(4));
    for (int i = 5; i < segments.size(); i++) {
        String segment = segments.get(i);
        if (!TextUtils.isEmpty(segment)) {
            path.append('/').append(segment);
        }
    }
    if (TextUtils.isEmpty(path)) {
        return null;
    }
    Content contents = ServiceGenerator.createService(context, RepositoryContentService.class).getContents(owner, name, path.toString(), branch).blockingGet().body();
    if (contents.content() != null) {
        byte[] content = Base64.decode(contents.content(), DEFAULT);
        Bitmap bitmap = ImageUtils.getBitmap(content, width, MAX_VALUE);
        if (bitmap == null) {
            return loading.getDrawable(source);
        }
        BitmapDrawable drawable = new BitmapDrawable(context.getResources(), bitmap);
        drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
        return drawable;
    } else {
        return null;
    }
}
Also used : Bitmap(android.graphics.Bitmap) Content(com.meisolsson.githubsdk.model.Content) RepositoryContentService(com.meisolsson.githubsdk.service.repositories.RepositoryContentService) BitmapDrawable(android.graphics.drawable.BitmapDrawable) Uri(android.net.Uri)

Example 5 with Content

use of com.meisolsson.githubsdk.model.Content in project gh4a by slapperwan.

the class ContentListFragment method onCreateContextMenu.

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    ContextMenuAwareRecyclerView.RecyclerContextMenuInfo info = (ContextMenuAwareRecyclerView.RecyclerContextMenuInfo) menuInfo;
    Content contents = mAdapter.getItemFromAdapterPosition(info.position);
    Set<String> subModules = mCallback.getSubModuleNames(this);
    if (subModules == null || !subModules.contains(contents.name())) {
        menu.add(Menu.NONE, MENU_HISTORY, Menu.NONE, R.string.history);
    }
}
Also used : Content(com.meisolsson.githubsdk.model.Content) ContextMenuAwareRecyclerView(com.gh4a.widget.ContextMenuAwareRecyclerView)

Aggregations

Content (com.meisolsson.githubsdk.model.Content)7 RepositoryContentService (com.meisolsson.githubsdk.service.repositories.RepositoryContentService)3 Uri (android.net.Uri)2 ContextMenuAwareRecyclerView (com.gh4a.widget.ContextMenuAwareRecyclerView)2 RxLoader (com.philosophicalhacker.lib.RxLoader)2 Context (android.content.Context)1 Intent (android.content.Intent)1 Bitmap (android.graphics.Bitmap)1 BitmapDrawable (android.graphics.drawable.BitmapDrawable)1 Bundle (android.os.Bundle)1 Nullable (android.support.annotation.Nullable)1 Fragment (android.support.v4.app.Fragment)1 FragmentManager (android.support.v4.app.FragmentManager)1 FragmentTransaction (android.support.v4.app.FragmentTransaction)1 TextUtils (android.text.TextUtils)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 BaseActivity (com.gh4a.BaseActivity)1 R (com.gh4a.R)1