Search in sources :

Example 36 with SiteModel

use of org.wordpress.android.fluxc.model.SiteModel in project WordPress-Android by wordpress-mobile.

the class RoleSelectDialogFragment method onCreateDialog.

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    SiteModel site = (SiteModel) getArguments().getSerializable(WordPress.SITE);
    final Role[] roles = Role.inviteRoles(site);
    final String[] stringRoles = new String[roles.length];
    for (int i = 0; i < roles.length; i++) {
        stringRoles[i] = roles[i].toDisplayString();
    }
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.Calypso_AlertDialog);
    builder.setTitle(R.string.role);
    builder.setItems(stringRoles, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (!isAdded()) {
                return;
            }
            if (getTargetFragment() instanceof OnRoleSelectListener) {
                ((OnRoleSelectListener) getTargetFragment()).onRoleSelected(roles[which]);
            } else if (getActivity() instanceof OnRoleSelectListener) {
                ((OnRoleSelectListener) getActivity()).onRoleSelected(roles[which]);
            }
        }
    });
    return builder.create();
}
Also used : AlertDialog(android.app.AlertDialog) DialogInterface(android.content.DialogInterface) SiteModel(org.wordpress.android.fluxc.model.SiteModel) Role(org.wordpress.android.models.Role)

Example 37 with SiteModel

use of org.wordpress.android.fluxc.model.SiteModel in project WordPress-Android by wordpress-mobile.

the class StatsAbstractFragment method refreshStats.

// call an update for the stats shown in the fragment
void refreshStats(int pageNumberRequested, StatsService.StatsEndpointsEnum[] sections) {
    if (!isAdded()) {
        return;
    }
    // if no sections to update is passed to the method, default to fragment
    if (sections == null) {
        sections = sectionsToUpdate();
    }
    SiteModel site = mSiteStore.getSiteByLocalId(getLocalTableBlogID());
    if (site == null) {
        AppLog.w(AppLog.T.STATS, "Current blsiteog is null. This should never happen here.");
        return;
    }
    final long siteId = site.getSiteId();
    // Make sure the blogId is available.
    if (siteId == 0) {
        AppLog.e(AppLog.T.STATS, "remote siteId is 0: " + site.getUrl());
        return;
    }
    // Check credentials for jetpack blogs first
    if (!SiteUtils.isAccessibleViaWPComAPI(site) && !mAccountStore.hasAccessToken()) {
        AppLog.w(AppLog.T.STATS, "Current blog is accessible via .com API without valid .com credentials");
        return;
    }
    // Do not pass the array of StatsEndpointsEnum to the Service. Otherwise we get
    // java.lang.RuntimeException: Unable to start service org.wordpress.android.ui.stats.service.StatsService
    // with Intent { cmp=org.wordpress.android/.ui.stats.service.StatsService (has extras) }: java.lang.ClassCastException:
    // java.lang.Object[] cannot be cast to org.wordpress.android.ui.stats.service.StatsService$StatsEndpointsEnum[]
    // on older devices.
    // We should use Enumset, or array of int. Going for the latter, since we have an array and cannot create an Enumset easily.
    int[] sectionsForTheService = new int[sections.length];
    for (int i = 0; i < sections.length; i++) {
        sectionsForTheService[i] = sections[i].ordinal();
    }
    // start service to get stats
    Intent intent = new Intent(getActivity(), StatsService.class);
    intent.putExtra(StatsService.ARG_BLOG_ID, siteId);
    intent.putExtra(StatsService.ARG_PERIOD, mStatsTimeframe);
    intent.putExtra(StatsService.ARG_DATE, mDate);
    if (isSingleView()) {
        // Single Item screen: request 20 items per page on paged requests. Default to the first 100 items otherwise.
        int maxElementsToRetrieve = pageNumberRequested > 0 ? StatsService.MAX_RESULTS_REQUESTED_PER_PAGE : MAX_RESULTS_REQUESTED;
        intent.putExtra(StatsService.ARG_MAX_RESULTS, maxElementsToRetrieve);
    }
    if (pageNumberRequested > 0) {
        intent.putExtra(StatsService.ARG_PAGE_REQUESTED, pageNumberRequested);
    }
    intent.putExtra(StatsService.ARG_SECTION, sectionsForTheService);
    getActivity().startService(intent);
}
Also used : SiteModel(org.wordpress.android.fluxc.model.SiteModel) Intent(android.content.Intent)

Example 38 with SiteModel

use of org.wordpress.android.fluxc.model.SiteModel in project WordPress-Android by wordpress-mobile.

