Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix string translation in Stats #10520

Merged
merged 4 commits into from
Sep 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.wordpress.android.ui.stats.refresh.lists.widget.configuration.StatsWidgetSiteSelectionDialogFragment;
import org.wordpress.android.ui.stats.refresh.lists.widget.minified.StatsMinifiedWidgetConfigureFragment;
import org.wordpress.android.util.wizard.WizardManager;
import org.wordpress.android.viewmodel.ContextProvider;
import org.wordpress.android.viewmodel.helpers.ConnectionStatus;
import org.wordpress.android.viewmodel.helpers.ConnectionStatusLiveData;

Expand All @@ -40,8 +41,8 @@ public abstract class ApplicationModule {
abstract Context bindContext(Application application);

@Provides
public static NewsService provideLocalNewsService(Context context) {
return new LocalNewsService(context);
public static NewsService provideLocalNewsService(ContextProvider contextProvider) {
return new LocalNewsService(contextProvider);
}

@ContributesAndroidInjector
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package org.wordpress.android.ui.news

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import org.wordpress.android.models.news.LocalNewsItem
import org.wordpress.android.models.news.NewsItem
import org.wordpress.android.viewmodel.ContextProvider
import javax.inject.Inject

/**
* Service for fetching data for a News Card (Card with a new feature/update announcement) from a local resource.
*
* This is just a temporary solution until News Cards are supported on the server.
*/
class LocalNewsService @Inject constructor(private val context: Context) : NewsService {
class LocalNewsService @Inject constructor(private val contextProvider: ContextProvider) : NewsService {
val data: MutableLiveData<NewsItem> = MutableLiveData()

override fun newsItemSource(): LiveData<NewsItem> {
Expand All @@ -29,10 +29,10 @@ class LocalNewsService @Inject constructor(private val context: Context) : NewsS

private fun loadCardFromResources(): NewsItem {
return NewsItem(
context.getString(LocalNewsItem.titleResId),
context.getString(LocalNewsItem.contentResId),
context.getString(LocalNewsItem.actionResId),
context.getString(LocalNewsItem.urlResId),
contextProvider.getContext().getString(LocalNewsItem.titleResId),
contextProvider.getContext().getString(LocalNewsItem.contentResId),
contextProvider.getContext().getString(LocalNewsItem.actionResId),
contextProvider.getContext().getString(LocalNewsItem.urlResId),
LocalNewsItem.version
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.wordpress.android.util.WPActivityUtils;
import org.wordpress.android.util.WPPrefUtils;
import org.wordpress.android.util.analytics.AnalyticsUtils;
import org.wordpress.android.viewmodel.ContextProvider;

import java.util.EnumSet;
import java.util.HashMap;
Expand Down Expand Up @@ -65,6 +66,7 @@ public class AppSettingsFragment extends PreferenceFragment
@Inject SiteStore mSiteStore;
@Inject AccountStore mAccountStore;
@Inject Dispatcher mDispatcher;
@Inject ContextProvider mContextProvider;

@Override
public void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -317,6 +319,7 @@ private void changeLanguage(String languageCode) {
LocaleManager.setNewLocale(WordPress.getContext(), languageCode);
WordPress.updateContextLocale();
updateLanguagePreference(languageCode);
mContextProvider.refreshContext();

// Track language change on Analytics because we have both the device language and app selected language
// data in Tracks metadata.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package org.wordpress.android.ui.stats.refresh.utils

import android.content.Context
import android.text.format.DateFormat
import android.text.format.DateUtils
import org.wordpress.android.util.DateTimeUtils
import org.wordpress.android.util.LocaleManagerWrapper
import org.wordpress.android.viewmodel.ContextProvider
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale
import javax.inject.Inject

class DateUtils @Inject constructor(private val context: Context) {
class DateUtils @Inject constructor(
private val contextProvider: ContextProvider,
private val localeManagerWrapper: LocaleManagerWrapper
) {
fun getWeekDay(dayOfTheWeek: Int): String {
val c = Calendar.getInstance()
c.firstDayOfWeek = Calendar.MONDAY
Expand All @@ -24,12 +27,12 @@ class DateUtils @Inject constructor(private val context: Context) {
6 -> c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY)
}

val formatter = SimpleDateFormat("EEEE", Locale.getDefault())
val formatter = SimpleDateFormat("EEEE", localeManagerWrapper.getLocale())
return formatter.format(c.time).capitalize()
}

fun getHour(hour: Int): String {
val formatter = DateFormat.getTimeFormat(context)
val formatter = DateFormat.getTimeFormat(contextProvider.getContext())
val c = Calendar.getInstance()
c.set(Calendar.HOUR_OF_DAY, hour)
c.set(Calendar.MINUTE, 0)
Expand All @@ -38,7 +41,7 @@ class DateUtils @Inject constructor(private val context: Context) {

fun formatDateTime(dateIso8601: String): String {
return DateUtils.formatDateTime(
context,
contextProvider.getContext(),
DateTimeUtils.timestampFromIso8601Millis(dateIso8601),
getDateTimeFlags()
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package org.wordpress.android.util

import android.content.Context
import java.util.Calendar
import java.util.Locale
import java.util.TimeZone
import javax.inject.Inject

class LocaleManagerWrapper
@Inject constructor(private val context: Context) {
fun getLocale(): Locale = LocaleManager.getSafeLocale(context)
@Inject constructor() {
fun getLocale(): Locale = Locale.getDefault()
fun getTimeZone(): TimeZone = TimeZone.getDefault()
fun getCurrentCalendar(): Calendar = Calendar.getInstance(getLocale())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.wordpress.android.viewmodel

import android.content.Context
import org.wordpress.android.util.LocaleManager
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class ContextProvider
@Inject constructor(private var context: Context) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like this Singleton Provider approach 👍

fun refreshContext() {
this.context = LocaleManager.setLocale(this.context)
}

fun getContext(): Context = context
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
package org.wordpress.android.viewmodel

import android.content.Context
import androidx.annotation.ColorRes
import androidx.annotation.DimenRes
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import javax.inject.Inject

class ResourceProvider @Inject constructor(private val context: Context) {
class ResourceProvider @Inject constructor(private val contextProvider: ContextProvider) {
fun getString(@StringRes resourceId: Int): String {
return context.getString(resourceId)
return contextProvider.getContext().getString(resourceId)
}

fun getString(@StringRes resourceId: Int, vararg formatArgs: Any): String {
return context.getString(resourceId, *formatArgs)
return contextProvider.getContext().getString(resourceId, *formatArgs)
}

fun getColor(@ColorRes resourceId: Int): Int {
return ContextCompat.getColor(context, resourceId)
return ContextCompat.getColor(contextProvider.getContext(), resourceId)
}

fun getDimensionPixelSize(@DimenRes dimen: Int): Int {
val resources = context.resources
val resources = contextProvider.getContext().resources
return resources.getDimensionPixelSize(dimen)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.mockito.Mock
import org.mockito.junit.MockitoJUnitRunner
import org.wordpress.android.models.news.LocalNewsItem
import org.wordpress.android.models.news.NewsItem
import org.wordpress.android.viewmodel.ContextProvider

@RunWith(MockitoJUnitRunner::class)
class LocalNewsServiceTest {
Expand All @@ -23,6 +24,7 @@ class LocalNewsServiceTest {
val rule = InstantTaskExecutorRule()

@Mock private lateinit var observer: Observer<NewsItem>
@Mock private lateinit var contextProvider: ContextProvider
@Mock private lateinit var context: Context

private lateinit var newsItem: NewsItem
Expand All @@ -32,7 +34,8 @@ class LocalNewsServiceTest {

@Before
fun setUp() {
localNewsService = LocalNewsService(context)
localNewsService = LocalNewsService(contextProvider)
whenever(contextProvider.getContext()).thenReturn(context)
whenever(context.getString(any())).thenReturn(dummyString)
newsItem = NewsItem(dummyString, dummyString, dummyString, dummyString, LocalNewsItem.version)
}
Expand Down