Chapters

Hide chapters

Android Apprentice

Third Edition · Android 10 · Kotlin 1.3 · Android Studio 3.6

Before You Begin

Section 0: 4 chapters
Show chapters Hide chapters

Section II: Building a List App

Section 2: 7 chapters
Show chapters Hide chapters

Section III: Creating Map-Based Apps

Section 3: 7 chapters
Show chapters Hide chapters

24. Podcast Subscriptions, Part One
Written by Tom Blankenship

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

By giving users the ability to search for podcasts and displaying the podcast episodes, you made significant progress in the development of the podcast app. In this section, you’ll add the ability to subscribe to favorite podcasts.

Over the next two chapters, you’ll add the following features to the app:

  1. Storing the podcast details and episode lists locally for quick access. (this chapter)
  2. Displaying the list of subscribed podcasts by default. (this chapter)
  3. Notifying the user when new episodes are available. (next chapter)

You’ll cover several new topics throughout these two chapters including:

  1. Using Room to store multiple related database tables.
  2. Using JobScheduler services to check for new episodes periodically.
  3. Using local notifications to alert users when new episodes are available.

Getting started

If you’re following along with your own project, open it and keep using it with this chapter. If not, don’t worry. Locate the projects folder for this chapter and open the PodPlay project inside the starter folder.

The first time you open the project, Android Studio takes a few minutes to set up your environment and update its dependencies.

Saving podcasts

The first new feature you’ll implement is the ability to track podcast subscriptions. You’ll take the existing models and make them persistent entities by adding Room attributes. The database will only contain podcasts to which the user subscribes.

Adding Room support

Before getting into the code, you need to bring in the Room libraries.

room_version = '2.2.4'
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"

Annotating the models

Your first task is to properly annotate the existing models so that Room knows how to store the data. Start by getting the Podcast class into shape.

@Entity
data class Podcast(...)
@PrimaryKey(autoGenerate = true) var id: Long? = null,
return Podcast(null, feedUrl, rssResponse.title, description, 
    imageUrl, rssResponse.lastUpdated, 
    episodes = rssItemsToEpisodes(items))
