Skip to content
Browse files

Add ability to star/unstar repositories

Closes #236
  • Loading branch information...
1 parent 7541c66 commit 29f98f004ec957e3baed6462fd5b4bfc0bae8e9f @atermenji atermenji committed with kevinsawicki
View
23 app/res/menu/repository_star.xml
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ Copyright 2012 GitHub Inc.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<menu xmlns:android="http://schemas.android.com/apk/res/android" >
+
+ <item
+ android:id="@+id/m_star"
+ android:showAsAction="never" />
+
+</menu>
View
7 app/res/values/strings.xml
@@ -42,6 +42,9 @@
<string name="error_following_person">Following failed</string>
<string name="error_unfollowing_person">Unfollowing failed</string>
<string name="error_checking_following_status">Checking following status failed</string>
+ <string name="error_starring_repository">Starring failed</string>
+ <string name="error_unstarring_repository">Unstarring failed</string>
+ <string name="error_checking_starring_status">Checking starring status failed</string>
<!-- -->
@@ -180,6 +183,8 @@
<string name="following_self">Following</string>
<string name="follow">Follow</string>
<string name="unfollow">Unfollow</string>
+ <string name="star">Star</string>
+ <string name="unstar">Unstar</string>
<string name="members">Members</string>
<string name="closing_issue">Closing Issue…</string>
<string name="reopening_issue">Reopening Issue…</string>
@@ -227,6 +232,8 @@
<string name="code">Code</string>
<string name="following_user">Following…</string>
<string name="unfollowing_user">Unfollowing…</string>
+ <string name="starring_repository">Starring…</string>
+ <string name="unstarring_repository">Unstarring…</string>
<!-- Tab titles, should be as short as possible -->
<string name="tab_repositories">repositories</string>
View
77 app/src/main/java/com/github/mobile/core/repo/StarRepositoryTask.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2012 GitHub Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.github.mobile.core.repo;
+
+import org.eclipse.egit.github.core.IRepositoryIdProvider;
+import org.eclipse.egit.github.core.service.WatcherService;
+
+import android.accounts.Account;
+import android.content.Context;
+import android.util.Log;
+
+import com.github.mobile.R.string;
+import com.github.mobile.ui.ProgressDialogTask;
+import com.google.inject.Inject;
+
+/**
+ * Task to star a repository
+ */
+public class StarRepositoryTask extends ProgressDialogTask<Void> {
+
+ private static final String TAG = "StarRepositoryTask";
+
+ @Inject
+ private WatcherService service;
+
+ private final IRepositoryIdProvider repo;
+
+ /**
+ * Create task for context and id provider
+ *
+ * @param context
+ * @param repo
+ */
+ public StarRepositoryTask(Context context, IRepositoryIdProvider repo) {
+ super(context);
+
+ this.repo = repo;
+ }
+
+ /**
+ * Execute the task with a progress dialog displaying.
+ * <p>
+ * This method must be called from the main thread.
+ */
+ public void start() {
+ showIndeterminate(string.starring_repository);
+
+ execute();
+ }
+
+ @Override
+ protected Void run(Account account) throws Exception {
+ service.watch(repo);
+
+ return null;
+ }
+
+ @Override
+ protected void onException(Exception e) throws RuntimeException {
+ super.onException(e);
+
+ Log.d(TAG, "Exception starring repository", e);
+ }
+}
View
64 app/src/main/java/com/github/mobile/core/repo/StarringRepositoryTask.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2012 GitHub Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.github.mobile.core.repo;
+
+import org.eclipse.egit.github.core.IRepositoryIdProvider;
+import org.eclipse.egit.github.core.service.WatcherService;
+
+import android.accounts.Account;
+import android.content.Context;
+import android.util.Log;
+
+import com.github.mobile.accounts.AuthenticatedUserTask;
+import com.google.inject.Inject;
+
+/**
+ * Task to check repository starring status
+ */
+public class StarringRepositoryTask extends AuthenticatedUserTask<Boolean> {
+
+ private static final String TAG = "StarringRepositoryTask";
+
+ @Inject
+ private WatcherService service;
+
+ private final IRepositoryIdProvider repo;
+
+ /**
+ * Create task for context and id provider
+ *
+ * @param context
+ * @param repo
+ */
+ public StarringRepositoryTask(Context context, IRepositoryIdProvider repo) {
+ super(context);
+
+ this.repo = repo;
+ }
+
+ @Override
+ protected Boolean run(Account account) throws Exception {
+
+ return service.isWatching(repo);
+ }
+
+ @Override
+ protected void onException(Exception e) throws RuntimeException {
+ super.onException(e);
+
+ Log.d(TAG, "Exception checking starring repository status", e);
+ }
+}
View
77 app/src/main/java/com/github/mobile/core/repo/UnstarRepositoryTask.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2012 GitHub Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.github.mobile.core.repo;
+
+import org.eclipse.egit.github.core.IRepositoryIdProvider;
+import org.eclipse.egit.github.core.service.WatcherService;
+
+import android.accounts.Account;
+import android.content.Context;
+import android.util.Log;
+
+import com.github.mobile.R.string;
+import com.github.mobile.ui.ProgressDialogTask;
+import com.google.inject.Inject;
+
+/**
+ * Task to unstar a repository
+ */
+public class UnstarRepositoryTask extends ProgressDialogTask<Void> {
+
+ private static final String TAG = "UnstarRepositoryTask";
+
+ @Inject
+ private WatcherService service;
+
+ private final IRepositoryIdProvider repo;
+
+ /**
+ * Create task for context and id provider
+ *
+ * @param context
+ * @param repo
+ */
+ public UnstarRepositoryTask(Context context, IRepositoryIdProvider repo) {
+ super(context);
+
+ this.repo = repo;
+ }
+
+ /**
+ * Execute the task with a progress dialog displaying.
+ * <p>
+ * This method must be called from the main thread.
+ */
+ public void start() {
+ showIndeterminate(string.unstarring_repository);
+
+ execute();
+ }
+
+ @Override
+ protected Void run(Account account) throws Exception {
+ service.unwatch(repo);
+
+ return null;
+ }
+
+ @Override
+ protected void onException(Exception e) throws RuntimeException {
+ super.onException(e);
+
+ Log.d(TAG, "Exception unstarring repository", e);
+ }
+}
View
98 app/src/main/java/com/github/mobile/ui/repo/RepositoryViewActivity.java
@@ -23,31 +23,36 @@
import static com.github.mobile.util.TypefaceUtils.ICON_COMMIT;
import static com.github.mobile.util.TypefaceUtils.ICON_ISSUE_OPEN;
import static com.github.mobile.util.TypefaceUtils.ICON_NEWS;
+
+import org.eclipse.egit.github.core.Repository;
+import org.eclipse.egit.github.core.User;
+
+import roboguice.inject.InjectExtra;
+import roboguice.inject.InjectView;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ProgressBar;
import com.actionbarsherlock.app.ActionBar;
+import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.github.kevinsawicki.wishlist.ViewUtils;
import com.github.mobile.Intents.Builder;
import com.github.mobile.R.id;
import com.github.mobile.R.layout;
+import com.github.mobile.R.menu;
import com.github.mobile.R.string;
import com.github.mobile.core.repo.RefreshRepositoryTask;
import com.github.mobile.core.repo.RepositoryUtils;
+import com.github.mobile.core.repo.UnstarRepositoryTask;
+import com.github.mobile.core.repo.StarRepositoryTask;
+import com.github.mobile.core.repo.StarringRepositoryTask;
import com.github.mobile.ui.TabPagerActivity;
import com.github.mobile.ui.user.HomeActivity;
import com.github.mobile.util.AvatarLoader;
import com.github.mobile.util.ToastUtils;
import com.google.inject.Inject;
-import org.eclipse.egit.github.core.Repository;
-import org.eclipse.egit.github.core.User;
-
-import roboguice.inject.InjectExtra;
-import roboguice.inject.InjectView;
-
/**
* Activity to view a repository
*/
@@ -73,6 +78,10 @@ public static Intent createIntent(Repository repository) {
@InjectView(id.pb_loading)
private ProgressBar loadingBar;
+ private boolean isStarring;
+
+ private boolean starringStatusChecked;
+
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -115,6 +124,23 @@ protected void onException(Exception e) throws RuntimeException {
}
@Override
+ public boolean onCreateOptionsMenu(Menu optionsMenu) {
+ getSupportMenuInflater().inflate(menu.repository_star, optionsMenu);
+
+ return super.onCreateOptionsMenu(optionsMenu);
+ }
+
+ @Override
+ public boolean onPrepareOptionsMenu(Menu menu) {
+ MenuItem followItem = menu.findItem(id.m_star);
+
+ followItem.setVisible(starringStatusChecked);
+ followItem.setTitle(isStarring ? string.unstar : string.star);
+
+ return super.onPrepareOptionsMenu(menu);
+ }
+
+ @Override
public boolean onSearchRequested() {
if (pager.getCurrentItem() == 1) {
Bundle args = new Bundle();
@@ -137,11 +163,15 @@ private void configurePager() {
configureTabPager();
ViewUtils.setGone(loadingBar, true);
setGone(false);
+ checkStarringRepositoryStatus();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
+ case id.m_star:
+ starRepository();
+ return true;
case android.R.id.home:
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_SINGLE_TOP);
@@ -183,4 +213,58 @@ protected String getIcon(int position) {
return super.getIcon(position);
}
}
-}
+
+ private void starRepository() {
+ if (isStarring)
+ new UnstarRepositoryTask(this, repository) {
+
+ @Override
+ protected void onSuccess(Void v) throws Exception {
+ super.onSuccess(v);
+
+ isStarring = !isStarring;
+ }
+
+ @Override
+ protected void onException(Exception e) throws RuntimeException {
+ super.onException(e);
+
+ ToastUtils.show(RepositoryViewActivity.this,
+ string.error_unstarring_repository);
+ }
+ }.start();
+ else
+ new StarRepositoryTask(this, repository) {
+
+ @Override
+ protected void onSuccess(Void v) throws Exception {
+ super.onSuccess(v);
+
+ isStarring = !isStarring;
+ }
+
+ @Override
+ protected void onException(Exception e) throws RuntimeException {
+ super.onException(e);
+
+ ToastUtils.show(RepositoryViewActivity.this,
+ string.error_starring_repository);
+ }
+ }.start();
+ }
+
+ private void checkStarringRepositoryStatus() {
+ starringStatusChecked = false;
+ new StarringRepositoryTask(this, repository) {
+
+ @Override
+ protected void onSuccess(Boolean watching) throws Exception {
+ super.onSuccess(watching);
+
+ isStarring = watching;
+ starringStatusChecked = true;
+ invalidateOptionsMenu();
+ }
+ }.execute();
+ }
+}

0 comments on commit 29f98f0

Please sign in to comment.
Something went wrong with that request. Please try again.