Skip to content

Commit

Permalink
✨ Finally figure out what was going on with bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
sduduzog committed Mar 16, 2022
1 parent fca3a9d commit f36cd23
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 98 deletions.
21 changes: 10 additions & 11 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ plugins {
id("com.android.application")
id("dagger.hilt.android.plugin")
kotlin("android")
kotlin("android.extensions")
kotlin("kapt")
}

Expand All @@ -27,6 +26,10 @@ android {
}
}

buildFeatures {
viewBinding = true
}

buildTypes {
named("release").configure {
isMinifyEnabled = true
Expand Down Expand Up @@ -67,13 +70,13 @@ dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.5.21")

// Support Libraries
implementation("androidx.appcompat:appcompat:1.3.1")
implementation("androidx.appcompat:appcompat:1.4.1")
implementation("androidx.recyclerview:recyclerview:1.2.1")
implementation("androidx.constraintlayout:constraintlayout:2.1.0")

// Arch Components
implementation("androidx.core:core-ktx:1.7.0-beta01")
implementation("androidx.fragment:fragment-ktx:1.3.6")
implementation("androidx.fragment:fragment-ktx:1.4.1")
implementation("androidx.lifecycle:lifecycle-extensions:2.2.0")
implementation("androidx.navigation:navigation-fragment-ktx:2.3.5")
implementation("androidx.room:room-runtime:2.3.0")
Expand All @@ -83,10 +86,8 @@ dependencies {
//3rd party libs
implementation("com.intuit.sdp:sdp-android:1.0.6")
implementation("com.intuit.ssp:ssp-android:1.0.6")
implementation("com.google.dagger:hilt-android:2.38.1")
// implementation("androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03")
kapt("androidx.hilt:hilt-compiler:1.0.0")
kapt("com.google.dagger:hilt-android-compiler:2.38.1")
implementation("com.google.dagger:hilt-android:2.41")
kapt("com.google.dagger:hilt-compiler:2.41")


// Unit test libs
Expand All @@ -96,12 +97,10 @@ dependencies {
testImplementation("org.robolectric:robolectric:4.4")

testImplementation("androidx.arch.core:core-testing:2.1.0")
// testImplementation("androidx.test:core:1.4.0")
// testImplementation("org.mockito:mockito-core:2.24.5")


testImplementation("com.google.dagger:hilt-android-testing:2.35")
kaptTest("com.google.dagger:hilt-android-compiler:2.38.1")
testImplementation("com.google.dagger:hilt-android-testing:2.41")
kaptTest("com.google.dagger:hilt-android-compiler:2.41")

androidTestImplementation("androidx.test:runner:1.4.0")
androidTestImplementation ("androidx.test.ext:junit:1.1.3")
Expand Down
3 changes: 3 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

-keepnames class androidx.navigation.fragment.NavHostFragment
-keep class * extends androidx.fragment.app.Fragment{}
5 changes: 4 additions & 1 deletion app/src/main/java/com/sduduzog/slimlauncher/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.NavController
import androidx.navigation.Navigation.findNavController
import com.sduduzog.slimlauncher.databinding.MainActivityBinding
import com.sduduzog.slimlauncher.utils.BaseFragment
import com.sduduzog.slimlauncher.utils.HomeWatcher
import com.sduduzog.slimlauncher.utils.IPublisher
Expand All @@ -22,6 +23,7 @@ class MainActivity : AppCompatActivity(),
private lateinit var settings: SharedPreferences
private lateinit var navigator: NavController
private lateinit var homeWatcher: HomeWatcher
private lateinit var binding: MainActivityBinding
private val subscribers: MutableSet<BaseFragment> = mutableSetOf()

override fun attachSubscriber(s: ISubscriber) {
Expand All @@ -43,7 +45,8 @@ class MainActivity : AppCompatActivity(),

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
binding = MainActivityBinding.inflate(layoutInflater)
setContentView(binding.root)
settings = getSharedPreferences(getString(R.string.prefs_settings), MODE_PRIVATE)
settings.registerOnSharedPreferenceChangeListener(this)
navigator = findNavController(this, R.id.nav_host_fragment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@ package com.sduduzog.slimlauncher.ui.dialogs

import android.app.Dialog
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
import com.sduduzog.slimlauncher.R
import com.sduduzog.slimlauncher.models.HomeApp
import com.sduduzog.slimlauncher.databinding.RenameDialogEditTextBinding
import com.sduduzog.slimlauncher.models.CustomiseAppsViewModel
import kotlinx.android.synthetic.main.customise_apps_fragment.*
import com.sduduzog.slimlauncher.models.HomeApp

class RenameAppDialog : DialogFragment() {

private var _binding: RenameDialogEditTextBinding? = null
private val binding get() = _binding
private lateinit var app: HomeApp
private lateinit var model: CustomiseAppsViewModel

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val view = LayoutInflater.from(context).inflate(R.layout.rename_dialog_edit_text, customise_apps_fragment, false)
val editText: EditText = view.findViewById(R.id.rename_editText)
_binding = RenameDialogEditTextBinding.inflate(layoutInflater)
val view = binding!!.root
val editText: EditText = binding!!.renameEditText
val appName: String = app.appNickname ?: app.appName
editText.text.append(appName)
val builder = AlertDialog.Builder(requireContext())
Expand All @@ -38,12 +39,18 @@ class RenameAppDialog : DialogFragment() {
return builder.create()
}

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

private fun updateApp(newName: String) {
if (newName.isNotEmpty()) {
app.appNickname = newName
model.update(app)
} else {
Toast.makeText(context, "Couldn't save, App name shouldn't be empty", Toast.LENGTH_LONG).show()
Toast.makeText(context, "Couldn't save, App name shouldn't be empty", Toast.LENGTH_LONG)
.show()
}
}

Expand Down
52 changes: 27 additions & 25 deletions app/src/main/java/com/sduduzog/slimlauncher/ui/main/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,37 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.navigation.Navigation
import com.sduduzog.slimlauncher.R
import com.sduduzog.slimlauncher.adapters.HomeAdapter
import com.sduduzog.slimlauncher.databinding.HomeFragmentBinding
import com.sduduzog.slimlauncher.models.HomeApp
import com.sduduzog.slimlauncher.models.MainViewModel
import com.sduduzog.slimlauncher.utils.BaseFragment
import com.sduduzog.slimlauncher.utils.OnLaunchAppListener
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.android.synthetic.main.home_fragment.*
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.*

@AndroidEntryPoint
class HomeFragment : BaseFragment(), OnLaunchAppListener {

private var _binding: HomeFragmentBinding? = null
private val binding get() = _binding
private val viewModel: MainViewModel by viewModels()

private lateinit var receiver: BroadcastReceiver

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View {
return inflater.inflate(R.layout.home_fragment, container, false)
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = HomeFragmentBinding.inflate(inflater, container, false)
val adapter1 = HomeAdapter(this)
val adapter2 = HomeAdapter(this)
home_fragment_list.adapter = adapter1
home_fragment_list_exp.adapter = adapter2
binding!!.homeFragmentList.adapter = adapter1
binding!!.homeFragmentListExp.adapter = adapter2

viewModel.apps.observe(viewLifecycleOwner) { list ->
list?.let { apps ->
Expand All @@ -54,9 +53,10 @@ class HomeFragment : BaseFragment(), OnLaunchAppListener {
})
}
}

//
setEventListeners()
home_fragment_options.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_homeFragment_to_optionsFragment))
binding!!.homeFragmentOptions.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_homeFragment_to_optionsFragment))
return binding?.root
}

override fun onStart() {
Expand All @@ -65,7 +65,7 @@ class HomeFragment : BaseFragment(), OnLaunchAppListener {
activity?.registerReceiver(receiver, IntentFilter(Intent.ACTION_TIME_TICK))
}

override fun getFragmentView(): ViewGroup = home_fragment
override fun getFragmentView(): ViewGroup = binding!!.root

override fun onResume() {
super.onResume()
Expand All @@ -79,7 +79,7 @@ class HomeFragment : BaseFragment(), OnLaunchAppListener {

private fun setEventListeners() {

home_fragment_time.setOnClickListener {
binding!!.homeFragmentTime.setOnClickListener {
try {
val intent = Intent(AlarmClock.ACTION_SHOW_ALARMS)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
Expand All @@ -90,7 +90,7 @@ class HomeFragment : BaseFragment(), OnLaunchAppListener {
}
}

home_fragment_date.setOnClickListener {
binding!!.homeFragmentDate.setOnClickListener {
try {
val builder = CalendarContract.CONTENT_URI.buildUpon().appendPath("time")
val intent = Intent(Intent.ACTION_VIEW, builder.build())
Expand All @@ -101,7 +101,7 @@ class HomeFragment : BaseFragment(), OnLaunchAppListener {
}
}

home_fragment_call.setOnClickListener { view ->
binding!!.homeFragmentCall .setOnClickListener { view ->
try {
val pm = context?.packageManager!!
val intent = Intent(Intent.ACTION_DIAL)
Expand All @@ -115,7 +115,7 @@ class HomeFragment : BaseFragment(), OnLaunchAppListener {
}
}

home_fragment_camera.setOnClickListener {
binding!!.homeFragmentCamera .setOnClickListener {
try {
val intent = Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA)
launchActivity(it, intent)
Expand All @@ -126,26 +126,28 @@ class HomeFragment : BaseFragment(), OnLaunchAppListener {
}

fun updateClock() {
val active = context?.getSharedPreferences(getString(R.string.prefs_settings), Context.MODE_PRIVATE)
val active =
context?.getSharedPreferences(getString(R.string.prefs_settings), Context.MODE_PRIVATE)
?.getInt(getString(R.string.prefs_settings_key_time_format), 0)
val date = Date()

val fWatchTime = when(active) {
val fWatchTime = when (active) {
1 -> SimpleDateFormat("H:mm", Locale.ROOT)
2 -> SimpleDateFormat("h:mm aa", Locale.ROOT)
else -> DateFormat.getTimeInstance(DateFormat.SHORT)
}
home_fragment_time.text = fWatchTime.format(date)
binding!!.homeFragmentTime .text = fWatchTime.format(date)


val fWatchDate = SimpleDateFormat("EEE, MMM dd", Locale.ROOT)
home_fragment_date.text = fWatchDate.format(date)
binding!!.homeFragmentDate .text = fWatchDate.format(date)
}

override fun onLaunch(app: HomeApp, view: View) {
try {
val manager = requireContext().getSystemService(Context.USER_SERVICE) as UserManager
val launcher = requireContext().getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
val launcher =
requireContext().getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps

val componentName = ComponentName(app.packageName, app.activityName)
val userHandle = manager.getUserForSerialNumber(app.userSerial)
Expand All @@ -157,12 +159,12 @@ class HomeFragment : BaseFragment(), OnLaunchAppListener {
}

override fun onBack(): Boolean {
home_fragment.transitionToStart()
binding!!.root.transitionToStart()
return true
}

override fun onHome() {
home_fragment.transitionToEnd()
binding!!.root.transitionToEnd()
}

inner class ClockReceiver : BroadcastReceiver() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,53 @@ import com.sduduzog.slimlauncher.BuildConfig
import com.sduduzog.slimlauncher.R
import com.sduduzog.slimlauncher.adapters.AddAppAdapter
import com.sduduzog.slimlauncher.data.model.App
import com.sduduzog.slimlauncher.databinding.AddAppFragmentBinding
import com.sduduzog.slimlauncher.models.AddAppViewModel
import com.sduduzog.slimlauncher.utils.BaseFragment
import com.sduduzog.slimlauncher.utils.OnAppClickedListener
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.android.synthetic.main.add_app_fragment.*

@AndroidEntryPoint
class AddAppFragment : BaseFragment(), OnAppClickedListener {

override fun getFragmentView(): ViewGroup = add_app_fragment
private var _binding: AddAppFragmentBinding? = null
private val binding get() = _binding
override fun getFragmentView(): ViewGroup = binding!!.root

private val viewModel: AddAppViewModel by viewModels()

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.add_app_fragment, container, false)
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
_binding = AddAppFragmentBinding.inflate(inflater, container, false)
val adapter = AddAppAdapter(this)

add_app_fragment_list.adapter = adapter
binding!!.addAppFragmentList.adapter = adapter

viewModel.apps.observe(viewLifecycleOwner) {
it?.let { apps ->
adapter.setItems(apps)
add_app_fragment_progress_bar.visibility = View.GONE
binding!!.addAppFragmentProgressBar.visibility = View.GONE
} ?: run {
add_app_fragment_progress_bar.visibility = View.VISIBLE
binding!!.addAppFragmentProgressBar.visibility = View.VISIBLE
}
}
return binding?.root
}

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

override fun onResume() {
super.onResume()
viewModel.setInstalledApps(getInstalledApps())
viewModel.filterApps("")
add_app_fragment_edit_text.addTextChangedListener(onTextChangeListener)
binding!!.addAppFragmentEditText.addTextChangedListener(onTextChangeListener)
}

override fun onPause() {
super.onPause()
add_app_fragment_edit_text?.removeTextChangedListener(onTextChangeListener)
binding!!.addAppFragmentEditText.removeTextChangedListener(onTextChangeListener)
}

private val onTextChangeListener: TextWatcher = object : TextWatcher {
Expand All @@ -78,7 +82,7 @@ class AddAppFragment : BaseFragment(), OnAppClickedListener {

override fun onAppClicked(app: App) {
viewModel.addAppToHomeScreen(app)
Navigation.findNavController(add_app_fragment).popBackStack()
Navigation.findNavController(binding!!.root).popBackStack()
}

private fun getInstalledApps(): List<App> {
Expand Down
Loading

0 comments on commit f36cd23

Please sign in to comment.