Bitiruvchi: Berdiyorova. Sh


Download 1.9 Mb.
bet18/18
Sana19.06.2023
Hajmi1.9 Mb.
#1613266
1   ...   10   11   12   13   14   15   16   17   18
Bog'liq
Diplom ishi

Ilovalar
@Entity(tableName = "dictionary")
data class Dictionary(
@PrimaryKey(autoGenerate = true) var id: Int,
@ColumnInfo(name = "english")
val english: String,
@ColumnInfo(name = "type")
val type: String,
@ColumnInfo(name = "transcript")
val transcript: String,
@ColumnInfo(name = "uzbek")
val uzbek: String,
@ColumnInfo(name = "countable")
val countable: String,
@ColumnInfo(name = "is_favourite")
val isFavorite: Int,
)
@Dao
interface DictionaryDao {
@Query("SELECT * FROM dictionary WHERE english LIKE '%' || :searchQuery || '%'")
fun searchDatabase(searchQuery: String): PagingSource
@Query("SELECT * FROM dictionary WHERE is_favourite=1")
fun getFavorites(): PagingSource
@Query("UPDATE dictionary SET is_favourite=:isFavorite WHERE id = :id")
suspend fun update(id: Int, isFavorite: Int)
@Query("SELECT * FROM dictionary WHERE id=:id")
fun getById(id: Int): LiveData
@Query("SELECT DISTINCT * FROM dictionary INNER JOIN history on history.dictionaryId = dictionary.id")
fun getHistory(): PagingSource
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun addHistory(history: History)
}
@Entity(tableName = "history")
data class History(
@PrimaryKey
@ColumnInfo(name = "dictionaryId")
val dictionaryId: Int
)
class MainAdapter(val listener: (id: Int) -> Unit) :
PagingDataAdapter<Dictionary, MainAdapter.MainViewHolder>(DIFF_CALLBACK) {
companion object {
val DIFF_CALLBACK = object : DiffUtil.ItemCallback() {
override fun areItemsTheSame(oldItem: Dictionary, newItem: Dictionary): Boolean =
oldItem.id == newItem.id
override fun areContentsTheSame(oldItem: Dictionary, newItem: Dictionary): Boolean =
oldItem == newItem
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainViewHolder {
return MainViewHolder(
LayoutItemListBinding.inflate(LayoutInflater.from(parent.context), parent, false)
)
}
override fun onBindViewHolder(holder: MainViewHolder, position: Int) {
val item: Dictionary? = getItem(position)
holder.onBind(item)
}
inner class MainViewHolder(private val binding: LayoutItemListBinding) :
RecyclerView.ViewHolder(binding.root) {
private val tvEnglish = binding.tvEnglish
var item: Dictionary? = null
fun onBind(item: Dictionary?) {
this.item = item
tvEnglish.text = item?.english
tvEnglish.setOnClickListener {
listener(item!!.id)
}
}
}
}
@Database(entities = [Dictionary::class, History::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun dictionaryDao(): DictionaryDao
companion object {
@Volatile
private var INSTANCE: AppDatabase? = null
fun getDatabase(context: Context): AppDatabase {
return INSTANCE ?: synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext,
AppDatabase::class.java,
"dictionary_database.db"
)
.createFromAsset("database/dictionary.db")
.build()
INSTANCE = instance
instance
}
}
}
}
class DBViewModel(application: Application) : AndroidViewModel(application) {
private val dictionaryDao =
AppDatabase.getDatabase(application.applicationContext).dictionaryDao()
fun getFavorites(): Flow
> {
return Pager(PagingConfig(pageSize = 50, enablePlaceholders = true, maxSize = 200)) {
dictionaryDao.getFavorites()
}.flow
}
fun update(id: Int, isFavorite: Int) = viewModelScope.launch(Dispatchers.IO) {
dictionaryDao.update(id, isFavorite)
}
fun getBySearchQuery(searchQuery: String): Flow
> {
return Pager(PagingConfig(pageSize = 50, enablePlaceholders = true, maxSize = 200)) {
dictionaryDao.searchDatabase(searchQuery)
}.flow
}
fun getById(id: Int): LiveData = dictionaryDao.getById(id)
fun getHistory(): Flow
> {
return Pager(PagingConfig(pageSize = 50, enablePlaceholders = true, maxSize = 200)) {
dictionaryDao.getHistory()
}.flow
}
fun addHistory(history: History) = viewModelScope.launch(Dispatchers.IO) {
dictionaryDao.addHistory(history)
}
}
class FavoritesFragment : Fragment() {
private var _binding: FragmentFavoritesBinding? = null
private val binding get() = _binding!!
lateinit var viewModel: DBViewModel
private var adapter: MainAdapter? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentFavoritesBinding.inflate(layoutInflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel = ViewModelProvider(
this, ViewModelProvider.AndroidViewModelFactory.getInstance(application = requireActivity().application)
)[DBViewModel::class.java]
initRV()
}
private fun initRV() {
adapter = MainAdapter { id->
val bundle = Bundle()
bundle.putInt("myId", id)
findNavController().navigate(R.id.action_favoritesFragment_to_dictionaryFragment, bundle)
}
binding.rvFavorites.adapter = adapter
lifecycleScope.launch {
viewModel.getFavorites().collectLatest {
adapter?.submitData(it)
}
}
}
}
class HistoryFragment : Fragment() {
private var _binding: FragmentHistoryBinding? = null
private val binding get() = _binding!!
lateinit var viewModel: DBViewModel
private var adapter: MainAdapter? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentHistoryBinding.inflate(layoutInflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel = ViewModelProvider(
this,
ViewModelProvider.AndroidViewModelFactory.getInstance(application = requireActivity().application)
)[DBViewModel::class.java]
initRV()
}
private fun initRV() {
adapter = MainAdapter { id->
val bundle = Bundle()
bundle.putInt("myId", id)
findNavController().navigate(R.id.action_historyFragment_to_dictionaryFragment, bundle)
}
binding.rvHistory.adapter = adapter
lifecycleScope.launch {
viewModel.getHistory().collectLatest {
adapter?.submitData(it)
}
}
}
}
class HomeFragment : Fragment() {
private var _binding: FragmentHomeBinding? = null
private val binding get() = _binding!!
lateinit var viewModel: DBViewModel
private var adapter: MainAdapter? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View {
_binding = FragmentHomeBinding.inflate(layoutInflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel = ViewModelProvider(
this,
ViewModelProvider.AndroidViewModelFactory.getInstance(application = requireActivity().application)
)[DBViewModel::class.java]
initRV()
initSearchView()
}
private fun initRV() {
adapter = MainAdapter { id->
val bundle = Bundle()
bundle.putInt("myId", id)
findNavController().navigate(R.id.action_homeFragment_to_dictionaryFragment, bundle)
viewModel.addHistory(History(dictionaryId = id))
}
binding.rvDictionary.adapter = adapter
lifecycleScope.launch {
viewModel.getBySearchQuery("").collectLatest {
adapter?.submitData(it)
}
}
}
private fun initSearchView() {
binding.etSearch.doOnTextChanged { text, start, before, count ->
lifecycleScope.launch {
viewModel.getBySearchQuery(text.toString()).collectLatest {
adapter?.submitData(it)
}
}
}
}
}
class InfoFragment : Fragment() {
private var _binding: FragmentInfoBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentInfoBinding.inflate(layoutInflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.tvAbout.movementMethod = LinkMovementMethod.getInstance()
binding.tvLink.movementMethod = LinkMovementMethod.getInstance()
}

}
class DictionaryFragment : Fragment() {


private var _binding: FragmentDictionaryBinding? = null
private val binding get() = _binding!!
lateinit var viewModel: DBViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentDictionaryBinding.inflate(layoutInflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel = ViewModelProvider(
this,
ViewModelProvider.AndroidViewModelFactory.getInstance(application = requireActivity().application)
)[DBViewModel::class.java]
initBack()
initTranslate()
}
private fun initBack() {
binding.ivBack.setOnClickListener {
findNavController().popBackStack()
}
}
private fun initTranslate() {
val itemId = arguments?.getInt("myId", 1)
viewModel.getById(itemId!!).observe(viewLifecycleOwner, Observer { list ->
list?.let {
binding.tvWord.text = it.english
binding.tvTranslate.text = "${it.countable} ${it.uzbek}"
binding.tvTranscript.text = it.transcript
binding.tvType.text = it.type
binding.ivFavorite.isSelected = it.isFavorite != 0
}
})
binding.ivFavorite.setOnClickListener {
it.isSelected = !it.isSelected
val isFavorite = if (it.isSelected) 1 else 0
viewModel.update(itemId, isFavorite)
}
}
}
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val navController = findNavController(R.id.nav_fragment)
binding.bottomNavigation.setupWithNavController(navController)
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.homeFragment, R.id.historyFragment, R.id.favoritesFragment, R.id.infoFragment
)
)
navController.addOnDestinationChangedListener { _, destination, _ ->
if (appBarConfiguration.topLevelDestinations.contains(destination.id)) {
showBottomNavigationView(binding.bottomNavigation)
} else {
hideBottomNavigationView(binding.bottomNavigation)
}
}
}
private fun hideBottomNavigationView(view: BottomNavigationView) {
view.clearAnimation()
view.animate().translationY(view.height.toFloat()).duration = 300
}
private fun showBottomNavigationView(view: BottomNavigationView) {
view.clearAnimation()
view.animate().translationY(0f).duration = 300
}
}




Download 1.9 Mb.

Do'stlaringiz bilan baham:
1   ...   10   11   12   13   14   15   16   17   18




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling