Skip to content

Commit

Permalink
Merge pull request #138 from Giphy/task/update-demo-2.1.2
Browse files Browse the repository at this point in the history
- update to SDK 2.1.2
  • Loading branch information
ALexanderLonsky authored Jul 2, 2021
2 parents 1c4f33a + fb10f49 commit 79510f0
Show file tree
Hide file tree
Showing 17 changed files with 662 additions and 169 deletions.
2 changes: 1 addition & 1 deletion Docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The latest release is available on [Maven Central](https://search.maven.org/arti

Add the GIPHY SDK dependency in the module ```build.gradle``` file:
```
implementation 'com.giphy.sdk:ui:2.1.1'
implementation 'com.giphy.sdk:ui:2.1.2'
```

### Basic Setup
Expand Down
10 changes: 9 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ android {
targetSdkVersion 28
versionCode 1
versionName "1.0"
multiDexEnabled true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}


Expand All @@ -36,7 +44,7 @@ dependencies {
implementation "androidx.appcompat:appcompat:1.1.0"
implementation "androidx.constraintlayout:constraintlayout:1.1.3"
implementation "com.google.android.material:material:1.1.0"
implementation "com.giphy.sdk:ui:2.1.1"
implementation "com.giphy.sdk:ui:2.1.2"
implementation "com.squareup.leakcanary:leakcanary-android:1.6.3"
implementation "com.github.savvyapps:ToggleButtonLayout:1.2.0"
implementation "androidx.gridlayout:gridlayout:1.0.0"
Expand Down
69 changes: 69 additions & 0 deletions app/src/main/java/com/giphy/sdk/uidemo/ClipDialogFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.giphy.sdk.uidemo

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.giphy.sdk.core.models.Media
import com.giphy.sdk.ui.views.GPHVideoPlayer
import kotlinx.android.synthetic.main.fragment_video_player.*

class ClipDialogFragment : androidx.fragment.app.DialogFragment() {

private var media: Media? = null
private var videoPlayer: GPHVideoPlayer? = null

companion object {
private val KEY_VIDEO_PLAYER = "key_video_player"
fun newInstance(media: Media): ClipDialogFragment {
val fragment = ClipDialogFragment()
val bundle = Bundle()
bundle.putParcelable(KEY_VIDEO_PLAYER, media)
fragment.arguments = bundle
return fragment
}
}

override fun getTheme(): Int {
return R.style.ClipsDialogStyle
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

media = arguments!!.getParcelable(KEY_VIDEO_PLAYER)
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return inflater.inflate(R.layout.fragment_video_player, container, false)
}

override fun onDestroyView() {
super.onDestroyView()
videoPlayer?.onDestroy()
}

override fun onPause() {
super.onPause()
videoPlayer?.onPause()
}

override fun onResume() {
super.onResume()
videoPlayer?.onResume()
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
media?.let { media ->
gphVideoPlayerView.preloadFirstFrame(media)
videoPlayer?.onDestroy()
videoPlayer = GPHVideoPlayer(gphVideoPlayerView, true)
videoPlayer?.loadMedia(media)
}
}
}
96 changes: 85 additions & 11 deletions app/src/main/java/com/giphy/sdk/uidemo/DemoActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@ import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import com.giphy.sdk.core.models.Media
import com.giphy.sdk.tracking.isVideo
import com.giphy.sdk.ui.GPHContentType
import com.giphy.sdk.ui.GPHSettings
import com.giphy.sdk.ui.Giphy
import com.giphy.sdk.ui.themes.GPHTheme
import com.giphy.sdk.ui.themes.GridType
import com.giphy.sdk.ui.views.GPHVideoPlayer
import com.giphy.sdk.ui.views.GPHVideoPlayerState
import com.giphy.sdk.ui.views.GiphyDialogFragment
import com.giphy.sdk.uidemo.feed.Author
import com.giphy.sdk.uidemo.feed.FeedDataItem
import com.giphy.sdk.uidemo.feed.GifItem
import com.giphy.sdk.uidemo.feed.InvalidKeyItem
import com.giphy.sdk.uidemo.feed.MessageFeedAdapter
import com.giphy.sdk.uidemo.feed.MessageItem
import com.giphy.sdk.uidemo.feed.*
import kotlinx.android.synthetic.main.activity_demo.*
import timber.log.Timber

Expand All @@ -35,10 +33,14 @@ class DemoActivity : AppCompatActivity() {
var settings = GPHSettings(gridType = GridType.waterfall, theme = GPHTheme.Light, stickerColumnCount = 3)
var feedAdapter: MessageFeedAdapter? = null
var messageItems = ArrayList<FeedDataItem>()
var contentType = GPHContentType.gif

//TODO: Set a valid API KEY
val YOUR_API_KEY = INVALID_KEY

val player: GPHVideoPlayer = createVideoPlayer()
private var clipsPlaybackSetting = SettingsDialogFragment.ClipsPlaybackSetting.inline

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand All @@ -49,24 +51,48 @@ class DemoActivity : AppCompatActivity() {
setupFeed()

launchGiphyBtn.setOnClickListener {
val dialog = GiphyDialogFragment.newInstance(settings)
player.onPause()
val dialog = GiphyDialogFragment.newInstance(settings.copy(selectedContentType = contentType))
dialog.gifSelectionListener = getGifSelectionListener()
dialog.show(supportFragmentManager, "gifs_dialog")
}
}

override fun onDestroy() {
player.onDestroy()
super.onDestroy()
}

override fun onPause() {
super.onPause()
player.onPause()
}

override fun onResume() {
super.onResume()
player.onResume()
}

private fun getGifSelectionListener() = object : GiphyDialogFragment.GifSelectionListener {
override fun onGifSelected(
media: Media,
searchTerm: String?,
selectedContentType: GPHContentType
) {
Log.d(TAG, "onGifSelected")
messageItems.add(GifItem(media, Author.Me))
feedAdapter?.notifyItemInserted(messageItems.size - 1) }
if (selectedContentType == GPHContentType.clips && media.isVideo) {
messageItems.add(ClipItem(media, Author.Me))
} else {
messageItems.add(GifItem(media, Author.Me))
}
feedAdapter?.notifyItemInserted(messageItems.size - 1)
contentType = selectedContentType
}


override fun onDismissed(selectedContentType: GPHContentType) {
Log.d(TAG, "onDismissed")
contentType = selectedContentType
}

override fun didSearchTerm(term: String) {
Expand Down Expand Up @@ -121,19 +147,67 @@ class DemoActivity : AppCompatActivity() {
messageItems.add(InvalidKeyItem(Author.GifBot))
}
feedAdapter = MessageFeedAdapter(messageItems)
feedAdapter?.itemSelectedListener = ::onGifSelected
feedAdapter?.adapterHelper?.player = player
feedAdapter?.adapterHelper?.clipsPlaybackSetting = clipsPlaybackSetting

messageFeed.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this)
messageFeed.adapter = feedAdapter
}

private fun createVideoPlayer(): GPHVideoPlayer {
val player = GPHVideoPlayer(null, true)
player.addListener { playerState ->
when (playerState) {
is GPHVideoPlayerState.MediaChanged -> {
val position = messageItems.map {
if (it is ClipItem) {
return@map it.media
}
return@map null
}.indexOfFirst {
it?.id == playerState.media.id
}
if (position > -1) {
messageFeed.smoothScrollToPosition(position)
}
}
else -> return@addListener
}
}
return player
}

private fun onGifSelected(itemData: FeedDataItem) {
if (itemData is MessageItem) {
Timber.d("onItemSelected ${itemData.text}")
} else if (itemData is InvalidKeyItem) {
Timber.d("onItemSelected InvalidKeyItem")
} else if (itemData is GifItem) {
Timber.d("onItemSelected ${itemData.media}")
} else if (itemData is ClipItem) {
Timber.d("onItemSelected ${itemData.media}")
showVideoPlayerDialog(itemData.media)
}
}

private fun showSettingsDialog(): Boolean {
val dialog = SettingsDialogFragment.newInstance(settings)
val dialog = SettingsDialogFragment.newInstance(settings, clipsPlaybackSetting)
dialog.dismissListener = ::applyNewSettings
dialog.show(supportFragmentManager, "settings_dialog")
return true
}

private fun applyNewSettings(settings: GPHSettings) {
private fun showVideoPlayerDialog(media: Media): Boolean {
val dialog = ClipDialogFragment.newInstance(media)
dialog.show(supportFragmentManager, "video_player_dialog")
return true
}

private fun applyNewSettings(settings: GPHSettings, clipsPlaybackSetting: SettingsDialogFragment.ClipsPlaybackSetting) {
this.settings = settings
this.clipsPlaybackSetting = clipsPlaybackSetting
feedAdapter?.adapterHelper?.clipsPlaybackSetting = clipsPlaybackSetting
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class GridViewDemoActivity : AppCompatActivity(R.layout.grid_view_demo_activity)

private fun setTrendingQuery() {
gifsGridView.content = when (DemoConfig.contentType) {
GPHContentType.clips -> GPHContent.trendingVideos
GPHContentType.gif -> GPHContent.trendingGifs
GPHContentType.sticker -> GPHContent.trendingStickers
GPHContentType.text -> GPHContent.trendingText
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/java/com/giphy/sdk/uidemo/GridViewSetupActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.os.Bundle
import android.widget.SeekBar
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.giphy.sdk.core.models.enums.MediaType
import com.giphy.sdk.ui.GPHContentType
import com.giphy.sdk.ui.Giphy
import kotlinx.android.synthetic.main.grid_view_activity.*
Expand Down Expand Up @@ -41,8 +42,14 @@ class GridViewSetupActivity : AppCompatActivity() {
})

mediaTypeContainer.setOnCheckedChangeListener { radioGroup, id ->
if (id == R.id.mediaClips) {
DemoConfig.mediaType = MediaType.video
} else {
DemoConfig.mediaType = MediaType.gif
}
when (id) {
R.id.mediaGif -> DemoConfig.contentType = GPHContentType.gif
R.id.mediaClips -> DemoConfig.contentType = GPHContentType.clips
R.id.mediaStickers -> DemoConfig.contentType = GPHContentType.sticker
R.id.mediaText -> DemoConfig.contentType = GPHContentType.text
R.id.mediaEmoji -> DemoConfig.contentType = GPHContentType.emoji
Expand Down Expand Up @@ -98,5 +105,15 @@ class GridViewSetupActivity : AppCompatActivity() {
private fun displayConfig() {
spanCountView.text = DemoConfig.spanCount.toString()
paddingView.text = DemoConfig.cellPadding.toString()
when (DemoConfig.contentType) {
GPHContentType.clips -> mediaClips.isChecked = true
GPHContentType.gif -> mediaGif.isChecked = true
GPHContentType.sticker -> mediaStickers.isChecked = true
GPHContentType.text -> mediaText.isChecked = true
GPHContentType.emoji -> mediaEmoji.isChecked = true
GPHContentType.recents -> mediaRecents.isChecked = true
}
fixedSizeCells.isChecked = DemoConfig.fixedSizeCells
showCheckeredBackground.isChecked = DemoConfig.showCheckeredBackground
}
}
32 changes: 29 additions & 3 deletions app/src/main/java/com/giphy/sdk/uidemo/SettingsDialogFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,26 @@ import kotlinx.android.synthetic.main.fragment_settings.*
*/
class SettingsDialogFragment : androidx.fragment.app.DialogFragment() {

enum class ClipsPlaybackSetting {
inline,
popup;
}

private var settings: GPHSettings = GPHSettings()
var dismissListener: (GPHSettings) -> Unit = { settings -> }
private var clipsPlaybackSetting = ClipsPlaybackSetting.inline
var dismissListener: (GPHSettings, ClipsPlaybackSetting) -> Unit = { settings, clipsPlaybackSetting -> }

companion object {
private const val PICK_GRID_RENDITION = 201
private const val PICK_ATTRIBUTION_RENDTION = 202

private val KEY_SETTINGS = "key_settings"
fun newInstance(gphSettings: GPHSettings): SettingsDialogFragment {
private val KEY_SETTINGS_CLIPS = "key_settings_clips"
fun newInstance(gphSettings: GPHSettings, clipsPlaybackSetting: ClipsPlaybackSetting): SettingsDialogFragment {
val fragment = SettingsDialogFragment()
val bundle = Bundle()
bundle.putParcelable(KEY_SETTINGS, gphSettings)
bundle.putSerializable(KEY_SETTINGS_CLIPS, clipsPlaybackSetting)
fragment.arguments = bundle
return fragment
}
Expand All @@ -43,6 +51,7 @@ class SettingsDialogFragment : androidx.fragment.app.DialogFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
settings = arguments!!.getParcelable(KEY_SETTINGS)
clipsPlaybackSetting = arguments!!.getSerializable(KEY_SETTINGS_CLIPS) as ClipsPlaybackSetting
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
Expand All @@ -58,10 +67,15 @@ class SettingsDialogFragment : androidx.fragment.app.DialogFragment() {
GPHTheme.Automatic -> R.id.autoTheme
}, true)
layoutSelector.setToggled(if (settings.gridType == GridType.waterfall) R.id.waterfall else R.id.carousel, true)
clipsPlaybackSettingsSelector.setToggled(
if (clipsPlaybackSetting == ClipsPlaybackSetting.inline) R.id.inline else R.id.popup,
true
)
mediaTypeSelector.inflateMenu(if (settings.gridType == GridType.waterfall) R.menu.waterfal_media_types else R.menu.carousel_media_types)
settings.mediaTypeConfig.forEach {
val id = when (it) {
GPHContentType.gif -> R.id.typeGif
GPHContentType.clips -> R.id.typeClips
GPHContentType.sticker -> R.id.typeStickers
GPHContentType.text -> R.id.typeText
GPHContentType.emoji -> R.id.typeEmoji
Expand All @@ -85,6 +99,17 @@ class SettingsDialogFragment : androidx.fragment.app.DialogFragment() {
}
}
}

clipsPlaybackSettingsSelector.onToggledListener = { toggle, selected ->
if (toggle.id == R.id.inline) {
clipsPlaybackSetting = ClipsPlaybackSetting.inline
clipsPlaybackSettingsSelector.setToggled(R.id.inline, true)
} else {
clipsPlaybackSetting = ClipsPlaybackSetting.popup
clipsPlaybackSettingsSelector.setToggled(R.id.popup, true)
}
}

showAttributionCheck.isChecked = settings.showAttribution
showConfirmationScreen.isChecked = settings.showConfirmationScreen
showCheckeredBackground.isChecked = settings.showCheckeredBackground
Expand All @@ -111,6 +136,7 @@ class SettingsDialogFragment : androidx.fragment.app.DialogFragment() {
mediaTypeSelector.selectedToggles().forEach {
when (it.id) {
R.id.typeGif -> contentTypes.add(GPHContentType.gif)
R.id.typeClips -> contentTypes.add(GPHContentType.clips)
R.id.typeStickers -> contentTypes.add(GPHContentType.sticker)
R.id.typeText -> contentTypes.add(GPHContentType.text)
R.id.typeEmoji -> contentTypes.add(GPHContentType.emoji)
Expand All @@ -122,7 +148,7 @@ class SettingsDialogFragment : androidx.fragment.app.DialogFragment() {
settings.showAttribution = showAttributionCheck.isChecked
settings.showConfirmationScreen = showConfirmationScreen.isChecked
settings.showCheckeredBackground = showCheckeredBackground.isChecked
dismissListener(settings)
dismissListener(settings, clipsPlaybackSetting)
super.onDismiss(dialog)
}

Expand Down
Loading

0 comments on commit 79510f0

Please sign in to comment.