1
0
mirror of https://github.com/etesync/android synced 2024-12-23 07:08:16 +00:00

Overhaul Etebase collection activity.

This commit is contained in:
Tom Hacohen 2020-08-27 16:12:33 +03:00
parent be22beb7f9
commit 74b4ef3ee9
4 changed files with 90 additions and 73 deletions

View File

@ -18,26 +18,36 @@ import org.jetbrains.anko.uiThread
class CollectionActivity() : BaseActivity() {
private lateinit var account: Account
private lateinit var colUid: String
private val model: AccountCollectionViewModel by viewModels()
private val model: AccountViewModel by viewModels()
private val collectionModel: CollectionViewModel by viewModels()
private val itemsModel: ItemsViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
account = intent.extras!!.getParcelable(EXTRA_ACCOUNT)!!
colUid = intent.extras!!.getString(EXTRA_COLLECTION_UID)!!
val colUid = intent.extras!!.getString(EXTRA_COLLECTION_UID)
val colType = intent.extras!!.getString(EXTRA_COLLECTION_TYPE)
setContentView(R.layout.etebase_collection_activity)
if (savedInstanceState == null) {
model.loadCollection(this, account, colUid)
model.loadAccount(this, account)
if (colUid != null) {
model.observe(this) {
itemsModel.loadItems(it)
collectionModel.loadCollection(it, colUid)
collectionModel.observe(this) { cachedCollection ->
itemsModel.loadItems(it, cachedCollection)
}
}
supportFragmentManager.commit {
replace(R.id.fragment_container, ViewCollectionFragment())
}
} else if (colType != null) {
supportFragmentManager.commit {
// replace(R.id.fragment_container, CreateCollectionFragment(colType))
}
}
}
supportActionBar?.setDisplayHomeAsUpEnabled(true)
@ -46,6 +56,7 @@ class CollectionActivity() : BaseActivity() {
companion object {
private val EXTRA_ACCOUNT = "account"
private val EXTRA_COLLECTION_UID = "collectionUid"
private val EXTRA_COLLECTION_TYPE = "collectionType"
fun newIntent(context: Context, account: Account, colUid: String): Intent {
val intent = Intent(context, CollectionActivity::class.java)
@ -53,48 +64,74 @@ class CollectionActivity() : BaseActivity() {
intent.putExtra(EXTRA_COLLECTION_UID, colUid)
return intent
}
fun newCreateCollectionIntent(context: Context, account: Account, colType: String): Intent {
val intent = Intent(context, CollectionActivity::class.java)
intent.putExtra(EXTRA_ACCOUNT, account)
intent.putExtra(EXTRA_COLLECTION_TYPE, colType)
return intent
}
}
}
class AccountCollectionViewModel : ViewModel() {
private val collection = MutableLiveData<AccountCollectionHolder>()
class AccountViewModel : ViewModel() {
private val holder = MutableLiveData<AccountHolder>()
fun loadCollection(context: Context, account: Account, colUid: String) {
fun loadAccount(context: Context, account: Account) {
doAsync {
val settings = AccountSettings(context, account)
val etebaseLocalCache = EtebaseLocalCache.getInstance(context, account.name)
val etebase = EtebaseLocalCache.getEtebase(context, HttpClient.sharedClient, settings)
val colMgr = etebase.collectionManager
val cachedCollection = synchronized(etebaseLocalCache) {
etebaseLocalCache.collectionGet(colMgr, colUid)!!
}
uiThread {
collection.value = AccountCollectionHolder(
holder.value = AccountHolder(
account,
etebaseLocalCache,
etebase,
colMgr,
cachedCollection
colMgr
)
}
}
}
fun observe(owner: LifecycleOwner, observer: (AccountCollectionHolder) -> Unit) =
collection.observe(owner, observer)
fun observe(owner: LifecycleOwner, observer: (AccountHolder) -> Unit) =
holder.observe(owner, observer)
val value: AccountCollectionHolder?
get() = collection.value
val value: AccountHolder?
get() = holder.value
}
data class AccountCollectionHolder(val account: Account, val etebaseLocalCache: EtebaseLocalCache, val etebase: com.etebase.client.Account, val colMgr: CollectionManager, val cachedCollection: CachedCollection)
data class AccountHolder(val account: Account, val etebaseLocalCache: EtebaseLocalCache, val etebase: com.etebase.client.Account, val colMgr: CollectionManager)
class CollectionViewModel : ViewModel() {
private val collection = MutableLiveData<CachedCollection>()
fun loadCollection(accountHolder: AccountHolder, colUid: String) {
doAsync {
val etebaseLocalCache = accountHolder.etebaseLocalCache
val colMgr = accountHolder.colMgr
val cachedCollection = synchronized(etebaseLocalCache) {
etebaseLocalCache.collectionGet(colMgr, colUid)!!
}
uiThread {
collection.value = cachedCollection
}
}
}
fun observe(owner: LifecycleOwner, observer: (CachedCollection) -> Unit) =
collection.observe(owner, observer)
val value: CachedCollection?
get() = collection.value
}
class ItemsViewModel : ViewModel() {
private val cachedItems = MutableLiveData<List<CachedItem>>()
fun loadItems(accountCollectionHolder: AccountCollectionHolder) {
fun loadItems(accountCollectionHolder: AccountHolder, cachedCollection: CachedCollection) {
doAsync {
val col = accountCollectionHolder.cachedCollection.col
val col = cachedCollection.col
val itemMgr = accountCollectionHolder.colMgr.getItemManager(col)
val items = accountCollectionHolder.etebaseLocalCache.itemList(itemMgr, col.uid, withDeleted = true)
uiThread {

View File

@ -12,6 +12,7 @@ import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import com.etebase.client.Collection
import com.etebase.client.exceptions.EtebaseException
import com.etesync.syncadapter.CachedCollection
import com.etesync.syncadapter.Constants
import com.etesync.syncadapter.R
import com.etesync.syncadapter.resource.LocalCalendar
@ -20,10 +21,10 @@ import org.apache.commons.lang3.StringUtils
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
import yuku.ambilwarna.AmbilWarnaDialog
import java.lang.String
class EditCollectionFragment() : Fragment() {
private val model: AccountCollectionViewModel by activityViewModels()
class EditCollectionFragment(private val cachedCollection: CachedCollection) : Fragment() {
private val model: AccountViewModel by activityViewModels()
private val collectionModel: CollectionViewModel by activityViewModels()
private val loadingModel: LoadingViewModel by viewModels()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
@ -31,51 +32,30 @@ class EditCollectionFragment() : Fragment() {
setHasOptionsMenu(true)
if (savedInstanceState == null) {
model.observe(this) {
updateTitle(it)
updateTitle()
if (container != null) {
initUi(inflater, ret, it)
}
initUi(inflater, ret)
}
}
return ret
}
fun updateTitle(accountCollectionHolder: AccountCollectionHolder) {
accountCollectionHolder.let {
val new = false
var titleId: Int = R.string.create_calendar
if (new) {
when (it.cachedCollection.meta.collectionType) {
Constants.ETEBASE_TYPE_CALENDAR -> {
titleId = R.string.create_calendar
}
Constants.ETEBASE_TYPE_TASKS -> {
titleId = R.string.create_tasklist
}
Constants.ETEBASE_TYPE_ADDRESS_BOOK -> {
titleId = R.string.create_addressbook
}
}
} else {
titleId = R.string.edit_collection
}
(activity as? BaseActivity?)?.supportActionBar?.setTitle(titleId)
}
fun updateTitle() {
(activity as? BaseActivity?)?.supportActionBar?.setTitle(R.string.edit_collection)
}
private fun initUi(inflater: LayoutInflater, v: View, collectionHolder: AccountCollectionHolder) {
private fun initUi(inflater: LayoutInflater, v: View) {
val title = v.findViewById<EditText>(R.id.display_name)
val desc = v.findViewById<EditText>(R.id.description)
val meta = collectionHolder.cachedCollection.meta
val meta = cachedCollection.meta
title.setText(meta.name)
desc.setText(meta.description)
val colorSquare = v.findViewById<View>(R.id.color)
when (collectionHolder.cachedCollection.meta.collectionType) {
when (cachedCollection.meta.collectionType) {
Constants.ETEBASE_TYPE_CALENDAR -> {
title.setHint(R.string.create_calendar_display_name_hint)
@ -133,7 +113,7 @@ class EditCollectionFragment() : Fragment() {
}
private fun deleteColection() {
val meta = model.value!!.cachedCollection.meta
val meta = cachedCollection.meta
val name = meta.name
AlertDialog.Builder(requireContext())
@ -151,7 +131,7 @@ class EditCollectionFragment() : Fragment() {
loadingModel.setLoading(true)
doAsync {
try {
val col = model.value!!.cachedCollection.col
val col = cachedCollection.col
col.delete()
uploadCollection(col)
activity?.finish()
@ -174,7 +154,7 @@ class EditCollectionFragment() : Fragment() {
private fun saveCollection() {
var ok = true
val meta = model.value!!.cachedCollection.meta
val meta = cachedCollection.meta
val v = requireView()
var edit = v.findViewById<EditText>(R.id.display_name)
@ -201,7 +181,7 @@ class EditCollectionFragment() : Fragment() {
loadingModel.setLoading(true)
doAsync {
try {
val col = model.value!!.cachedCollection.col
val col = cachedCollection.col
col.meta = meta
uploadCollection(col)
parentFragmentManager.popBackStack()
@ -230,6 +210,6 @@ class EditCollectionFragment() : Fragment() {
synchronized(etebaseLocalCache) {
etebaseLocalCache.collectionSet(colMgr, col)
}
model.loadCollection(requireContext(), accountHolder.account, col.uid)
collectionModel.loadCollection(model.value!!, col.uid)
}
}

View File

@ -16,7 +16,7 @@ import java.text.SimpleDateFormat
import java.util.concurrent.Future
class ListEntriesFragment : ListFragment(), AdapterView.OnItemClickListener {
private val model: AccountCollectionViewModel by activityViewModels()
private val collectionModel: CollectionViewModel by activityViewModels()
private val itemsModel: ItemsViewModel by activityViewModels()
private var asyncTask: Future<Unit>? = null
@ -35,12 +35,12 @@ class ListEntriesFragment : ListFragment(), AdapterView.OnItemClickListener {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
model.observe(this) { col ->
collectionModel.observe(this) { col ->
itemsModel.observe(this) {
val entries = it.sortedByDescending { item ->
item.meta.mtime ?: 0
}
val listAdapter = EntriesListAdapter(requireContext(), col.cachedCollection)
val listAdapter = EntriesListAdapter(requireContext(), col)
setListAdapter(listAdapter)
listAdapter.addAll(entries)

View File

@ -1,6 +1,5 @@
package com.etesync.syncadapter.ui.etebase
import android.content.Context
import android.content.DialogInterface
import android.graphics.Color.parseColor
import android.os.Bundle
@ -11,6 +10,7 @@ import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.commit
import com.etesync.syncadapter.CachedCollection
import com.etesync.syncadapter.Constants
import com.etesync.syncadapter.R
import com.etesync.syncadapter.resource.LocalCalendar
@ -23,7 +23,7 @@ import tourguide.tourguide.ToolTip
import java.util.*
class ViewCollectionFragment : Fragment() {
private val model: AccountCollectionViewModel by activityViewModels()
private val collectionModel: CollectionViewModel by activityViewModels()
private val itemsModel: ItemsViewModel by activityViewModels()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
@ -31,8 +31,8 @@ class ViewCollectionFragment : Fragment() {
setHasOptionsMenu(true)
if (savedInstanceState == null) {
model.observe(this) {
(activity as? BaseActivity?)?.supportActionBar?.title = it.cachedCollection.meta.name
collectionModel.observe(this) {
(activity as? BaseActivity?)?.supportActionBar?.title = it.meta.name
if (container != null) {
initUi(inflater, ret, it)
}
@ -42,7 +42,7 @@ class ViewCollectionFragment : Fragment() {
return ret
}
private fun initUi(inflater: LayoutInflater, container: View, collectionHolder: AccountCollectionHolder) {
private fun initUi(inflater: LayoutInflater, container: View, cachedCollection: CachedCollection) {
val title = container.findViewById<TextView>(R.id.display_name)
if (!HintManager.getHintSeen(requireContext(), HINT_IMPORT)) {
val tourGuide = ShowcaseBuilder.getBuilder(requireActivity())
@ -63,8 +63,8 @@ class ViewCollectionFragment : Fragment() {
.setPositiveButton(android.R.string.yes) { _, _ -> }.show()
}
val col = collectionHolder.cachedCollection.col
val meta = collectionHolder.cachedCollection.meta
val col = cachedCollection.col
val meta = cachedCollection.meta
val isAdmin = col.accessLevel == "adm"
val colorSquare = container.findViewById<View>(R.id.color)
@ -112,7 +112,7 @@ class ViewCollectionFragment : Fragment() {
when (item.itemId) {
R.id.on_edit -> {
parentFragmentManager.commit {
replace(R.id.fragment_container, EditCollectionFragment())
replace(R.id.fragment_container, EditCollectionFragment(collectionModel.value!!))
addToBackStack(EditCollectionFragment::class.java.name)
}
}