the class StatsService method updateWidgetsUI.

// Call an updates on the installed widgets if the blog is the primary, the endpoint is Visits
// the timeframe is DAY or INSIGHTS, and the date = TODAY
private void updateWidgetsUI(long siteId, final StatsEndpointsEnum endpointName, StatsTimeframe timeframe, String date, int pageRequested, Serializable responseObjectModel) {
    if (pageRequested != -1) {
        return;
    }
    if (endpointName != StatsEndpointsEnum.VISITS) {
        return;
    }
    if (timeframe != StatsTimeframe.DAY && timeframe != StatsTimeframe.INSIGHTS) {
        return;
    }
    SiteModel site = mSiteStore.getSiteBySiteId(siteId);
    // make sure the data is for the current date
    if (!date.equals(StatsUtils.getCurrentDateTZ(site))) {
        return;
    }
    if (responseObjectModel == null) {
        // TODO What we want to do here?
        return;
    }
    if (!StatsWidgetProvider.isBlogDisplayedInWidget(siteId)) {
        AppLog.d(AppLog.T.STATS, "The blog with remoteID " + siteId + " is NOT displayed in any widget. Stats Service doesn't call an update of the widget.");
        return;
    }
    if (responseObjectModel instanceof VisitsModel) {
        VisitsModel visitsModel = (VisitsModel) responseObjectModel;
        if (visitsModel.getVisits() == null || visitsModel.getVisits().size() == 0) {
            return;
        }
        List<VisitModel> visits = visitsModel.getVisits();
        VisitModel data = visits.get(visits.size() - 1);
        StatsWidgetProvider.updateWidgets(getApplicationContext(), site, data);
    } else if (responseObjectModel instanceof VolleyError) {
        VolleyError error = (VolleyError) responseObjectModel;
        StatsWidgetProvider.updateWidgets(getApplicationContext(), site, mSiteStore, error);
    } else if (responseObjectModel instanceof StatsError) {
        StatsError statsError = (StatsError) responseObjectModel;
        StatsWidgetProvider.updateWidgets(getApplicationContext(), site, mSiteStore, statsError);
    }
}
Also used : VolleyError(com.android.volley.VolleyError) VisitModel(org.wordpress.android.ui.stats.models.VisitModel) SiteModel(org.wordpress.android.fluxc.model.SiteModel) VisitsModel(org.wordpress.android.ui.stats.models.VisitsModel) StatsError(org.wordpress.android.ui.stats.exceptions.StatsError)

Example 39 with SiteModel

use of org.wordpress.android.fluxc.model.SiteModel in project WordPress-Android by wordpress-mobile.

the class StatsWidgetProvider method setupNewWidget.

// This is called by the Widget config activity at the end if the process
static void setupNewWidget(Context context, int widgetID, int localBlogID, SiteStore siteStore) {
    AppLog.d(AppLog.T.STATS, "setupNewWidget called");
    SiteModel site = siteStore.getSiteByLocalId(localBlogID);
    if (site == null) {
        // it's unlikely that blog is null here.
        // This method is called from config activity which has loaded the blog fine.
        showMessage(context, new int[] { widgetID }, context.getString(R.string.stats_widget_error_readd_widget), siteStore);
        AppLog.e(AppLog.T.STATS, "setupNewWidget: No blog found in the db!");
        return;
    }
    // At this point the remote ID cannot be null.
    long remoteBlogID = site.getSiteId();
    // Add the following check just to be safe
    if (remoteBlogID == 0) {
        showMessage(context, new int[] { widgetID }, context.getString(R.string.stats_widget_error_readd_widget), siteStore);
        return;
    }
    AnalyticsUtils.trackWithSiteDetails(AnalyticsTracker.Stat.STATS_WIDGET_ADDED, site);
    AnalyticsTracker.flush();
    // Store the association between the widget ID and the remote blog id into prefs.
    setRemoteBlogIDForWidgetIDs(new int[] { widgetID }, site.getSiteId());
    String currentDate = StatsUtils.getCurrentDateTZ(site);
    // Load cached data if available and show it immediately
    JSONObject cache = getCacheDataForBlog(remoteBlogID, currentDate);
    if (cache != null) {
        showStatsData(context, new int[] { widgetID }, site, cache);
        return;
    }
    if (!NetworkUtils.isNetworkAvailable(context)) {
        showMessage(context, new int[] { widgetID }, context.getString(R.string.no_network_title), siteStore);
    } else {
        showMessage(context, new int[] { widgetID }, context.getString(R.string.stats_widget_loading_data), siteStore);
        enqueueStatsRequestForBlog(context, remoteBlogID, currentDate);
    }
}
Also used : JSONObject(org.json.JSONObject) SiteModel(org.wordpress.android.fluxc.model.SiteModel)

