Skip to content

Commit

Permalink
create my channel
Browse files Browse the repository at this point in the history
  • Loading branch information
brussee committed Aug 7, 2016
1 parent 6ce918f commit cc40c14
Show file tree
Hide file tree
Showing 12 changed files with 511 additions and 69 deletions.
5 changes: 5 additions & 0 deletions android/TriblerApp/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,16 @@
</activity>
<activity
android:name=".BeamActivity"
android:label="@string/action_send_app"
android:launchMode="singleTop"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".ChannelActivity"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".EditChannelActivity"
android:label="@string/action_my_channel"
android:theme="@style/AppTheme.NoActionBar" />
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

public class ChannelActivity extends BaseActivity {

public static final String ACTION_TOGGLE_SUBSCRIBED = "org.tribler.android.TOGGLE_SUBSCRIBED";
public static final String EXTRA_SUBSCRIBED = "org.tribler.android.SUBSCRIBED";
public static final String EXTRA_DISPERSY_CID = "org.tribler.android.dispersy.CID";
public static final String ACTION_TOGGLE_SUBSCRIBED = "org.tribler.android.channel.TOGGLE_SUBSCRIBED";

public static final String EXTRA_DISPERSY_CID = "org.tribler.android.channel.dispersy.CID";
public static final String EXTRA_NAME = "org.tribler.android.channel.NAME";
public static final String EXTRA_DESCRIPTION = "org.tribler.android.channel.DESCRIPTION";
public static final String EXTRA_SUBSCRIBED = "org.tribler.android.channel.SUBSCRIBED";

private ChannelFragment _fragment;

Expand Down Expand Up @@ -106,9 +109,9 @@ protected void handleIntent(Intent intent) {
if (TextUtils.isEmpty(action)) {
return;
}
String title = intent.getStringExtra(Intent.EXTRA_TITLE);
String dispersyCid = intent.getStringExtra(ChannelActivity.EXTRA_DISPERSY_CID);
boolean subscribed = intent.getBooleanExtra(ChannelActivity.EXTRA_SUBSCRIBED, false);
String dispersyCid = intent.getStringExtra(EXTRA_DISPERSY_CID);
String name = intent.getStringExtra(EXTRA_NAME);
boolean subscribed = intent.getBooleanExtra(EXTRA_SUBSCRIBED, false);

switch (action) {

Expand All @@ -117,20 +120,20 @@ protected void handleIntent(Intent intent) {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
if (!subscribed) {
title = getString(R.string.title_channel_preview) + ": " + title;
name = getString(R.string.title_channel_preview) + ": " + name;
}
actionBar.setTitle(title);
actionBar.setTitle(name);
}
return;

case ACTION_TOGGLE_SUBSCRIBED:
if (subscribed) {
_fragment.unsubscribe(dispersyCid, subscribed, title);
_fragment.unsubscribe(dispersyCid, subscribed, name);
} else {
_fragment.subscribe(dispersyCid, subscribed, title);
_fragment.subscribe(dispersyCid, subscribed, name);
}
// Update view
intent.putExtra(ChannelActivity.EXTRA_SUBSCRIBED, !subscribed);
intent.putExtra(EXTRA_SUBSCRIBED, !subscribed);
invalidateOptionsMenu();

// Flag modification
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public void onDetach() {
public void onClick(final TriblerChannel channel) {
Intent intent = new Intent(_context, ChannelActivity.class);
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_TITLE, channel.getName());
intent.putExtra(ChannelActivity.EXTRA_DISPERSY_CID, channel.getDispersyCid());
intent.putExtra(ChannelActivity.EXTRA_NAME, channel.getName());
intent.putExtra(ChannelActivity.EXTRA_SUBSCRIBED, channel.isSubscribed());
startActivityForResult(intent, CHANNEL_ACTIVITY_REQUEST_CODE);
}
Expand All @@ -84,18 +84,25 @@ public void onClick(final TriblerChannel channel) {
*/
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CHANNEL_ACTIVITY_REQUEST_CODE) {
if (resultCode == Activity.RESULT_FIRST_USER) {
// Update the subscription status of the channel identified by dispersy_cid
String dispersyCid = data.getStringExtra(ChannelActivity.EXTRA_DISPERSY_CID);
boolean subscribed = data.getBooleanExtra(ChannelActivity.EXTRA_SUBSCRIBED, false);
TriblerChannel channel = adapter.findByDispersyCid(dispersyCid);
if (channel != null) {
channel.setSubscribed(subscribed);
// Update view
adapter.notifyObjectChanged(channel);
switch (requestCode) {

case CHANNEL_ACTIVITY_REQUEST_CODE:
switch (resultCode) {

case Activity.RESULT_FIRST_USER:
// Update the subscription status of the channel identified by dispersy_cid
String dispersyCid = data.getStringExtra(ChannelActivity.EXTRA_DISPERSY_CID);
boolean subscribed = data.getBooleanExtra(ChannelActivity.EXTRA_SUBSCRIBED, false);

TriblerChannel channel = adapter.findByDispersyCid(dispersyCid);
if (channel != null) {
channel.setSubscribed(subscribed);
// Update view
adapter.notifyObjectChanged(channel);
}
break;
}
}
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package org.tribler.android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import org.tribler.android.restapi.IRestApi;
import org.tribler.android.restapi.TriblerService;
import org.tribler.android.restapi.json.AddedChannelAck;
import org.tribler.android.restapi.json.ModifiedAck;

import butterknife.BindView;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class EditChannelActivity extends BaseActivity {

public static final String ACTION_CREATE_CHANNEL = "org.tribler.android.channel.CREATE";

private IRestApi _service;

@BindView(R.id.channel_icon_wrapper)
View iconWrapper;

@BindView(R.id.channel_icon)
ImageView icon;

@BindView(R.id.channel_capital)
TextView nameCapital;

@BindView(R.id.my_channel_explanation)
TextView explanation;

@BindView(R.id.channel_name_input)
EditText nameInput;

@BindView(R.id.channel_description_input)
EditText descriptionInput;

@BindView(R.id.btn_channel_save)
Button btnSave;

@BindView(R.id.channel_progress)
View progressView;

@BindView(R.id.channel_progress_status)
TextView statusBar;

/**
* {@inheritDoc}
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_channel);

String baseUrl = getString(R.string.service_url) + ":" + getString(R.string.service_port_number);
String authToken = getString(R.string.service_auth_token);
_service = TriblerService.createService(baseUrl, authToken);

handleIntent(getIntent());
}

@Override
protected void onDestroy() {
super.onDestroy();
_service = null;
}

/**
* {@inheritDoc}
*/
@Override
protected void handleIntent(Intent intent) {
String action = intent.getAction();
if (TextUtils.isEmpty(action)) {
return;
}
switch (action) {

case ACTION_CREATE_CHANNEL:
btnSave.setText(getText(R.string.action_create));
explanation.setVisibility(View.VISIBLE);
return;

case Intent.ACTION_EDIT:
String dispersyCid = intent.getStringExtra(ChannelActivity.EXTRA_DISPERSY_CID);
String name = intent.getStringExtra(ChannelActivity.EXTRA_NAME);
String description = intent.getStringExtra(ChannelActivity.EXTRA_DESCRIPTION);
int color = MyUtils.getColor(dispersyCid.hashCode());
nameCapital.setText(MyUtils.getCapitals(name, 2));
nameInput.setText(name);
descriptionInput.setText(description);
MyUtils.setCicleBackground(icon, color);
iconWrapper.setVisibility(View.VISIBLE);
return;
}
}

public void btnChannelSaveClicked(@Nullable View view) {
// Lock input fields
btnSave.setEnabled(false);
nameInput.setEnabled(false);
descriptionInput.setEnabled(false);

// Show loading indicator
progressView.setVisibility(View.VISIBLE);
btnSave.setVisibility(View.GONE);

String name = nameInput.getText().toString();
String description = descriptionInput.getText().toString();

if (ACTION_CREATE_CHANNEL.equals(getIntent().getAction())) {
statusBar.setText(getText(R.string.status_creating_channel));

rxSubs.add(_service.createChannel(name, description)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<AddedChannelAck>() {

public void onNext(AddedChannelAck ack) {
}

public void onCompleted() {
setResult(Activity.RESULT_OK);
finish();
}

public void onError(Throwable e) {
Log.e("btnChannelSaveClicked", "createChannel", e);
// Retry
btnChannelSaveClicked(null);
}
}));
} else {

rxSubs.add(_service.editMyChannel(name, description)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<ModifiedAck>() {

public void onNext(ModifiedAck ack) {
}

public void onCompleted() {
setResult(Activity.RESULT_OK);
finish();
}

public void onError(Throwable e) {
Log.e("btnChannelSaveClicked", "editMyChannel", e);
// Retry
btnChannelSaveClicked(null);
}
}));
}
}
}
Loading

0 comments on commit cc40c14

Please sign in to comment.