Skip to content

Commit

Permalink
Merge pull request #5 from ukrainefield/feature/google-analytics
Browse files Browse the repository at this point in the history
Feature/google analytics
  • Loading branch information
GewoonJaap authored Mar 13, 2022
2 parents 6d16552 + 0ec008d commit 33cb8a5
Show file tree
Hide file tree
Showing 16 changed files with 224 additions and 20 deletions.
17 changes: 17 additions & 0 deletions app/.idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.recyclerview.widget.RecyclerView
import com.google.firebase.analytics.FirebaseAnalytics
import nl.gardensnakes.ukrainefield.data.remote.SavedPreferences
import nl.gardensnakes.ukrainefield.helper.BookmarkHelper
import nl.gardensnakes.ukrainefield.helper.FirebaseHelper
import nl.gardensnakes.ukrainefield.view.adapter.FeedCardAdapter

class BookmarkFragment : Fragment() {
Expand All @@ -34,6 +35,7 @@ class BookmarkFragment : Fragment() {
val view = inflater.inflate(R.layout.fragment_bookmark, container, false)

mFirebaseAnalytics = FirebaseAnalytics.getInstance(view.context);
FirebaseHelper.updateCurrentScreen(mFirebaseAnalytics, this.requireActivity(), this.javaClass.simpleName, this.javaClass.simpleName)

useProxyServer = SavedPreferences.useProxyServer(requireContext())
feedRecyclerView = view.findViewById(R.id.bookmark_recycler_view)
Expand All @@ -48,7 +50,7 @@ class BookmarkFragment : Fragment() {
else {
noBookmarksText.visibility = View.GONE
feedRecyclerView.visibility = View.VISIBLE
feedCardAdapter = FeedCardAdapter(bookmarkedItems.sortedByDescending { it.epochTime}, true)
feedCardAdapter = FeedCardAdapter(bookmarkedItems.sortedByDescending { it.epochTime}, mFirebaseAnalytics, true)
feedRecyclerView.adapter = feedCardAdapter
feedRecyclerView.layoutManager = LinearLayoutManager(view.context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import kotlinx.coroutines.*
import nl.gardensnakes.ukrainefield.data.remote.FeedService
import nl.gardensnakes.ukrainefield.data.remote.MapService
import nl.gardensnakes.ukrainefield.data.remote.SavedPreferences
import nl.gardensnakes.ukrainefield.helper.FirebaseHelper
import nl.gardensnakes.ukrainefield.helper.SwipeRefreshHelper
import nl.gardensnakes.ukrainefield.view.adapter.FeedCardAdapter
import nl.gardensnakes.ukrainefield.view.adapter.MapCardAdapter
Expand Down Expand Up @@ -45,13 +46,14 @@ class MapFragment : Fragment() {
val view = inflater.inflate(R.layout.fragment_map, container, false)

mFirebaseAnalytics = FirebaseAnalytics.getInstance(view.context);
FirebaseHelper.updateCurrentScreen(mFirebaseAnalytics, this.requireActivity(), this.javaClass.simpleName, this.javaClass.simpleName)

useProxyServer = SavedPreferences.useProxyServer(requireContext())

swipeRefreshLayout = view.findViewById(R.id.map_refresh_layout)
mapRecyclerView = view.findViewById(R.id.map_recycle_view)

mapCardAdapter = MapCardAdapter(emptyList())
mapCardAdapter = MapCardAdapter(emptyList(), mFirebaseAnalytics)
mapRecyclerView.adapter = mapCardAdapter

setupRefreshLayout()
Expand All @@ -73,7 +75,7 @@ class MapFragment : Fragment() {
val feedData = feedService.getAllMaps()
swipeRefreshLayout.isRefreshing = false
if(feedData != null && context != null) {
mapCardAdapter = MapCardAdapter(feedData.mapData)
mapCardAdapter = MapCardAdapter(feedData.mapData, mFirebaseAnalytics)
mapRecyclerView.adapter = mapCardAdapter
mapRecyclerView.layoutManager = LinearLayoutManager(requireView().context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.github.piasy.biv.loader.glide.GlideImageLoader
import com.github.piasy.biv.view.BigImageView
import com.google.firebase.analytics.FirebaseAnalytics
import nl.gardensnakes.ukrainefield.data.remote.HttpRoutes
import nl.gardensnakes.ukrainefield.helper.FirebaseHelper
import nl.gardensnakes.ukrainefield.helper.PreferenceHelper

class MediaDetailActivity : AppCompatActivity() {
Expand All @@ -27,6 +28,7 @@ class MediaDetailActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)

mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
FirebaseHelper.updateCurrentScreen(mFirebaseAnalytics, this, this.javaClass.simpleName, this.javaClass.simpleName)

BigImageViewer.initialize(GlideImageLoader.with(this));
setContentView(R.layout.activity_media_detail)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.google.firebase.analytics.FirebaseAnalytics
import kotlinx.coroutines.*
import nl.gardensnakes.ukrainefield.data.remote.FeedService
import nl.gardensnakes.ukrainefield.data.remote.SavedPreferences
import nl.gardensnakes.ukrainefield.helper.FirebaseHelper
import nl.gardensnakes.ukrainefield.helper.SwipeRefreshHelper
import nl.gardensnakes.ukrainefield.view.adapter.FeedCardAdapter

Expand Down Expand Up @@ -42,13 +43,14 @@ class NewsFeedFragment : Fragment() {
val view = inflater.inflate(R.layout.fragment_newsfeed, container, false)

mFirebaseAnalytics = FirebaseAnalytics.getInstance(view.context);
FirebaseHelper.updateCurrentScreen(mFirebaseAnalytics, this.requireActivity(), this.javaClass.simpleName, this.javaClass.simpleName)

useProxyServer = SavedPreferences.useProxyServer(requireContext())

swipeRefreshLayout = view.findViewById(R.id.newsfeed_refresh_layout)
feedRecyclerView = view.findViewById(R.id.newsfeed_recycle_view)

feedCardAdapter = FeedCardAdapter(emptyList())
feedCardAdapter = FeedCardAdapter(emptyList(), mFirebaseAnalytics)
feedRecyclerView.adapter = feedCardAdapter

setupRefreshLayout()
Expand All @@ -70,7 +72,7 @@ class NewsFeedFragment : Fragment() {
val feedData = feedService.getAllFeed()
swipeRefreshLayout.isRefreshing = false
if(feedData != null && context != null) {
feedCardAdapter = FeedCardAdapter(feedData.messages)
feedCardAdapter = FeedCardAdapter(feedData.messages, mFirebaseAnalytics)
feedRecyclerView.adapter = feedCardAdapter
feedRecyclerView.layoutManager = LinearLayoutManager(requireView().context);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,67 @@
package nl.gardensnakes.ukrainefield

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.preference.*
import androidx.core.content.ContextCompat
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import com.google.firebase.analytics.FirebaseAnalytics
import nl.gardensnakes.ukrainefield.helper.BookmarkHelper
import nl.gardensnakes.ukrainefield.helper.Consts
import nl.gardensnakes.ukrainefield.helper.FirebaseHelper

class SettingsFragment : PreferenceFragmentCompat() {

private lateinit var mFirebaseAnalytics: FirebaseAnalytics

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mFirebaseAnalytics = FirebaseAnalytics.getInstance(requireContext());
FirebaseHelper.updateCurrentScreen(mFirebaseAnalytics, this.requireActivity(), this.javaClass.simpleName, this.javaClass.simpleName)
}

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.settings, rootKey)
setupResetBookmarksClick()
setupNewsSourcesClick()
setupGithubReposClick()
setupTwitterClick()
}

private fun setupResetBookmarksClick() {
findPreference<Preference>("reset_bookmarks")?.setOnPreferenceClickListener {
FirebaseHelper.logResetBookmarks(mFirebaseAnalytics)
context?.let { context -> BookmarkHelper().resetBookmarks(context) }
true
}
}

private fun setupTwitterClick() {
findPreference<Preference>("twitter_link")?.setOnPreferenceClickListener {
FirebaseHelper.logVisitOfficialLink(mFirebaseAnalytics, Consts.TWITTER_LINK)
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(Consts.TWITTER_LINK))
ContextCompat.startActivity(requireContext(), browserIntent, null)
true
}
}

private fun setupGithubReposClick() {
findPreference<Preference>("github_link")?.setOnPreferenceClickListener {
FirebaseHelper.logVisitOfficialLink(mFirebaseAnalytics, Consts.GITHUB_LINK)
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(Consts.GITHUB_LINK))
ContextCompat.startActivity(requireContext(), browserIntent, null)
true
}
}

private fun setupNewsSourcesClick() {
findPreference<Preference>("news_sources_link")?.setOnPreferenceClickListener {
FirebaseHelper.logVisitOfficialLink(mFirebaseAnalytics, Consts.NEWSSOURCES_LINK)
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(Consts.NEWSSOURCES_LINK))
ContextCompat.startActivity(requireContext(), browserIntent, null)
true
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class BookmarkHelper {
ret = stringBuilder.toString()
}
} catch (e: FileNotFoundException) {
resetBookmarks(context)
Log.e("BookmarkHelper", "File not found: $e")
} catch (e: IOException) {
Log.e("BookmarkHelper", "Can not read file: $e")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package nl.gardensnakes.ukrainefield.helper

object Consts {
val NEWSSOURCES_LINK: String = "https://github.com/ukrainefield/data-fetcher/blob/main/consts.json"
val GITHUB_LINK: String = "https://github.com/ukrainefield"
val TWITTER_LINK: String = "https://twitter.com/fieldukraine"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package nl.gardensnakes.ukrainefield.helper

import android.app.Activity
import android.os.Bundle
import com.google.firebase.analytics.FirebaseAnalytics

class FirebaseHelper {
companion object {
fun updateCurrentScreen(firebase: FirebaseAnalytics, activity: Activity, activityName: String, className: String){
firebase.setCurrentScreen(activity, activityName, className);
}
fun logShareEvent(firebase: FirebaseAnalytics, messageUrl: String){
val params = Bundle()
params.putString("share_message", messageUrl)
firebase.logEvent("eventShareMessage", params)
}
fun logOpenInBrowserEvent(firebase: FirebaseAnalytics, messageUrl: String){
val params = Bundle()
params.putString("open_message_in_browser", messageUrl)
firebase.logEvent("eventOpenMessageInBrowser", params)
}
fun logBookmarkEvent(firebase: FirebaseAnalytics, messageId: String){
val params = Bundle()
params.putString("message_id", messageId)
firebase.logEvent("eventBookmarkMessage", params)
}
fun logUnbookmarkEvent(firebase: FirebaseAnalytics, messageUrl: String){
val params = Bundle()
params.putString("message_id", messageUrl)
firebase.logEvent("eventUnbookmarkMessage", params)
}
fun logVisitOfficialLink(firebase: FirebaseAnalytics, linkUrl: String){
val params = Bundle()
params.putString("link_url", linkUrl)
firebase.logEvent("eventVisitOfficialLink", params)
}
fun logResetBookmarks(firebase: FirebaseAnalytics){
val params = Bundle()
firebase.logEvent("eventResetBookmarks", params)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ import androidx.recyclerview.widget.RecyclerView
import com.afdhal_fa.imageslider.ImageSlider
import com.afdhal_fa.imageslider.`interface`.ItemClickListener
import com.afdhal_fa.imageslider.model.SlideUIModel
import com.google.firebase.analytics.FirebaseAnalytics
import nl.gardensnakes.ukrainefield.MediaDetailActivity
import nl.gardensnakes.ukrainefield.R
import nl.gardensnakes.ukrainefield.data.remote.HttpRoutes
import nl.gardensnakes.ukrainefield.data.remote.dto.feed.FeedMessageResponse
import nl.gardensnakes.ukrainefield.helper.BookmarkHelper
import nl.gardensnakes.ukrainefield.helper.FirebaseHelper
import nl.gardensnakes.ukrainefield.helper.PreferenceHelper
import nl.gardensnakes.ukrainefield.helper.TimeHelper
import java.lang.Exception


class FeedCardAdapter(private var mList: List<FeedMessageResponse>, private val deleteOnUnBookmark: Boolean = false) :
class FeedCardAdapter(private var mList: List<FeedMessageResponse>, private val mFirebaseAnalytics: FirebaseAnalytics, private val deleteOnUnBookmark: Boolean = false) :
RecyclerView.Adapter<FeedCardAdapter.ViewHolder>() {

lateinit var context: Context
Expand All @@ -42,7 +44,6 @@ class FeedCardAdapter(private var mList: List<FeedMessageResponse>, private val
override fun onBindViewHolder(holder: ViewHolder, position: Int) {

val feedData = mList[position]
val bookmarkHelper = BookmarkHelper()

resetView(holder)

Expand Down Expand Up @@ -70,7 +71,7 @@ class FeedCardAdapter(private var mList: List<FeedMessageResponse>, private val
holder.postedAtText.text =
"${context.getString(R.string.posted_at)} ${TimeHelper.epochToTimeString(feedData.epochTime.toLong())}"

updateBookmarkText(feedData.messageURL ?: "", holder, position)
updateBookmarkText(feedData.messageURL ?: "", holder, position, false)

if (feedData.videos.isEmpty() && feedData.images.isEmpty()) {
holder.imageSlide.visibility = View.GONE
Expand Down Expand Up @@ -105,15 +106,17 @@ class FeedCardAdapter(private var mList: List<FeedMessageResponse>, private val
}

holder.shareView.setOnClickListener {
FirebaseHelper.logShareEvent(mFirebaseAnalytics, feedData.messageURL ?: "Unknown")
val sendIntent = Intent()
sendIntent.action = Intent.ACTION_SEND
sendIntent.putExtra(Intent.EXTRA_SUBJECT, getTitleText(feedData));
sendIntent.putExtra(Intent.EXTRA_SUBJECT, getTitleText(feedData))
sendIntent.putExtra(Intent.EXTRA_TEXT, feedData.messageURL)
sendIntent.type = "text/plain"
startActivity(context, sendIntent, null)
}

holder.browserButtonView.setOnClickListener {
FirebaseHelper.logOpenInBrowserEvent(mFirebaseAnalytics, feedData.messageURL ?: "Unkown")
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(feedData.messageURL))
startActivity(context, browserIntent, null)
}
Expand All @@ -130,12 +133,18 @@ class FeedCardAdapter(private var mList: List<FeedMessageResponse>, private val
return mList.size
}

private fun updateBookmarkText(messageUrl: String, holder: ViewHolder, position: Int){
var isBookmarked = BookmarkHelper().isFavorite(messageUrl, context)
private fun updateBookmarkText(messageUrl: String, holder: ViewHolder, position: Int, logEvent: Boolean = true){
val isBookmarked = BookmarkHelper().isFavorite(messageUrl, context)
if(isBookmarked){
if(logEvent) {
FirebaseHelper.logBookmarkEvent(mFirebaseAnalytics, messageUrl)
}
holder.bookmarkButton.text = context.getString(R.string.remove_bookmark)
}
else{
if(logEvent) {
FirebaseHelper.logUnbookmarkEvent(mFirebaseAnalytics, messageUrl)
}
holder.bookmarkButton.text = context.getString(R.string.bookmark)
if(deleteOnUnBookmark){
try {
Expand Down Expand Up @@ -166,7 +175,7 @@ class FeedCardAdapter(private var mList: List<FeedMessageResponse>, private val

// Holds the views for adding it to image and text
class ViewHolder(ItemView: View) : RecyclerView.ViewHolder(ItemView) {
val imageSlide = itemView.findViewById<ImageSlider>(R.id.feed_card_thumbnail)
val imageSlide: ImageSlider = itemView.findViewById(R.id.feed_card_thumbnail)
val titleView: TextView = itemView.findViewById(R.id.feed_card_title)
val textView: TextView = itemView.findViewById(R.id.feed_card_text)
val postedAtText: TextView = itemView.findViewById(R.id.feed_card_posted_at_text)
Expand Down
Loading

0 comments on commit 33cb8a5

Please sign in to comment.