Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into 92x-mainactivity-vi…
Browse files Browse the repository at this point in the history
…ewmodel

# Conflicts:
#	app/src/main/java/app/pachli/MainActivity.kt
#	app/src/main/java/app/pachli/components/compose/ComposeViewModel.kt
#	app/src/main/java/app/pachli/components/preference/PreferencesFragment.kt
  • Loading branch information
nikclayton committed Oct 18, 2024
2 parents b45f497 + c8aa4fd commit 85681bf
Show file tree
Hide file tree
Showing 56 changed files with 208 additions and 119 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/apk-on-comment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
status: pending

- name: Checkout PR branch ${{ steps.comment-branch.outputs.head_ref }} / ${{ steps.comment-branch.outputs.head_sha }}
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4
with:
ref: ${{ steps.comment-branch.outputs.head_sha }}

Expand Down Expand Up @@ -53,7 +53,7 @@ jobs:

- name: Upload APK Release Asset
id: upload-release-asset-apk
uses: actions/upload-artifact@50769540e7f4bd5e21e526ee35c689e35e0d6874 # v4
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4
with:
name: app-release.apk
path: ${{steps.sign_app_apk.outputs.signedReleaseFile}}
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4

- uses: ./.github/actions/setup-build-env
with:
Expand All @@ -37,7 +37,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4

- uses: ./.github/actions/setup-build-env
with:
Expand All @@ -53,7 +53,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4

- uses: ./.github/actions/setup-build-env
with:
Expand All @@ -75,7 +75,7 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4

- uses: ./.github/actions/setup-build-env
with:
Expand All @@ -96,7 +96,7 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4

- uses: ./.github/actions/setup-build-env
with:
Expand All @@ -117,7 +117,7 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4

- uses: ./.github/actions/setup-build-env
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pr-conventional-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: PR Conventional Commit Validation
uses: ytanikin/PRConventionalCommits@8d258b54939f6769fcd935a52b96d6b0383a00c5 # 1.2.0
uses: ytanikin/PRConventionalCommits@b628c5a234cc32513014b7bfdd1e47b532124d98 # 1.3.0
with:
task_types: '["build","change","chore","ci","deprecate","docs","feat","fix","perf","refactor","remove","revert","security","style","test"]'
add_label: 'false'
4 changes: 2 additions & 2 deletions .github/workflows/upload-blue-release-google-play.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4

- uses: ./.github/actions/setup-build-env
with:
Expand Down Expand Up @@ -47,7 +47,7 @@ jobs:

- name: Upload APK Release Asset
id: upload-release-asset-apk
uses: actions/upload-artifact@50769540e7f4bd5e21e526ee35c689e35e0d6874 # v4
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4
with:
name: app-release.apk
path: ${{steps.sign_app_apk.outputs.signedReleaseFile}}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/upload-orange-release-google-play.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4
with:
fetch-depth: 0

Expand Down Expand Up @@ -52,7 +52,7 @@ jobs:

- name: Upload APK Release Asset
id: upload-release-asset-apk
uses: actions/upload-artifact@50769540e7f4bd5e21e526ee35c689e35e0d6874 # v4
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4
with:
name: app-release.apk
path: ${{steps.sign_app_apk.outputs.signedReleaseFile}}
Expand Down
68 changes: 54 additions & 14 deletions app/src/main/java/app/pachli/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ import com.mikepenz.materialdrawer.model.interfaces.nameRes
import com.mikepenz.materialdrawer.model.interfaces.nameText
import com.mikepenz.materialdrawer.util.AbstractDrawerImageLoader
import com.mikepenz.materialdrawer.util.DrawerImageLoader
import com.mikepenz.materialdrawer.util.addItemAtPosition
import com.mikepenz.materialdrawer.util.addItems
import com.mikepenz.materialdrawer.util.addItemsAtPosition
import com.mikepenz.materialdrawer.util.getPosition
Expand Down Expand Up @@ -368,6 +369,7 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
viewModel.uiState.collect { uiState ->
bindMainDrawerSearch(this@MainActivity, initialAccount.id, uiState.hideTopToolbar)
bindMainDrawerProfileHeader(uiState)
bindMainDrawerScheduledPosts(this@MainActivity, initialAccount.id, uiState.canSchedulePost)
updateShortCuts(uiState.accounts)
}
}
Expand Down Expand Up @@ -762,9 +764,10 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
}

// Add a "Search" menu item.
binding.mainDrawer.addItemsAtPosition(
binding.mainDrawer.addItemAtPosition(
4,
primaryDrawerItem {
identifier = DRAWER_ITEM_SEARCH
nameRes = R.string.action_search
iconicsIcon = GoogleMaterial.Icon.gmd_search
onClick = {
Expand All @@ -779,6 +782,45 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
)
}

/**
* Binds the "Scheduled posts" menu item in the main drawer.
*
* @param context
* @param pachliAccountId
* @param showSchedulePosts True if a "Scheduled posts" menu item should be added
* to the list, false if any existing item should be removed.
*/
private fun bindMainDrawerScheduledPosts(context: Context, pachliAccountId: Long, showSchedulePosts: Boolean) {
val existingPosition = binding.mainDrawer.getPosition(DRAWER_ITEM_SCHEDULED_POSTS)
val showing = existingPosition != -1

if (showing == showSchedulePosts) return

if (!showSchedulePosts) {
binding.mainDrawer.removeItemByPosition(existingPosition)
return
}

// Add the "Scheduled posts" item immediately after "Drafts"
binding.mainDrawer.addItemAtPosition(
binding.mainDrawer.getPosition(DRAWER_ITEM_DRAFTS) + 1,
primaryDrawerItem {
identifier = DRAWER_ITEM_SCHEDULED_POSTS
nameRes = R.string.action_access_scheduled_posts
iconRes = R.drawable.ic_access_time
onClick = {
startActivityWithDefaultTransition(
ScheduledStatusActivityIntent(context, pachliAccountId),
)
}
},
)

updateMainDrawerTypeface(
EmbeddedFontFamily.from(sharedPreferencesRepository.getString(FONT_FAMILY, "default")),
)
}

/** Binds [lists] to the "Lists" section in the main drawer. */
private fun bindMainDrawerLists(pachliAccountId: Long, lists: List<MastodonList>) {
binding.mainDrawer.removeItems(*listDrawerItems.toTypedArray())
Expand Down Expand Up @@ -925,6 +967,7 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
},
DividerDrawerItem(),
primaryDrawerItem {
identifier = DRAWER_ITEM_DRAFTS
nameRes = R.string.action_access_drafts
iconRes = R.drawable.ic_notebook
onClick = {
Expand All @@ -933,15 +976,6 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
)
}
},
primaryDrawerItem {
nameRes = R.string.action_access_scheduled_posts
iconRes = R.drawable.ic_access_time
onClick = {
startActivityWithDefaultTransition(
ScheduledStatusActivityIntent(context, pachliAccountId),
)
}
},
primaryDrawerItem {
identifier = DRAWER_ITEM_ANNOUNCEMENTS
nameRes = R.string.title_announcements
Expand Down Expand Up @@ -1375,14 +1409,20 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, MenuProvider {
}

companion object {
private const val DRAWER_ITEM_ADD_ACCOUNT: Long = -13
private const val DRAWER_ITEM_ANNOUNCEMENTS: Long = 14
private const val DRAWER_ITEM_ADD_ACCOUNT = -13L
private const val DRAWER_ITEM_ANNOUNCEMENTS = 14L

/** Drawer identifier for the "Lists" section header. */
private const val DRAWER_ITEM_LISTS: Long = 15
private const val DRAWER_ITEM_LISTS = 15L

/** Drawer identifier for the "Drafts" item. */
private const val DRAWER_ITEM_DRAFTS = 16L

/** Drawer identifier for the "Search" item. */
private const val DRAWER_ITEM_SEARCH: Long = 16
private const val DRAWER_ITEM_SEARCH = 17L

/** Drawer identifier for the "Scheduled posts" item. */
private const val DRAWER_ITEM_SCHEDULED_POSTS = 18L
}
}

Expand Down
32 changes: 23 additions & 9 deletions app/src/main/java/app/pachli/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import androidx.annotation.StringRes
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import app.pachli.core.common.PachliError
import app.pachli.core.data.model.Server
import app.pachli.core.data.repository.AccountManager
import app.pachli.core.data.repository.RefreshAccountError
import app.pachli.core.data.repository.SetActiveAccountError
import app.pachli.core.database.model.AccountEntity
import app.pachli.core.model.ServerOperation
import app.pachli.core.model.Timeline
import app.pachli.core.preferences.MainNavigationPosition
import app.pachli.core.preferences.PrefKeys
Expand All @@ -34,6 +36,7 @@ import com.github.michaelbull.result.Result
import com.github.michaelbull.result.mapEither
import com.github.michaelbull.result.onSuccess
import dagger.hilt.android.lifecycle.HiltViewModel
import io.github.z4kn4fein.semver.constraints.toConstraint
import javax.inject.Inject
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.MutableSharedFlow
Expand Down Expand Up @@ -102,6 +105,7 @@ internal sealed class UiError(
* @param mainNavigationPosition See [SharedPreferencesRepository.mainNavigationPosition].
* @param displaySelfUsername See [ShowSelfUsername].
* @param accounts Unordered list of available accounts.
* @param canSchedulePost True if the account can schedule posts
*/
data class UiState(
val animateAvatars: Boolean,
Expand All @@ -111,9 +115,10 @@ data class UiState(
val mainNavigationPosition: MainNavigationPosition,
val displaySelfUsername: Boolean,
val accounts: List<AccountEntity>,
val canSchedulePost: Boolean,
) {
companion object {
fun make(prefs: SharedPreferencesRepository, accounts: List<AccountEntity>) = UiState(
fun make(prefs: SharedPreferencesRepository, accounts: List<AccountEntity>, server: Server?) = UiState(
animateAvatars = prefs.animateAvatars,
animateEmojis = prefs.animateEmojis,
enableTabSwipe = prefs.enableTabSwipe,
Expand All @@ -125,6 +130,7 @@ data class UiState(
ShowSelfUsername.NEVER -> false
},
accounts = accounts,
canSchedulePost = server?.can(ServerOperation.ORG_JOINMASTODON_STATUSES_SCHEDULED, ">= 1.0.0".toConstraint()) == true,
)
}
}
Expand Down Expand Up @@ -162,15 +168,23 @@ internal class MainViewModel @Inject constructor(
PrefKeys.SHOW_SELF_USERNAME,
)

val uiState = sharedPreferencesRepository.changes.filter { watchedPrefs.contains(it) }.onStart { emit(null) }
.combine(accountManager.accountsFlow) { _, accounts ->
UiState.make(sharedPreferencesRepository, accounts)
val uiState =
combine(
sharedPreferencesRepository.changes.filter { watchedPrefs.contains(it) }.onStart { emit(null) },
accountManager.accountsFlow,
pachliAccountFlow,
) { _, accounts, pachliAccount ->
UiState.make(
sharedPreferencesRepository,
accounts,
pachliAccount.server,
)
}
.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5000),
initialValue = UiState.make(sharedPreferencesRepository, accountManager.accounts),
)
.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5000),
initialValue = UiState.make(sharedPreferencesRepository, accountManager.accounts, null),
)

init {
viewModelScope.launch { uiAction.collect { launch { onUiAction(it) } } }
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/app/pachli/PachliApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,13 @@ class PachliApplication : Application() {
// General usage is:
//
// if (oldVersion < ...) {
// ... use editor modify the preferences ...
// ... use `editor` to modify the preferences ...
// }

if (oldVersion < 2024101701) {
editor.remove(PrefKeys.Deprecated.WELLBEING_LIMITED_NOTIFICATIONS)
}

editor.putInt(PrefKeys.SCHEMA_VERSION, newVersion)
editor.apply()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,15 @@ class ComposeActivity :
}
}

// Hide the "Schedule" button if the server can't schedule. Simply
// disabling it could be confusing to users wondering why they can't
// use it.
lifecycleScope.launch {
viewModel.serverCanSchedule.collect {
binding.composeScheduleButton.visible(it)
}
}

lifecycleScope.launch {
viewModel.media.combine(viewModel.poll) { media, poll ->
val active = poll == null &&
Expand Down
Loading

0 comments on commit 85681bf

Please sign in to comment.