Example 40 with SiteModel

use of org.wordpress.android.fluxc.model.SiteModel in project WordPress-Android by wordpress-mobile.

the class StatsWidgetProvider method refreshWidgets.

private static void refreshWidgets(Context context, int[] appWidgetIds, SiteStore siteStore) {
    // TODO: FluxC: This file must be refactored, we probably want a "WidgetManager" and keep the bare minimum
    // here in the AppWidgetProvider.
    // if (!mAccountStore.isSignedIn()) {
    //     showMessage(context, appWidgetIds, context.getString(R.string.stats_widget_error_no_account));
    //     return;
    // }
    SparseArray<ArrayList<Integer>> blogsToWidgetIDs = new SparseArray<>();
    for (int widgetId : appWidgetIds) {
        int remoteBlogID = getRemoteBlogIDFromWidgetID(widgetId);
        if (remoteBlogID == 0) {
            // This could happen on logout when prefs are erased completely since we cannot remove
            // widgets programmatically from the screen, or during the configuration of new widgets!!!
            AppLog.e(AppLog.T.STATS, "No remote blog ID for widget ID " + widgetId);
            showMessage(context, new int[] { widgetId }, context.getString(R.string.stats_widget_error_readd_widget), siteStore);
            continue;
        }
        ArrayList<Integer> widgetIDs = blogsToWidgetIDs.get(remoteBlogID, new ArrayList<Integer>());
        widgetIDs.add(widgetId);
        blogsToWidgetIDs.append(remoteBlogID, widgetIDs);
    }
    // we now have an optimized data structure for our needs. BlogId -> widgetIDs list
    for (int i = 0; i < blogsToWidgetIDs.size(); i++) {
        int remoteBlogID = blogsToWidgetIDs.keyAt(i);
        // get the object by the key.
        ArrayList<Integer> widgetsList = blogsToWidgetIDs.get(remoteBlogID);
        int[] currentWidgets = ArrayUtils.toPrimitive(widgetsList.toArray(new Integer[widgetsList.size()]));
        SiteModel site = siteStore.getSiteBySiteId(remoteBlogID);
        if (site == null) {
            // No site in the app
            showMessage(context, currentWidgets, context.getString(R.string.stats_widget_error_readd_widget), siteStore);
            continue;
        }
        String currentDate = StatsUtils.getCurrentDateTZ(site);
        // Load cached data if available and show it immediately
        JSONObject cache = getCacheDataForBlog(remoteBlogID, currentDate);
        if (cache != null) {
            showStatsData(context, currentWidgets, site, cache);
        }
        // If network is available always start a refresh, and show prev data or the loading in progress message.
        if (!NetworkUtils.isNetworkAvailable(context)) {
            if (cache == null) {
                showMessage(context, currentWidgets, context.getString(R.string.stats_widget_error_generic), siteStore);
            }
        } else {
            if (cache == null) {
                showMessage(context, currentWidgets, context.getString(R.string.stats_widget_loading_data), siteStore);
            }
            // Make sure to refresh widget data now.
            enqueueStatsRequestForBlog(context, remoteBlogID, currentDate);
        }
    }
}
Also used : SparseArray(android.util.SparseArray) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) SiteModel(org.wordpress.android.fluxc.model.SiteModel)

Aggregations

SiteModel (org.wordpress.android.fluxc.model.SiteModel)40 Intent (android.content.Intent)7 Subscribe (org.greenrobot.eventbus.Subscribe)5 View (android.view.View)4 Bundle (android.os.Bundle)3 TextView (android.widget.TextView)3 VolleyError (com.android.volley.VolleyError)3 ArrayList (java.util.ArrayList)3 JSONObject (org.json.JSONObject)3 AlertDialog (android.app.AlertDialog)2 Context (android.content.Context)2 DialogInterface (android.content.DialogInterface)2 ImageView (android.widget.ImageView)2 ScrollView (android.widget.ScrollView)2 RestRequest (com.wordpress.rest.RestRequest)2 CommentModel (org.wordpress.android.fluxc.model.CommentModel)2 PostModel (org.wordpress.android.fluxc.model.PostModel)2 WPNetworkImageView (org.wordpress.android.widgets.WPNetworkImageView)2 SuppressLint (android.annotation.SuppressLint)1 Activity (android.app.Activity)1