<commit_before>/*
 * Copyright 2018 Google, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.plaidapp.core.designernews.data.login

import android.content.SharedPreferences
import androidx.core.content.edit
import io.plaidapp.core.designernews.data.users.model.LoggedInUser

/**
 * Local storage for Designer News login related data, implemented using SharedPreferences
 */
class LoginLocalDataSource(private val prefs: SharedPreferences) {

    /**
     * Instance of the logged in user. If missing, null is returned
     */
    var user: LoggedInUser?
        get() {
            val userId = prefs.getLong(KEY_USER_ID, 0L)
            val username = prefs.getString(KEY_USER_NAME, null)
            val userAvatar = prefs.getString(KEY_USER_AVATAR, null)
            if (userId == 0L && username == null && userAvatar == null) {
                return null
            }
            // TODO save the entire user
            return LoggedInUser(
                id = userId,
                firstName = "",
                lastName = "",
                displayName = username,
                portraitUrl = userAvatar,
                upvotes = emptyList()
            )
        }
        set(value) {
            if (value != null) {
                prefs.edit {
                    putLong(KEY_USER_ID, value.id)
                    putString(KEY_USER_NAME, value.displayName)
                    putString(KEY_USER_AVATAR, value.portraitUrl)
                }
            }
        }<commit_msg>Returning null user in LoginLocalDataSource if user id is 0 or the username is missing<commit_after>/*
 * Copyright 2018 Google, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.plaidapp.core.designernews.data.login

import android.content.SharedPreferences
import androidx.core.content.edit
import io.plaidapp.core.designernews.data.users.model.LoggedInUser

/**
 * Local storage for Designer News login related data, implemented using SharedPreferences
 */
class LoginLocalDataSource(private val prefs: SharedPreferences) {

    /**
     * Instance of the logged in user. If missing, null is returned
     */
    var user: LoggedInUser?
        get() {
            val userId = prefs.getLong(KEY_USER_ID, 0L)
            val username = prefs.getString(KEY_USER_NAME, null)
            val userAvatar = prefs.getString(KEY_USER_AVATAR, null)
            if (userId == 0L || username == null) {
                return null
            }
            // TODO save the entire user
            return LoggedInUser(
                id = userId,
                firstName = "",
                lastName = "",
                displayName = username,
                portraitUrl = userAvatar,
                upvotes = emptyList()
            )
        }
        set(value) {
            if (value != null) {
                prefs.edit {
                    putLong(KEY_USER_ID, value.id)
                    putString(KEY_USER_NAME, value.displayName)
                    putString(KEY_USER_AVATAR, value.portraitUrl)
                }
            }
        }
{'fraction_non_alphanumeric': 0.07828835399951374, 'fraction_numerical': 0.005105762217359592, 'mean_word_length': 2.0724421209858104, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6892975', 'n_tokens_mistral': 1093, 'n_tokens_neox': 1056, 'n_words': 354}