Bug fixes & code improvements

- simplified access to MainActivity
- fixed bug where the 'unread' tag wasn't updated
- aboutlibraries version bump
This commit is contained in:
Christian Basler 2018-01-11 17:25:26 +01:00
parent d88d3c900e
commit 4c89bfe1cf
9 changed files with 67 additions and 51 deletions

View File

@ -79,7 +79,7 @@ dependencies {
implementation('com.mikepenz:materialdrawer:6.0.2@aar') {
transitive = true
}
implementation('com.mikepenz:aboutlibraries:6.0.1@aar') {
implementation('com.mikepenz:aboutlibraries:6.0.2@aar') {
transitive = true
}
implementation "com.mikepenz:iconics-core:3.0.0@aar"

View File

@ -94,9 +94,10 @@ class AddressDetailFragment : Fragment() {
.setMessage(warning)
.setPositiveButton(android.R.string.yes) { _, _ ->
Singleton.getAddressRepository(ctx).remove(item)
val mainActivity = MainActivity.getInstance()
if (item.privateKey != null && mainActivity != null) {
mainActivity.removeIdentityEntry(item)
MainActivity.apply {
if (item.privateKey != null) {
removeIdentityEntry(item)
}
}
this.item = null
ctx.onBackPressed()
@ -109,13 +110,19 @@ class AddressDetailFragment : Fragment() {
AlertDialog.Builder(ctx)
.setMessage(R.string.confirm_export)
.setPositiveButton(android.R.string.yes) { _, _ ->
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.type = "text/plain"
shareIntent.putExtra(Intent.EXTRA_TITLE, item.toString() + EXPORT_POSTFIX)
val exporter = WifExporter(Singleton
.getBitmessageContext(ctx))
exporter.addIdentity(item)
shareIntent.putExtra(Intent.EXTRA_TEXT, exporter.toString())
val shareIntent = Intent(Intent.ACTION_SEND).apply {
type = "text/plain"
putExtra(
Intent.EXTRA_TITLE,
"$item$EXPORT_POSTFIX"
)
putExtra(
Intent.EXTRA_TEXT,
WifExporter(Singleton.getBitmessageContext(ctx)).apply {
addIdentity(item)
}.toString()
)
}
startActivity(Intent.createChooser(shareIntent, null))
}
.setNegativeButton(android.R.string.no, null)
@ -186,7 +193,7 @@ class AddressDetailFragment : Fragment() {
item?.let { item ->
Singleton.getAddressRepository(context!!).save(item)
if (item.privateKey != null) {
MainActivity.getInstance()?.updateIdentityEntry(item)
MainActivity.apply { updateIdentityEntry(item) }
}
}
super.onPause()

View File

@ -62,10 +62,9 @@ class ImportIdentitiesFragment : Fragment() {
view.findViewById<Button>(R.id.finish).setOnClickListener {
importer.importAll(adapter.selected)
val mainActivity = MainActivity.getInstance()
if (mainActivity != null) {
MainActivity.apply {
for (selected in adapter.selected) {
mainActivity.addIdentityEntry(selected)
addIdentityEntry(selected)
}
}
activity.finish()

View File

@ -298,9 +298,8 @@ class MainActivity : AppCompatActivity(), ListSelectionListener<Serializable> {
if (listFragment is ListHolder<*>) {
val listHolder = listFragment as ListHolder<*>
if (listHolder.showPreviousList()) {
val drawerItem = drawer.getDrawerItem(listHolder.currentLabel)
if (drawerItem != null) {
drawer.setSelection(drawerItem)
drawer.getDrawerItem(listHolder.currentLabel)?.let {
drawer.setSelection(it)
}
return
}
@ -510,7 +509,7 @@ class MainActivity : AppCompatActivity(), ListSelectionListener<Serializable> {
private var instance: WeakReference<MainActivity>? = null
fun updateNodeSwitch() {
getInstance()?.apply {
apply {
runOnUiThread {
nodeSwitch.withChecked(Preferences.isFullNodeActive(this))
drawer.updateStickyFooterItem(nodeSwitch)
@ -518,6 +517,12 @@ class MainActivity : AppCompatActivity(), ListSelectionListener<Serializable> {
}
}
fun getInstance() = instance?.get()
/**
* Runs the given code in the main activity context, if it currently exists. Otherwise,
* it's ignored.
*/
fun apply(run: MainActivity.() -> Unit){
instance?.get()?.let { run.invoke(it) }
}
}
}

View File

@ -79,7 +79,7 @@ class MessageListFragment : Fragment(), ListHolder<Label> {
if (!isLoading && !isLastPage) {
if (visibleItemCount + firstVisibleItemPosition >= totalItemCount - 5
&& firstVisibleItemPosition >= 0) {
&& firstVisibleItemPosition >= 0) {
loadMoreItems()
}
}
@ -160,7 +160,7 @@ class MessageListFragment : Fragment(), ListHolder<Label> {
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View =
inflater.inflate(R.layout.fragment_message_list, container, false)
inflater.inflate(R.layout.fragment_message_list, container, false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
@ -224,7 +224,7 @@ class MessageListFragment : Fragment(), ListHolder<Label> {
recycler_view.addOnScrollListener(recyclerViewOnScrollListener)
recycler_view.addItemDecoration(SimpleListDividerDecorator(
ContextCompat.getDrawable(context, R.drawable.list_divider_h), true))
ContextCompat.getDrawable(context, R.drawable.list_divider_h), true))
// NOTE:
// The initialization order is very important! This order determines the priority of
@ -262,6 +262,11 @@ class MessageListFragment : Fragment(), ListHolder<Label> {
}
}
}
if (removed.any { it.type == Label.Type.UNREAD } || added.any { it.type == Label.Type.UNREAD }) {
MainActivity.apply {
updateUnread()
}
}
}
}
@ -270,29 +275,29 @@ class MessageListFragment : Fragment(), ListHolder<Label> {
menu.add(R.string.broadcast).setIcon(R.drawable.ic_action_broadcast)
menu.add(R.string.personal_message).setIcon(R.drawable.ic_action_personal)
FabUtils.initFab(context, R.drawable.ic_action_compose_message, menu)
.addOnMenuItemClickListener { _, _, itemId ->
val identity = Singleton.getIdentity(context)
if (identity == null) {
Toast.makeText(activity, R.string.no_identity_warning,
Toast.LENGTH_LONG).show()
} else {
when (itemId) {
1 -> {
val intent = Intent(activity, ComposeMessageActivity::class.java)
intent.putExtra(EXTRA_IDENTITY, identity)
intent.putExtra(EXTRA_BROADCAST, true)
startActivity(intent)
}
2 -> {
val intent = Intent(activity, ComposeMessageActivity::class.java)
intent.putExtra(EXTRA_IDENTITY, identity)
startActivity(intent)
}
else -> {
}
.addOnMenuItemClickListener { _, _, itemId ->
val identity = Singleton.getIdentity(context)
if (identity == null) {
Toast.makeText(activity, R.string.no_identity_warning,
Toast.LENGTH_LONG).show()
} else {
when (itemId) {
1 -> {
val intent = Intent(activity, ComposeMessageActivity::class.java)
intent.putExtra(EXTRA_IDENTITY, identity)
intent.putExtra(EXTRA_BROADCAST, true)
startActivity(intent)
}
2 -> {
val intent = Intent(activity, ComposeMessageActivity::class.java)
intent.putExtra(EXTRA_IDENTITY, identity)
startActivity(intent)
}
else -> {
}
}
}
}
}
override fun onDestroyView() {

View File

@ -71,8 +71,9 @@ class AddIdentityDialogFragment : AppCompatDialogFragment() {
Toast.makeText(ctx,
R.string.toast_identity_created,
Toast.LENGTH_SHORT).show()
val mainActivity = MainActivity.getInstance()
mainActivity?.addIdentityEntry(identity)
MainActivity.apply {
addIdentityEntry(identity)
}
}
}
}
@ -106,7 +107,7 @@ class AddIdentityDialogFragment : AppCompatDialogFragment() {
Toast.makeText(ctx,
R.string.toast_chan_created,
Toast.LENGTH_SHORT).show()
MainActivity.getInstance()?.addIdentityEntry(chan)
MainActivity.apply { addIdentityEntry(chan) }
}
}
}

View File

@ -81,9 +81,9 @@ class DeterministicIdentityDialogFragment : AppCompatDialogFragment() {
Toast.makeText(context,
messageRes,
Toast.LENGTH_SHORT).show()
MainActivity.getInstance()?.let { mainActivity ->
MainActivity.apply {
identities.forEach { identity ->
mainActivity.addIdentityEntry(identity)
addIdentityEntry(identity)
}
}
}

View File

@ -54,7 +54,7 @@ class MessageListener(ctx: Context) : BitmessageContext.Listener {
notification.show()
// If MainActivity is shown, update the sidebar badges
MainActivity.getInstance()?.updateUnread()
MainActivity.apply { updateUnread() }
}
}

View File

@ -104,8 +104,7 @@ object Singleton {
Toast.makeText(ctx,
R.string.toast_identity_created,
Toast.LENGTH_SHORT).show()
val mainActivity = MainActivity.getInstance()
mainActivity?.addIdentityEntry(identity)
MainActivity.apply { addIdentityEntry(identity) }
}
}
}