Search in sources :

Example 21 with Chapter

use of ar.rulosoft.mimanganu.componentes.Chapter in project MiMangaNu by raulhaag.

the class MainFragment method onContextItemSelected.

@Override
public boolean onContextItemSelected(MenuItem item) {
    final Manga manga = ((MangasRecAdapter) recyclerView.getAdapter()).getItem(lastContextMenuIndex);
    if (item.getItemId() == R.id.delete) {
        new AlertDialog.Builder(getContext()).setTitle(R.string.app_name).setMessage(getString(R.string.manga_delete_confirm, manga.getTitle())).setNegativeButton(getString(android.R.string.no), new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                DownloadPoolService.forceStop(manga.getId());
                ServerBase serverBase = ServerBase.getServer(manga.getServerId(), getContext());
                String path = Paths.generateBasePath(serverBase, manga, getActivity());
                Util.getInstance().deleteRecursive(new File(path));
                Database.deleteManga(getActivity(), manga.getId());
                mMAdapter.remove(manga);
                Util.getInstance().showFastSnackBar(getResources().getString(R.string.deleted, manga.getTitle()), getView(), getContext());
                dialog.dismiss();
            }
        }).show();
    } else if (item.getItemId() == R.id.noupdate) {
        if (manga.isFinished()) {
            manga.setFinished(false);
            Database.setUpgradable(getActivity(), manga.getId(), false);
        } else {
            manga.setFinished(true);
            Database.setUpgradable(getActivity(), manga.getId(), true);
        }
    } else if (item.getItemId() == R.id.download_all_chapters) {
        ArrayList<Chapter> chapters = Database.getChapters(getActivity(), manga.getId(), Database.COL_CAP_DOWNLOADED + " != 1", true);
        for (Chapter chapter : chapters) {
            try {
                DownloadPoolService.addChapterDownloadPool(getActivity(), chapter, false);
            } catch (Exception e) {
                Log.e(TAG, "Download add pool error", e);
            }
        }
        if (chapters.size() > 1)
            Util.getInstance().showFastSnackBar(getString(R.string.downloading) + " " + chapters.size() + " " + getString(R.string.chapters), getView(), getContext());
        else
            Util.getInstance().showFastSnackBar(getString(R.string.downloading) + " " + chapters.size() + " " + getString(R.string.chapter), getView(), getContext());
    }
    return super.onContextItemSelected(item);
}
Also used : AlertDialog(android.app.AlertDialog) ServerBase(ar.rulosoft.mimanganu.servers.ServerBase) DialogInterface(android.content.DialogInterface) Chapter(ar.rulosoft.mimanganu.componentes.Chapter) MangasRecAdapter(ar.rulosoft.mimanganu.adapters.MangasRecAdapter) File(java.io.File) Manga(ar.rulosoft.mimanganu.componentes.Manga)

Example 22 with Chapter

use of ar.rulosoft.mimanganu.componentes.Chapter in project MiMangaNu by raulhaag.

the class MangaFragment method onActivityCreated.

