-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
532 additions
and
28 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
13 changes: 13 additions & 0 deletions
13
app/src/main/java/com/example/githubuser/data/model/local/Favorite.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.example.githubuser.data.model.local | ||
|
||
import androidx.room.Entity | ||
import androidx.room.PrimaryKey | ||
import java.io.Serializable | ||
|
||
@Entity(tableName = "favorite_list") | ||
data class Favorite ( | ||
val login: String, | ||
@PrimaryKey | ||
val id: Int, | ||
val avatar_url: String | ||
): Serializable |
21 changes: 21 additions & 0 deletions
21
app/src/main/java/com/example/githubuser/data/model/local/FavoriteDao.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.example.githubuser.data.model.local | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.room.Dao | ||
import androidx.room.Insert | ||
import androidx.room.Query | ||
|
||
@Dao | ||
interface FavoriteDao { | ||
@Insert | ||
suspend fun addFavorite(favorite: Favorite) | ||
|
||
@Query("SELECT * FROM favorite_list") | ||
fun getFavorite(): LiveData<List<Favorite>> | ||
|
||
@Query("SELECT count(*) FROM favorite_list WHERE favorite_list.id = :id") | ||
suspend fun checkUser(id: Int): Int | ||
|
||
@Query("DELETE FROM favorite_list WHERE favorite_list.id = :id") | ||
suspend fun deleteUser(id: Int): Int | ||
} |
26 changes: 26 additions & 0 deletions
26
app/src/main/java/com/example/githubuser/data/model/local/FavoriteDatabase.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.example.githubuser.data.model.local | ||
|
||
import android.content.Context | ||
import androidx.room.Database | ||
import androidx.room.Room | ||
import androidx.room.RoomDatabase | ||
|
||
@Database( | ||
version = 1, | ||
entities = [Favorite::class] | ||
) | ||
abstract class FavoriteDatabase: RoomDatabase() { | ||
companion object{ | ||
var INSTANCE : FavoriteDatabase? = null | ||
|
||
fun getDatabase(context: Context): FavoriteDatabase?{ | ||
if(INSTANCE == null) { | ||
synchronized(FavoriteDatabase::class) { | ||
INSTANCE = Room.databaseBuilder(context.applicationContext, FavoriteDatabase::class.java, "favorite_database").build() | ||
} | ||
} | ||
return INSTANCE | ||
} | ||
} | ||
abstract fun favoriteDao() : FavoriteDao | ||
} |
57 changes: 57 additions & 0 deletions
57
app/src/main/java/com/example/githubuser/data/model/local/SettingPreference.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.example.githubuser.data.model.local | ||
|
||
import android.content.Context | ||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.booleanPreferencesKey | ||
import androidx.datastore.preferences.core.edit | ||
import androidx.datastore.preferences.preferencesDataStore | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
|
||
val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings") | ||
class SettingPreferences private constructor(private val dataStore: DataStore<Preferences>) { | ||
|
||
private val THEME_KEY = booleanPreferencesKey("theme_setting") | ||
|
||
fun getThemeSetting(): Flow<Boolean> { | ||
return dataStore.data.map { preferences -> | ||
preferences[THEME_KEY] ?: false | ||
} | ||
} | ||
|
||
suspend fun saveThemeSetting(isDarkModeActive: Boolean) { | ||
dataStore.edit { preferences -> | ||
preferences[THEME_KEY] = isDarkModeActive | ||
} | ||
} | ||
|
||
companion object { | ||
@Volatile | ||
private var INSTANCE: SettingPreferences? = null | ||
|
||
fun getInstance(dataStore: DataStore<Preferences>): SettingPreferences { | ||
return INSTANCE ?: synchronized(this) { | ||
val instance = SettingPreferences(dataStore) | ||
INSTANCE = instance | ||
instance | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
//class SettingPreference private constructor(context: Context) { | ||
// private val settingsDataStore = context.prefDataStore | ||
// private val themeKEY = booleanPreferencesKey("theme_setting") | ||
// | ||
// fun getThemeSetting(): Flow<Boolean> = | ||
// settingsDataStore.data.map { preferences -> | ||
// preferences[themeKEY]?:false | ||
// } | ||
// suspend fun saveThemeSetting(isDarkModeActive: Boolean) { | ||
// settingsDataStore.edit { preferences -> | ||
// preferences[themeKEY] = isDarkModeActive | ||
// } | ||
// } | ||
//} |
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
75 changes: 75 additions & 0 deletions
75
app/src/main/java/com/example/githubuser/ui/FavoriteActivity.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.example.githubuser.ui | ||
|
||
import android.content.Intent | ||
import androidx.appcompat.app.AppCompatActivity | ||
import android.os.Bundle | ||
import android.view.MenuItem | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.recyclerview.widget.LinearLayoutManager | ||
import com.example.githubuser.R | ||
import com.example.githubuser.data.model.UserDetail | ||
import com.example.githubuser.data.model.local.Favorite | ||
import com.example.githubuser.databinding.ActivityFavoriteBinding | ||
|
||
class FavoriteActivity : AppCompatActivity() { | ||
|
||
private lateinit var binding: ActivityFavoriteBinding | ||
private lateinit var viewModel: FavoriteViewModel | ||
private lateinit var adapter: UserAdapter | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
binding = ActivityFavoriteBinding.inflate(layoutInflater) | ||
setContentView(binding.root) | ||
supportActionBar?.setDisplayHomeAsUpEnabled(true) | ||
|
||
adapter = UserAdapter() | ||
adapter.notifyDataSetChanged() | ||
|
||
viewModel = ViewModelProvider(this)[FavoriteViewModel::class.java] | ||
|
||
adapter.setOnItemClickFunction(object :UserAdapter.OnItemClickCallback { | ||
override fun onFunctionClicked(data: UserDetail) { | ||
Intent(this@FavoriteActivity, ProfileActivity::class.java).also { | ||
it.putExtra(ProfileActivity.EXTRA_USERNAME, data.login) | ||
it.putExtra(ProfileActivity.EXTRA_ID, data.id) | ||
it.putExtra(ProfileActivity.EXTRA_URL, data.avatar_url) | ||
startActivity(it) | ||
} | ||
} | ||
}) | ||
|
||
binding.apply { | ||
rvUserFavorite.setHasFixedSize(true) | ||
rvUserFavorite.layoutManager = LinearLayoutManager(this@FavoriteActivity) | ||
rvUserFavorite.adapter = adapter | ||
} | ||
|
||
viewModel.getFavorite()?.observe(this, { | ||
if(it != null) { | ||
val list = mapList(it) | ||
adapter.setList(list) | ||
} | ||
}) | ||
} | ||
private fun mapList(it: List<Favorite>): ArrayList<UserDetail> { | ||
val list = ArrayList<UserDetail>() | ||
for (user in it) { | ||
val userMapped = UserDetail( | ||
user.login, | ||
user.id, | ||
user.avatar_url | ||
) | ||
list.add(userMapped) | ||
} | ||
return list | ||
} | ||
|
||
override fun onOptionsItemSelected(item: MenuItem): Boolean { | ||
when (item.itemId) { | ||
android.R.id.home -> { | ||
finish() | ||
} | ||
} | ||
return super.onOptionsItemSelected(item) | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
app/src/main/java/com/example/githubuser/ui/FavoriteViewModel.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.example.githubuser.ui | ||
|
||
import android.app.Application | ||
import androidx.lifecycle.AndroidViewModel | ||
import androidx.lifecycle.LiveData | ||
import com.example.githubuser.data.model.local.Favorite | ||
import com.example.githubuser.data.model.local.FavoriteDao | ||
import com.example.githubuser.data.model.local.FavoriteDatabase | ||
|
||
class FavoriteViewModel(application: Application): AndroidViewModel(application) { | ||
private var userDao: FavoriteDao? | ||
private var userDb: FavoriteDatabase? | ||
|
||
init { | ||
userDb = FavoriteDatabase.getDatabase(application) | ||
userDao = userDb?.favoriteDao() | ||
} | ||
|
||
fun getFavorite():LiveData<List<Favorite>>? { | ||
return userDao?.getFavorite() | ||
} | ||
} |
Oops, something went wrong.