Search in sources :

Example 1 with Podcast

use of de.danoeh.antennapod.adapter.itunes.ItunesAdapter.Podcast in project AntennaPod by AntennaPod.

the class ItunesSearchFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View root = inflater.inflate(R.layout.fragment_itunes_search, container, false);
    gridView = (GridView) root.findViewById(R.id.gridView);
    adapter = new ItunesAdapter(getActivity(), new ArrayList<>());
    gridView.setAdapter(adapter);
    //Show information about the podcast when the list item is clicked
    gridView.setOnItemClickListener((parent, view1, position, id) -> {
        Podcast podcast = searchResults.get(position);
        if (!podcast.feedUrl.contains("itunes.apple.com")) {
            Intent intent = new Intent(getActivity(), OnlineFeedViewActivity.class);
            intent.putExtra(OnlineFeedViewActivity.ARG_FEEDURL, podcast.feedUrl);
            intent.putExtra(OnlineFeedViewActivity.ARG_TITLE, "iTunes");
            startActivity(intent);
        } else {
            gridView.setVisibility(View.GONE);
            progressBar.setVisibility(View.VISIBLE);
            subscription = Observable.create((Observable.OnSubscribe<String>) subscriber -> {
                OkHttpClient client = AntennapodHttpClient.getHttpClient();
                Request.Builder httpReq = new Request.Builder().url(podcast.feedUrl).header("User-Agent", ClientConfig.USER_AGENT);
                try {
                    Response response = client.newCall(httpReq.build()).execute();
                    if (response.isSuccessful()) {
                        String resultString = response.body().string();
                        JSONObject result = new JSONObject(resultString);
                        JSONObject results = result.getJSONArray("results").getJSONObject(0);
                        String feedUrl = results.getString("feedUrl");
                        subscriber.onNext(feedUrl);
                    } else {
                        String prefix = getString(R.string.error_msg_prefix);
                        subscriber.onError(new IOException(prefix + response));
                    }
                } catch (IOException | JSONException e) {
                    subscriber.onError(e);
                }
                subscriber.onCompleted();
            }).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(feedUrl -> {
                progressBar.setVisibility(View.GONE);
                gridView.setVisibility(View.VISIBLE);
                Intent intent = new Intent(getActivity(), OnlineFeedViewActivity.class);
                intent.putExtra(OnlineFeedViewActivity.ARG_FEEDURL, feedUrl);
                intent.putExtra(OnlineFeedViewActivity.ARG_TITLE, "iTunes");
                startActivity(intent);
            }, error -> {
                Log.e(TAG, Log.getStackTraceString(error));
                progressBar.setVisibility(View.GONE);
                gridView.setVisibility(View.VISIBLE);
                String prefix = getString(R.string.error_msg_prefix);
                new MaterialDialog.Builder(getActivity()).content(prefix + " " + error.getMessage()).neutralText(android.R.string.ok).show();
            });
        }
    });
    progressBar = (ProgressBar) root.findViewById(R.id.progressBar);
    txtvError = (TextView) root.findViewById(R.id.txtvError);
    butRetry = (Button) root.findViewById(R.id.butRetry);
    txtvEmpty = (TextView) root.findViewById(android.R.id.empty);
    loadToplist();
    return root;
}
Also used : Bundle(android.os.Bundle) GridView(android.widget.GridView) ProgressBar(android.widget.ProgressBar) SearchView(android.support.v7.widget.SearchView) ItunesAdapter(de.danoeh.antennapod.adapter.itunes.ItunesAdapter) AndroidSchedulers(rx.android.schedulers.AndroidSchedulers) Intent(android.content.Intent) MenuItem(android.view.MenuItem) ArrayList(java.util.ArrayList) Observable(rx.Observable) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) MenuItemUtils(de.danoeh.antennapod.menuhandler.MenuItemUtils) MenuInflater(android.view.MenuInflater) Locale(java.util.Locale) Menu(android.view.Menu) Schedulers(rx.schedulers.Schedulers) View(android.view.View) Button(android.widget.Button) Response(okhttp3.Response) AntennapodHttpClient(de.danoeh.antennapod.core.service.download.AntennapodHttpClient) Log(android.util.Log) Request(okhttp3.Request) LayoutInflater(android.view.LayoutInflater) R(de.danoeh.antennapod.R) MenuItemCompat(android.support.v4.view.MenuItemCompat) Fragment(android.support.v4.app.Fragment) ClientConfig(de.danoeh.antennapod.core.ClientConfig) IOException(java.io.IOException) ViewGroup(android.view.ViewGroup) URLEncoder(java.net.URLEncoder) List(java.util.List) TextView(android.widget.TextView) OkHttpClient(okhttp3.OkHttpClient) Podcast(de.danoeh.antennapod.adapter.itunes.ItunesAdapter.Podcast) OnlineFeedViewActivity(de.danoeh.antennapod.activity.OnlineFeedViewActivity) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Subscription(rx.Subscription) JSONArray(org.json.JSONArray) ItunesAdapter(de.danoeh.antennapod.adapter.itunes.ItunesAdapter) OnlineFeedViewActivity(de.danoeh.antennapod.activity.OnlineFeedViewActivity) OkHttpClient(okhttp3.OkHttpClient) ArrayList(java.util.ArrayList) Podcast(de.danoeh.antennapod.adapter.itunes.ItunesAdapter.Podcast) Request(okhttp3.Request) Intent(android.content.Intent) IOException(java.io.IOException) GridView(android.widget.GridView) SearchView(android.support.v7.widget.SearchView) View(android.view.View) TextView(android.widget.TextView) Observable(rx.Observable) Response(okhttp3.Response) JSONObject(org.json.JSONObject)

Example 2 with Podcast

use of de.danoeh.antennapod.adapter.itunes.ItunesAdapter.Podcast in project AntennaPod by AntennaPod.

the class ItunesSearchFragment method search.

private void search(String query) {
    if (subscription != null) {
        subscription.unsubscribe();
    }
    gridView.setVisibility(View.GONE);
    txtvError.setVisibility(View.GONE);
    butRetry.setVisibility(View.GONE);
    txtvEmpty.setVisibility(View.GONE);
    progressBar.setVisibility(View.VISIBLE);
    subscription = rx.Observable.create((Observable.OnSubscribe<List<Podcast>>) subscriber -> {
        String encodedQuery = null;
        try {
            encodedQuery = URLEncoder.encode(query, "UTF-8");
        } catch (UnsupportedEncodingException e) {
        }
        if (encodedQuery == null) {
            encodedQuery = query;
        }
        String formattedUrl = String.format(API_URL, query).replace(' ', '+');
        OkHttpClient client = AntennapodHttpClient.getHttpClient();
        Request.Builder httpReq = new Request.Builder().url(formattedUrl).header("User-Agent", ClientConfig.USER_AGENT);
        List<Podcast> podcasts = new ArrayList<>();
        try {
            Response response = client.newCall(httpReq.build()).execute();
            if (response.isSuccessful()) {
                String resultString = response.body().string();
                JSONObject result = new JSONObject(resultString);
                JSONArray j = result.getJSONArray("results");
                for (int i = 0; i < j.length(); i++) {
                    JSONObject podcastJson = j.getJSONObject(i);
                    Podcast podcast = Podcast.fromSearch(podcastJson);
                    podcasts.add(podcast);
                }
            } else {
                String prefix = getString(R.string.error_msg_prefix);
                subscriber.onError(new IOException(prefix + response));
            }
        } catch (IOException | JSONException e) {
            subscriber.onError(e);
        }
        subscriber.onNext(podcasts);
        subscriber.onCompleted();
    }).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(podcasts -> {
        progressBar.setVisibility(View.GONE);
        updateData(podcasts);
    }, error -> {
        Log.e(TAG, Log.getStackTraceString(error));
        progressBar.setVisibility(View.GONE);
        txtvError.setText(error.toString());
        txtvError.setVisibility(View.VISIBLE);
        butRetry.setOnClickListener(v -> search(query));
        butRetry.setVisibility(View.VISIBLE);
    });
}
Also used : Bundle(android.os.Bundle) GridView(android.widget.GridView) ProgressBar(android.widget.ProgressBar) SearchView(android.support.v7.widget.SearchView) ItunesAdapter(de.danoeh.antennapod.adapter.itunes.ItunesAdapter) AndroidSchedulers(rx.android.schedulers.AndroidSchedulers) Intent(android.content.Intent) MenuItem(android.view.MenuItem) ArrayList(java.util.ArrayList) Observable(rx.Observable) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) MenuItemUtils(de.danoeh.antennapod.menuhandler.MenuItemUtils) MenuInflater(android.view.MenuInflater) Locale(java.util.Locale) Menu(android.view.Menu) Schedulers(rx.schedulers.Schedulers) View(android.view.View) Button(android.widget.Button) Response(okhttp3.Response) AntennapodHttpClient(de.danoeh.antennapod.core.service.download.AntennapodHttpClient) Log(android.util.Log) Request(okhttp3.Request) LayoutInflater(android.view.LayoutInflater) R(de.danoeh.antennapod.R) MenuItemCompat(android.support.v4.view.MenuItemCompat) Fragment(android.support.v4.app.Fragment) ClientConfig(de.danoeh.antennapod.core.ClientConfig) IOException(java.io.IOException) ViewGroup(android.view.ViewGroup) URLEncoder(java.net.URLEncoder) List(java.util.List) TextView(android.widget.TextView) OkHttpClient(okhttp3.OkHttpClient) Podcast(de.danoeh.antennapod.adapter.itunes.ItunesAdapter.Podcast) OnlineFeedViewActivity(de.danoeh.antennapod.activity.OnlineFeedViewActivity) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Subscription(rx.Subscription) JSONArray(org.json.JSONArray) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) JSONArray(org.json.JSONArray) Podcast(de.danoeh.antennapod.adapter.itunes.ItunesAdapter.Podcast) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Observable(rx.Observable) Response(okhttp3.Response) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with Podcast