@Override
public void onActivityCreated(final Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    pm = PreferenceManager.getDefaultSharedPreferences(getActivity());
    super.onCreate(savedInstanceState);
    if (mMangaId == -1) {
        getActivity().onBackPressed();
    }
    mManga = Database.getManga(getActivity(), mMangaId);
    mServerBase = ServerBase.getServer(mManga.getServerId(), getContext());
    if (getView() == null) {
        try {
            finalize();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    } else {
        mListView = getView().findViewById(R.id.list);
        swipeReLayout = getView().findViewById(R.id.str);
        readerOptions = getView().findViewById(R.id.reader_options);
        mImageLoader = new ImageLoader(getActivity());
        final int[] colors = ThemeColors.getColors(pm);
        readerOptions.setBackgroundColor(colors[0]);
        readerOptions.setManga(mManga);
        swipeReLayout.setColorSchemeColors(colors[0], colors[1]);
        if (savedInstanceState != null) {
            if (searchForNewChapters.getStatus() == AsyncTask.Status.RUNNING) {
                swipeReLayout.post(new Runnable() {

                    @Override
                    public void run() {
                        swipeReLayout.setRefreshing(true);
                    }
                });
            }
        }
        swipeReLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh() {
                if ((searchForNewChapters.getStatus() != AsyncTask.Status.RUNNING)) {
                    searchForNewChapters = new SearchForNewChapters();
                    searchForNewChapters.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                }
            }
        });
        mListView.setDivider(new ColorDrawable(colors[0]));
        mListView.setDividerHeight(1);
        mInfo = new ControlInfoNoScroll(getActivity());
        mListView.addHeaderView(mInfo);
        mInfo.setColor(MainActivity.darkTheme, colors[0]);
        mInfo.enableTitleCopy(getActivity(), mManga.getTitle());
        ChapterAdapter.setColor(MainActivity.darkTheme, colors[1], colors[0]);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Chapter c = (Chapter) mListView.getAdapter().getItem(position);
                getPagesTask = new GetPagesTask();
                getPagesTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, c);
            }
        });
        mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        mListView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {

            @Override
            public boolean onPrepareActionMode(android.view.ActionMode mode, Menu menu) {
                return false;
            }

            @Override
            public void onDestroyActionMode(android.view.ActionMode mode) {
                mChapterAdapter.clearSelection();
            }

            @Override
            public boolean onCreateActionMode(android.view.ActionMode mode, Menu menu) {
                MenuInflater inflater = getActivity().getMenuInflater();
                inflater.inflate(R.menu.listitem_capitulo_menu_cab, menu);
                return true;
            }

            @Override
            public boolean onActionItemClicked(final android.view.ActionMode mode, MenuItem item) {
                final SparseBooleanArray selection = mChapterAdapter.getSelection();
                final ServerBase serverBase = ServerBase.getServer(mManga.getServerId(), getContext());
                boolean finish = true;
                switch(item.getItemId()) {
                    case R.id.select_all:
                        mChapterAdapter.selectAll();
                        return true;
                    case R.id.unselect:
                        mChapterAdapter.clearSelection();
                        return true;
                    case R.id.select_from:
                        mChapterAdapter.selectFrom(selection.keyAt(0));
                        return true;
                    case R.id.select_to:
                        mChapterAdapter.selectTo(selection.keyAt(0));
                        return true;
                    case R.id.download_selection:
                        new AsyncAddChapters().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, mChapterAdapter.getSelectedChapters());
                        break;
                    case R.id.mark_as_read_and_delete_images:
                        markSelectedAsRead = new MarkSelectedAsRead(selection.size());
                        markSelectedAsRead.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                        deleteImages = new DeleteImages(serverBase, selection.size());
                        deleteImages.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                        break;
                    case R.id.delete_images:
                        deleteImages = new DeleteImages(serverBase, selection.size());
                        deleteImages.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                        break;
                    case R.id.remove_chapter:
                        finish = false;
                        new AlertDialog.Builder(getContext()).setTitle(R.string.app_name).setMessage(R.string.delete_confirm).setNegativeButton(getString(android.R.string.no), new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        }).setPositiveButton(getString(android.R.string.ok), new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (selection.size() < 8) {
                                    // Remove chapters on UI Thread
                                    for (int i = selection.size() - 1; i >= 0; i--) {
                                        Chapter chapter = mChapterAdapter.getItem(selection.keyAt(i));
                                        chapter.delete(getActivity(), mManga, serverBase);
                                        mChapterAdapter.remove(chapter);
                                        mode.finish();
                                    }
                                } else {
                                    removeChapters = new RemoveChapters(serverBase, selection.size(), mode);
                                    removeChapters.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                                }
                                dialog.dismiss();
                            }
                        }).show();
                        break;
                    case R.id.reset_chapter:
                        resetChapters = new ResetChapters(serverBase, selection.size());
                        resetChapters.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                        break;
                    case R.id.mark_selected_as_read:
                        markSelectedAsRead = new MarkSelectedAsRead(selection.size());
                        markSelectedAsRead.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                        break;
                    case R.id.mark_selected_as_unread:
                        markSelectedAsUnread = new MarkSelectedAsUnread(selection.size());
                        markSelectedAsUnread.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                        break;
                }
                mChapterAdapter.notifyDataSetChanged();
                if (finish)
                    mode.finish();
                return false;
            }

            @Override
            public void onItemCheckedStateChanged(android.view.ActionMode mode, int position, long id, boolean checked) {
                mChapterAdapter.setSelectedOrUnselected(position);
            }
        });
        getActivity().setTitle(mManga.getTitle());
        Database.updateMangaRead(getActivity(), mManga.getId());
        loadInfo(mManga);
        chapters_order = pm.getInt(CHAPTERS_ORDER, 1);
        hide_read = pm.getBoolean(CHAPTERS_HIDE_READ, false);
    }
}
Also used : ActionMode(android.view.ActionMode) DialogInterface(android.content.DialogInterface) SwipeRefreshLayout(android.support.v4.widget.SwipeRefreshLayout) SparseBooleanArray(android.util.SparseBooleanArray) Menu(android.view.Menu) ServerBase(ar.rulosoft.mimanganu.servers.ServerBase) MenuInflater(android.view.MenuInflater) Chapter(ar.rulosoft.mimanganu.componentes.Chapter) AbsListView(android.widget.AbsListView) MenuItem(android.view.MenuItem) View(android.view.View) AdapterView(android.widget.AdapterView) AbsListView(android.widget.AbsListView) ListView(android.widget.ListView) ControlInfoNoScroll(ar.rulosoft.mimanganu.componentes.ControlInfoNoScroll) ColorDrawable(android.graphics.drawable.ColorDrawable) AdapterView(android.widget.AdapterView) ImageLoader(com.fedorvlasov.lazylist.ImageLoader)

