mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Add Campaign Activity, Fragment and Fetcher
Exposed as a separate launcher icon. Will add more cohesive UI in the future Change-Id: I9f15a9ac850d636d72b4892f2b5153f7e2b8ba28
This commit is contained in:
parent
0be6e87d5e
commit
27f4fe1481
8 changed files with 213 additions and 0 deletions
|
|
@ -82,6 +82,15 @@
|
|||
android:label="@string/title_activity_settings"
|
||||
/>
|
||||
<activity android:name=".AboutActivity" android:label="@string/title_activity_about"/>
|
||||
<activity android:name=".campaigns.CampaignActivity" android:label="Campaigns"
|
||||
android:icon="@drawable/ic_launcher"
|
||||
>
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN"/>
|
||||
<category android:name="android.intent.category.LAUNCHER"/>
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<service android:name=".upload.UploadService" >
|
||||
</service>
|
||||
<service
|
||||
|
|
|
|||
14
commons/res/layout/activity_campaigns.xml
Normal file
14
commons/res/layout/activity_campaigns.xml
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<fragment
|
||||
android:id="@+id/campaignsListFragment"
|
||||
class="org.wikimedia.commons.campaigns.CampaignsListFragment"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
/>
|
||||
</FrameLayout>
|
||||
18
commons/res/layout/fragment_campaigns.xml
Normal file
18
commons/res/layout/fragment_campaigns.xml
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<GridView android:id="@+id/campaignsList"
|
||||
android:layout_height="fill_parent"
|
||||
android:layout_width="fill_parent"
|
||||
android:stretchMode="columnWidth"
|
||||
android:columnWidth="240dp"
|
||||
android:numColumns="auto_fit"
|
||||
android:listSelector="@null"
|
||||
android:fadingEdge="none"
|
||||
android:fastScrollEnabled="false"
|
||||
/>
|
||||
</LinearLayout>
|
||||
12
commons/res/layout/layout_campaign_item.xml
Normal file
12
commons/res/layout/layout_campaign_item.xml
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
<TextView
|
||||
android:id="@+id/campaignItemName"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_width="match_parent"
|
||||
/>
|
||||
</LinearLayout>
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
package org.wikimedia.commons.campaigns;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import com.actionbarsherlock.app.SherlockFragmentActivity;
|
||||
import org.wikimedia.commons.R;
|
||||
|
||||
public class CampaignActivity extends SherlockFragmentActivity {
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_campaigns);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
package org.wikimedia.commons.campaigns;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.view.*;
|
||||
import android.widget.*;
|
||||
import org.wikimedia.commons.R;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class CampaignsListAdapter extends BaseAdapter {
|
||||
private ArrayList<Campaign> campaigns;
|
||||
private Activity activity;
|
||||
|
||||
public CampaignsListAdapter(Activity activity, ArrayList<Campaign> campaigns) {
|
||||
this.campaigns = campaigns;
|
||||
this.activity = activity;
|
||||
}
|
||||
|
||||
public ArrayList<Campaign> getCampaigns() {
|
||||
return campaigns;
|
||||
}
|
||||
|
||||
public void setCampaigns(ArrayList<Campaign> campaigns) {
|
||||
this.campaigns = campaigns;
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
if(campaigns == null) {
|
||||
return 0;
|
||||
}
|
||||
return campaigns.size();
|
||||
}
|
||||
|
||||
public Object getItem(int i) {
|
||||
return campaigns.get(i);
|
||||
}
|
||||
|
||||
public long getItemId(int i) {
|
||||
return i;
|
||||
}
|
||||
|
||||
public View getView(int i, View view, ViewGroup viewGroup) {
|
||||
if(view == null) {
|
||||
view = activity.getLayoutInflater().inflate(R.layout.layout_campaign_item, null);
|
||||
}
|
||||
|
||||
TextView campaignName = (TextView)view.findViewById(R.id.campaignItemName);
|
||||
|
||||
Campaign campaign = campaigns.get(i);
|
||||
|
||||
campaignName.setText(campaign.getName());
|
||||
return view;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
package org.wikimedia.commons.campaigns;
|
||||
|
||||
import android.os.*;
|
||||
import android.view.*;
|
||||
import android.widget.*;
|
||||
import com.actionbarsherlock.app.SherlockFragment;
|
||||
import org.wikimedia.commons.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class CampaignsListFragment extends SherlockFragment {
|
||||
private ArrayList<Campaign> campaigns;
|
||||
private CampaignsListAdapter listAdapter;
|
||||
private GridView campaignsListView;
|
||||
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.layout.fragment_campaigns, container, false);
|
||||
campaignsListView = (GridView)view.findViewById(R.id.campaignsList);
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
listAdapter = new CampaignsListAdapter(getActivity(), campaigns);
|
||||
campaignsListView.setAdapter(listAdapter);
|
||||
Utils.executeAsyncTask(new FetchCampaignsTask(getActivity()){
|
||||
@Override
|
||||
protected void onPostExecute(ArrayList<Campaign> campaigns) {
|
||||
super.onPostExecute(campaigns);
|
||||
CampaignsListFragment.this.campaigns = campaigns;
|
||||
listAdapter.setCampaigns(campaigns);
|
||||
listAdapter.notifyDataSetChanged();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
package org.wikimedia.commons.campaigns;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.AsyncTask;
|
||||
import android.util.Log;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.mediawiki.api.ApiResult;
|
||||
import org.mediawiki.api.MWApi;
|
||||
import org.wikimedia.commons.CommonsApplication;
|
||||
import org.wikimedia.commons.Utils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class FetchCampaignsTask extends AsyncTask<Void, Void, ArrayList<Campaign>>{
|
||||
|
||||
private Context context;
|
||||
private MWApi api;
|
||||
|
||||
public FetchCampaignsTask(Context context) {
|
||||
this.context = context;
|
||||
this.api = ((CommonsApplication)context.getApplicationContext()).getApi();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ArrayList<Campaign> doInBackground(Void... voids) {
|
||||
ArrayList<Campaign> campaigns = new ArrayList<Campaign>();
|
||||
ApiResult result;
|
||||
try {
|
||||
result = api.action("query").param("prop", "revisions")
|
||||
.param("rvprop", "content").param("generator", "allpages")
|
||||
.param("gapnamespace", 460).param("gaplimit", 500).get(); //FIXME: Actually paginate!
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e); // FIXME: Do something else ;)
|
||||
}
|
||||
ArrayList<ApiResult> pageNodes = result.getNodes("/api/query/pages/page");
|
||||
for(ApiResult pageNode : pageNodes) {
|
||||
Log.d("Commons", Utils.getStringFromDOM(pageNode.getDocument()));
|
||||
String configString = pageNode.getString("revisions/rev");
|
||||
String pageName = pageNode.getString("@title").split(":")[1];
|
||||
JSONObject config;
|
||||
try {
|
||||
config = new JSONObject(configString);
|
||||
} catch (JSONException e) {
|
||||
throw new RuntimeException(e); // NEVER HAPPENS!
|
||||
}
|
||||
campaigns.add(new Campaign(pageName, config));
|
||||
}
|
||||
return campaigns;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue