Skip to content

Commit

Permalink
Refactor: Refactor some of the setting to also work other places.
Browse files Browse the repository at this point in the history
  • Loading branch information
CreativeCodeCat committed May 20, 2024
1 parent 7d04c20 commit 72f212e
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,14 @@ object Constants {

const val TOGGLE_SETTING_LOCK = "TOGGLE_SETTING_LOCK"

const val URL_DUCK_SEARCH = "https://duckduckgo.com/?q="
const val URL_GOOGLE_SEARCH = "https://google.com/search?q="
const val URL_YAHOO_SEARCH = "https://search.yahoo.com/search?p="
const val URL_BING_SEARCH = "https://bing.com/search?q="
const val URL_BRAVE_SEARCH = "https://search.brave.com/search?q="
const val URL_SWISSCOW_SEARCH = "https://swisscows.com/web?query="
const val URL_GOOGLE_PLAY_STORE = "https://play.google.com/store/search?c=apps&q"
const val APP_GOOGLE_PLAY_STORE = "market://search?c=apps&q"

const val REQUEST_CODE_ENABLE_ADMIN = 123
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.github.droidworksstudio.launcher.helper

import android.app.SearchManager
import android.content.Context
import android.content.Intent
import android.content.pm.LauncherApps
import android.net.Uri
import android.os.UserHandle
import android.util.Log
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import com.github.droidworksstudio.launcher.Constants

fun View.hideKeyboard() {
this.clearFocus()
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(windowToken, 0)
}

fun View.showKeyboard(show: Boolean = true) {
if (show.not()) return
if (this.requestFocus())
this.postDelayed({
this.findViewById<EditText>(androidx.appcompat.R.id.search_src_text).apply {
textSize = 28f
isCursorVisible = false
}
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
@Suppress("DEPRECATION")
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY)
}, 100)
}

fun Context.openSearch(query: String? = null) {
val intent = Intent(Intent.ACTION_WEB_SEARCH)
intent.putExtra(SearchManager.QUERY, query ?: "")
startActivity(intent)
}

fun Context.openUrl(url: String) {
if (url.isEmpty()) return
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse(url)
startActivity(intent)
}

fun Context.searchOnPlayStore(query: String? = null): Boolean {
return try {
val playStoreIntent = Intent(Intent.ACTION_VIEW)
playStoreIntent.data = Uri.parse("${Constants.APP_GOOGLE_PLAY_STORE}=$query")

// Check if the Play Store app is installed
if (playStoreIntent.resolveActivity(packageManager) != null) {
startActivity(playStoreIntent)
} else {
// If Play Store app is not installed, open Play Store website in browser
playStoreIntent.data = Uri.parse("${Constants.URL_GOOGLE_PLAY_STORE}=$query")
startActivity(playStoreIntent)
}
true
} catch (e: Exception) {
e.printStackTrace()
false
}
}

fun Context.searchCustomSearchEngine(searchQuery: String? = null): Boolean {
val searchUrl = Constants.URL_GOOGLE_SEARCH
val encodedQuery = Uri.encode(searchQuery)
val fullUrl = "$searchUrl$encodedQuery"
Log.d("fullUrl", fullUrl)
openUrl(fullUrl)
return true
}

fun Context.isPackageInstalled(packageName: String, userHandle: UserHandle = android.os.Process.myUserHandle()): Boolean {
val launcher = getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
val activityInfo = launcher.getActivityList(packageName, userHandle)
return activityInfo.size > 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -261,19 +261,6 @@ class AppHelper @Inject constructor() {
return false
}

fun showSoftKeyboard(context: Context, view: View) {
if (view.requestFocus()) {
val inputMethodManager: InputMethodManager =
context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
}
}

fun hideKeyboard(context: Context, view: View) {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view.windowToken, 0)
}

