-
-
Save BrightnBubbly/e45ed25e6fa8df9d61159be3eb35f72e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.dialogflowapplication; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import androidx.appcompat.app.AppCompatActivity; | |
import androidx.databinding.DataBindingUtil; | |
import androidx.lifecycle.ViewModelProviders; | |
import com.example.dialogflowapplication.databinding.ActivityMainBinding; | |
import com.getstream.sdk.chat.StreamChat; | |
import com.getstream.sdk.chat.enums.FilterObject; | |
import com.getstream.sdk.chat.rest.User; | |
import com.getstream.sdk.chat.rest.core.Client; | |
import com.getstream.sdk.chat.viewmodel.ChannelListViewModel; | |
import java.util.HashMap; | |
import static com.getstream.sdk.chat.enums.Filters.eq; | |
public class MainActivity extends AppCompatActivity { | |
public static final String EXTRA_CHANNEL_TYPE = "com.example.dialogflowapplication.CHANNEL_TYPE"; | |
public static final String EXTRA_CHANNEL_ID = "com.example.dialogflowapplication.CHANNEL_ID"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
// setup the client using the example API key | |
// normally you would call init in your Application class and not the activity | |
StreamChat.init("your_API_KEY", this.getApplicationContext()); | |
Client client = StreamChat.getInstance(this.getApplication()); | |
HashMap<String, Object> extraData = new HashMap<>(); | |
extraData.put("name", "<your Full Name>"); | |
extraData.put("image", "https://bit.ly/2TIt8NR"); | |
User currentUser = new User("<your_USER_ID>", extraData); | |
// User token is typically provided by your server when the user authenticates | |
client.setUser(currentUser,"your_AUTH_TOKEN_FROM_BACKEND_SERVER"); | |
// we're using data binding in this example | |
ActivityMainBinding binding = | |
DataBindingUtil.setContentView(this, R.layout.activity_main); | |
// Specify the current activity as the lifecycle owner. | |
binding.setLifecycleOwner(this); | |
// most the business logic for chat is handled in the ChannelListViewModel view model | |
ChannelListViewModel viewModel = ViewModelProviders.of(this).get(ChannelListViewModel.class); | |
binding.setViewModel(viewModel); | |
binding.channelList.setViewModel(viewModel, this); | |
// query all channels of type messaging | |
FilterObject filter = eq("type", "messaging"); | |
viewModel.setChannelFilter(filter); | |
// click handlers for clicking a user avatar or channel | |
binding.channelList.setOnChannelClickListener(channel -> { | |
Intent intent = new Intent(MainActivity.this, ChannelActivity.class); | |
intent.putExtra(EXTRA_CHANNEL_TYPE, channel.getType()); | |
intent.putExtra(EXTRA_CHANNEL_ID, channel.getId()); | |
startActivity(intent); | |
}); | |
binding.channelList.setOnUserClickListener(user -> { | |
// open your user profile | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment