Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] fixes #711 #725

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package com.looker.droidify.ui.appList

import android.annotation.SuppressLint
import android.content.Context
import android.view.ContextMenu
import android.view.Gravity
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.google.android.material.R as MaterialR
import com.google.android.material.imageview.ShapeableImageView
import com.google.android.material.progressindicator.CircularProgressIndicator
import com.looker.core.common.extension.authentication
Expand All @@ -27,10 +29,13 @@ import com.looker.droidify.database.Database
import com.looker.droidify.utility.extension.ImageUtils.icon
import com.looker.droidify.utility.extension.resources.TypefaceExtra
import com.looker.droidify.widget.CursorRecyclerAdapter
import com.google.android.material.R as MaterialR

class AppListAdapter(
private val source: AppListFragment.Source,
private val onClick: (ProductItem) -> Unit
private val favouriteApps: Set<String>,
private val onClick: (ProductItem) -> Unit,
private val onContextAction: (ProductItem, MenuItem) -> Boolean
) : CursorRecyclerAdapter<AppListAdapter.ViewType, RecyclerView.ViewHolder>() {

enum class ViewType { PRODUCT, LOADING, EMPTY }
Expand Down Expand Up @@ -122,6 +127,44 @@ class AppListAdapter(
return when (viewType) {
ViewType.PRODUCT -> ProductViewHolder(parent.inflate(R.layout.product_item)).apply {
itemView.setOnClickListener { onClick(getProductItem(absoluteAdapterPosition)) }

itemView.setOnCreateContextMenuListener(
fun(
menu: ContextMenu?,
_: View?,
_: ContextMenu.ContextMenuInfo?
) {
menu?.setHeaderTitle("Pick an Action");

val installItem = menu?.add(
Menu.NONE,
Menu.NONE,
Menu.NONE,
if (getProductItem(absoluteAdapterPosition).installedVersion.nullIfEmpty() != null)
com.looker.core.common.R.string.uninstall
else
com.looker.core.common.R.string.install
);

val favItem = menu?.add(
Menu.NONE,
Menu.NONE,
Menu.NONE,
if (favouriteApps.contains(getProductItem(absoluteAdapterPosition).packageName))
com.looker.core.common.R.string.remove_from_favourites
else
com.looker.core.common.R.string.add_to_favourites
);

val clickListener = MenuItem.OnMenuItemClickListener {
onContextAction(
getProductItem(absoluteAdapterPosition), it
)
}

installItem?.setOnMenuItemClickListener(clickListener)
favItem?.setOnMenuItemClickListener(clickListener)
})
}

ViewType.LOADING -> LoadingViewHolder(parent.context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,27 @@ import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.looker.core.common.R as CommonR
import com.looker.core.common.R.string as stringRes
import com.looker.core.common.PackageName
import com.looker.core.common.extension.dp
import com.looker.core.common.extension.getPackageInfoCompat
import com.looker.core.common.extension.isFirstItemVisible
import com.looker.core.common.extension.systemBarsMargin
import com.looker.core.common.extension.systemBarsPadding
import com.looker.core.domain.Product
import com.looker.core.domain.ProductItem
import com.looker.core.domain.Repository
import com.looker.droidify.database.CursorOwner
import com.looker.droidify.database.Database
import com.looker.droidify.databinding.RecyclerViewWithFabBinding
import com.looker.droidify.service.DownloadService.State.Idle.packageName
import com.looker.droidify.utility.InstallUtils
import com.looker.droidify.utility.extension.screenActivity
import com.looker.droidify.utility.extension.toInstalledItem
import com.looker.installer.InstallManager
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import com.looker.core.common.R as CommonR
import com.looker.core.common.R.string as stringRes

@AndroidEntryPoint
class AppListFragment() : Fragment(), CursorOwner.Callback {
Expand Down Expand Up @@ -82,9 +91,65 @@ class AppListFragment() : Fragment(), CursorOwner.Callback {
isMotionEventSplittingEnabled = false
setHasFixedSize(true)
recycledViewPool.setMaxRecycledViews(AppListAdapter.ViewType.PRODUCT.ordinal, 30)
recyclerViewAdapter = AppListAdapter(source) {
screenActivity.navigateProduct(it.packageName)

var favouriteApps: Set<String> = emptySet()

lifecycleScope.launch {
favouriteApps = viewModel.settingsRepository.getInitial().favouriteApps
}

recyclerViewAdapter = AppListAdapter(
source,
favouriteApps,
{ screenActivity.navigateProduct(it.packageName) },
fun(productItem, menuItem): Boolean {

when (menuItem.title) {

getString(com.looker.core.common.R.string.install) -> lifecycleScope.launch {
VishnuSanal marked this conversation as resolved.
Show resolved Hide resolved

var products: List<Pair<Product, Repository>> = emptyList()

val packageInfo =
context.packageManager.getPackageInfoCompat(productItem.packageName)

if (packageInfo == null)
VishnuSanal marked this conversation as resolved.
Show resolved Hide resolved
return@launch

Database.ProductAdapter.getStream(packageName).collect {
val repository = Database.RepositoryAdapter.get(it[0].repositoryId)

if (repository != null)
products = listOf(Pair(it[0], repository))
}

/*InstallUtils.install(
context,
viewModel.settingsRepository,
lifecycleScope,
packageInfo.toInstalledItem(),
products
)*/
}

getString(com.looker.core.common.R.string.uninstall) -> lifecycleScope.launch {
InstallManager(context, viewModel.settingsRepository)
.uninstall(
PackageName(productItem.packageName)
)
}

else -> {
lifecycleScope.launch {
viewModel.settingsRepository.toggleFavourites(
productItem.packageName
)
}
}
}
return true
}
)
adapter = recyclerViewAdapter
systemBarsPadding()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import com.looker.droidify.database.Database
import com.looker.droidify.service.Connection
import com.looker.droidify.service.SyncService
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class AppListViewModel
Expand All @@ -37,6 +37,8 @@ class AppListViewModel
val sortOrderFlow = settingsRepository.get { sortOrder }
.asStateFlow(SortOrder.UPDATED)

val settingsRepository = settingsRepository
VishnuSanal marked this conversation as resolved.
Show resolved Hide resolved

private val sections = MutableStateFlow<ProductItem.Section>(All)

val searchQuery = MutableStateFlow("")
Expand Down
50 changes: 50 additions & 0 deletions app/src/main/kotlin/com/looker/droidify/utility/InstallUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.looker.droidify.utility

import android.content.Context
import com.looker.core.common.PackageName
import com.looker.core.datastore.SettingsRepository
import com.looker.core.domain.InstalledItem
import com.looker.core.domain.Product
import com.looker.core.domain.Repository
import com.looker.droidify.service.Connection
import com.looker.droidify.service.DownloadService
import com.looker.droidify.utility.extension.startUpdate
import com.looker.installer.InstallManager
import com.looker.installer.model.InstallItem
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

class InstallUtils {

companion object {
fun install(
context: Context,
settingsRepository: SettingsRepository,
scope: CoroutineScope,
installedItem: InstalledItem,
products: List<Pair<Product, Repository>>
) {
Connection(
serviceClass = DownloadService::class.java,
onBind = { _, binder ->
scope.launch {

val state = binder.downloadState.value

if (state.currentItem is DownloadService.State.Success) {

InstallManager(context, settingsRepository)
.install(
InstallItem(
PackageName(state.currentItem.packageName),
state.currentItem.release.cacheFileName
)
)
}
}
}
).startUpdate(installedItem.packageName, installedItem, products)
}
}

}
2 changes: 2 additions & 0 deletions core/common/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
<string name="export_repos_title">Export Repositories</string>
<string name="export_repos_DESC">Export all repositories to file</string>
<string name="enable_repo">Enable the repository</string>
<string name="add_to_favourites">Add to Favourites</string>
<string name="remove_from_favourites">Remove from Favourites</string>
<string name="favourites">Favourites</string>
<string name="file_format_error_DESC">Invalid file format.</string>
<string name="fingerprint">Fingerprint</string>
Expand Down
Loading