fun wordOfTheDay(resources: Resources): String {
val dailyWordsArray = resources.getStringArray(R.array.settings_appearance_daily_word_default)
val dayOfYear = Calendar.getInstance().get(Calendar.DAY_OF_YEAR)
Expand Down Expand Up @@ -329,5 +316,4 @@ class AppHelper @Inject constructor() {
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.github.droidworksstudio.launcher.data.entities.AppInfo
import com.github.droidworksstudio.launcher.databinding.ItemDrawBinding
import com.github.droidworksstudio.launcher.helper.PreferenceHelper
import com.github.droidworksstudio.launcher.listener.OnItemClickedListener

class DrawAdapter(private val onAppClickedListener: OnItemClickedListener.OnAppsClickedListener,
private val onAppLongClickedListener: OnItemClickedListener.OnAppLongClickedListener) :
private val onAppLongClickedListener: OnItemClickedListener.OnAppLongClickedListener,
private val preferenceHelperProvider: PreferenceHelper
) :
ListAdapter<AppInfo, RecyclerView.ViewHolder>(DiffCallback()) {

override fun onCreateViewHolder(
Expand All @@ -22,7 +25,9 @@ class DrawAdapter(private val onAppClickedListener: OnItemClickedListener.OnApps
parent,
false
)
return DrawViewHolder(binding, onAppClickedListener, onAppLongClickedListener)
val preferenceHelper = preferenceHelperProvider

return DrawViewHolder(binding, onAppClickedListener, onAppLongClickedListener, preferenceHelper)
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.github.droidworksstudio.launcher.ui.drawer

import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.appcompat.widget.SearchView
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.coroutineScope
Expand All @@ -20,6 +20,11 @@ import com.github.droidworksstudio.launcher.databinding.FragmentDrawBinding
import com.github.droidworksstudio.launcher.helper.AppHelper
import com.github.droidworksstudio.launcher.helper.FingerprintHelper
import com.github.droidworksstudio.launcher.helper.PreferenceHelper
import com.github.droidworksstudio.launcher.helper.hideKeyboard
import com.github.droidworksstudio.launcher.helper.openSearch
import com.github.droidworksstudio.launcher.helper.searchCustomSearchEngine
import com.github.droidworksstudio.launcher.helper.searchOnPlayStore
import com.github.droidworksstudio.launcher.helper.showKeyboard
import com.github.droidworksstudio.launcher.listener.OnItemClickedListener
import com.github.droidworksstudio.launcher.ui.bottomsheetdialog.AppInfoBottomSheetFragment
import com.github.droidworksstudio.launcher.viewmodel.AppViewModel
Expand All @@ -40,18 +45,18 @@ class DrawFragment : Fragment(), OnItemClickedListener.OnAppsClickedListener,

private val binding get() = _binding!!

private val viewModel: AppViewModel by viewModels()

private val drawAdapter: DrawAdapter by lazy { DrawAdapter(this, this) }
@Inject
lateinit var preferenceHelper: PreferenceHelper

@Inject
lateinit var appHelper: AppHelper

@Inject
lateinit var fingerHelper: FingerprintHelper

@Inject
lateinit var preferenceHelper: PreferenceHelper
private val viewModel: AppViewModel by viewModels()

private val drawAdapter: DrawAdapter by lazy { DrawAdapter(this, this, preferenceHelper) }

private lateinit var context: Context
override fun onCreateView(
Expand Down Expand Up @@ -96,29 +101,41 @@ class DrawFragment : Fragment(), OnItemClickedListener.OnAppsClickedListener,
}
}

@SuppressLint("RestrictedApi")
private fun setupSearch() {
binding.searchViewText.addTextChangedListener(object: TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
// Do Nothing
}

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
searchApp(s.toString())
binding.searchViewText.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
query?.let {
val trimmedQuery = it.trim()
if (trimmedQuery.isNotEmpty()) {
if (trimmedQuery.startsWith("!")) {
val searchQuery = trimmedQuery.substringAfter("!")
requireContext().searchCustomSearchEngine(searchQuery)
} else {
if (!requireContext().searchOnPlayStore(trimmedQuery)) {
requireContext().openSearch(trimmedQuery)
}
return true // Exit the function
}
}
}
return true
}

override fun afterTextChanged(s: Editable?) {
// Do Nothing
override fun onQueryTextChange(newText: String?): Boolean {
searchApp(newText.toString())
return true
}
})

}

private fun observeClickListener(){
binding.drawSearchButton.setOnClickListener {
appHelper.showSoftKeyboard(context, binding.searchViewText)
binding.searchViewText.showKeyboard()
}
}

private fun searchApp(query: String?) {
private fun searchApp(query: String) {
val searchQuery = "%$query%"
@Suppress("DEPRECATION")
viewLifecycleOwner.lifecycle.coroutineScope.launchWhenCreated {
Expand All @@ -142,7 +159,7 @@ class DrawFragment : Fragment(), OnItemClickedListener.OnAppsClickedListener,
}

private fun showSelectedApp(appInfo: AppInfo) {
binding.searchViewText.text?.clear()
binding.searchViewText.setQuery("", false)

val bottomSheetFragment = AppInfoBottomSheetFragment(appInfo)
bottomSheetFragment.setOnBottomSheetDismissedListener(this)
Expand All @@ -157,19 +174,19 @@ class DrawFragment : Fragment(), OnItemClickedListener.OnAppsClickedListener,

override fun onPause() {
super.onPause()
binding.searchViewText.text?.clear()
binding.searchViewText.setQuery("", false)
}

override fun onResume() {
super.onResume()
observeDrawerApps()
binding.drawAdapter.scrollToPosition(0)
if (preferenceHelper.automaticKeyboard) appHelper.showSoftKeyboard(context, binding.searchViewText)
if (preferenceHelper.automaticKeyboard) binding.searchViewText.showKeyboard()
}

override fun onStop() {
super.onStop()
appHelper.hideKeyboard(context, binding.searchView)
binding.searchViewText.hideKeyboard()
}

override fun onAppClicked(appInfo: AppInfo) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
package com.github.droidworksstudio.launcher.ui.drawer

import android.util.Log
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.recyclerview.widget.RecyclerView
import com.github.droidworksstudio.launcher.data.entities.AppInfo
import com.github.droidworksstudio.launcher.databinding.ItemDrawBinding
import com.github.droidworksstudio.launcher.helper.PreferenceHelper
import com.github.droidworksstudio.launcher.listener.OnItemClickedListener

class DrawViewHolder(private val binding: ItemDrawBinding,
private val onAppClickedListener: OnItemClickedListener.OnAppsClickedListener,
private val onAppLongClickedListener: OnItemClickedListener.OnAppLongClickedListener) :
private val onAppLongClickedListener: OnItemClickedListener.OnAppLongClickedListener,
private val preferenceHelper: PreferenceHelper):
RecyclerView.ViewHolder(binding.root) {
fun bind(appInfo: AppInfo) {
binding.apply {
val layoutParams = LinearLayoutCompat.LayoutParams(
LinearLayoutCompat.LayoutParams.WRAP_CONTENT,
LinearLayoutCompat.LayoutParams.WRAP_CONTENT
).apply {
gravity = preferenceHelper.homeAppAlignment
topMargin = preferenceHelper.homeAppPadding.toInt()
bottomMargin = preferenceHelper.homeAppPadding.toInt()
}

appDrawName.layoutParams = layoutParams
appDrawName.text = appInfo.appName
appDrawName.setTextColor(preferenceHelper.appColor)
appDrawName.textSize = preferenceHelper.appTextSize
Log.d("Tag", "Draw Adapter: ${appInfo.appName + appInfo.id}")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.github.droidworksstudio.launcher.databinding.FragmentHomeBinding
import com.github.droidworksstudio.launcher.helper.AppHelper
import com.github.droidworksstudio.launcher.helper.FingerprintHelper
import com.github.droidworksstudio.launcher.helper.PreferenceHelper
import com.github.droidworksstudio.launcher.helper.hideKeyboard
import com.github.droidworksstudio.launcher.listener.OnItemClickedListener
import com.github.droidworksstudio.launcher.listener.OnSwipeTouchListener
import com.github.droidworksstudio.launcher.listener.ScrollEventListener
Expand Down Expand Up @@ -98,7 +99,7 @@ class HomeFragment : Fragment(), OnItemClickedListener.OnAppsClickedListener,
@SuppressLint("ClickableViewAccessibility")
private fun initializeInjectedDependencies() {
context = requireContext()
appHelper.hideKeyboard(context, binding.mainView)
binding.mainView.hideKeyboard()

binding.nestScrollView.scrollEventListener = this

Expand Down Expand Up @@ -158,7 +159,7 @@ class HomeFragment : Fragment(), OnItemClickedListener.OnAppsClickedListener,
}

private fun observeUserInterfaceSettings() {
appHelper.hideKeyboard(context, binding.mainView)
binding.mainView.hideKeyboard()

preferenceViewModel.setShowTime(preferenceHelper.showTime)
preferenceViewModel.setShowDate(preferenceHelper.showDate)
Expand Down Expand Up @@ -301,12 +302,12 @@ class HomeFragment : Fragment(), OnItemClickedListener.OnAppsClickedListener,

override fun onPause() {
super.onPause()
appHelper.hideKeyboard(context, binding.mainView)
binding.mainView.hideKeyboard()
}

override fun onResume() {
super.onResume()
appHelper.hideKeyboard(context, binding.mainView)
binding.mainView.hideKeyboard()
observeUserInterfaceSettings()
observeFavoriteAppList()
}
Expand Down
Loading

0 comments on commit 72f212e

Please sign in to comment.