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

Adding Homepage feature #1

Merged
merged 1 commit into from
Oct 6, 2022
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
38 changes: 36 additions & 2 deletions src/Paperback/Common.ts → src/TachiDesk/Common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
PagedResults,
SourceStateManager,
RequestManager,
Response
Response,
MangaTile
} from "paperback-extensions-common";

export function getServerUnavailableMangaTiles() {
Expand Down Expand Up @@ -161,7 +162,7 @@ export async function searchRequest(
// TACHI API STATE METHODS
//

const DEFAULT_TACHI_SERVER_ADDRESS = 'http://192.168.1.116:4567'
const DEFAULT_TACHI_SERVER_ADDRESS = 'http://10.0.0.127:4567'
const DEFAULT_TACHI_API = DEFAULT_TACHI_SERVER_ADDRESS + '/api/v1'
const DEFAULT_TACHI_LANG = "en"
const DEFAULT_TACHI_USERNAME = ''
Expand Down Expand Up @@ -241,4 +242,37 @@ function createAuthorizationString(username: string, password: string): string {

function createtachiAPI(serverAddress: string): string {
return serverAddress + (serverAddress.slice(-1) === '/' ? 'api/v1' : '/api/v1')
}

export interface HomePageData {
mangaList:
[
{
id: number
title: string
}
];
}

export const parseHomePage = (data: HomePageData, tachiAPI: string, displayName: string): MangaTile[] => {
const results: MangaTile[] = []

for (const manga of data.mangaList) {
const id = manga.id.toString()
const title = manga.title
const image = `${tachiAPI}/manga/${manga.id}/thumbnail`
const subtitle = displayName

if (!id) continue

results.push(createMangaTile({
id,
image,
title: createIconText({ text: title }),
subtitleText: createIconText({ text: subtitle })
}))

}

return results
}
File renamed without changes.
132 changes: 132 additions & 0 deletions src/Paperback/Settings.ts → src/TachiDesk/Settings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
Button,
MangaTile,
NavigationButton,
RequestManager,
SourceStateManager,
Expand Down Expand Up @@ -170,6 +171,137 @@ export const testServerSettingsMenu = (
});
};

interface TDSource {
id: string;
name: string;
lang: string;
iconUrl: string;
supportsLatest: boolean;
isConfigurable: boolean;
isNsfw: boolean;
displayName: string;
default?: boolean;
}


class SourceClass {
Sources: TDSource[] = []

constructor() {
this.Sources = this.Sources.sort((a, b) => a.displayName > b.displayName ? 1 : -1)
}

getIDList(): string[] {
return this.Sources.map(Sources => Sources.id)
}

getSelectedSources(sources: string[]): TDSource[] {
const FilteredSources: TDSource[] = []

for(const source of sources){
const fSources: TDSource[] = this.Sources.filter(MSources => MSources.id === source)

if(fSources && fSources[0]){
FilteredSources.push(fSources[0])
}
}

const SortedSources: TDSource[] = FilteredSources.sort((a, b) => a.displayName > b.displayName ? 1 : -1)

return SortedSources
}

getNameFromID(id: string): string {
return this.Sources.filter(Sources => Sources.id == id)[0]?.displayName ?? 'Unknown'
}

getDefault(): string[] {
return this.Sources.filter(Sources => Sources.default).map(Sources => Sources.id)
}

}

export const TDSources = new SourceClass

export const getSourcesList = async (stateManager: SourceStateManager): Promise<string[]> => {
return (await stateManager.retrieve('tdsources') as string[]) ?? [
{
id: "0",
name: "Local source",
displayName: "Local source"
},
]
}


export const getSources = async (stateManager: SourceStateManager) =>{
const tachiAPI = await getTachiAPI(stateManager);

const requestManager = createRequestManager({
requestsPerSecond: 4,
requestTimeout: 20000,
});

const request = createRequestObject({
url: `${tachiAPI}/source/list`,
method: "GET",
})
const response = await requestManager.schedule(request, 1)

let data: TDSource[]
try {
data = JSON.parse(response.data)
} catch (e) {
throw new Error(`${e}`)
}
if(data.length === 0) throw Error('Could not Find any sources avaliable in the api')

TDSources.Sources = data
}
export const TDSettings = (stateManager: SourceStateManager): NavigationButton => {
return createNavigationButton({
id: 'tdsource_settings',
value: '',
label: 'TachiDesk Source Settings',
form: createForm({
onSubmit: (values: any) => {
return Promise.all([
stateManager.store('tdsources', values.tdsources),
]).then()
},
validate: () => {
return Promise.resolve(true)
},
sections: () => {
return Promise.resolve([
createSection({
id: 'tachidesk_sources',
footer: '',
rows: () => {
return Promise.all([
getSourcesList(stateManager),
]).then(async values => {
return [
createSelect({
id: 'tdsources',
label: 'Sources',
options: TDSources.getIDList(),
displayLabel: option => TDSources.getNameFromID(option),
value: values[0],
allowsMultiselect: true,
minimumOptionCount: 1,
})
]
})
}
})
])
}
})
})
}


export const resetSettingsButton = (
stateManager: SourceStateManager
): Button => {
Expand Down
Loading