|
| 1 | +package com.lenovo.twitter; |
| 2 | + |
| 3 | +import android.content.DialogInterface; |
| 4 | +import android.content.Intent; |
| 5 | +import android.support.v7.app.AlertDialog; |
| 6 | +import android.support.v7.app.AppCompatActivity; |
| 7 | +import android.os.Bundle; |
| 8 | +import android.util.Log; |
| 9 | +import android.view.Menu; |
| 10 | +import android.view.MenuInflater; |
| 11 | +import android.view.MenuItem; |
| 12 | +import android.view.View; |
| 13 | +import android.widget.AbsListView; |
| 14 | +import android.widget.AdapterView; |
| 15 | +import android.widget.ArrayAdapter; |
| 16 | +import android.widget.CheckedTextView; |
| 17 | +import android.widget.EditText; |
| 18 | +import android.widget.ListView; |
| 19 | +import android.widget.Toast; |
| 20 | + |
| 21 | +import com.parse.FindCallback; |
| 22 | +import com.parse.Parse; |
| 23 | +import com.parse.ParseException; |
| 24 | +import com.parse.ParseObject; |
| 25 | +import com.parse.ParseQuery; |
| 26 | +import com.parse.ParseUser; |
| 27 | +import com.parse.SaveCallback; |
| 28 | + |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +public class Main2Activity extends AppCompatActivity { |
| 33 | + |
| 34 | + ArrayList<String> users = new ArrayList<>(); |
| 35 | + |
| 36 | + ArrayAdapter arrayAdapter; |
| 37 | + |
| 38 | + @Override |
| 39 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 40 | + |
| 41 | + MenuInflater menuInflater = new MenuInflater(this); |
| 42 | + |
| 43 | + menuInflater.inflate(R.menu.tweet_menu, menu); |
| 44 | + |
| 45 | + return super.onCreateOptionsMenu(menu); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 50 | + |
| 51 | + if (item.getItemId() == R.id.tweet) { |
| 52 | + |
| 53 | + AlertDialog.Builder builder = new AlertDialog.Builder(this); |
| 54 | + |
| 55 | + builder.setTitle("Send a tweet"); |
| 56 | + |
| 57 | + final EditText tweetContentEditText = new EditText(this); |
| 58 | + |
| 59 | + builder.setView(tweetContentEditText); |
| 60 | + |
| 61 | + builder.setPositiveButton("Send", new DialogInterface.OnClickListener() { |
| 62 | + @Override |
| 63 | + public void onClick(DialogInterface dialogInterface, int i) { |
| 64 | + |
| 65 | + Log.i("Info", tweetContentEditText.getText().toString()); |
| 66 | + |
| 67 | + ParseObject tweet = new ParseObject("Tweet"); |
| 68 | + |
| 69 | + tweet.put("username", ParseUser.getCurrentUser().getUsername()); |
| 70 | + |
| 71 | + tweet.put("tweet", tweetContentEditText.getText().toString()); |
| 72 | + |
| 73 | + tweet.saveInBackground(new SaveCallback() { |
| 74 | + @Override |
| 75 | + public void done(ParseException e) { |
| 76 | + |
| 77 | + if (e == null) { |
| 78 | + |
| 79 | + Toast.makeText(Main2Activity.this, "Tweet sent succesfully!", Toast.LENGTH_SHORT).show(); |
| 80 | + |
| 81 | + } else { |
| 82 | + |
| 83 | + Toast.makeText(Main2Activity.this, "Tweet failed - please try again later.", Toast.LENGTH_SHORT).show(); |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
| 94 | + @Override |
| 95 | + public void onClick(DialogInterface dialogInterface, int i) { |
| 96 | + |
| 97 | + dialogInterface.cancel(); |
| 98 | + |
| 99 | + } |
| 100 | + }); |
| 101 | + |
| 102 | + builder.show(); |
| 103 | + |
| 104 | + } else if (item.getItemId() == R.id.logout) { |
| 105 | + |
| 106 | + ParseUser.logOut(); |
| 107 | + |
| 108 | + Intent intent = new Intent(getApplicationContext(), MainActivity.class); |
| 109 | + startActivity(intent); |
| 110 | + |
| 111 | + } else if (item.getItemId() == R.id.viewFeed) { |
| 112 | + |
| 113 | + Intent intent = new Intent(getApplicationContext(), Feed.class); |
| 114 | + startActivity(intent); |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + return super.onOptionsItemSelected(item); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + protected void onCreate(Bundle savedInstanceState) { |
| 123 | + super.onCreate(savedInstanceState); |
| 124 | + setContentView(R.layout.activity_main2); |
| 125 | + |
| 126 | + setTitle("User List"); |
| 127 | + |
| 128 | + if (ParseUser.getCurrentUser().get("isFollowing") == null) { |
| 129 | + |
| 130 | + List<String> emptyList = new ArrayList<>(); |
| 131 | + ParseUser.getCurrentUser().put("isFollowing", emptyList); |
| 132 | + |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | + final ListView listView = (ListView) findViewById(R.id.listView); |
| 138 | + |
| 139 | + listView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE); |
| 140 | + |
| 141 | + arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_checked, users); |
| 142 | + |
| 143 | + listView.setAdapter(arrayAdapter); |
| 144 | + |
| 145 | + listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { |
| 146 | + @Override |
| 147 | + public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { |
| 148 | + |
| 149 | + CheckedTextView checkedTextView = (CheckedTextView) view; |
| 150 | + |
| 151 | + if (checkedTextView.isChecked()) { |
| 152 | + |
| 153 | + Log.i("Info", "Row is checked."); |
| 154 | + |
| 155 | + // ParseUser.getCurrentUser().getList("isFollowing").add(users.get(i)); |
| 156 | + |
| 157 | + ParseUser.getCurrentUser().add("isFollowing", users.get(i)); |
| 158 | + |
| 159 | + ParseUser.getCurrentUser().saveInBackground(); |
| 160 | + |
| 161 | + } else { |
| 162 | + |
| 163 | + Log.i("Info", "Row is not checked."); |
| 164 | + |
| 165 | + // ParseUser.getCurrentUser().getList("isFollowing").remove(users.get(i)); |
| 166 | + |
| 167 | + ParseUser.getCurrentUser().getList("isFollowing").remove(users.get(i)); |
| 168 | + List newlist = ParseUser.getCurrentUser().getList("isFollowing"); |
| 169 | + ParseUser.getCurrentUser().remove("isFollowing"); |
| 170 | + ParseUser.getCurrentUser().put("isFollowing", newlist); |
| 171 | + |
| 172 | + ParseUser.getCurrentUser().saveInBackground(); |
| 173 | + |
| 174 | + } |
| 175 | + } |
| 176 | + }); |
| 177 | + |
| 178 | + users.clear(); |
| 179 | + |
| 180 | + ParseQuery<ParseUser> query = ParseUser.getQuery(); |
| 181 | + |
| 182 | + query.whereNotEqualTo("username", ParseUser.getCurrentUser().getUsername()); |
| 183 | + |
| 184 | + query.findInBackground(new FindCallback<ParseUser>() { |
| 185 | + @Override |
| 186 | + public void done(List<ParseUser> objects, ParseException e) { |
| 187 | + |
| 188 | + if (e == null) { |
| 189 | + |
| 190 | + if (objects.size() > 0 ) { |
| 191 | + |
| 192 | + for (ParseUser user : objects) { |
| 193 | + |
| 194 | + users.add(user.getUsername()); |
| 195 | + |
| 196 | + } |
| 197 | + |
| 198 | + arrayAdapter.notifyDataSetChanged(); |
| 199 | + |
| 200 | + for (String username : users) { |
| 201 | + |
| 202 | + if (ParseUser.getCurrentUser().getList("isFollowing").contains(username)) { |
| 203 | + |
| 204 | + listView.setItemChecked(users.indexOf(username), true); |
| 205 | + |
| 206 | + } |
| 207 | + |
| 208 | + } |
| 209 | + |
| 210 | + } |
| 211 | + |
| 212 | + } |
| 213 | + |
| 214 | + |
| 215 | + } |
| 216 | + }); |
| 217 | + |
| 218 | + |
| 219 | + |
| 220 | + } |
| 221 | +} |
| 222 | + |
0 commit comments