Skip to content

Commit

Permalink
add: ui
Browse files Browse the repository at this point in the history
  • Loading branch information
amirisback committed Jan 21, 2024
1 parent e237371 commit 09be3e8
Show file tree
Hide file tree
Showing 16 changed files with 494 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ abstract class BaseActivity<VB : ViewBinding> : AppCompatActivity() {

abstract fun setupViewBinding(): VB

abstract fun initUI(savedInstanceState: Bundle?)

abstract fun initObserver()
abstract fun initUI()

protected val binding by lazy { setupViewBinding() }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
initUI()
initUI(savedInstanceState)
initObserver()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.qomunal.opensource.androidresearch.common.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding

/**
* Created by faisalamircs on 21/01/2024
* -----------------------------------------
* Name : Muhammad Faisal Amir
* E-mail : [email protected]
* Github : github.com/amirisback
* -----------------------------------------
*/


abstract class BaseFragment<VB : ViewBinding> : Fragment() {

companion object {
val TAG: String = BaseFragment::class.java.simpleName
}

private var _binding: VB? = null

protected val binding: VB get() = _binding!!

abstract fun setupViewBinding(inflater: LayoutInflater, container: ViewGroup?): VB

abstract fun initUI(savedInstanceState: Bundle?)

abstract fun initObserver()

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = setupViewBinding(inflater, container)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initUI(savedInstanceState)
initObserver()
}

override fun onDestroy() {
super.onDestroy()
_binding = null
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ import com.google.gson.reflect.TypeToken

inline fun <reified T> fromJson(json: String?): T {
return Gson().fromJson(json, object : TypeToken<T>() {}.type)
}

inline fun <reified T> T.toJson(): String {
return Gson().toJson(this)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.qomunal.opensource.androidresearch.common.ext

import android.net.Uri
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.qomunal.opensource.androidresearch.R

/**
* Created by faisalamircs on 21/01/2024
* -----------------------------------------
* Name : Muhammad Faisal Amir
* E-mail : [email protected]
* Github : github.com/amirisback
* -----------------------------------------
*/


fun ImageView.setImageExt(
uri: Any? = null,
placeHolder: Int = R.drawable.ic_launcher_background,
isCenterCrop: Boolean = false
) {
if (uri != null) {
when (uri) {
is String,
is Int,
is Uri,
is ByteArray -> {
if (isCenterCrop) {
Glide.with(context)
.load(uri)
.placeholder(placeHolder)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.centerCrop()
.into(this)
} else {
Glide.with(context)
.load(uri)
.placeholder(placeHolder)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(this)
}

}
}
} else {
Glide.with(context)
.load("")
.placeholder(placeHolder)
.into(this)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.qomunal.opensource.androidresearch.model

import android.os.Parcelable
import androidx.annotation.Keep
import kotlinx.parcelize.Parcelize

/**
* Created by faisalamircs on 21/01/2024
* -----------------------------------------
* Name : Muhammad Faisal Amir
* E-mail : [email protected]
* Github : github.com/amirisback
* -----------------------------------------
*/


@Keep
@Parcelize
data class MainModel(
var title: String,
var description: String,
var image: String,
var data: String,
) : Parcelable

Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,8 @@ class DetailActivity : BaseActivity<ActivityDetailBinding>() {
return ActivityDetailBinding.inflate(layoutInflater)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}

override fun initUI() {
override fun initUI(savedInstanceState: Bundle?) {
binding.apply {

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.qomunal.opensource.androidresearch.ui.main

import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import com.qomunal.opensource.androidresearch.R
import com.qomunal.opensource.androidresearch.common.base.BaseActivity
import com.qomunal.opensource.androidresearch.databinding.ActivityMainBinding
import com.qomunal.opensource.androidresearch.domain.Resource
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
Expand All @@ -20,66 +22,47 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
return ActivityMainBinding.inflate(layoutInflater)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
override fun initUI(savedInstanceState: Bundle?) {
if (savedInstanceState == null) {
viewModel.getNewsArticle()
viewModel.getMeals()
replaceFragment(MainFragment.newInstance(MainMenuType.NEWS))
}
}

override fun initUI() {
binding.apply {
bottomNav.setOnItemSelectedListener {
when (it.itemId) {

}
}

override fun initObserver() {
viewModel.apply {
newsArticlesState.observe(this@MainActivity) {
when (it) {
is Resource.Error -> {
Log.d("MainActivity", "Error")
R.id.nv_news -> {
replaceFragment(MainFragment.newInstance(MainMenuType.NEWS))
return@setOnItemSelectedListener true
}

is Resource.Loading -> {
Log.d("MainActivity", "Loading")
R.id.nv_meal -> {
replaceFragment(MainFragment.newInstance(MainMenuType.MEAL))
return@setOnItemSelectedListener true
}

is Resource.Success -> {
Log.d("MainActivity", "Success")
Log.d("MainActivity", it.data.toString())
}
}
return@setOnItemSelectedListener false
}

mealState.observe(this@MainActivity) {
when (it) {
is Resource.Error -> {
Log.d("MainActivity", "Error")
}

is Resource.Loading -> {
Log.d("MainActivity", "Loading")
}

is Resource.Success -> {
Log.d("MainActivity", "Success")
Log.d("MainActivity", it.data.toString())

it.data?.forEach { meal ->
}
}

Log.d("Meal Name", meal.strMeal)
override fun initObserver() {
viewModel.apply {

meal.strIngredient.forEach { ingredient ->
Log.d("Meal Ingredient", ingredient)
}
}
}

Log.d("Meal Space", "------------------------------")
}
}
}
}
private fun replaceFragment(fragment: Fragment) {
val backStateName: String = fragment::class.java.name
val manager: FragmentManager = supportFragmentManager
val fragmentPopped: Boolean = manager.popBackStackImmediate(backStateName, 0)
if (!fragmentPopped) { //fragment not in back stack, create it.
val ft: FragmentTransaction = manager.beginTransaction()
ft.replace(binding.fragmentMain.id, fragment)
ft.addToBackStack(backStateName)
ft.commit()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.qomunal.opensource.androidresearch.ui.main

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.qomunal.opensource.androidresearch.databinding.ItemMainBinding
import com.qomunal.opensource.androidresearch.model.MainModel

/**
* Created by faisalamircs on 21/01/2024
* -----------------------------------------
* Name : Muhammad Faisal Amir
* E-mail : [email protected]
* Github : github.com/amirisback
* -----------------------------------------
*/


class MainAdapter : RecyclerView.Adapter<MainHolder>() {

var listItems = mutableListOf<MainModel>()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainHolder {
val binding = ItemMainBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return MainHolder(binding)
}

override fun getItemCount(): Int {
return listItems.size
}

override fun onBindViewHolder(holder: MainHolder, position: Int) {
holder.bind(listItems[position])
}

fun setItem(item: MutableList<MainModel>) {
listItems = item
notifyDataSetChanged()
}

}
Loading

0 comments on commit 09be3e8

Please sign in to comment.