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

View File

@ -12,6 +12,7 @@ import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels import androidx.fragment.app.viewModels
import com.etebase.client.Collection import com.etebase.client.Collection
import com.etebase.client.exceptions.EtebaseException import com.etebase.client.exceptions.EtebaseException
import com.etesync.syncadapter.CachedCollection
import com.etesync.syncadapter.Constants import com.etesync.syncadapter.Constants
import com.etesync.syncadapter.R import com.etesync.syncadapter.R
import com.etesync.syncadapter.resource.LocalCalendar 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.doAsync
import org.jetbrains.anko.uiThread import org.jetbrains.anko.uiThread
import yuku.ambilwarna.AmbilWarnaDialog import yuku.ambilwarna.AmbilWarnaDialog
import java.lang.String
class EditCollectionFragment() : Fragment() { class EditCollectionFragment(private val cachedCollection: CachedCollection) : Fragment() {
private val model: AccountCollectionViewModel by activityViewModels() private val model: AccountViewModel by activityViewModels()
private val collectionModel: CollectionViewModel by activityViewModels()
private val loadingModel: LoadingViewModel by viewModels() private val loadingModel: LoadingViewModel by viewModels()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
@ -31,51 +32,30 @@ class EditCollectionFragment() : Fragment() {
setHasOptionsMenu(true) setHasOptionsMenu(true)
if (savedInstanceState == null) { if (savedInstanceState == null) {
model.observe(this) { updateTitle()
updateTitle(it)
if (container != null) { if (container != null) {
initUi(inflater, ret, it) initUi(inflater, ret)
}
} }
} }
return ret return ret
} }
fun updateTitle(accountCollectionHolder: AccountCollectionHolder) { fun updateTitle() {
accountCollectionHolder.let { (activity as? BaseActivity?)?.supportActionBar?.setTitle(R.string.edit_collection)
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)
}
} }
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 title = v.findViewById<EditText>(R.id.display_name)
val desc = v.findViewById<EditText>(R.id.description) val desc = v.findViewById<EditText>(R.id.description)
val meta = collectionHolder.cachedCollection.meta val meta = cachedCollection.meta
title.setText(meta.name) title.setText(meta.name)
desc.setText(meta.description) desc.setText(meta.description)
val colorSquare = v.findViewById<View>(R.id.color) val colorSquare = v.findViewById<View>(R.id.color)
when (collectionHolder.cachedCollection.meta.collectionType) { when (cachedCollection.meta.collectionType) {
Constants.ETEBASE_TYPE_CALENDAR -> { Constants.ETEBASE_TYPE_CALENDAR -> {
title.setHint(R.string.create_calendar_display_name_hint) title.setHint(R.string.create_calendar_display_name_hint)
@ -133,7 +113,7 @@ class EditCollectionFragment() : Fragment() {
} }
private fun deleteColection() { private fun deleteColection() {
val meta = model.value!!.cachedCollection.meta val meta = cachedCollection.meta
val name = meta.name val name = meta.name
AlertDialog.Builder(requireContext()) AlertDialog.Builder(requireContext())
@ -151,7 +131,7 @@ class EditCollectionFragment() : Fragment() {
loadingModel.setLoading(true) loadingModel.setLoading(true)
doAsync { doAsync {
try { try {
val col = model.value!!.cachedCollection.col val col = cachedCollection.col
col.delete() col.delete()
uploadCollection(col) uploadCollection(col)
activity?.finish() activity?.finish()
@ -174,7 +154,7 @@ class EditCollectionFragment() : Fragment() {
private fun saveCollection() { private fun saveCollection() {
var ok = true var ok = true
val meta = model.value!!.cachedCollection.meta val meta = cachedCollection.meta
val v = requireView() val v = requireView()
var edit = v.findViewById<EditText>(R.id.display_name) var edit = v.findViewById<EditText>(R.id.display_name)
@ -201,7 +181,7 @@ class EditCollectionFragment() : Fragment() {
loadingModel.setLoading(true) loadingModel.setLoading(true)
doAsync { doAsync {
try { try {
val col = model.value!!.cachedCollection.col val col = cachedCollection.col
col.meta = meta col.meta = meta
uploadCollection(col) uploadCollection(col)
parentFragmentManager.popBackStack() parentFragmentManager.popBackStack()
@ -230,6 +210,6 @@ class EditCollectionFragment() : Fragment() {
synchronized(etebaseLocalCache) { synchronized(etebaseLocalCache) {
etebaseLocalCache.collectionSet(colMgr, col) 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 import java.util.concurrent.Future
class ListEntriesFragment : ListFragment(), AdapterView.OnItemClickListener { class ListEntriesFragment : ListFragment(), AdapterView.OnItemClickListener {
private val model: AccountCollectionViewModel by activityViewModels() private val collectionModel: CollectionViewModel by activityViewModels()
private val itemsModel: ItemsViewModel by activityViewModels() private val itemsModel: ItemsViewModel by activityViewModels()
private var asyncTask: Future<Unit>? = null private var asyncTask: Future<Unit>? = null
@ -35,12 +35,12 @@ class ListEntriesFragment : ListFragment(), AdapterView.OnItemClickListener {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
model.observe(this) { col -> collectionModel.observe(this) { col ->
itemsModel.observe(this) { itemsModel.observe(this) {
val entries = it.sortedByDescending { item -> val entries = it.sortedByDescending { item ->
item.meta.mtime ?: 0 item.meta.mtime ?: 0
} }
val listAdapter = EntriesListAdapter(requireContext(), col.cachedCollection) val listAdapter = EntriesListAdapter(requireContext(), col)
setListAdapter(listAdapter) setListAdapter(listAdapter)
listAdapter.addAll(entries) listAdapter.addAll(entries)

View File

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