Skip to content

Commit

Permalink
Better Landing (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
KoalaSat authored Dec 28, 2022
2 parents e81aebc + 12acf14 commit 9f3599e
Show file tree
Hide file tree
Showing 16 changed files with 229 additions and 109 deletions.
9 changes: 5 additions & 4 deletions android/app/src/main/java/com/nostros/classes/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,19 +202,20 @@ protected void saveDirectMessage(SQLiteDatabase database) throws JSONException {

protected void saveUserMeta(SQLiteDatabase database) throws JSONException {
JSONObject userContent = new JSONObject(content);
String query = "SELECT * FROM nostros_users WHERE id = ?";
String query = "SELECT created_at FROM nostros_users WHERE id = ?";
@SuppressLint("Recycle") Cursor cursor = database.rawQuery(query, new String[] {pubkey});

ContentValues values = new ContentValues();
values.put("name", userContent.optString("name"));
values.put("picture", userContent.optString("picture"));
values.put("about", userContent.optString("about"));
values.put("lnurl", userContent.optString("lnurl"));
values.put("lnurl", userContent.optString("lud06"));
values.put("main_relay", userContent.optString("main_relay"));
values.put("created_at", created_at);
if (cursor.getCount() == 0) {
values.put("id", pubkey);
database.insert("nostros_users", null, values);
} else {
} else if (cursor.moveToFirst() && created_at > cursor.getInt(0)) {
String whereClause = "id = ?";
String[] whereArgs = new String[] {
this.pubkey
Expand All @@ -233,7 +234,7 @@ protected void savePets(SQLiteDatabase database) throws JSONException {
ContentValues values = new ContentValues();
values.put("id", petId);
values.put("name", tag.getString(3));
values.put("contact", true);
values.put("contact", true);
database.insert("nostros_users", null, values);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public class DatabaseModule {
try {
database.execSQL("ALTER TABLE nostros_users ADD COLUMN lnurl TEXT;");
} catch (SQLException e) { }
try {
database.execSQL("ALTER TABLE nostros_users ADD COLUMN created_at INT DEFAULT 0;");
} catch (SQLException e) { }
}

public void saveEvent(JSONObject data, String userPubKey) throws JSONException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.io.IOException;
import java.util.List;
import java.util.ListIterator;

public class RelayPoolModule extends ReactContextBaseJavaModule {
protected List<Relay> relays;
Expand Down Expand Up @@ -48,13 +49,16 @@ public void add(String url) {

@ReactMethod
public void remove(String url, Callback callback) {
for (Relay relay : relays) {
if (relay.url.equals(url)) {
ListIterator<Relay> iterator = relays.listIterator();
while(iterator.hasNext()){
Relay relay = iterator.next();
if(url.equals(relay.url)){
relay.disconnect();
relays.remove(relay);
iterator.remove();
database.destroyRelay(relay);
}
}

callback.invoke();
}

Expand All @@ -64,6 +68,7 @@ public void connect(String pubKey, Callback callback) {
relays = database.getRelays(context);
if (relays.isEmpty()) {
add("wss://relay.damus.io");
add("wss://nostr-relay.wlvs.space");
}
for (Relay relay : relays) {
try {
Expand Down
2 changes: 1 addition & 1 deletion frontend/Components/ConfigPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const ConfigPage: React.FC = () => {
name,
about,
picture,
lnurl,
lud06: lnurl,
}),
created_at: moment().unix(),
kind: EventKind.meta,
Expand Down
30 changes: 25 additions & 5 deletions frontend/Components/HomePage/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Card, Layout, Text, useTheme } from '@ui-kitten/components'
import { Button, Card, Layout, Text, useTheme } from '@ui-kitten/components'
import React, { useCallback, useContext, useEffect, useState } from 'react'
import { t } from 'i18next'
import {
Expand Down Expand Up @@ -46,7 +46,7 @@ export const HomePage: React.FC = () => {

const message: RelayFilters = {
kinds: [EventKind.textNote, EventKind.recommendServer],
authors: users.map((user) => user.id)
authors: [...users.map((user) => user.id), publicKey],
}

if (lastNote && lastNotes.length > 0 && !past) {
Expand All @@ -65,7 +65,7 @@ export const HomePage: React.FC = () => {
setRefreshing(false)
relayPool?.subscribe('main-channel', {
kinds: [EventKind.meta],
authors: notes.map((note) => note.pubkey)
authors: notes.map((note) => note.pubkey),
})
})
}
Expand Down Expand Up @@ -132,6 +132,11 @@ export const HomePage: React.FC = () => {
height: 32,
},
empty: {
height: 128,
justifyContent: 'center',
alignItems: 'center',
},
noContacts: {
height: 64,
justifyContent: 'center',
alignItems: 'center',
Expand All @@ -142,12 +147,27 @@ export const HomePage: React.FC = () => {
<>
<Layout style={styles.container} level='3'>
{notes.length > 0 ? (
<ScrollView onScroll={onScroll} horizontal={false} refreshControl={<RefreshControl refreshing={refreshing} onRefresh={onRefresh} />}>
<ScrollView
onScroll={onScroll}
horizontal={false}
refreshControl={<RefreshControl refreshing={refreshing} onRefresh={onRefresh} />}
>
{notes.map((note) => itemCard(note))}
</ScrollView>
) : (
<Layout style={styles.empty} level='3'>
<Text>{t('homePage.noContacts')}</Text>
<Layout style={styles.noContacts} level='3'>
<Text>{t('homePage.noContacts')}</Text>
</Layout>
<Button
onPress={() => goToPage('config')}
status='warning'
accessoryLeft={
<Icon name='address-book' size={16} color={theme['text-basic-color']} solid />
}
>
{t('homePage.addContacts')}
</Button>
</Layout>
)}
</Layout>
Expand Down
104 changes: 104 additions & 0 deletions frontend/Components/LandingPage/Loader/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import React, { useContext, useEffect, useState } from 'react'
import { Button, Layout, Text, useTheme } from '@ui-kitten/components'
import { StyleSheet } from 'react-native'
import { RelayPoolContext } from '../../../Contexts/RelayPoolContext'
import { EventKind } from '../../../lib/nostr/Events'
import { AppContext } from '../../../Contexts/AppContext'
import { getUser, getUsers, User } from '../../../Functions/DatabaseFunctions/Users'
import Icon from 'react-native-vector-icons/FontAwesome5'
import { useTranslation } from 'react-i18next'

export const Loader: React.FC = () => {
const { goToPage, loadingDb, database } = useContext(AppContext)
const { publicKey, relayPool, lastEventId, loadingRelayPool } = useContext(RelayPoolContext)
const theme = useTheme()
const { t } = useTranslation('common')
const [profileFound, setProfileFound] = useState<boolean>(false)
const [contactsCount, setContactsCount] = useState<number>()

useEffect(() => {
if (!loadingRelayPool && !loadingDb && publicKey) {
relayPool?.subscribe('main-channel', {
kinds: [EventKind.petNames, EventKind.meta, EventKind.directMessage],
authors: [publicKey],
})
}
}, [loadingRelayPool, publicKey, loadingDb])

useEffect(() => {
loadPets()
loadProfile()
}, [lastEventId])

const loadPets: () => void = () => {
if (database) {
getUsers(database, { contacts: true }).then((results) => {
setContactsCount(results.length)
if (results && results.length > 0) {
relayPool?.subscribe('main-channel', {
kinds: [EventKind.meta],
authors: results.map((user: User) => user.id),
})
}
})
}
}

const loadProfile: () => void = () => {
if (database && publicKey) {
getUser(publicKey, database).then((result) => {
if (result) {
setProfileFound(true)
}
})
}
}

const styles = StyleSheet.create({
container: {
padding: 32,
},
text: {
marginVertical: 10,
padding: 32,
alignItems: 'center',
justifyContent: 'center',
},
action: {
backgroundColor: 'transparent',
marginTop: 12,
},
})

return (
<>
<Layout style={styles.text}>
<Text>{profileFound ? t('loader.profileFound') : t('loader.searchingProfile')}</Text>
<Text>{`${t('loader.searchingContacts')} ${contactsCount}`}</Text>
</Layout>
<Layout>
<Text>{t('loader.help1')}</Text>
<Text>{t('loader.help2')}</Text>
</Layout>
<Layout style={styles.action}>
<Button
onPress={() => goToPage('relays')}
status='warning'
accessoryLeft={<Icon name='server' size={16} color={theme['text-basic-color']} solid />}
>
{t('loader.relays')}
</Button>
</Layout>
<Layout style={styles.action}>
<Button
onPress={() => goToPage('home')}
accessoryLeft={<Icon name='home' size={16} color={theme['text-basic-color']} solid />}
>
{t('loader.home')}
</Button>
</Layout>
</>
)
}

export default Loader
71 changes: 10 additions & 61 deletions frontend/Components/LandingPage/Logger/index.tsx
Original file line number Diff line number Diff line change
@@ -1,71 +1,27 @@
import React, { useContext, useEffect, useState } from 'react'
import React, { useContext, useState } from 'react'
import { Button, Input, Layout, useTheme } from '@ui-kitten/components'
import { Clipboard, StyleSheet } from 'react-native'
import { RelayPoolContext } from '../../../Contexts/RelayPoolContext'
import { useTranslation } from 'react-i18next'
import { EventKind } from '../../../lib/nostr/Events'
import { AppContext } from '../../../Contexts/AppContext'
import SInfo from 'react-native-sensitive-info'
import Icon from 'react-native-vector-icons/FontAwesome5'
import { generateRandomKey, getPublickey } from '../../../lib/nostr/Bip'
import { generateRandomKey } from '../../../lib/nostr/Bip'
import { showMessage } from 'react-native-flash-message'
import { getUsers, User } from '../../../Functions/DatabaseFunctions/Users'

export const Logger: React.FC = () => {
const { goToPage, loadingDb, database } = useContext(AppContext)
const { privateKey, publicKey, relayPool, loadingRelayPool, setPrivateKey, setPublicKey } =
useContext(RelayPoolContext)
const { publicKey, setPrivateKey, setPublicKey } = useContext(RelayPoolContext)
const { t } = useTranslation('common')
const theme = useTheme()
const [loading, setLoading] = useState<boolean>(false)
const [status, setStatus] = useState<number>(0)
const [isPrivate, setIsPrivate] = useState<boolean>(true)
const [inputValue, setInputValue] = useState<string>('')

useEffect(() => {
if (loading) {
setStatus(1)
const onPress: () => void = () => {
if (inputValue && inputValue !== '') {
if (isPrivate) {
setPrivateKey(inputValue)
const publicKey: string = getPublickey(inputValue)
setPublicKey(publicKey)
SInfo.setItem('privateKey', inputValue, {})
SInfo.setItem('publicKey', publicKey, {})
} else {
setPublicKey(inputValue)
SInfo.setItem('publicKey', inputValue, {})
}
}
}, [loading])

useEffect(() => {
if (!loadingRelayPool && !loadingDb && publicKey) {
relayPool?.subscribe('main-channel', {
kinds: [EventKind.petNames, EventKind.meta],
authors: [publicKey],
})
setTimeout(loadPets, 4000)
}
}, [loadingRelayPool, publicKey, loadingDb])

const onPress: () => void = () => {
if (inputValue && inputValue !== '') {
setLoading(true)
}
}

const loadPets: () => void = () => {
if (database) {
getUsers(database, { contacts: true }).then((results) => {
if (results && results.length > 0) {
relayPool?.subscribe('main-channel', {
kinds: [EventKind.textNote, EventKind.recommendServer, EventKind.meta],
authors: results.map((user: User) => user.id),
})
}
setTimeout(() => goToPage('home', true), 5000)
})
}
}

const randomKeyGenerator: () => JSX.Element = () => {
Expand Down Expand Up @@ -95,11 +51,6 @@ export const Logger: React.FC = () => {
)
}

const statusName: { [status: number]: string } = {
0: t('landing.connect'),
1: t('landing.connecting'),
2: t('landing.ready'),
}
const styles = StyleSheet.create({
inputsContainer: {
flexDirection: 'row',
Expand Down Expand Up @@ -130,7 +81,7 @@ export const Logger: React.FC = () => {

const label: string = isPrivate ? t('landing.privateKey') : t('landing.publicKey')

return !privateKey || !publicKey || status !== 0 ? (
return (
<>
<Layout style={styles.inputsContainer}>
<Layout style={styles.input}>
Expand All @@ -139,7 +90,7 @@ export const Logger: React.FC = () => {
label={label}
onChangeText={setInputValue}
value={inputValue}
disabled={loading}
disabled={publicKey !== undefined}
accessoryRight={randomKeyGenerator}
/>
</Layout>
Expand All @@ -151,20 +102,18 @@ export const Logger: React.FC = () => {
setIsPrivate(!isPrivate)
setInputValue('')
}}
disabled={loading}
disabled={publicKey !== undefined}
accessoryLeft={keyButton}
status={isPrivate ? 'warning' : 'default'}
/>
</Layout>
<Layout style={styles.buttonRight}>
<Button onPress={onPress} disabled={loading}>
{statusName[status]}
<Button onPress={onPress} disabled={publicKey !== undefined}>
{t('landing.connect')}
</Button>
</Layout>
</Layout>
</>
) : (
<></>
)
}

Expand Down
Loading

0 comments on commit 9f3599e

Please sign in to comment.