use of de.danoeh.antennapod.adapter.itunes.ItunesAdapter.Podcast in project AntennaPod by AntennaPod.

the class FyydSearchFragment method processSearchResult.

void processSearchResult(FyydResponse response) {
    adapter.clear();
    if (!response.getData().isEmpty()) {
        adapter.clear();
        searchResults = new ArrayList<>();
        for (SearchHit searchHit : response.getData().values()) {
            Podcast podcast = Podcast.fromSearch(searchHit);
            searchResults.add(podcast);
        }
    } else {
        searchResults = emptyList();
    }
    for (Podcast podcast : searchResults) {
        adapter.add(podcast);
    }
    adapter.notifyDataSetInvalidated();
    gridView.setVisibility(!searchResults.isEmpty() ? View.VISIBLE : View.GONE);
    txtvEmpty.setVisibility(searchResults.isEmpty() ? View.VISIBLE : View.GONE);
}
Also used : SearchHit(de.mfietz.fyydlin.SearchHit) Podcast(de.danoeh.antennapod.adapter.itunes.ItunesAdapter.Podcast)

Example 4 with Podcast

use of de.danoeh.antennapod.adapter.itunes.ItunesAdapter.Podcast in project AntennaPod by AntennaPod.

the class ItunesSearchFragment method updateData.

/**
     * Replace adapter data with provided search results from SearchTask.
     * @param result List of Podcast objects containing search results
     */
void updateData(List<Podcast> result) {
    this.searchResults = result;
    adapter.clear();
    if (result != null && result.size() > 0) {
        gridView.setVisibility(View.VISIBLE);
        txtvEmpty.setVisibility(View.GONE);
        for (Podcast p : result) {
            adapter.add(p);
        }
        adapter.notifyDataSetInvalidated();
    } else {
        gridView.setVisibility(View.GONE);
        txtvEmpty.setVisibility(View.VISIBLE);
    }
}
Also used : Podcast(de.danoeh.antennapod.adapter.itunes.ItunesAdapter.Podcast)

Example 5 with Podcast

use of de.danoeh.antennapod.adapter.itunes.ItunesAdapter.Podcast in project AntennaPod by AntennaPod.

the class ItunesSearchFragment method loadToplist.