Example 23 with Chapter

use of ar.rulosoft.mimanganu.componentes.Chapter in project MiMangaNu by raulhaag.

the class NineManga method loadMangaInformation.

@Override
public void loadMangaInformation(Manga manga, boolean forceReload) throws Exception {
    if (manga.getChapters().isEmpty() || forceReload) {
        String data = getNavigatorWithNeededHeader().get(HOST + manga.getPath());
        if (data.contains("?waring=1")) {
            // es.ninemanga delivers invalid chapter links if ?waring=1 is already passed on
            // non-warned Manga - so check if there is a link and then retry with the extension
            data = getNavigatorWithNeededHeader().get(HOST + manga.getPath().replaceAll(" ", "+") + "?waring=1");
        }
        // cover image
        manga.setImages(getFirstMatchDefault(PATTERN_COVER, data, ""));
        // summary
        manga.setSynopsis(getFirstMatchDefault(PATTERN_SUMMARY, data, context.getString(R.string.nodisponible)));
        assert manga.getSynopsis() != null;
        manga.setSynopsis(manga.getSynopsis().replaceFirst("^.+?: ", "").replace("\\", ""));
        // ongoing or completed
        manga.setFinished(data.contains(PATTERN_COMPLETED));
        // author
        manga.setAuthor(getFirstMatchDefault(PATTERN_AUTHOR, data, context.getString(R.string.nodisponible)));
        // genre
        manga.setGenre(getFirstMatchDefault(PATTERN_GENRE, data, context.getString(R.string.nodisponible)).replace("</a>", "</a>,"));
        // chapter
        Pattern p = Pattern.compile(PATTERN_CHAPTER, Pattern.DOTALL);
        Matcher m = p.matcher(data);
        while (m.find()) {
            manga.addChapterFirst(new Chapter(m.group(3), m.group(1)));
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Chapter(ar.rulosoft.mimanganu.componentes.Chapter)

Example 24 with Chapter

use of ar.rulosoft.mimanganu.componentes.Chapter in project MiMangaNu by raulhaag.

the class ReadMangaToday method loadMangaInformation.

@Override
public void loadMangaInformation(Manga manga, boolean forceReload) throws Exception {
    if (manga.getChapters().isEmpty() || forceReload) {
        String source = getNavigatorAndFlushParameters().getAndReturnResponseCodeOnFailure(manga.getPath());
        if (source.equals("400")) {
            // ReadMangaToday returns 400 Bad Request sometimes
            // deleting it's cookies will usually get rid of the error
            Util.getInstance().removeSpecificCookies(context, HOST);
            source = getNavigatorAndFlushParameters().get(manga.getPath());
        }
        // Cover
        if (manga.getImages() == null || manga.getImages().isEmpty() || manga.getImages().contains("thumb")) {
            manga.setImages(getFirstMatchDefault("<div class=\"col-md-3\">.+?<img src=\"(.+?)\" alt=", source, ""));
        }
        // Summary
        String summary = getFirstMatchDefault("<li class=\"list-group-item movie-detail\">(.+?)</li>", source, context.getString(R.string.nodisponible));
        manga.setSynopsis(summary);
        // summary can be empty after cleaning, so check again here
        if (manga.getSynopsis() == null || manga.getSynopsis().isEmpty()) {
            manga.setSynopsis(context.getString(R.string.nodisponible));
        }
        // Status
        manga.setFinished(source.contains("<dd>Completed</dd>"));
        // Author (can be multi-part)
        ArrayList<String> authors = getAllMatch("<li><a href=\"http://www\\.readmanga\\.today/people/[^\"]+?\">([^\"]+?)</a>", source);
        if (authors.isEmpty()) {
            manga.setAuthor(context.getString(R.string.nodisponible));
        } else {
            manga.setAuthor(TextUtils.join(", ", authors));
        }
        // Genre
        String genre = getFirstMatchDefault("<dt>Categories:</dt>.+?<dd>(.+?)</dd>", source, context.getString(R.string.nodisponible)).replaceAll("</a>", ",");
        manga.setGenre(genre.substring(0, genre.lastIndexOf(",")));
        // Chapters
        Pattern p = Pattern.compile("<li>[\\s]*<a href=\"([^\"]+?)\">[\\s]*<span class=\"val\"><span class=\"icon-arrow-.\"></span>(.+?)</span>", Pattern.DOTALL);
        Matcher matcher = p.matcher(source);
        while (matcher.find()) {
            manga.addChapterFirst(new Chapter(matcher.group(2), matcher.group(1)));
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Chapter(ar.rulosoft.mimanganu.componentes.Chapter)

Example 25 with Chapter

use of ar.rulosoft.mimanganu.componentes.Chapter in project MiMangaNu by raulhaag.

the class ServerBase method searchForNewChapters.

/**
 * Searches for new chapters for a given Manga.
 * Loads information from the database and fetches the current state from the server. Afterwards
 * the server information is compared to the local information to check if new chapters are
 * available.
 * <p>
 * A fast check can be triggered to reduce load and compare time by checking only the last 20
 * chapters for differences.
 * <p>
 * All detected changes are also stored in the database.
 *
 * @param id      the Manga id to check new chapters for
 * @param context the Context object to use for checking
 * @param fast    <code>true</code> to perform a fast check (first 20 chapters only)
 * @return the count of new chapters found
 * @throws Exception if an error occurred. The exception is thrown back to UI, so its message
 *                   should be a translatable string.
 */
public int searchForNewChapters(int id, Context context, boolean fast) throws Exception {
    int returnValue;
    Manga mangaDb = Database.getFullManga(context, id);
    Manga manga = new Manga(mangaDb.getServerId(), mangaDb.getTitle(), mangaDb.getPath(), false);
    manga.setId(mangaDb.getId());
    try {
        this.loadMangaInformation(manga, true);
        this.loadChapters(manga, false);
    } catch (Exception e) {
        // to many messages are annoying
        // Util.getInstance().toast(context, context.getResources().getString(R.string.update_search_failed, mangaDb.getTitle(), getServerName()));
        e.printStackTrace();
        return 0;
    }
    if (fast && manga.getChapters().size() > 21) {
        manga.getChapters().subList(0, manga.getChapters().size() - 20).clear();
    }
    manga.getChapters().removeAll(mangaDb.getChapters());
    ArrayList<Chapter> simpleList = manga.getChapters();
    for (Chapter chapter : simpleList) {
        chapter.setMangaID(mangaDb.getId());
        chapter.setReadStatus(Chapter.NEW);
        Database.addChapter(context, chapter, mangaDb.getId());
    }
    if (simpleList.size() > 0) {
        Database.updateMangaRead(context, mangaDb.getId());
        Database.updateNewMangas(context, mangaDb, simpleList.size());
    }
    if (!simpleList.isEmpty())
        new CreateGroupByMangaNotificationsTask(simpleList, manga, context).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    returnValue = simpleList.size();
    boolean changes = false;
    if (!mangaDb.getAuthor().equals(manga.getAuthor()) && manga.getAuthor() != null && manga.getAuthor().length() > 2) {
        mangaDb.setAuthor(manga.getAuthor());
        changes = true;
    }
    if (mangaDb.getImages() != null && !mangaDb.getImages().equals(manga.getImages()) && manga.getImages() != null && manga.getImages().length() > 2) {
        mangaDb.setImages(manga.getImages());
        changes = true;
    }
    if (!mangaDb.getSynopsis().equals(manga.getSynopsis()) && manga.getSynopsis().length() > 3) {
        mangaDb.setSynopsis(manga.getSynopsis());
        changes = true;
    }
    if (!mangaDb.getGenre().equals(manga.getGenre()) && manga.getGenre().length() > 2) {
        mangaDb.setGenre(manga.getGenre());
        changes = true;
    }
    if (mangaDb.isFinished() != manga.isFinished()) {
        mangaDb.setFinished(manga.isFinished());
        changes = true;
    }
    if (!simpleList.isEmpty()) {
        DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy", Locale.getDefault());
        Date date = new Date();
        String lastUpdate = dateFormat.format(date);
        if (mangaDb.getLastUpdate() != null && !mangaDb.getLastUpdate().equals(lastUpdate)) {
            mangaDb.setLastUpdate(lastUpdate);
            changes = true;
        }
    }
    if (changes)
        Database.updateManga(context, mangaDb, false);
    return returnValue;
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Chapter(ar.rulosoft.mimanganu.componentes.Chapter) SimpleDateFormat(java.text.SimpleDateFormat) Manga(ar.rulosoft.mimanganu.componentes.Manga) Date(java.util.Date)

Aggregations

Chapter (ar.rulosoft.mimanganu.componentes.Chapter)37 Matcher (java.util.regex.Matcher)27 Pattern (java.util.regex.Pattern)27 ArrayList (java.util.ArrayList)5 Manga (ar.rulosoft.mimanganu.componentes.Manga)4 DialogInterface (android.content.DialogInterface)3 View (android.view.View)3 AlertDialog (android.app.AlertDialog)2 SharedPreferences (android.content.SharedPreferences)2 TextView (android.widget.TextView)2 ServerBase (ar.rulosoft.mimanganu.servers.ServerBase)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 SuppressLint (android.annotation.SuppressLint)1 ColorDrawable (android.graphics.drawable.ColorDrawable)1 SwipeRefreshLayout (android.support.v4.widget.SwipeRefreshLayout)1 SparseBooleanArray (android.util.SparseBooleanArray)1 ActionMode (android.view.ActionMode)1 LayoutInflater (android.view.LayoutInflater)1 Menu (android.view.Menu)1