@Entity(
    foreignKeys = [
      ForeignKey(
          entity = Podcast::class,
          parentColumns = ["id"],
          childColumns = ["podcastId"],
          onDelete = ForeignKey.CASCADE
      )
    ],
    indices = [Index("podcastId")]
)
data class Episode (
@PrimaryKey var guid: String = "",
var podcastId: Long? = null,
return episodeResponses.map {
  Episode(
      it.guid ?: "",
      null,
      it.title ?: "",
      it.description ?: "",
      it.url ?: "",
      it.type ?: "",
      DateUtils.xmlDateToDate(it.pubDate),
      it.duration ?: ""
  )
}

Data access object

Before you can define the main Room database object, you need to create the DAO to read and write to the database. This is where you define all of the SQL statements for the basic database operations. You’ll add additional methods later, but for now, all you need is the ability to save and load podcasts and their corresponding episodes.

// 1
@Dao
interface PodcastDao {
  // 2
  @Query("SELECT * FROM Podcast ORDER BY FeedTitle")
  fun loadPodcasts(): LiveData<List<Podcast>>
  // 3
  @Query("SELECT * FROM Episode WHERE podcastId = :podcastId
      ORDER BY releaseDate DESC")
  fun loadEpisodes(podcastId: Long): List<Episode>
  // 4
  @Insert(onConflict = REPLACE)
  fun insertPodcast(podcast: Podcast): Long
  // 5
  @Insert(onConflict = REPLACE)
  fun insertEpisode(episode: Episode): Long
}

Define the Room database

All that’s left to do is define the Room database object and have it instantiate the PodcastDao object.

// 1
@Database(entities = arrayOf(Podcast::class, Episode::class),
    version = 1)
abstract class PodPlayDatabase : RoomDatabase() {
  // 2
  abstract fun podcastDao(): PodcastDao
  // 3
  companion object {
    // 4
    private var instance: PodPlayDatabase? = null
    // 5
    fun getInstance(context: Context): PodPlayDatabase {
      if (instance == null) {
        // 6
        instance = Room.databaseBuilder(context.applicationContext,
            PodPlayDatabase::class.java, "PodPlayer").build()
      }
      // 7
      return instance as PodPlayDatabase
    }
  }
}
- Cannot figure out how to save this field into the database. You can consider adding a type converter for it.
- Cannot figure out how to read this field from a cursor.
var lastUpdated: Date = Date()
var episodes: List<Episode> = listOf()
var releaseDate: Date = Date()

Room type converters

Although Room can’t handle complex types directly, it provides a concept known as TypeConverters that let you define how to convert them to-and-from basic types. This is the perfect solution for the Date properties.

class Converters {
  @TypeConverter
  fun fromTimestamp(value: Long?): Date? {
    return if (value == null) null else Date(value)
  }

  @TypeConverter
  fun toTimestamp(date: Date?): Long? {
    return (date?.time)
  }
}
@Database(entities = arrayOf(Podcast::class, Episode::class), 
    version = 1)
@TypeConverters(Converters::class)
abstract class PodPlayDatabase : RoomDatabase() {...}

Room object references

Now back to the episodes list in the Podcast model. Since Room does not support defining object references in Entity classes, you need to tell it to ignore the episodes property.

@Ignore
var episodes: List<Episode> = listOf()

Update the podcast repo

The podcast repo currently uses only the RssFeedService to retrieve podcast data. One benefit of using the repository pattern is that a single repository can access data from multiple sources or services.

class PodcastRepo(private var feedService: FeedService) {
class PodcastRepo(private var feedService: FeedService,
                  private var podcastDao: PodcastDao) {
podcastViewModel.podcastRepo = PodcastRepo(rssService)
val db = PodPlayDatabase.getInstance(this)
val podcastDao = db.podcastDao()
podcastViewModel.podcastRepo = PodcastRepo(rssService, podcastDao)
fun save(podcast: Podcast) {
  GlobalScope.launch {
    // 1
    val podcastId = podcastDao.insertPodcast(podcast)
    // 2
    for (episode in podcast.episodes) {
      // 3
      episode.podcastId = podcastId
      podcastDao.insertEpisode(episode)
    }
  }
}
fun getAll(): LiveData<List<Podcast>>
{
  return podcastDao.loadPodcasts()
}

Updating the view model

One more step is needed before you can connect the subscribe menu item. Since the view only talks to the view model, you need to update the podcast view model to use the new repository methods.

private var activePodcast: Podcast? = null
activePodcast = it
fun saveActivePodcast() {
  val repo = podcastRepo ?: return
  activePodcast?.let {
    repo.save(it)
  }
}
private fun podcastToSummaryView(podcast: Podcast):
    PodcastSummaryViewData {
  return PodcastSummaryViewData(
      podcast.feedTitle,
      DateUtils.dateToShortDate(podcast.lastUpdated),
      podcast.imageUrl,
      podcast.feedUrl)
}
var livePodcastData: LiveData<List<PodcastSummaryViewData>>? = null
fun getPodcasts(): LiveData<List<PodcastSummaryViewData>>? {
  val repo = podcastRepo ?: return null
    // 1
  if (livePodcastData == null) {
    // 2
    val liveData = repo.getAll()
    // 3
    livePodcastData = Transformations.map(liveData) { podcastList ->
      podcastList.map { podcast ->
        podcastToSummaryView(podcast)
      }
    }
  }

  // 4
  return livePodcastData
}

Connecting the subscribe menu item

Everything is now in place to hook-up the subscribe menu item on the podcast detail screen.

interface OnPodcastDetailsListener {
  fun onSubscribe()
}
private var listener: OnPodcastDetailsListener? = null
override fun onAttach(context: Context) {
  super.onAttach(context)
  if (context is OnPodcastDetailsListener) {
    listener = context
  } else {
    throw RuntimeException(context.toString() + 
        " must implement OnPodcastDetailsListener")
  }
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
  when (item.itemId) {
    R.id.menu_feed_action -> {
      podcastViewModel.activePodcastViewData?.feedUrl?.let {
         listener?.onSubscribe()
      }
      return true
    }
    else ->
      return super.onOptionsItemSelected(item)
  }
}
class PodcastActivity : AppCompatActivity(), PodcastListAdapterListener,
    OnPodcastDetailsListener {
override fun onSubscribe() {
  podcastViewModel.saveActivePodcast()
  supportFragmentManager.popBackStack()
}

Displaying subscribed podcasts

That completes the code to subscribe to a podcast. Of course, subscribing to a podcast isn’t useful if you don’t let the user see their subscriptions!

private fun showSubscribedPodcasts()
{
  // 1
  val podcasts = podcastViewModel.getPodcasts()?.value
  // 2
  if (podcasts != null) {
    toolbar.title = getString(R.string.subscribed_podcasts)
    podcastListAdapter.setSearchData(podcasts)
  }
}
<string name="subscribed_podcasts">Subscribed</string>
private fun setupPodcastListView() {
  podcastViewModel.getPodcasts()?.observe(this, Observer {
    if (it != null) {
      showSubscribedPodcasts()
    }
  })
}
setupPodcastListView()

@Query("SELECT * FROM Podcast WHERE feedUrl = :url")
fun loadPodcast(url: String): Podcast?
GlobalScope.launch {

  val podcast = podcastDao.loadPodcast(feedUrl)

  if (podcast != null) {
    podcast.id?.let {
      podcast.episodes = podcastDao.loadEpisodes(it)
      GlobalScope.launch(Dispatchers.Main) {
        callback(podcast)
      }
    }
  } else {
  }
}
return PodcastViewData(
    podcast.id != null,
    podcast.feedTitle,
    podcast.feedUrl,
    podcast.feedDesc,
    podcast.imageUrl,
    episodesToEpisodesView(podcast.episodes)
)
fun onUnsubscribe()
private var menuItem: MenuItem? = null
private fun updateMenuItem() {
  // 1
  val viewData = podcastViewModel.activePodcastViewData ?: return
  // 2
  menuItem?.title = if (viewData.subscribed)
      getString(R.string.unsubscribe) else getString(R.string.subscribe)
}
<string name="unsubscribe">Unsubscribe</string>
menuItem = menu.findItem(R.id.menu_feed_action)
updateMenuItem()
  listener?.onSubscribe()
if (podcastViewModel.activePodcastViewData?.subscribed) {
  listener?.onUnsubscribe()
} else {
  listener?.onSubscribe()
}
@Delete
fun deletePodcast(podcast: Podcast)
fun delete(podcast: Podcast) {
  GlobalScope.launch {
    podcastDao.deletePodcast(podcast)
  }
}
fun deleteActivePodcast() {
  val repo = podcastRepo ?: return
  activePodcast?.let {
    repo.delete(it)
  }
}
override fun onUnsubscribe() {
  podcastViewModel.deleteActivePodcast()
  supportFragmentManager.popBackStack()
}

searchMenuItem.setOnActionExpandListener(object: MenuItem.OnActionExpandListener {
  override fun onMenuItemActionExpand(p0: MenuItem?): Boolean {
    return true
  }
  override fun onMenuItemActionCollapse(p0: MenuItem?): Boolean {
    showSubscribedPodcasts()
    return true
  }
})

Where to go from here?

Good job! You made it through the first part of podcast subscriptions. Take a breather, and pick up with part two when you’re ready to finish!

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2024 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now