private void loadToplist() {
    if (subscription != null) {
        subscription.unsubscribe();
    }
    gridView.setVisibility(View.GONE);
    txtvError.setVisibility(View.GONE);
    butRetry.setVisibility(View.GONE);
    txtvEmpty.setVisibility(View.GONE);
    progressBar.setVisibility(View.VISIBLE);
    subscription = Observable.create((Observable.OnSubscribe<List<Podcast>>) subscriber -> {
        String lang = Locale.getDefault().getLanguage();
        String url = "https://itunes.apple.com/" + lang + "/rss/toppodcasts/limit=25/explicit=true/json";
        OkHttpClient client = AntennapodHttpClient.getHttpClient();
        Request.Builder httpReq = new Request.Builder().url(url).header("User-Agent", ClientConfig.USER_AGENT);
        List<Podcast> results = new ArrayList<>();
        try {
            Response response = client.newCall(httpReq.build()).execute();
            if (!response.isSuccessful()) {
                url = "https://itunes.apple.com/us/rss/toppodcasts/limit=25/explicit=true/json";
                httpReq = new Request.Builder().url(url).header("User-Agent", ClientConfig.USER_AGENT);
                response = client.newCall(httpReq.build()).execute();
            }
            if (response.isSuccessful()) {
                String resultString = response.body().string();
                JSONObject result = new JSONObject(resultString);
                JSONObject feed = result.getJSONObject("feed");
                JSONArray entries = feed.getJSONArray("entry");
                for (int i = 0; i < entries.length(); i++) {
                    JSONObject json = entries.getJSONObject(i);
                    Podcast podcast = Podcast.fromToplist(json);
                    results.add(podcast);
                }
            } else {
                String prefix = getString(R.string.error_msg_prefix);
                subscriber.onError(new IOException(prefix + response));
            }
        } catch (IOException | JSONException e) {
            subscriber.onError(e);
        }
        subscriber.onNext(results);
        subscriber.onCompleted();
    }).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(podcasts -> {
        progressBar.setVisibility(View.GONE);
        topList = podcasts;
        updateData(topList);
    }, error -> {
        Log.e(TAG, Log.getStackTraceString(error));
        progressBar.setVisibility(View.GONE);
        txtvError.setText(error.toString());
        txtvError.setVisibility(View.VISIBLE);
        butRetry.setOnClickListener(v -> loadToplist());
        butRetry.setVisibility(View.VISIBLE);
    });
}
Also used : Bundle(android.os.Bundle) GridView(android.widget.GridView) ProgressBar(android.widget.ProgressBar) SearchView(android.support.v7.widget.SearchView) ItunesAdapter(de.danoeh.antennapod.adapter.itunes.ItunesAdapter) AndroidSchedulers(rx.android.schedulers.AndroidSchedulers) Intent(android.content.Intent) MenuItem(android.view.MenuItem) ArrayList(java.util.ArrayList) Observable(rx.Observable) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) MenuItemUtils(de.danoeh.antennapod.menuhandler.MenuItemUtils) MenuInflater(android.view.MenuInflater) Locale(java.util.Locale) Menu(android.view.Menu) Schedulers(rx.schedulers.Schedulers) View(android.view.View) Button(android.widget.Button) Response(okhttp3.Response) AntennapodHttpClient(de.danoeh.antennapod.core.service.download.AntennapodHttpClient) Log(android.util.Log) Request(okhttp3.Request) LayoutInflater(android.view.LayoutInflater) R(de.danoeh.antennapod.R) MenuItemCompat(android.support.v4.view.MenuItemCompat) Fragment(android.support.v4.app.Fragment) ClientConfig(de.danoeh.antennapod.core.ClientConfig) IOException(java.io.IOException) ViewGroup(android.view.ViewGroup) URLEncoder(java.net.URLEncoder) List(java.util.List) TextView(android.widget.TextView) OkHttpClient(okhttp3.OkHttpClient) Podcast(de.danoeh.antennapod.adapter.itunes.ItunesAdapter.Podcast) OnlineFeedViewActivity(de.danoeh.antennapod.activity.OnlineFeedViewActivity) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Subscription(rx.Subscription) JSONArray(org.json.JSONArray) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) JSONArray(org.json.JSONArray) Podcast(de.danoeh.antennapod.adapter.itunes.ItunesAdapter.Podcast) IOException(java.io.IOException) Observable(rx.Observable) Response(okhttp3.Response) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Podcast (de.danoeh.antennapod.adapter.itunes.ItunesAdapter.Podcast)6 Intent (android.content.Intent)4 SearchView (android.support.v7.widget.SearchView)4 View (android.view.View)4 GridView (android.widget.GridView)4 TextView (android.widget.TextView)4 ItunesAdapter (de.danoeh.antennapod.adapter.itunes.ItunesAdapter)4 ArrayList (java.util.ArrayList)4 Bundle (android.os.Bundle)3 Fragment (android.support.v4.app.Fragment)3 MenuItemCompat (android.support.v4.view.MenuItemCompat)3 Log (android.util.Log)3 LayoutInflater (android.view.LayoutInflater)3 Menu (android.view.Menu)3 MenuInflater (android.view.MenuInflater)3 MenuItem (android.view.MenuItem)3 ViewGroup (android.view.ViewGroup)3 Button (android.widget.Button)3 ProgressBar (android.widget.ProgressBar)3 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)3