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') { implementation('com.mikepenz:materialdrawer:6.0.2@aar') {
transitive = true transitive = true
} }
implementation('com.mikepenz:aboutlibraries:6.0.1@aar') { implementation('com.mikepenz:aboutlibraries:6.0.2@aar') {
transitive = true transitive = true
} }
implementation "com.mikepenz:iconics-core:3.0.0@aar" implementation "com.mikepenz:iconics-core:3.0.0@aar"

View File

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

View File

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

View File

@ -298,9 +298,8 @@ class MainActivity : AppCompatActivity(), ListSelectionListener<Serializable> {
if (listFragment is ListHolder<*>) { if (listFragment is ListHolder<*>) {
val listHolder = listFragment as ListHolder<*> val listHolder = listFragment as ListHolder<*>
if (listHolder.showPreviousList()) { if (listHolder.showPreviousList()) {
val drawerItem = drawer.getDrawerItem(listHolder.currentLabel) drawer.getDrawerItem(listHolder.currentLabel)?.let {
if (drawerItem != null) { drawer.setSelection(it)
drawer.setSelection(drawerItem)
} }
return return
} }
@ -510,7 +509,7 @@ class MainActivity : AppCompatActivity(), ListSelectionListener<Serializable> {
private var instance: WeakReference<MainActivity>? = null private var instance: WeakReference<MainActivity>? = null
fun updateNodeSwitch() { fun updateNodeSwitch() {
getInstance()?.apply { apply {
runOnUiThread { runOnUiThread {
nodeSwitch.withChecked(Preferences.isFullNodeActive(this)) nodeSwitch.withChecked(Preferences.isFullNodeActive(this))
drawer.updateStickyFooterItem(nodeSwitch) 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 (!isLoading && !isLastPage) {
if (visibleItemCount + firstVisibleItemPosition >= totalItemCount - 5 if (visibleItemCount + firstVisibleItemPosition >= totalItemCount - 5
&& firstVisibleItemPosition >= 0) { && firstVisibleItemPosition >= 0) {
loadMoreItems() loadMoreItems()
} }
} }
@ -160,7 +160,7 @@ class MessageListFragment : Fragment(), ListHolder<Label> {
} }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View = 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?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
@ -224,7 +224,7 @@ class MessageListFragment : Fragment(), ListHolder<Label> {
recycler_view.addOnScrollListener(recyclerViewOnScrollListener) recycler_view.addOnScrollListener(recyclerViewOnScrollListener)
recycler_view.addItemDecoration(SimpleListDividerDecorator( recycler_view.addItemDecoration(SimpleListDividerDecorator(
ContextCompat.getDrawable(context, R.drawable.list_divider_h), true)) ContextCompat.getDrawable(context, R.drawable.list_divider_h), true))
// NOTE: // NOTE:
// The initialization order is very important! This order determines the priority of // 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.broadcast).setIcon(R.drawable.ic_action_broadcast)
menu.add(R.string.personal_message).setIcon(R.drawable.ic_action_personal) menu.add(R.string.personal_message).setIcon(R.drawable.ic_action_personal)
FabUtils.initFab(context, R.drawable.ic_action_compose_message, menu) FabUtils.initFab(context, R.drawable.ic_action_compose_message, menu)
.addOnMenuItemClickListener { _, _, itemId -> .addOnMenuItemClickListener { _, _, itemId ->
val identity = Singleton.getIdentity(context) val identity = Singleton.getIdentity(context)
if (identity == null) { if (identity == null) {
Toast.makeText(activity, R.string.no_identity_warning, Toast.makeText(activity, R.string.no_identity_warning,
Toast.LENGTH_LONG).show() Toast.LENGTH_LONG).show()
} else { } else {
when (itemId) { when (itemId) {
1 -> { 1 -> {
val intent = Intent(activity, ComposeMessageActivity::class.java) val intent = Intent(activity, ComposeMessageActivity::class.java)
intent.putExtra(EXTRA_IDENTITY, identity) intent.putExtra(EXTRA_IDENTITY, identity)
intent.putExtra(EXTRA_BROADCAST, true) intent.putExtra(EXTRA_BROADCAST, true)
startActivity(intent) startActivity(intent)
} }
2 -> { 2 -> {
val intent = Intent(activity, ComposeMessageActivity::class.java) val intent = Intent(activity, ComposeMessageActivity::class.java)
intent.putExtra(EXTRA_IDENTITY, identity) intent.putExtra(EXTRA_IDENTITY, identity)
startActivity(intent) startActivity(intent)
} }
else -> { else -> {
}
} }
} }
} }
}
} }
override fun onDestroyView() { override fun onDestroyView() {

View File

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

View File

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

View File

@ -54,7 +54,7 @@ class MessageListener(ctx: Context) : BitmessageContext.Listener {
notification.show() notification.show()
// If MainActivity is shown, update the sidebar badges // 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, Toast.makeText(ctx,
R.string.toast_identity_created, R.string.toast_identity_created,
Toast.LENGTH_SHORT).show() Toast.LENGTH_SHORT).show()
val mainActivity = MainActivity.getInstance() MainActivity.apply { addIdentityEntry(identity) }
mainActivity?.addIdentityEntry(identity)
} }
} }
} }