rifki_cs29

FavouriteProvider

Jul 1st, 2020
1,178
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.rifki.kotlin.mygithubfinal.provider
  2.  
  3. import android.content.ContentProvider
  4. import android.content.ContentValues
  5. import android.content.Context
  6. import android.content.UriMatcher
  7. import android.database.Cursor
  8. import android.net.Uri
  9. import com.rifki.kotlin.mygithubfinal.db.DatabaseContract.AUTHORITY
  10. import com.rifki.kotlin.mygithubfinal.db.DatabaseContract.FavouriteColumns.Companion.CONTENT_URI
  11. import com.rifki.kotlin.mygithubfinal.db.DatabaseContract.FavouriteColumns.Companion.TABLE_NAME
  12. import com.rifki.kotlin.mygithubfinal.db.FavouriteHelper
  13.  
  14. class FavouriteProvider : ContentProvider() {
  15.  
  16.     companion object {
  17.         private const val FAVOURITE = 1
  18.         private const val FAVOURITE_ID = 2
  19.         private val sUriMatcher = UriMatcher(UriMatcher.NO_MATCH)
  20.         private lateinit var favouriteHelper: FavouriteHelper
  21.  
  22.         init {
  23.             sUriMatcher.addURI(AUTHORITY, TABLE_NAME, FAVOURITE)
  24.             sUriMatcher.addURI(AUTHORITY, "$TABLE_NAME/#", FAVOURITE_ID)
  25.         }
  26.     }
  27.  
  28.     override fun onCreate(): Boolean {
  29.         favouriteHelper = FavouriteHelper.getInstance(context as Context)
  30.         favouriteHelper.open()
  31.         return true
  32.     }
  33.  
  34.     override fun query(uri: Uri, strings: Array<String>?, s: String?, strings1: Array<String>?, s1: String?): Cursor?  {
  35.         val cursor : Cursor?
  36.         when(sUriMatcher.match(uri)){
  37.             FAVOURITE -> cursor = favouriteHelper.queryAll()
  38.             FAVOURITE_ID -> cursor = favouriteHelper.queryByUsername(uri.lastPathSegment.toString())
  39.             else -> cursor = null
  40.         }
  41.         return cursor
  42.     }
  43.  
  44.     override fun insert(uri: Uri, contentValues: ContentValues?): Uri? {
  45.         val added : Long = when (FAVOURITE){
  46.             sUriMatcher.match(uri) -> favouriteHelper.insert(contentValues)
  47.             else -> 0
  48.         }
  49.         context?.contentResolver?.notifyChange(CONTENT_URI, null)
  50.         return Uri.parse("$CONTENT_URI/$added")
  51.     }
  52.  
  53.     override fun delete(uri: Uri, s: String?, strings: Array<String>?): Int  {
  54.         val deleted: Int = when (FAVOURITE_ID) {
  55.             sUriMatcher.match(uri) -> favouriteHelper.deleteByUsername(uri.lastPathSegment.toString())
  56.             else -> 0
  57.         }
  58.         context?.contentResolver?.notifyChange(CONTENT_URI, null)
  59.         return deleted
  60.     }
  61.  
  62.     override fun getType(uri: Uri): String? {
  63.         return null
  64.     }
  65.  
  66.     override fun update(uri: Uri, contentValues: ContentValues?, s: String?, strings: Array<String>?): Int {
  67.         return 0
  68.     }
  69. }
RAW Paste Data