Search in sources :

Example 6 with RAction

use of io.github.mthli.Bitocle.Database.Repo.RAction in project Bitocle by mthli.

the class RepoTask method doInBackground.

@Override
protected Boolean doInBackground(Void... params) {
    if (flag == Flag.REPO_FIRST) {
        RAction action = new RAction(context);
        try {
            action.openDatabase(true);
        } catch (SQLException s) {
            action.closeDatabase();
            return false;
        }
        List<Repository> repositories;
        try {
            repositories = service.getRepositories();
        } catch (IOException i) {
            action.closeDatabase();
            return false;
        }
        if (isCancelled()) {
            action.closeDatabase();
            return false;
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        if (flag == Flag.REPO_FIRST) {
            for (Repository r : repositories) {
                if (!action.checkRepo(r.getGitUrl())) {
                    Repo repo = new Repo();
                    repo.setName(r.getName());
                    repo.setDate(format.format(r.getCreatedAt()));
                    repo.setDescription(r.getDescription());
                    repo.setLang(r.getLanguage());
                    repo.setStar(r.getWatchers());
                    repo.setFork(r.getForks());
                    repo.setOwner(r.getOwner().getLogin());
                    repo.setGit(r.getGitUrl());
                    action.addRepo(repo);
                }
            }
        }
        action.closeDatabase();
    }
    if (isCancelled()) {
        return false;
    }
    return true;
}
Also used : Repository(org.eclipse.egit.github.core.Repository) Repo(io.github.mthli.Bitocle.Database.Repo.Repo) SQLException(android.database.SQLException) RAction(io.github.mthli.Bitocle.Database.Repo.RAction) IOException(java.io.IOException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 7 with RAction

use of io.github.mthli.Bitocle.Database.Repo.RAction in project Bitocle by mthli.

the class StarItemAdapter method getView.

@Override
public View getView(final int position, final View convertView, ViewGroup viewGroup) {
    Holder holder;
    View view = convertView;
    if (view == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        view = inflater.inflate(layoutResId, viewGroup, false);
        holder = new Holder();
        holder.icon = (ImageView) view.findViewById(R.id.repo_item_icon);
        holder.name = (TextView) view.findViewById(R.id.repo_item_name);
        holder.date = (TextView) view.findViewById(R.id.repo_item_date);
        holder.description = (TextView) view.findViewById(R.id.repo_item_description);
        holder.info = (TextView) view.findViewById(R.id.repo_item_info);
        holder.owner = (TextView) view.findViewById(R.id.repo_item_owner);
        holder.overflow = (ImageButton) view.findViewById(R.id.repo_item_overflow);
        view.setTag(holder);
    } else {
        holder = (Holder) view.getTag();
    }
    final StarItem starItem = list.get(position);
    holder.icon.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_type_repo));
    holder.name.setText(starItem.getName());
    holder.date.setText(starItem.getDate());
    String description;
    try {
        description = starItem.getDescription();
        if (description.length() == 0) {
            description = context.getString(R.string.repo_empty_description);
        }
    } catch (NullPointerException n) {
        description = context.getString(R.string.repo_empty_description);
    }
    holder.description.setText(description);
    String lang;
    try {
        lang = starItem.getLang();
        if (lang.equals("null")) {
            lang = context.getString(R.string.repo_item_unknown);
        }
    } catch (NullPointerException n) {
        lang = context.getString(R.string.repo_item_unknown);
    }
    holder.info.setText(lang + "   " + context.getString(R.string.repo_item_star) + " " + starItem.getStar() + "   " + context.getString(R.string.repo_item_fork) + " " + starItem.getFork());
    holder.owner.setText(starItem.getOwner());
    final PopupMenu menu = new PopupMenu(context, holder.overflow);
    menu.getMenuInflater().inflate(R.menu.star_item_overflow, menu.getMenu());
    holder.overflow.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            menu.show();
        }
    });
    menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch(item.getItemId()) {
                case R.id.star_item_overflow_commit:
                    fragment.setLocation(position);
                    fragment.changeToCommit(Flag.STAR_COMMIT_FIRST);
                    break;
                case R.id.star_item_overflow_add:
                    RAction action = new RAction(context);
                    try {
                        action.openDatabase(true);
                    } catch (SQLException s) {
                        action.closeDatabase();
                        SuperToast.create(context, context.getString(R.string.overflow_add_failed), SuperToast.Duration.VERY_SHORT, Style.getStyle(Style.RED)).show();
                        return false;
                    }
                    if (!action.checkRepo(starItem.getGit())) {
                        Repo repo = new Repo();
                        repo.setName(starItem.getName());
                        repo.setDate(starItem.getDate());
                        repo.setDescription(starItem.getDescription());
                        if (starItem.getLang() == null) {
                            repo.setLang(context.getString(R.string.repo_item_unknown));
                        } else {
                            repo.setLang(starItem.getLang());
                        }
                        repo.setStar(starItem.getStar());
                        repo.setFork(starItem.getFork());
                        repo.setOwner(starItem.getOwner());
                        repo.setGit(starItem.getGit());
                        action.addRepo(repo);
                    }
                    action.closeDatabase();
                    SuperToast.create(context, context.getString(R.string.overflow_add_successful), SuperToast.Duration.VERY_SHORT, Style.getStyle(Style.BLUE)).show();
                    break;
                default:
                    break;
            }
            //
            return true;
        }
    });
    return view;
}
Also used : SQLException(android.database.SQLException) Activity(android.app.Activity) MenuItem(android.view.MenuItem) View(android.view.View) Repo(io.github.mthli.Bitocle.Database.Repo.Repo) LayoutInflater(android.view.LayoutInflater) RAction(io.github.mthli.Bitocle.Database.Repo.RAction)

Example 8 with RAction

use of io.github.mthli.Bitocle.Database.Repo.RAction in project Bitocle by mthli.

the class RepoItemAdapter method getView.

@Override
public View getView(final int position, final View convertView, ViewGroup viewGroup) {
    Holder holder;
    View view = convertView;
    if (view == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        view = inflater.inflate(layoutResId, viewGroup, false);
        holder = new Holder();
        holder.icon = (ImageView) view.findViewById(R.id.repo_item_icon);
        holder.name = (TextView) view.findViewById(R.id.repo_item_name);
        holder.date = (TextView) view.findViewById(R.id.repo_item_date);
        holder.description = (TextView) view.findViewById(R.id.repo_item_description);
        holder.info = (TextView) view.findViewById(R.id.repo_item_info);
        holder.owner = (TextView) view.findViewById(R.id.repo_item_owner);
        holder.overflow = (ImageButton) view.findViewById(R.id.repo_item_overflow);
        view.setTag(holder);
    } else {
        holder = (Holder) view.getTag();
    }
    final RepoItem repoItem = list.get(position);
    holder.icon.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_type_repo));
    holder.name.setText(repoItem.getName());
    holder.date.setText(repoItem.getDate());
    String description;
    try {
        description = repoItem.getDescription();
        if (description.length() == 0) {
            description = context.getString(R.string.repo_empty_description);
        }
    } catch (NullPointerException n) {
        description = context.getString(R.string.repo_empty_description);
    }
    holder.description.setText(description);
    String lang;
    try {
        lang = repoItem.getLang();
        if (lang.equals("null")) {
            lang = context.getString(R.string.repo_item_unknown);
        }
    } catch (NullPointerException n) {
        lang = context.getString(R.string.repo_item_unknown);
    }
    holder.info.setText(lang + "   " + context.getString(R.string.repo_item_star) + " " + repoItem.getStar() + "   " + context.getString(R.string.repo_item_fork) + " " + repoItem.getFork());
    holder.owner.setText(repoItem.getOwner());
    final PopupMenu menu = new PopupMenu(context, holder.overflow);
    menu.getMenuInflater().inflate(R.menu.repo_item_overflow, menu.getMenu());
    holder.overflow.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            menu.show();
        }
    });
    menu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch(item.getItemId()) {
                case R.id.repo_item_overflow_commit:
                    fragment.setLocation(position);
                    fragment.changeToCommit(Flag.REPO_COMMIT_FIRST);
                    break;
                case R.id.repo_item_overflow_remove:
                    RAction rAction = new RAction(context);
                    BAction bAction = new BAction(context);
                    try {
                        rAction.openDatabase(true);
                        bAction.openDatabase(true);
                    } catch (SQLException s) {
                        rAction.closeDatabase();
                        bAction.closeDatabase();
                        SuperToast.create(context, context.getString(R.string.overflow_remove_failed), SuperToast.Duration.VERY_SHORT, Style.getStyle(Style.RED)).show();
                        return false;
                    }
                    rAction.deleteRepo(repoItem.getGit());
                    String key = repoItem.getOwner() + "/" + repoItem.getName();
                    bAction.unMarkByKey(key);
                    rAction.closeDatabase();
                    bAction.closeDatabase();
                    list.remove(position);
                    List<Map<String, String>> autoList = new ArrayList<Map<String, String>>();
                    autoList.clear();
                    for (RepoItem r : list) {
                        Map<String, String> map = new HashMap<String, String>();
                        map.put("owner", r.getOwner());
                        map.put("name", r.getName());
                        autoList.add(map);
                    }
                    SimpleAdapter autoAdapter = new SimpleAdapter(context, autoList, R.layout.auto_item, new String[] { "owner", "name" }, new int[] { R.id.auto_item_owner, R.id.auto_item_name });
                    autoAdapter.notifyDataSetChanged();
                    fragment.getSearch().setAdapter(autoAdapter);
                    if (list.size() <= 0) {
                        fragment.setContentEmpty(true);
                        fragment.setEmptyText(R.string.repo_empty_list);
                        fragment.setContentShown(true);
                    } else {
                        fragment.setContentEmpty(false);
                        fragment.getRepoItemAdapter().notifyDataSetChanged();
                        fragment.setContentShown(true);
                        SuperToast.create(context, context.getString(R.string.overflow_remove_successful), SuperToast.Duration.VERY_SHORT, Style.getStyle(Style.BLUE)).show();
                    }
                    break;
                default:
                    break;
            }
            return true;
        }
    });
    return view;
}
Also used : SQLException(android.database.SQLException) Activity(android.app.Activity) MenuItem(android.view.MenuItem) View(android.view.View) LayoutInflater(android.view.LayoutInflater) BAction(io.github.mthli.Bitocle.Database.Bookmark.BAction) ArrayList(java.util.ArrayList) List(java.util.List) RAction(io.github.mthli.Bitocle.Database.Repo.RAction) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

SQLException (android.database.SQLException)8 RAction (io.github.mthli.Bitocle.Database.Repo.RAction)8 Repo (io.github.mthli.Bitocle.Database.Repo.Repo)6 Activity (android.app.Activity)3 LayoutInflater (android.view.LayoutInflater)3 View (android.view.View)3 MenuItem (android.view.MenuItem)2 SimpleAdapter (android.widget.SimpleAdapter)2 BAction (io.github.mthli.Bitocle.Database.Bookmark.BAction)2 IOException (java.io.IOException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Repository (org.eclipse.egit.github.core.Repository)2 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1